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.