How to write primary key constraints in mysql

下次还敢
Release: 2024-04-26 07:00:24
Original
1097 people have browsed it

The primary key constraint in MySQL is a unique constraint that clearly defines the unique identifier of each row in the table. Primary key constraints can be created by using the PRIMARY KEY keyword or by specifying it when creating the table. The primary key can be a single column or a compound column (composed of multiple columns). It also supports auto-incrementing primary keys, which will automatically generate unique values when inserting new rows. Primary key constraints ensure data integrity and accuracy because each row has a unique value.

How to write primary key constraints in mysql

Primary key constraints in MySQL

What are primary key constraints?

A primary key constraint is a unique constraint that uniquely identifies each row of data in a table. It enforces that every row in the table has a unique value, ensuring data integrity and accuracy.

How to create a primary key constraint?

In MySQL, use thePRIMARY KEYkeyword to create a primary key constraint. The syntax is as follows:

CREATE TABLE table_name ( column_name PRIMARY KEY );
Copy after login

Alternatively, you can specify primary key constraints when creating the table:

CREATE TABLE table_name ( column_name1 INT NOT NULL, column_name2 VARCHAR(255) NOT NULL, PRIMARY KEY (column_name1, column_name2) );
Copy after login
Copy after login

Compound primary key

A composite primary key consists of two or more consists of columns, which together form a unique identifier for the table. The syntax is similar to a single-column primary key:

CREATE TABLE table_name ( column_name1 INT NOT NULL, column_name2 VARCHAR(255) NOT NULL, PRIMARY KEY (column_name1, column_name2) );
Copy after login
Copy after login

Auto-increment primary key

MySQL supports auto-increment primary key, which will automatically generate a unique value for each new row when inserting a new row. value. To create an auto-incrementing primary key, use theAUTO_INCREMENTKeyword:

CREATE TABLE table_name ( id INT NOT NULL AUTO_INCREMENT, column_name1 VARCHAR(255) NOT NULL, PRIMARY KEY (id) );
Copy after login

Notes

  • The primary key column must have a unique value.
  • The primary key column cannot beNULL.
  • Primary key constraints can be created explicitly or implicitly.
  • If no primary key constraint is specified, MySQL will automatically select the first non-null column in the table as the implicit primary key.

The above is the detailed content of How to write primary key constraints in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!