SQL server deletes foreign key constraints

Source: Internet
Author: User

X first find the constraint name

Then delete it.

Here is an example.

-- Test environment

-- Master table

Create table test1 (id int primary key not null, value int)

Insert test1 select 1, 2

Go

-- Slave table

Create table test2 (id int references test1 (id), value int)

Go

-- Step 1: Find the foreign key constraint name in Table test2

-- 2000

Exec sp_helpconstraint 'test2'

-- The foreign key constraint name can be found in the constraint_name attribute.

-- 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 2: delete the foreign key constraint

Alter table test2 drop constraint FK _ test2 _ id _ 08EA5793

-- Step 3: Check whether the table contains foreign key constraints.

-- You only need to use the search statement in step 1.

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.