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.