Home > Database > SQL > body text

Briefly understand SQL Server primary key constraints (PRIMARY KEY)

WBOY
Release: 2022-09-06 17:30:57
forward
3089 people have browsed it

This article brings you relevant knowledge about SQL server, which mainly introduces SQL Server primary key constraints (PRIMARY KEY). The primary key is a column or a group that uniquely identifies each row in the table. Column, the article expands on the topic in detail. Let’s take a look at it together. I hope it will be helpful to everyone.

Briefly understand SQL Server primary key constraints (PRIMARY KEY)

Recommended study: "SQL Tutorial"

SQL Server PRIMARY KEY (primary key) constraint introduction

Primary key Is a column or set of columns that uniquely identifies each row in a table. You can use primary key constraints to create a primary key for a table.
If the primary key contains only one column, you can use PRIMARY KEY constraints as column constraints:

CREATE TABLE table_name (
    pk_column data_type PRIMARY KEY,
    ...
);
Copy after login

If the primary key has two or more columns, you must use the primary key constraint Table constraints:

CREATE TABLE table_name (
    pk_column_1 data_type,
    pk_column_2 data type,
    ...
    PRIMARY KEY (pk_column_1, pk_column_2)
);
Copy after login

Each table can only contain one primary key, and one primary key can contain multiple columns, that is, the combination of multiple columns cannot be repeated. All columns participating in the primary key must be defined as NOT NULL. If NOT NULL constraints are not specified for all primary key columns, SQL Server automatically sets non-null constraints for these columns.

SQL Server PRIMARY KEY constraint example

The following example creates a table with a primary key consisting of one column:

CREATE TABLE dbo.activities (
    activity_id INT PRIMARY KEY IDENTITY,--主键
    activity_name VARCHAR (255) NOT NULL,
    activity_date DATE NOT NULL
);
Copy after login

In table## In #dbo.activities, the activity_id column is the primary key column, which means that the value of this column cannot be repeated.

IDENTITY attribute is used for activity_idColumns automatically generate unique integer values.

The following creates a new table with two columns that form the foreign key:

CREATE TABLE dbo.participants(
    activity_id int,
    customer_id int,
    PRIMARY KEY(activity_id, customer_id)
);
Copy after login

In this example,

activity_id or customer_id Values ​​in columns can be repeated, but each combination of values ​​in the two columns must be unique.

Normally, the primary key is always defined when a table is created. Sometimes, however, an existing table may not have a primary key defined. In this case, you can use the

ALTER TABLE statement to add a primary key to the table. For example:

First create a table without a primary key column:

CREATE TABLE dbo.events(
    event_id INT NOT NULL,
    event_name VARCHAR(255),
    start_date DATE NOT NULL,
    duration DEC(5,2)
);
Copy after login

Then make the

event_id column a primary key:

ALTER TABLE sales.events 
ADD PRIMARY KEY(event_id);
Copy after login
Note that if the

sales.events table already has data, before promoting the event_id column to the primary key, you must ensure that the value in event_id is not Repeated.

Recommended study: "

SQL Tutorial"

The above is the detailed content of Briefly understand SQL Server primary key constraints (PRIMARY KEY). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
sql
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template