Home > Database > Mysql Tutorial > body text

What are the keywords of primary key in mysql

下次还敢
Release: 2024-04-26 07:09:14
Original
569 people have browsed it

In MySQL, use the PRIMARY KEY keyword to specify the primary key. The primary key is a field or field combination that uniquely identifies each row of records. It is unique (the primary key value is different for each row) and non-null (the primary key value is different). is NULL) property. Additionally, define the primary key using the following syntax: CREATE TABLE table_name ( column_name1 DATA_TYPE PRIMARY KEY, ... ).

What are the keywords of primary key in mysql

Keywords for primary key in MySQL

In MySQL, the keyword used to specify the primary key isPRIMARY KEY.

Definition of primary key

The primary key is a field or combination of fields that uniquely identifies each row of records in a database table. It has the following properties:

  • Uniqueness: Primary key values ​​are unique for all rows in a given table.
  • Non-null: The primary key cannot be a NULL value.

Use the PRIMARY KEY keyword

To designate one or more fields as the primary key, use the PRIMARY KEY keyword when creating the table, as follows Display:

<code class="sql">CREATE TABLE table_name (
  column_name1 DATA_TYPE PRIMARY KEY,
  column_name2 DATA_TYPE,
  ...
);</code>
Copy after login

where:

  • table_name is the name of the table.
  • column_name1 is the name of the primary key field.
  • DATA_TYPE Specifies the data type of the field.

Example

The following example creates a table named customers where customer_id is the primary key:

<code class="sql">CREATE TABLE customers (
  customer_id INT PRIMARY KEY,
  name VARCHAR(255),
  email VARCHAR(255)
);</code>
Copy after login

In the above example, the customer_id field is unique and non-null, which means that no two rows in the table have the same customer_id value, and customer_id The value is never NULL.

The above is the detailed content of What are the keywords of primary key 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
Popular Tutorials
More>
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!