Cocould not drop object & #39; student & #39; because it is referenced by a foreign key constraint, constraint

Source: Internet
Author: User

Cocould not drop object 'student 'because it is referenced by a foreign key constraint, constraint

1. Find foreign keys

SELECT * FROM sys. foreign_keysWHERE referenced_object_id = object_id ('student ')

2. Delete foreign keys

SELECT 'alter table' + OBJECT_SCHEMA_NAME (parent_object_id) + '. [' + OBJECT_NAME (parent_object_id) + '] drop constraint' + nameFROM sys. foreign_keysWHERE response = object_id ('student ')


The SQL truncate table prompts a foreign key error.

The nocheck option is only useful for inserting and updating records, but not for truncate. It is useless to disable the foreign key of MRB_OrdersHeaders, the main reason for not truncate is that the MRB_OrdersHeaders table is referenced by foreign keys of other tables.
If the business permits, the corresponding foreign keys of other tables that reference the MRB_OrdersHeaders table are deleted.
If you want to retain those foreign keys, delete all the records in the corresponding table.
For example, if the MRB_OrdersHeaders table is referenced by Table A, delete the records in Table A first. Because cascade references may be involved, you need to delete them at the first level.
After deletion, it cannot be truncate and can only be deleted.

IF OBJECT_ID (N 'student ') is not null drop table student go This is a T-SQL statement, explained in detail

This statement is used to determine whether STUDENT exists in this table. If so, delete it! OBJECT_ID () is actually a system function used to return the Object ID number, because the same table name does not exist in a database in SQL Server, and each table name is identified by ID, so we can use this function to determine whether an object exists.

Related Article

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.