SQL Server sets multiple fields as the primary key method

Source: Internet
Author: User

Add the settings for the database multi-field composite primary key.

First, a table cannot have more than one primary key. But there are multiple fields that can be combined into one primary key, which is why sometimes the table has multiple fields with a primary key, because they are combined into a primary key.
We can set it through the Enterprise Manager or through the T-SQL statement.

ALTER TABLE tablename ADD constraint Pk_name primary key (Colname1,colname2)

Enterprise Manager sets the primary key by pressing CTRL to select the field.

Multi-field primary key is when the selected field in the input information, all fields enter the same information will play the role of the primary key constraints.

For example: In the student information system, the class number and the school number with a field are set as the primary key, while the table entered the class number 1, card number 123. If you enter the class number 1, the card number 123, the primary key will play a role in the constraints. And if the input card number 1, the class number 234, or the card number 2, the class number 123. The constraint of the primary key will not be triggered.

SQL Server sets multiple fields as the primary key method

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.