SQL resolution cannot delete table, prompt to be referenced by foreign KEY constraint

Source: Internet
Author: User

The drop TABLE command is used to delete a table or all rows in a table. The syntax format is:

drop TABLE "TableName"

Here's an example:

drop table employee;

To delete the entire table (including all rows), you can use the drop TABLE command to add tablename. The drop TABLE command is not the same as deleting all records in a table:

Tip "Cannot delete object ' Orders ' because the object is being referenced by a FOREIGN key constraint" because it is simply not irritable, it is referenced by foreign keys in other tables, so it cannot be deleted, so you just need to find out which table's foreign keys reference the table's fields. Can be solved by system functions (SQL Server system functions provide a very complete function to find and solve many problems in place of us).

The code is as follows Copy Code

Select
Fk.name,fk.object_id,object_name (fk.parent_object_id) as Referencetablename
From Sys.foreign_keys as FK
Join sys.objects as O on fk.referenced_object_id=o.object_id
where o.name= ' referenced table name '

The above SQL statement can obtain which foreign key references a table, and also shows the table name of the foreign-key table. You can delete a foreign key by executing the following statement with the table name and foreign key name of the foreign key table.


ALTER table dbo. Referenced table name DROP CONSTRAINT FOREIGN Key Name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.