SQL Server Performance

Generate Only ForeignKeys Script

Discussion in 'SQL Server 2005 General Developer Questions' started by madhuottapalam, Dec 10, 2006.

  1. madhuottapalam New Member

    Hi Experts,

    Is there any way to generate only foreignkey script(CREATE /Drop ) in the database. ie. without Table script.

    thanks

    Madhu
  2. FrankKalis Moderator

    It should be doable using the INFORMATION_SCHEMA.REFERENTIAL_CONTRAINTS view.

    --
    Frank Kalis
    Moderator
    Microsoft SQL Server MVP
    Webmaster:http://www.insidesql.de
  3. dineshasanka Moderator

    small error
    it is INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS

    ----------------------------------------
    Contributing Editor, Writer & Forums Moderator
    http://www.SQL-Server-Performance.Com

    Visit my Blog at
    http://dineshasanka.spaces.live.com/
  4. FrankKalis Moderator

  5. dineshasanka Moderator

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlpro03/html/sp03l12.asp

    ----------------------------------------
    Contributing Editor, Writer & Forums Moderator
    http://www.SQL-Server-Performance.Com

    Visit my Blog at
    http://dineshasanka.spaces.live.com/
  6. jezemine New Member

    here's a free tool I wrote that will do this for you:

    http://www.elsasoft.org/tools.htm

    EDIT: it generates scripts for ALL objects (including fks), a separate file for each. you could easily modify it to generate scripts for ONLY fks if that's your need. source code is available.

    www.elsasoft.org
  7. MohammedU New Member

  8. DilliGrg Member

    <blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by MohammedU</i><br /><br />What about...<br />select * from sys.foreign_keys<br /<a target="_blank" href=http://msdn2.microsoft.com/en-us/library/ms189807.aspx>http://msdn2.microsoft.com/en-us/library/ms189807.aspx</a><br /><br /><br />Mohammed U.<br /><hr height="1" noshade id="quote"></font id="quote"></blockquote id="quote"><br /><br /><br />Pretty cool. [<img src='/community/emoticons/emotion-1.gif' alt=':)' />]<br /><br />Thanks,<br />DilliGrg
  9. madhuottapalam New Member

    Hi Mohammed,

    My qeustion was how to create *Only* ForeignKey script of a Database

    select * from sys.foreign_keys , this query only will give u the list of FKs

    I was wondering whether there is any customized script or tool available. I think I must try

    http://www.elsasoft.org/tools.htm, as told by jezemine


    Thanks .....

    regards

    Madhu

  10. MohammedU New Member

    If you want details then use sys.foreign_keys view but if you want to create the script to generated for fks then you have to use some tool or sp_OA.... objects...


    Mohammed U.

Share This Page