How to set a primary key when creating a table in SQL Server?

In SQL Server, you can use the following syntax to set a primary key when creating a table:

CREATE TABLE table_name
(
    column1 data_type PRIMARY KEY,
    column2 data_type,
    ...
)

In the grammar above, column1 is the name of the column that should be set as the primary key, data_type is the data type of the column, and PRIMARY KEY is used to specify that the column is the primary key.

If you want to set multiple columns as primary keys, you can add multiple column names after the PRIMARY KEY keyword, separated by commas.

For example, the following example creates a table named students, with the id column set as the primary key:

CREATE TABLE students
(
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT
)

In the above example, the id column is designated as the primary key with a data type of INT.

广告
Closing in 10 seconds
bannerAds