cannot drop table users because other objects depend on it

前端 未结 3 1706
不知归路
不知归路 2021-02-05 00:36

I want to drop my tables in database. But, when I use, for example, DROP TABLE if exists users; I receive this message:

cannot drop tab

3条回答
  •  逝去的感伤
    2021-02-05 01:01

    In general, to drop several interdependent tables you start from the tables that nothing depends on (the ones that have foreign keys pointing to other tables), and work backwards. E.g., if the table transactions depends on the table users, you'd drop transactions first. In short: Delete tables in the reverse order from how they were created.

    If you manage to create tables with circular dependencies, you can first delete the foreign key constraint that prevents deletion. Or you can use the modifier CASCADE, which (as @a_horse explained in the comments), will drop any foreign key constraints that involve the deleted table. But note that not all DBMS's support CASCADE: Postgres does, but MySQL does not (the keyword is accepted but has no effect).

提交回复
热议问题