Adding, deleting data when SQL Server has a primary foreign key relationship

Source: Internet
Author: User

Delete data is constrained when there is a primary foreign key relationship between tables, add, delete fails

Solution, we can first set the check of the primary foreign key relationship to shut down → then delete the data → then open the constraint

Query the statement that turns off all foreign key constraints

SELECT 'ALTER TABLE' +O.name+ 'NOCHECK CONSTRAINT [' +F.name+ '];'  asCOMMAND fromSYS. Foreign_keys FINNER JOINSYS. All_objects O onf.parent_object_id=O.object_id WHEREO.type= 'U'  andF.type= 'F';

Query the statement that opens all foreign key constraints, the difference from the above is to change the NOCHECK to CHECK

SELECT 'ALTER TABLE' +O.name+ 'CHECK CONSTRAINT [' +F.name+ '];'  asCOMMAND fromSYS. Foreign_keys FINNER JOINSYS. All_objects O onf.parent_object_id=O.object_id WHEREO.type= 'U'  andF.type= 'F';

Adding, deleting data when SQL Server has a primary foreign key relationship

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.