Those keys and constraints in the Sqlsever

Source: Internet
Author: User

SQL  server Primary key constraints, Foreign key constraints, unique constraints, default constraints, and check constraints. View or create constraints to use to   microsoft sql server  Managment studio

1. PRIMARY KEY constraint

There is often a combination of one or more columns in a table whose values uniquely identify each row in the table. Such a column or columns becomes the primary key of the table (Primary key). A table can have only one primary key, and a column in a PRIMARY KEY constraint cannot be a null value.

view primary key constraints can be in object explorer databases  – >  Select the database you want to view (in my case, chargesystem Span style= "font-family: Song Body" >)  – >  table  – >  The table you want to view (in my case, basictale " ->  column

As shown,Primary key has a small golden key. Modifyidea is the primary key for the basictable table.

To create a PRIMARY key constraint, you can right-click on the table and select design, open the Table Designer, select the row, and click on the Golden key above to create the PRIMARY key.

You can also right-click the row that you want to set as the primary key, and then choose Set Key.

2. FOREIGN KEY constraint

foreign key Span style= "Font-family:times New Roman" > (Foreign key) ( Main table and from table " The connection between one or more columns of data. The order in which constraints are created is to define the primary key for the primary table, and then to define a foreign key constraint from the table.

View FOREIGN key constraints, expand the column, you can see the small gray key for FOREIGN key; expand keystosee The name of the Foreign Key constraint is fk_contact_company.

In the Table Designer, you can also click the Relationships button above, so you can see all the Foreign Key constraints

The above example can see the operatorid of the billtable table as the foreign key, the operatoridea table Operatoridea the primary key.

Here's a demonstration of how to create the Foreign Key constraint.

Also, after clicking on the Relationship button, select Add in the dialog box that pops up

Then click the button in the red box below:

Set the primary table, primary key, and from table, foreign key as in

Then click OKand don't forget to save your design.

3.  unique constraint (unique key or index)

UNIQUE constraints are used to ensure that two rows of data in a table do not have the same column values in a non-primary key. like the PRIMARY key constraint, a unique constraint is also mandatory for uniqueness, but a unique constraint is used for one or more column combinations of non-primary keys. And a table can define multiple unique constraints, and a unique constraint can be used to define multiple-column combinations.

Also take billtable As an example, suppose we want to constrain Billidea to be unique, click Manage indexes and keys

Then click Add to add a Unique constraint

Select column to be billid(ASC) , is the only to be is a .

Close and save your design so that a Unique constraint is created.

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.