SQLServer sets two primary keys, and sqlserver sets the primary key

Source: Internet
Author: User

SQLServer sets two primary keys, and sqlserver sets the primary key
Zookeeper

First, a table cannot have two primary keys. However, two fields can be combined into a primary key, which is why two fields in the table sometimes have a primary key flag, because they are combined into a primary key.
We can delete the original primary key and use TSQL to create it.
Alter table tablename add constraint pk_name primary key (colname1, colname2)


Or press ctrl in the Table Designer to select the two columns to set the primary key and then set the primary key.


How to use code to set two primary keys in SQL server

It is actually very simple. For example, how to set two primary keys

-- Create a student table
Create table student TABLE
(
Student ID int not null primary key (student ID ),
Name nvarchar (20) not null,
Gender bit,
Birth date int not null,
Encoded int not null
)

-- Create a course schedule
Create table curriculum
(
Course No. int not null primary key (course No ),
Course name nvarchar (100) not null,
Elective Course number int,
Credits decimal (2, 1)
)

-- Create a course selection table
Create table Course Selection TABLE
(
Student ID int not null,
Course No. int not null,
Decimal (3, 1)
Primary key (student ID, course number) -- set student ID and course number as the primary key
)

In my SQLserver database, how does one table have two primary keys and one table does not have one primary key? Does each table have a primary key?

It's okay if you don't set the primary key, but the primary key has an irreplaceable effect.
Ensure the integrity of entities;
Accelerate database operations
When a new record is added to a table, SQL server automatically checks the primary key value of the new record and does not allow this value to be fu-heavy with the primary key value of other records.
SQL server automatically displays records in the table in the order of primary key values. If no primary key is defined, records in the table are displayed in the order of input records.

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.