SQL截断表

SQL截断表 (SQL Truncate Table)

Assume a case when we want to have the database but we would like to delete the data of the table. In such cases, TRUNCATE command is used to delete just the data from the table. The table structure will remain as it is.

假设有一种情况,当我们想拥有数据库但想删除表中的数据时。 在这种情况下,TRUNCATE命令用于仅删除表中的数据。 表结构将保持原样。

Syntax for SQL TRUNCATE

SQL TRUNCATE的语法

TRUNCATE TABLE <table_name>;

We will try to understand how the TRUNCATE command works in the following databases.

我们将尝试了解TRUNCATE命令在以下数据库中如何工作。

  1. MySQL

    MySQL
  2. PostgreSQL

    PostgreSQL
  3. SQL Server

    SQL服务器

MySQL截断表 (MySQL Truncate Table)

Let us first create a Teacher table and we will insert data in the table as follows.

让我们首先创建一个Teacher表,然后将数据插入表中,如下所示。

Create Table: –

创建表:–

CREATE TABLE `teacher` ( `TeacherId` INT NOT NULL, `TeacherName` VARCHAR(45) NULL, `State` VARCHAR(45) NULL, `Country` VARCHAR(45) NULL, PRIMARY KEY (`TeacherId`), UNIQUE INDEX `TeacherId_UNIQUE` (`TeacherId` ASC) VISIBLE);

Insert Data: –

插入数据:–

Insert into Teacher(TeacherId,TeacherName,State,Country) VALUES (1, 'Amit','Bengaluru','India'), (2, 'Harry','Texas','US'), (3, 'John','London','UK'), (4, 'Rohit','Delhi','India');

Let us validate if the table creation and data insertion worked fine using the following query.

让我们使用以下查询验证表创建和数据插入是否工作正常。

Select * from teacher;
SQL Insert Into Select

Table After SQL Insert

SQL插入后的表

Now we will TRUNCATE the data of the table using the following command.

现在,我们将使用以下命令截断表的数据。

TRUNCATE TABLE teacher;
MySQL Truncate Table

MySQL Truncate Table

MySQL截断表

We will recheck if the data still exists in the table using the following query.

我们将使用以下查询重新检查表中数据是否仍然存在。

Select * from Teacher;
MySQL Table After Truncate

MySQL Table After Truncate

截断后MySQL表

PostgreSQL截断表 (PostgreSQL Truncate Table)

We will create the same table in PostgreSQL table

我们将在PostgreSQL表中创建相同的表

Create Table: –

创建表:–

CREATE TABLE public."Teacher"
(
"TeacherId" bigint,
"TeacherName" character varying,
"State" character varying,
"Country" character varying,
PRIMARY KEY ("TeacherId")
)
Insert Data: -
INSERT INTO public."Teacher"(
	"TeacherId", "TeacherName", "State", "Country")
	VALUES  (1, 'Amit','Bengaluru','India'), (2, 'Harry','Texas','US'), (3, 'John','London','UK'), (4, 'Rohit','Delhi','India');

Let us validate if the table creation and data insertion worked fine using the following query.

让我们使用以下查询验证表创建和数据插入是否工作正常。

Select * from teacher;
PostgreSQL Table After Insert

PostgreSQL Table After Insert

插入后的PostgreSQL表

Now we will TRUNCATE the data of the table using the following command.

现在,我们将使用以下命令截断表的数据。

TRUNCATE TABLE teacher;
PostgreSQL Truncate Table

PostgreSQL Truncate Table

PostgreSQL截断表

We will recheck if the data still exists in the table using the following query.

我们将使用以下查询重新检查表中数据是否仍然存在。

Select * from Teacher;
PostgreSQL Table After Truncate

PostgreSQL Table After Truncate

截断后的PostgreSQL表

SQL Server截断表 (SQL Server Truncate Table)

We have already created the table and have inserted the data using following commands.

我们已经创建了表,并使用以下命令插入了数据。

Create Table: –

创建表:–

CREATE TABLE teacher ( TeacherId INT NOT NULL, TeacherName VARCHAR(45) NULL, State VARCHAR(45) NULL, Country VARCHAR(45) NULL, PRIMARY KEY (TeacherId));

Insert Data: –

插入数据:–

Insert into Teacher(TeacherId,TeacherName,State,Country) VALUES (1, 'Amit','Bengaluru','India'), (2, 'Harry','Texas','US'), (3, 'John','London','UK'), (4, 'Rohit','Delhi','India');

Let us validate if the table creation and data insertion worked fine using the following query.

让我们使用以下查询验证表创建和数据插入是否工作正常。

Select * from teacher;
SQL Server Table After Insert

SQL Server Table After Insert

插入后SQL Server表

Now we will TRUNCATE the data of the table using the following command.

现在,我们将使用以下命令截断表的数据。

TRUNCATE TABLE teacher;
SQL Server Truncate Table

SQL Server Truncate Table

SQL Server截断表

We will recheck the data in the table using the following query.

我们将使用以下查询重新检查表中的数据。

Select * from Teacher;
SQL Server Table After Truncate

SQL Server Table After Truncate

截断后SQL Server表

翻译自: https://www.journaldev.com/28009/sql-truncate-table

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值