SQL server2008 How to create a foreign key

Source: Internet
Author: User

The primary key was created when the database was created, but the relationship between the tables was not linked, and it was not possible to build the database diagram only if the primary key did not have a foreign key.

The foreign key is built on the premise that the key must be the primary key of the other table. Steps to build a foreign key:

The first step is to open the designer for the foreign key table you want to build, and right-click to select Relationships.

Then the foreign key relationship form pops up, we select Add, and then click the Small button after table and column specification,

The other form will pop up and let's select the primary key table and column, then click OK after selecting it.

Then we expand the Insert and update specifications, there are four options for updating the rule and deleting the rule, namely "do Nothing," "Cascade," "Set to NULL," "Set default value."

The default does not take any action. If you do not do anything, when we delete or update data from the primary key table, we tell the user that the operation cannot be deleted or updated.

"cascading" means that when we delete or update data from the primary key table, all rows of related data involved in the foreign key table are deleted or updated.

"Set NULL" means that when we delete or update data from the primary key table, the value of the foreign key column in the Foreign key table is set to NULL, provided that the column is allowed to be empty.

"Set default" means that if we define a default value for a foreign key column, the value of the foreign key column in the Foreign key table is set to the default value defined when we delete or update the data in the primary key table.

Of course we can create it with code, and when we create the database table T--card, just add a sentence, "Foreign key (Studentno) references t_student (Studentno)". If we have already created a change chart, then how to implement it in code, it is also very simple saying "add constraint cmpkey (foreign key name) foreign key (Studentno) references t_student (Studentno)".

SQL server2008 How to create a foreign key

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.