Use of FOREIGN key foreign in SQL Server version 2008r2

Source: Internet
Author: User

1. In the database design process often want to let 2 tables to correlate and use to foreign thus strengthen the constraints between the 2 tables ()

Once a problem has not been understood, that is the value of the foreign key column, the normal case of the table column value can be null or satisfy the column of the type defined by the data, but the foreign key is not the same?

The answer is clearly not, what is the situation?

The official help document shows that the FOREIGN key constraint is not only linked to the PRIMARY key constraint of another table, it can also be defined as a UNIQUE constraint that references another table. The FOREIGN key constraint can contain null values, but if any of the columns that combine FOREIGN key constraints contain null values, the validation of all values that make up the FOREIGN key constraint is skipped. "


2. In order to verify the official statement, it was deliberately tested, so-called "experimentation is the only criterion to test truth."

First, insert a data into the usertype table--insert usertype (id,typename,createdatetime,modifydatetime) VALUES (NEWID (), ' System Administrator ', GETDATE ( ), GETDATE ())

The first time I inserted a piece of data into userinfo (from the table), but the foreign key here Usertypeid I didn't give the value, but the data was inserted successfully.

Second insertion ()

The foreign key I gave a value of "satisfy type", result insert failed

Last insertion ()

OK Insert success!

3. It is advisable to borrow the official words "theFOREIGN key constraint can contain null values, but if any of the columns that combine FOREIGN key constraints contain null values, the validation of all the values that make up the FOREIGN key constraint will be skipped ."

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.