MySQL 表的 DROP 和 DELETE 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8555154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:05:46  来源:igfitidea点击:

What is the difference between DROP and DELETE for tables?

mysqlsqldatabase

提问by Omneya

Which one will deleteall data from the table, and which one will remove the tablefrom the database?

哪一个会从表中删除所有数据,哪一个会从数据库中删除表

What is the right thing to do if I don't want this table in my database any more? Should I drop or delete?

如果我不想再在我的数据库中使用此表,该怎么办?我应该删除还是删除?

回答by Sona

DROP command deleting the table and its structure from the data base.

DROP 命令从数据库中删除表及其结构。

DROP TABLE tbl_user;

DELETE command used for deleting the records from the table,and it removing the table space which is allocated by the data base, and returns number of rows deleted.

DELETE 命令用于从表中删除记录,并删除数据库分配的表空间,并返回删除的行数。

DELETE FROM tbl_user WHERE id = 1;

TRUNCATE command is also delete the records but it doesn't delete the table space which is created by the data base, and does not return number of deleted rows.

TRUNCATE 命令也是删除记录,但它不删除数据库创建的表空间,也不返回删除的行数。

TRUNCATE TABLE tbl_user;

回答by ReadWriteCode

DROPis used to remove tables (and databases).

DROP用于删除表(和数据库)。

DELETEis used to delete rows from tables.

DELETE用于从表中删除行。

回答by SlavaNov

Maybe are you talking about TRUNCATEand DELETE?

也许你在谈论TRUNCATEDELETE

TRUNCATE TABLE users;

is equivalent (logically) to

等价于(逻辑上)

DELETE FROM users;

This will erase all data in table users. If you want to delete the whole table structure you should write:

这将删除 table 中的所有数据users。如果你想删除整个表结构,你应该写:

DROP TABLE users;

But, DELETE is DML command while TRUNCATE and DROP are DDL commands. There is also some other differences in different RDBMS. More info - here

但是,DELETE 是 DML 命令,而 TRUNCATE 和 DROP 是 DDL 命令。不同的 RDBMS 还存在一些其他差异。更多信息 -在这里

And another useful link: Difference between TRUNCATE, DELETE and DROP commands

另一个有用的链接:TRUNCATE、DELETE 和 DROP 命令之间的区别

回答by Mat

dropremoves the contents andthe table (but not user permissions on the table). This is what you want if you want to completely remove the table from your schema.

drop删除内容表(但不删除表的用户权限)。如果您想从架构中完全删除表,这就是您想要的。

deleteselectively (or not) removes rows from a table. It does not alter the table structure.

delete有选择地(或不)从表中删除行。它不会改变表结构。

回答by abcde123483

There's no such thing as DELETE TABLE. You should use DROP TABLEto delete your table.

没有DELETE TABLE这样的东西。您应该使用DROP TABLE删除您的表。

回答by Debasish Pati

The DROP TABLE statement is used to delete a table.

DROP TABLE 语句用于删除表。

DROP TABLE table_name

删除表表名

The DELETE statement is used to delete data(records) in a table.

DELETE 语句用于删除表中的数据(记录)。

DELETE FROM table_name WHERE some_column=some_value;

DELETE FROM table_name WHERE some_column=some_value;

So, as you want to delete table from database then you will go for DROPCommand

因此,当您想从数据库中删除表时,您将使用DROP命令

回答by Laxmi

delete- removes the rows from the table - where clause can be used to delete specific rows. - If where is not specified, then all the rows in the table will be removed - rollback can be done.

delete- 从表中删除行 - where 子句可用于删除特定行。- 如果没有指定 where,那么表中的所有行都将被删除 - 可以进行回滚。

without using where condition

不使用 where 条件

DELETE * FROM empl;

using where condition

使用 where 条件

DELETE FROM empl WHERE job = 'Manager';

truncate- removes all the rows from the table. - rollback cannot be done.

truncate- 从表中删除所有行。- 无法进行回滚。

SQL> TRUNCATE TABLE empl;

drop- removes a table from the database. - rows, indexes and privileges will also be removed. - rollback cannot be done.

drop- 从数据库中删除一个表。- 行、索引和权限也将被删除。- 无法进行回滚。

SQL> DROP TABLE empl;

回答by Alok Paul.

Delete remove all data from a specific table and drop remove whole database and also remove specific table from database. If you don't want the table in database any more then you should drop the table.

删除从特定表中删除所有数据并删除删除整个数据库并从数据库中删除特定表。如果您不再需要数据库中的表,那么您应该删除该表。

回答by Christofer Eliasson

DELETE is used to delete one or several rows from the table. DROP TABLE would remove the entire table from the database, so if you want to remove the table, you should use DROP TABLE.

DELETE 用于从表中删除一行或几行。DROP TABLE 会从数据库中删除整个表,所以如果你想删除表,你应该使用 DROP TABLE。

DROP TABLE reference

删除表参考

回答by user1104966

Delete will update the log, while drop does not.

Delete 会更新日志,而 drop 不会。

Delete is used to remove the rows, while drop is used to remove the tables and DB.

Delete 用于删除行,而 drop 用于删除表和 DB。