SQL 如果存在则丢弃 VS 丢弃?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9565818/
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
DROP IF EXISTS VS DROP?
提问by AbdulAziz
Can someone tell me if there is any difference between
有人可以告诉我两者之间是否有任何区别
DROP IF EXISTS [TABLE_NAME]
DROP [TABLE_NAME]
I am asking this because I am using JDBC template in my MVC web application. If I use DROP [TABLE_NAME]
the error said that Table exist. And if I use DROP IF EXISTS [TABLE_NAME]
it says bad SQL grammar. Can some one help?
我问这个是因为我在我的 MVC Web 应用程序中使用了 JDBC 模板。如果我使用DROP [TABLE_NAME]
错误说表存在。如果我使用DROP IF EXISTS [TABLE_NAME]
它,则表示 SQL 语法错误。有人可以帮忙吗?
回答by Mike Sherrill 'Cat Recall'
Standard SQL syntax is
标准 SQL 语法是
DROP TABLE table_name;
IF EXISTS
is not standard; different platforms might support it with different syntax, or not support it at all. In PostgreSQL, the syntax is
IF EXISTS
不标准;不同的平台可能以不同的语法支持它,或者根本不支持它。在 PostgreSQL 中,语法是
DROP TABLE IF EXISTS table_name;
The first one will throw an error if the table doesn't exist, or if other database objects depend on it. Most often, the other database objects will be foreign key references, but there may be others, too. (Views, for example.) The second will not throw an error if the table doesn't exist, but it will still throw an error if other database objects depend on it.
如果表不存在,或者其他数据库对象依赖于它,第一个将抛出错误。大多数情况下,其他数据库对象将是外键引用,但也可能有其他对象。(例如,视图。)如果表不存在,第二个不会抛出错误,但如果其他数据库对象依赖它,它仍然会抛出错误。
To drop a table, and all the other objects that depend on it, use one of these.
要删除一个表以及依赖它的所有其他对象,请使用其中一个。
DROP TABLE table_name CASCADE;
DROP TABLE IF EXISTS table_name CASCADE;
Use CASCADE with great care.
小心使用 CASCADE。
回答by Squazz
It is not what is asked directly. But looking for how to do drop tables properly, I stumbled over this question, as I guess many others do too.
不是直接问的。但是在寻找如何正确执行删除表的问题时,我偶然发现了这个问题,我想很多其他人也是如此。
From SQL Server 2016+ you can use
从 SQL Server 2016+ 你可以使用
DROP TABLE IF EXISTS dbo.Table
For SQL Server <2016 what I do is the following for a permanent table
对于 SQL Server <2016,我对永久表执行以下操作
IF OBJECT_ID('dbo.Table', 'U') IS NOT NULL
DROP TABLE dbo.Table;
Or this, for a temporary table
或者这个,对于临时表
IF OBJECT_ID('tempdb.dbo.#T', 'U') IS NOT NULL
DROP TABLE #T;
回答by juergen d
You forgot the table
in your syntax:
你忘记了 table
你的语法:
drop table [table_name]
which drops a table.
它掉了一张桌子。
Using
使用
drop table if exists [table_name]
checks if the table exists before dropping it.
If it exists, it gets dropped.
If not, no error will be thrown and no action be taken.
在删除表之前检查表是否存在。
如果它存在,它将被丢弃。
如果不是,则不会抛出任何错误并且不会采取任何措施。
回答by Flakron Bytyqi
DROP TABLE IF EXISTS [table_name]
it first checks if the table exists, if it does it deletes the table while
它首先检查表是否存在,如果存在则删除该表,同时
DROP TABLE [table_name]
it deletes without checking, so if it doesn't exist it exits with an error
它会在不检查的情况下删除,因此如果它不存在,则会退出并显示错误
回答by SJuan76
If no table with such name exists, DROP
fails with error while DROP IF EXISTS
just does nothing.
如果不存在具有此类名称的表,DROP
则会失败并显示错误,而DROP IF EXISTS
什么也不做。
This is useful if you create/modifi your database with a script; this way you do not have to ensure manually that previous versions of the table are deleted. You just do a DROP IF EXISTS
and forget about it.
如果您使用脚本创建/修改数据库,这将很有用;这样您就不必手动确保删除先前版本的表。你只做一个DROP IF EXISTS
然后忘记它。
Of course, your current DB engine may not support this option, it is hard to tell more about the error with the information you provide.
当然,您当前的数据库引擎可能不支持此选项,您提供的信息很难详细说明错误。