SQL Server Constraints

Source: Internet
Author: User

FK _ foreign key

 

Select tab for primary key constraints. name as [Table name], idx. name as [primary key name], Col. name as [primary key column name] From sys. indexes idx join sys. index_columns idxcol on (idx. object_id = idxcol. object_id and idx. index_id = idxcol. index_id and idx. is_primary_key = 1) join sys. tables tab on (idx. object_id = tab. object_id) join sys. columns Col on (idx. object_id = Col. object_id and idxcol. column_id = Col. column_id); the only constraint is select tab. name as [Table name], idx. name as [constraint name], Col. name as [constraint column name] From sys. indexes idx join sys. index_columns idxcol on (idx. object_id = idxcol. object_id and idx. index_id = idxcol. index_id and idx. is_unique_constraint = 1) join sys. tables tab on (idx. object_id = tab. object_id) join sys. columns Col on (idx. object_id = Col. object_id and idxcol. column_id = Col. column_id); foreign key constraint select osub. name as [subtable name], FK. name as [foreign key name], subcol. name as [subtable column name], omain. name as [master table name], maincol. name as [primary table column name] From sys. foreign_keys FK join sys. all_objects osub on (FK. parent_object_id = osub. object_id) join sys. all_objects omain on (FK. referenced_object_id = omain. object_id) join sys. foreign_key_columns fkcols on (FK. object_id = fkcols. constraint_object_id) join sys. columns subcol on (osub. object_id = subcol. object_id and fkcols. parent_column_id = subcol. column_id) join sys. columns maincol on (omain. object_id = maincol. object_id and fkcols. referenced_column_id = maincol. column_id) Check constraints select tab. name as [Table name], chk. name as [Check constraint name], Col. name as [column name], chk. definitionfrom sys. check_constraints chk join sys. tables tab on (chk. parent_object_id = tab. object_id) join sys. columns Col on (chk. parent_object_id = Col. object_id and chk. parent_column_id = Col. column_id)
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.