Methods for establishing foreign keys in SQL Server

Source: Internet
Author: User

A FOREIGN KEY constraint is established in SQL to cascade the data in the query table, and in C # code generator, the corresponding foreign key table data model can be generated according to the foreign key relationship. Foreign keys also prevent the deletion of records with foreign key relationships, to some extent protecting the security of the data.

Steps:

1, to establish a foreign key relationship, first of all to ensure that the column used to establish a foreign key relationship is unique, that is, a unique constraint is usually the primary key of a table as a foreign key of another table

2, open the database table and locate the table where you want to create the foreign key. and ensure that the columns in which the foreign key relationship is to be established are exactly the same as the data types in the primary key table

 

3, in the table where you want to create a foreign key relationship, right-click on any column and select relationship

 

4, in the Foreign Key Relationship dialog box, click Add

in the lower-left corner

 

5, click on the "Table and column Specification" item to the right of ... Small button

 

6, in the Table and Columns dialog box, under the Primary key table, select the table that contains the foreign key column and the foreign key column

7, under the Foreign key table, simply select the columns in the table that correspond to the columns of the primary key table

 

8 then click "OK" and go back to the foreign key relationship table

9, if there is a foreign key relationship needs to be established, continue to click on the bottom left side of the foreign key relationship table "Add" No, click "Close" to

10, at this point, a primary foreign key relationship has been established successfully. Right-click any column in the table and select the relationship to see .

source: http://jingyan.baidu.com/article/ 5225f26b05a25de6fa0908ae.html

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.