SQL Server deletes a FOREIGN key constraint

Source: Internet
Author: User

X First find the constraint name

And then delete it

I'll give you an example.

--Test environment

--Main Table

CREATE table test1 (ID int primary key not null,value int)

Insert Test1 Select 1,2

Go

--From the table

CREATE table test2 (id int references test1 (ID), value int)

Go

--First step: Find out the FOREIGN KEY constraint name on the Test2 table

--2000

exec sp_helpconstraint ' test2 '

--The FOREIGN KEY constraint name can be found in the Constraint_name property

--2005

Select Name

From Sys.foreign_key_columns F join sys.objects O on f.constraint_object_id=o.object_id

where f.parent_object_id=object_id (' test2 ')

/*

Name

---------------------------------

fk__test2__id__08ea5793*/

--Step Two: Delete the foreign KEY constraint

ALTER TABLE TEST2 drop constraint fk__test2__id__08ea5793

--Step Three: Check if there are foreign key constraints on the table

--just use the Find statement in the first step

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.