I got error “The DELETE statement conflicted with the REFERENCE constraint”

前端 未结 4 727
醉酒成梦
醉酒成梦 2020-12-01 23:08

I tried to truncate a table with foreign keys and got the message:

\"Cannot truncate table because it is being referenced by a FOREIGN KEY co

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 00:01

    To DELETE, without changing the references, you should first delete or otherwise alter (in a manner suitable for your purposes) all relevant rows in other tables.

    To TRUNCATE you must remove the references. TRUNCATE is a DDL statement (comparable to CREATE and DROP) not a DML statement (like INSERT and DELETE) and doesn't cause triggers, whether explicit or those associated with references and other constraints, to be fired. Because of this, the database could be put into an inconsistent state if TRUNCATE was allowed on tables with references. This was a rule when TRUNCATE was an extension to the standard used by some systems, and is mandated by the the standard, now that it has been added.

提交回复
热议问题