Search code examples
sql-serversql-server-2008sql-server-2000

How to get FK script from existing tables in SQL server?


Suppose I have many tables which have Foreign keys created.

I want to get only the scripts for those FKs like so:

ALTER TABLE dbo.MyTable ADD CONSTRAINT...

I've tried to use tools from Studio management, however, it returns the script and create table together. I only want to return the FK scripts.

How can I return only the FK scripts from an SQL table?


Solution

  • If you're using Management Studio, expand the Keys tree of the table you want to script for, right-click the Foreign Key and choose Script key as... CREATE from the menu.