Those keys and constraints in the Sqlsever

Source: Internet
Author: User

There are five types of constraints in SQL Server, each of which is PRIMARY key constraint,FOREIGN key constraint,UNIQUE constraints, DEFAULT constraints, and CHECK constraints. View or create constraints that you want 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. This one or more columns become 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 sample, chargesystem . Span style= "font-family: Song Body" >)  – >  table  – >  The table you want to view (in my sample is basictale " ->  column

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

Create PRIMARY key constraint to right click on the table. then select Design. Open the Table Designer. Select the line and click on the Golden key above. To create the Primary Key.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhu5mzaxmjq=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

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

2. FOREIGN KEY constraint

A foreign key (Foreign key) is used to establish and strengthen a connection between two tables ( the primary table and A column or columns of data from a table).

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 the FOREIGN KEY constraint. Expand the column to see the small gray key as Foreign key; expand keys. The name of the Foreign Key constraint can be seen as Fk_contact_company.

In the Table Designer, you can also click the Relationship button above. This allows you to see all of the Foreign Key constraints

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhu5mzaxmjq=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

The above example can see that the Operatorid of the billtable table is a foreign key, and the operatoridea table Operatoridea the primary key.

To demonstrate how to create the Foreign Key constraint.

The same is done after clicking on the Relationship button, select Join in the dialog box that pops up

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbhu5mzaxmjq=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

Then click on 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. If we want to constrain Billidea to be unique, click Manage indexes and keys

Then click Add to add the 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.

Those keys and constraints in the Sqlsever

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.