SQL Server finds all tables for which a foreign key is referenced

Source: Internet
Author: User

Directly on the code:

1 SELECTOsub.name as [child table name] ,2Fk.name as [FOREIGN Key Name] ,3Subcol.name as [child table column name] ,4Omain.name as [Primary Table name] ,5Maincol.name as [primary table column name]6  fromSys.foreign_keys FK7         JOINSys.all_objects osub on(fk.parent_object_id=Osub.object_id )8         JOINSys.all_objects Omain on(fk.referenced_object_id=Omain.object_id )9         JOINSys.foreign_key_columns Fkcols on(FK.object_id =fkcols.constraint_object_id)Ten         JOINSys.columns Subcol on(Osub.object_id =Subcol.object_id One                                       andfkcols.parent_column_id=subcol.column_id A                                    ) -         JOINSys.columns Maincol on(Omain.object_id =Maincol.object_id -                                        andfkcols.referenced_column_id=maincol.column_id the                                     ) - WHEREFk.name like @FOREIGN KEY;
@FOREIGN KEY

SQL Server finds all tables for which a foreign key is referenced

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.