DROP IF EXISTS VS DROP?

前端 未结 5 523
南方客
南方客 2020-12-07 09:29

Can someone tell me if there is any difference between

DROP IF EXISTS [TABLE_NAME]
DROP [TABLE_NAME]

I am askin

5条回答
  •  时光说笑
    2020-12-07 09:58

    Standard SQL syntax is

    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

    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.

提交回复
热议问题