SQL Server Create constraint plot unique primary key

Source: Internet
Author: User
Tags sql 2008

There are five types of constraints in SQL Server,Primary key constraints,Foreign key constraints,Unique constraints,Default constraints, and Check constraints, use SQL Server2008 today to demonstrate how these constraints are created and used.

"Reprint"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 (PrimaryKey) of the table. A table can have only one primary key, and a column in a PRIMARY KEY constraint cannot be a null value. Only the primary key column can be created as a foreign key for other tables.

To create a primary key constraint, you can right-click the table and select design.

Select the column where you want to create the primary key, and then click the Small key above.

You can also right-click the column where you want to create the primary key, and then press the small key.

2.Foreign Key constraint

FOREIGN KEY constraints are used to strengthen the connection between two tables (the primary table and the table) between one column or more columns of data. The order in which foreign key constraints are created is to first define the primary key of the primary table and then define the foreign key from the table. In other words, only the primary key of the main table can be used as a foreign key from the table, and the constrained column from the table may not be the primary key, and the primary table restricts the actions that are updated and inserted from the table.

Right-click the column where you want to set the foreign key (at which point the table appears as an external key from the table), and select the relationship.

Next click Add --table and column specifications.

In the primary key table, select the primary key column for the primary table and the primary table.

Save it when you're finished setting it up.

3.Unique constraint

A unique constraint ensures that a column of data in a table does not have the same value. Like a primary KEY constraint, a unique constraint also enforces uniqueness, but a unique constraint is used for a combination of one or more columns of a non-primary key, and a table can define multiple unique constraints.

Right-click the column you want to set to select the index / key.

And then click the Add button.

Select the columns you want to set, either a column or a combination of multiple columns.

Close and save the settings.

4.Default constraint

If a default value constraint is defined in a table, when a user inserts a new data row, if the row does not have the specified data, the system assigns the default value to the column, and the system defaults to NULL if we do not set the default value .

For the Student information table, for example, in Table Designer, fill in the default value for the sex sex column male.

5.Check constraint

a Check constraint uses a logical expression to determine the validity of the data and to limit the range of values that are entered into one or more columns. When you update data in a column, the content that you want to enter must meet the criteria for the Check constraint, or it will not be entered correctly.

In the case of sex in the Student information table , we want to limit the value of the sex column to only male or female.

Close and save the design.

To this, five kinds of constraints in the database are reviewed again, constraints ensure the integrity of the data in the database, but only the constraints are far from enough.

Report:

When a user changes the table structure in SQL Server 2008, you must delete the original table and recreate the new table to complete the table changes, and if you forcibly change the following prompt: Save changes are not allowed. Your changes require that the following tables be deleted and recreated. You have made changes to the labels that cannot be recreated or have enabled the Prevent saving changes that require the table to be recreated option. If you want to get rid of this hint, open SQL 2008 on the top tool-〉 options-〉 There is a designer-〉 Table Designer and Database Designer on the left-block save changes that require the table to be recreated (right) remove the hook.

SQL Server Create constraint plot unique primary key

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.