Home  >  Article  >  Database  >  How many primary keys are there in a single data table of the database?

How many primary keys are there in a single data table of the database?

烟雨青岚
烟雨青岚Original
2020-07-06 15:20:0014643browse

There can only be one primary key in a single data table of the database. A database primary key refers to a column or a combination of multiple columns, whose value can uniquely identify each row in the table, through which the entity integrity of the table can be enforced; the primary key is mainly used to associate foreign keys with other tables, as well as this Modification and deletion of records.

How many primary keys are there in a single data table of the database?

There can only be one primary key in a single data table of the database.

The database primary key refers to a column or a combination of multiple columns whose value can uniquely identify each row in the table, through which the entity integrity of the table can be enforced. The primary key is mainly used to associate with foreign keys in other tables, as well as modify and delete this record.

1. Each table in the database can only have one primary key, and it is impossible to have multiple primary keys.

2. The so-called multiple primary keys of a table are called joint primary keys.

Note: Joint primary key: using multiple fields together as the primary key of a table.

3. The role of the primary key is to ensure the uniqueness and integrity of the data. At the same time, retrieving the table through the primary key can increase the retrieval speed.

Let’s talk about how to create a joint primary key:

1. Select multiple columns at the same time in the GUI and click Set as primary key.

2. The sql statement sets multiple columns as primary keys:

One is to write it out when the table is being built. The statement is as follows:

Create Table 表名 (字段名1 Int Not Null,
                   字段名2 nvarchar(13) Not Null Primary Key (字段名1, 字段名2),
                    字段名3…………
                    字段名N………… )

The other is to write it out when the table is being built. After changing the table, the statement is as follows:

ALTER TABLE 表名 WITH NOCHECK ADD 
CONSTRAINT [PK_表名] PRIMARY KEY  NONCLUSTERED 
(
  [字段名1],
  [字段名2]
)

Recommended tutorial: "sql video tutorial"

The above is the detailed content of How many primary keys are there in a single data table of the database?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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