Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 drop foreign key

Author  Topic 

eevans
Starting Member

48 Posts

Posted - 2009-02-20 : 09:03:52
Hello,

I created a foreign key using the following syntax:

ALTER TABLE alumni_master
FOREIGN KEY (user_def_id_2) REFERENCES name_master(id_num)

Now I want to drop that foreign key constraint but I can't seem to write an appropriate statement to do so.

Any suggestions?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-20 : 09:25:42
just use ALTER TABLE alumni_master DROP CONSTRAINT . get constraint name from view as below:-

SELECT tc.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c
ON c.CONSTRAINT_NAME=tc.CONSTRAINT_NAME
WHERE c.COLUMN_NAME='user_def_id_2'
AND tc.TABLE_NAME='alumni_master'
AND tc.CONSTRAINT_TYPE='FOREIGN KEY'


then use it in

ALTER TABLE alumni_master DROP CONSTRAINT <name obtained from above>
Go to Top of Page

eevans
Starting Member

48 Posts

Posted - 2009-02-20 : 09:30:11
Thanks! I never would have figured that out.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-20 : 09:32:55
you're welcome
Go to Top of Page
   

- Advertisement -