sql删除表结构
In a real-time situation, there are scenarios when we would like to remove the table from the database. Let us discuss in detail about the process of removing a table from the database.
在实时情况下,有些情况下我们想从数据库中删除表。 让我们详细讨论从数据库中删除表的过程。
SQL删除表 (SQL DROP TABLE)
The SQL DROP TABLE statement informs the database that a particular table needs to be removed. SQL DROP TABLE command removes all the data, indexes, triggers, constraints and permission specifications for that table.
SQL DROP TABLE语句通知数据库需要删除特定的表。 SQL DROP TABLE命令删除该表的所有数据,索引,触发器,约束和权限规范。
We will use the following three databases for better understanding DROP Table.
我们将使用以下三个数据库来更好地理解DROP表。
- MySQL MySQL
- PostgreSQL PostgreSQL
- SQL Server SQL服务器
MySQL删除表 (MySQL Drop Table)
Syntax
句法
DROP TABLE table_name;
In the syntax above, table_name is the table that needs to be dropped.
在上面的语法中,table_name是需要删除的表。
Let us try to understand the SQL DROP TABLE using an example.
让我们尝试使用一个示例来理解SQL DROP TABLE。
Create a table Customer using the following SQL COMMAND.
使用以下SQL命令创建表Customer。
CREATE TABLE `test`.`customer`
( `CustomrId` INT NOT NULL, `CustomerName` VARCHAR(45) NULL,
`ProductId` VARCHAR(45) NOT NULL, `State` VARCHAR(45) NULL,
PRIMARY KEY (`CustomrId`, `ProductId`),
UNIQUE INDEX `CustomrId_UNIQUE` (`CustomrId` ASC) VISIBLE);
Check if the table exists using the SQL SELECT statement as shown in the below image.
如下图所示,使用SQL SELECT语句检查表是否存在。
Now we will remove the table using SQL DROP TABLE command.
现在,我们将使用SQL DROP TABLE命令删除该表。
Query
询问
DROP TABLE customer;
Executing the above query will remove the customer table along with all the constraints, data, index, etc.
执行以上查询将删除客户表以及所有约束,数据,索引等。
PostgreSQL删除表 (PostgreSQL Drop Table)
Create a Customer table using below query.
使用下面的查询创建一个Customer表。
CREATE TABLE "test.customer"
( "CustomerId" integer NOT NULL,
"CustomerName" character varying(45),
"ProductId" character varying(45),
"Country" character varying(45),
PRIMARY KEY ("CustomerId","ProductId") );
Using SQL Select check if the table exists, as shown below.
使用SQL选择检查表是否存在,如下所示。
Now we will drop the table using below query.
现在,我们将使用以下查询删除表。
DROP table public."Customer";
SQL Server删除表 (SQL Server Drop Table)
Create a Customer table using below query.
使用下面的查询创建一个Customer表。
CREATE TABLE customer
( CustomrId INT NOT NULL,
CustomerName VARCHAR(45) NULL,
ProductId VARCHAR(45) NOT NULL,
State VARCHAR(45) NULL,
PRIMARY KEY (CustomrId, ProductId));
Use SQL select to validate if the table exists.
使用SQL select验证表是否存在。
Now we will drop the table using below query.
现在,我们将使用以下查询删除表。
DROP TABLE dbo.customer;
摘要 (Summary)
SQL Drop Table command will delete the table and all the data associated with it. So it’s advisable to have a backup first before you actually drop the table.
SQL Drop Table命令将删除该表以及与其关联的所有数据。 因此,建议您在实际删除表之前先进行备份。
However, if there are foreign key constraints on the table with another table then the error will be thrown. You will first have to explicitly remove the foreign-key constraints and then drop the table.
但是,如果在具有另一个表的表上存在外键约束,则将引发错误。 您首先必须明确删除外键约束,然后删除表。
sql删除表结构