GaussDB database SQL series: DROP & TRUNCATE & DELETE

Table of contents

I. Introduction

2. Brief introduction of DROP & TRUNCATE & DELETE of GaussDB

1. Brief description of the command

2. Command comparison

3. GaussDB's DROP TABLE command and examples

1. Function description

2. Grammar

3. Examples

4. GaussDB's TRUNCATE command and examples

1. Function description

2. Grammar

3. Examples

4. Examples

5. GaussDB's DELETE command and examples

1. Function description

2. Matters needing attention

3. Grammar

4. Examples

6. Application scenarios

7. Summary

I. Introduction

In databases, SQL, as a commonly used database programming language, plays a vital role. SQL can be used not only to create, modify, and query databases, but also to delete data through statements such as DROP, DELETE, and TRUNCATE. These statements are the most commonly used commands in the SQL language, and they have different meanings and usage scenarios.

Using the GaussDB database as a platform, this article will introduce in detail the meaning, usage scenarios, and precautions of DROP, TRUNCATE, and DELETE statements in SQL, helping readers better understand and master these commonly used database operation commands.

2. Brief introduction of DROP & TRUNCATE & DELETE of GaussDB

1. Brief introduction

  • The DROP statement can delete the entire table, including table structure and data;
  • The TRUNCATE statement can quickly delete all the data in the table, but does not delete the table structure.
  • The DELETE statement can delete the data in the table according to the conditions, but does not include the table structure;

2. Command comparison

category

DROP

TRUNCATE

DELETE

SQL type

DDL

DDL

DML

delete content

Delete all data of the table, including table structure, indexes and permissions, etc.

Delete all data in the table, or the data of the specified partition

Delete all or some (+ conditional) data of a table

execution speed

the fastest

medium speed

the slowest

Tip: In the GaussDB database, DROP is one of the commands used to define or modify objects in the database. Objects mainly include: libraries, schemas, table spaces, tables, indexes, views, stored procedures, functions, encryption keys, etc. This time, only operations on tables are targeted.

3. GaussDB's DROP TABLE command and examples

1. Function description

The function of DROP TABLE is to delete the existing Table.

2. Grammar

DROP TABLE [IF EXISTS] [db_name.]table_name;

Note: Adding [IF EXISTS] to SQL can prevent execution errors caused by non-existing tables.

Parameters: db_name: Database name. If not specified, the current database will be selected. table_name: The name of the Table to be deleted.

3. Examples

The following example demonstrates the use of the DROP command, executing the following SQL statements in sequence:

--删除整个表course
DROP TABLE IF EXISTS course

--创建course表
CREATE TABLE course(cid VARCHAR(10),cname VARCHAR(10),teid VARCHAR(10));

--初始化数据
INSERT INTO course VALUES('01' , '语文' , '02');
INSERT INTO course VALUES('02' , '数学' , '01');
INSERT INTO course VALUES('03' , '英语' , '03');

--3条记录
SELECT count(1) FROM  course;

--删除整个表
DROP TABLE IF EXISTS course

--查看结果,表不存在(表结构及数据不存在)
SELECT count(1) FROM  course;

1) DROP TABLE, prompt table does not exist

2) Create and initialize an experiment table

3) DROP TABLE executed successfully

 

4) View execution results

4. GaussDB's TRUNCATE command and examples

1. Function description

Remove all data from a table or table partition, TRUNCATE quickly deletes all rows from the table. It has the same effect as an unconditional DELETE on the target table, but since TRUNCATE does not do a table scan, it is much faster and uses less system and transaction log resources. The operation effect is more obvious on a large table.

TRUNCATE TABLE deletes all rows in the table, but the table structure and its columns, constraints, indexes, etc. remain unchanged. The count value used for new row identification is reset to the column's seed.

2. Grammar

TRUNCATE [TABLE] table_name;

or

ALTER TABLE [IF EXISTS] table_name  TRUNCATE PARTITION { partition_name | FOR (  partition_value  [, ...] ) }

Parameters: table_name: the name of the Table whose data needs to be deleted. partition_name: The partition name of the partition table to be deleted. partition_value: The partition value of the partition table to be deleted.

3. Example 1

The following example demonstrates the use of the TRUNCATE command:

--创建course表
DROP TABLE IF EXISTS course;
CREATE TABLE course(cid VARCHAR(10),cname VARCHAR(10),teid VARCHAR(10));

--初始化数据
INSERT INTO course VALUES('01' , '语文' , '02');
INSERT INTO course VALUES('02' , '数学' , '01');
INSERT INTO course VALUES('03' , '英语' , '03');

--3条记录
SELECT count(1) FROM  course;

--清空表
TRUNCATE TABLE course;
--或
TRUNCATE course;

--0条记录
SELECT count(1) FROM  course;

1) Create an experiment table and initialize the data

 2) TRUNCATE TABLE executed successfully

 3) View execution results

4. Example 2

The following example demonstrates the delete partition table data of TRUNCATE command

--创建列表分区(LIST)
DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
    id INT PRIMARY KEY,
    customer_id INT,
    order_date DATE,
    product_id INT,
    quantity INT
) PARTITION BY LIST (customer_id) (
    PARTITION p1 VALUES (100),
    PARTITION p2 VALUES (200), 
    PARTITION p3 VALUES (300),
    PARTITION p4 VALUES (400),
    PARTITION p5 VALUES (500)
);

--插入测试数据
INSERT INTO orders(id,customer_id,order_date,product_id,quantity)VALUES(1001,100,date'20230822',1,10);
INSERT INTO orders(id,customer_id,order_date,product_id,quantity)VALUES(1002,100,date'20230822',2,20);
INSERT INTO orders(id,customer_id,order_date,product_id,quantity)VALUES(1003,100,date'20230822',3,30);
INSERT INTO orders(id,customer_id,order_date,product_id,quantity)VALUES(1004,200,date'20230822',4,40);

--查看分区p1、p2的数据
SELECT * FROM orders WHERE customer_id IN (100,200);
--或
--根据分区名称查询
SELECT * FROM  orders PARTITION(p2);

--清空分区p1。
ALTER TABLE orders TRUNCATE PARTITION p1;
--或者
--清空分区p2=200。
ALTER TABLE orders TRUNCATE PARTITION for (200);

--查看分区p1、p2的数据
SELECT * FROM orders WHERE customer_id IN (100,200);

1) Create an experiment table and initialize it

 2) Delete data according to the partition

5. GaussDB's DELETE command and examples

1. Function description

Deletes rows that satisfy the WHERE clause from the specified table. If the WHERE clause does not exist, all rows in the table will be deleted, leaving only the table structure as a result.

2. Matters needing attention

  • LIMIT is not supported in DELETE statements. The WHERE condition should be used to specify the target row that needs to be updated.
  • It does not support deleting multiple tables in a single SQL statement.
  • There must be a WHERE clause in the DELETE statement to avoid full table scans.
  • The ORDER BY and GROUP BY clauses should not be used in the DELETE statement to avoid unnecessary sorting.
  • If you need to clear a table, it is recommended to use TRUNCATE instead of DELETE.
  • TRUNCATE will create a new physical file and physically delete the original file at the end of the transaction to clear the disk space. DELETE will mark the data in the table, and will not actually clean up the disk space until the VACCUUM FULL stage.
  • When DELETE has a table with a primary key or index, the WHERE condition should be combined with the primary key or index to improve execution efficiency.
  • The DELETE statement deletes one row at a time and records an entry in the transaction log for each row deleted.
  • If you want to keep the identity count value, use DELETE instead

3. Grammar

DELETE FROM table_name [WHERE condition];

Parameters: table_name: the name of the Table whose data needs to be deleted. condition: Used to determine which rows need to be deleted.

4. Examples

Reuse the previous experiment table:

DELETE FROM orders WHERE  customer_id <200;

1) Delete all data of customer_id <200 in the orders table:

6. Application scenarios

  • When you need to delete data according to certain business conditions, and the data volume and performance are controllable, you can consider using DELETE.
  • TRUNCATE can be used when large batches of data need to be deleted and fast speed, high efficiency and no need to undo are required.
  • In enterprise-level development, logical deletion is actually performed (the data is "deleted" and processed) instead of physical deletion.
  • In the actual production environment, the data in the business process (transition table) is generally deleted.
  • In the actual enterprise development and maintenance process, data backup must be considered before using DELETE, TRUNCATE or DROP commands.

7. Summary

In databases such as GaussDB, DROP, TRUNCATE, and DELETE are commonly used commands to delete data. But in actual business use, it is necessary to make accurate choices according to different needs, but no matter which method of deleting data is selected, data security needs to be considered-the important thing is said three times: backup ! back up! back up!

--Finish.

Guess you like

Origin blog.csdn.net/GaussDB/article/details/132487990