云数据库 GAUSSDB-DELETE:示例
时间:2024-11-01 17:13:45
示例
- 删除部分数据
--建表。 gaussdb=# CREATE TABLE test_t1(col1 INT,col2 INT); gaussdb=# INSERT INTO test_t1 VALUES (1, 1), (2, 2), (3, 3), (4, 4), (4, 6); --删除表中部分记录。 gaussdb=# DELETE FROM test_t1 WHERE col1 = 4; --查询。 gaussdb=# SELECT * FROM test_t1; col1 | col2 ------+------ 1 | 1 2 | 2 3 | 3 (3 rows)
- 删除所有数据
--删除所有的数据。 gaussdb=# DELETE FROM test_t1; --查询。 gaussdb=# SELECT * FROM test_t1; col1 | col2 ------+------ (0 rows) --删除表。 gaussdb=# DROP TABLE test_t1;
- WITH [ RECURSIVE ] with_query [, ...]
--创建学生表。 gaussdb=# CREATE TABLE student(id INT,name varchar(50)); --创建成绩表。 gaussdb=# CREATE TABLE grade(id INT,score CHAR); gaussdb=# INSERT INTO student VALUES (1, 'tom'), (2, 'jerry'), (3, 'david'); gaussdb=# INSERT INTO grade VALUES (1, 'A'), (2, 'B'), (3, 'b'); --在学生表中删除id=2的数据同时删除该学生在成绩表中的数据。 gaussdb=# WITH del_stu AS(DELETE FROM student WHERE id = 2 RETURNING id) DELETE FROM grade WHERE id = (SELECT id FROM del_stu); --查询数据。 gaussdb=# SELECT * FROM student; id | name ----+------- 1 | tom 3 | david (2 rows) gaussdb=# SELECT * FROM grade; id | score ----+------- 1 | A 3 | b (2 rows) --删除表。 gaussdb=# DROP TABLE grade; gaussdb=# DROP TABLE student;
- 删除视图或子查询
示例1:删除子查询
--创建SCHEMA。 gaussdb=# CREATE SCHEMA del_subqry; CREATE SCHEMA gaussdb=# SET CURRENT_SCHEMA = 'del_subqry'; SET --创建表并插入数据。 gaussdb=# CREATE TABLE t1 (x1 int, y1 int); NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using 'x1' as the distribution column by default. HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column. CREATE TABLE gaussdb=# CREATE TABLE t2 (x2 int PRIMARY KEY, y2 int); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t2_pkey" for table "t2" CREATE TABLE gaussdb=# CREATE TABLE tdata (x INT PRIMARY KEY, y INT); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tdata_pkey" for table "tdata" CREATE TABLE gaussdb=# CREATE TABLE tinfo (z INT PRIMARY KEY, comm VARCHAR2(20)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tinfo_pkey" for table "tinfo" CREATE TABLE gaussdb=# INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3), (5, 5); INSERT 0 4 gaussdb=# INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3), (5, 5); INSERT 0 4 gaussdb=# INSERT INTO tdata VALUES (1, 1), (2, 2), (3, 3); INSERT 0 3 gaussdb=# INSERT INTO tinfo VALUES (1,'one'), (2, 'two'), (3, 'three'), (5, 'wrong three'); INSERT 0 4 --通过子查询删除t1中的数据。 gaussdb=# DELETE FROM (SELECT * FROM t1) where y1 = 3; DELETE 1 --子查询带READ ONLY,无法删除数据。 gaussdb=# DELETE FROM (SELECT * FROM t1 WITH READ ONLY) WHERE y1 = 1; ERROR: cannot perform a DML operation on a read-only subquery. --对多表连接的子查询删除。 gaussdb=# SELECT * FROM t1, t2 WHERE x1 = x2; x1 | y1 | x2 | y2 ----+----+----+---- 1 | 1 | 1 | 1 2 | 2 | 2 | 2 5 | 5 | 5 | 5 (3 rows) gaussdb=# DELETE FROM (SELECT * FROM t1, t2 WHERE x1 = x2) WHERE y2 = 5; DELETE 1 gaussdb=# SELECT * FROM t1, t2 WHERE x1 = x2; x1 | y1 | x2 | y2 ----+----+----+---- 1 | 1 | 1 | 1 2 | 2 | 2 | 2 (2 rows) --子查询带CHECK OPTION,tdata表重复,其中 td1不是保留键表,td2是保留键表。 gaussdb=# DELETE FROM (SELECT td1.x x1, td1.y y1, td2.x x2, td2.y y2 FROM tdata td1, tdata td2, tinfo WHERE td2.y=tinfo.z AND td1.x=td2.y WITH CHECK OPTION) WHERE y1 = 2; ERROR: cannot delete from view without exactly one key-preserved table --不带CHECK OPTION,创建同样结构的子查询,删除成功。 gaussdb=# DELETE FROM (SELECT td1.x x1, td1.y y1, td2.x x2, td2.y y2 FROM tdata td1, tdata td2, tinfo WHERE td2.y=tinfo.z AND td1.x=td2.y) WHERE y1 = 2; DELETE 1 --删除SCHEMA。 gaussdb=# RESET CURRENT_SCHEMA; RESET gaussdb=# DROP SCHEMA del_subqry CASCADE; NOTICE: drop cascades to 5 other objects DETAIL: drop cascades to table del_subqry.t1 drop cascades to table del_subqry.t2 drop cascades to table del_subqry.tdata drop cascades to table del_subqry.tinfo drop cascades to view del_subqry.vv_wco DROP SCHEMA
示例2: 删除视图
--创建SCHEMA。 gaussdb=# CREATE SCHEMA del_view; CREATE SCHEMA。 gaussdb=# SET CURRENT_SCHEMA = 'del_view'; SET --创建表并插入数据。 gaussdb=# CREATE TABLE t1 (x1 int, y1 int); NOTICE: The 'DISTRIBUTE BY' clause is not specified. Using 'x1' as the distribution column by default. HINT: Please use 'DISTRIBUTE BY' clause to specify suitable data distribution column. CREATE TABLE gaussdb=# CREATE TABLE t2 (x2 int PRIMARY KEY, y2 int); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t2_pkey" for table "t2" CREATE TABLE gaussdb=# CREATE TABLE tdata (x INT PRIMARY KEY, y INT); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tdata_pkey" for table "tdata" CREATE TABLE gaussdb=# CREATE TABLE tinfo (z INT PRIMARY KEY, comm VARCHAR2(20)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tinfo_pkey" for table "tinfo" CREATE TABLE gaussdb=# INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3), (5, 5); INSERT 0 4 gaussdb=# INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3), (5, 5); INSERT 0 4 gaussdb=# INSERT INTO tdata VALUES (1, 1), (2, 2), (3, 3); INSERT 0 3 gaussdb=# INSERT INTO tinfo VALUES (1,'one'), (2, 'two'), (3, 'three'), (5, 'wrong three'); INSERT 0 4 --创建单表视图。 gaussdb=# CREATE VIEW v_del1 AS SELECT * FROM t1; CREATE VIEW gaussdb=# CREATE VIEW v_del_read AS SELECT * FROM t1 WITH READ ONLY; CREATE VIEW --通过视图删除t1中的数据。 gaussdb=# DELETE FROM v_del1 where y1 = 3; DELETE 1 --视图带READ ONLY,无法删除数据。 gaussdb=# DELETE FROM v_del_read WHERE y1 = 1; ERROR: cannot perform a DML operation on a read-only subquery. --创建多表连接视图。 gaussdb=# CREATE VIEW vvt1t2 AS SELECT * FROM t1, t2 WHERE x1 = x2; CREATE VIEW gaussdb=# CREATE VIEW vv_dup AS SELECT td1.x x1, td1.y y1, td2.x x2, td2.y y2 FROM tdata td1, tdata td2, tinfo WHERE td2.y=tinfo.z AND td1.x=td2.y; CREATE VIEW gaussdb=# CREATE VIEW vv_dup_wco AS SELECT td1.x x1, td1.y y1, td2.x x2, td2.y y2 FROM tdata td1, tdata td2, tinfo WHERE td2.y=tinfo.z AND td1.x=td2.y WITH CHECK OPTION; CREATE VIEW --对多表连接的视图做删除操作。 gaussdb=# SELECT * FROM vvt1t2; x1 | y1 | x2 | y2 ----+----+----+---- 1 | 1 | 1 | 1 2 | 2 | 2 | 2 5 | 5 | 5 | 5 (3 rows) gaussdb=# DELETE FROM vvt1t2 WHERE y2 = 5; DELETE 1 gaussdb=# SELECT * FROM vvt1t2; x1 | y1 | x2 | y2 ----+----+----+---- 1 | 1 | 1 | 1 2 | 2 | 2 | 2 (2 rows) --视图带CHECK OPTION,tdata表重复,其中 td1不是保留键表,td2是保留键表。 gaussdb=# DELETE FROM vv_dup_wco WHERE y1 = 2; ERROR: cannot delete from view without exactly one key-preserved table --不带CHECK OPTION,创建同样结构的视图,删除成功。 gaussdb=# DELETE FROM vv_dup WHERE y1 = 2; DELETE 1 --删除SCHEMA。 gaussdb=# RESET CURRENT_SCHEMA; RESET gaussdb=# DROP SCHEMA del_view CASCADE; NOTICE: drop cascades to 9 other objects DETAIL: drop cascades to table del_view.t1 drop cascades to table del_view.t2 drop cascades to table del_view.tdata drop cascades to table del_view.tinfo drop cascades to view del_view.v_del1 drop cascades to view del_view.v_del_read drop cascades to view del_view.vvt1t2 drop cascades to view del_view.vv_dup drop cascades to view del_view.vv_dup_wco DROP SCHEMA
support.huaweicloud.com/distributed-devg-v8-gaussdb/gaussdb-12-0584.html
看了此文的人还看了
CDN加速
GaussDB
文字转换成语音
免费的服务器
如何创建网站
域名网站购买
私有云桌面
云主机哪个好
域名怎么备案
手机云电脑
SSL证书申请
云点播服务器
免费OCR是什么
电脑云桌面
域名备案怎么弄
语音转文字
文字图片识别
云桌面是什么
网址安全检测
网站建设搭建
国外CDN加速
SSL免费证书申请
短信批量发送
图片OCR识别
云数据库MySQL
个人域名购买
录音转文字
扫描图片识别文字
OCR图片识别
行驶证识别
虚拟电话号码
电话呼叫中心软件
怎么制作一个网站
Email注册网站
华为VNC
图像文字识别
企业网站制作
个人网站搭建
华为云计算
免费租用云托管
云桌面云服务器
ocr文字识别免费版
HTTPS证书申请
图片文字识别转换
国外域名注册商
使用免费虚拟主机
云电脑主机多少钱
鲲鹏云手机
短信验证码平台
OCR图片文字识别
SSL证书是什么
申请企业邮箱步骤
免费的企业用邮箱
云免流搭建教程
域名价格
推荐文章