Home > Database > Mysql Tutorial > body text

How to change column name as primary key in mysql?

青灯夜游
Release: 2020-10-09 09:11:16
Original
2461 people have browsed it

Mysql method to modify the column name as the primary key: Use the "ALTER TABLE data table name ADD PRIMARY KEY (field name/column name);" statement to set it; the field/column set as a primary key constraint must ensure the value There cannot be duplicates and must be non-empty.

How to change column name as primary key in mysql?

The full name of the primary key (PRIMARY KEY) is "primary key constraint", which is the most frequently used constraint in MySQL. Under normal circumstances, in order to facilitate the DBMS to find records in the table faster, a primary key will be set in the table.

(Recommended tutorial: mysql video tutorial)

You should pay attention to the following points when using primary keys:

  • Each table only Ability to define a primary key.

  • The primary key value must uniquely identify each row in the table and cannot be NULL, that is, there cannot be two rows of data with the same primary key value in the table. This is the principle of uniqueness.

  • A field name can only appear once in the joint primary key field table.

  • The joint primary key cannot contain unnecessary redundant fields. When a field in the joint primary key is deleted, if the primary key composed of the remaining fields still satisfies the uniqueness principle, then the joint primary key is incorrect. This is the principle of minimization.

Add primary key constraints when modifying the table

Primary key constraints can not only be created when the table is created, but can also be added when the table is modified. However, it should be noted that null values ​​are not allowed in fields set as primary key constraints.

The syntax format for adding primary key constraints when modifying the data table is as follows:

ALTER TABLE <数据表名> ADD PRIMARY KEY(<字段名/列名>);
Copy after login

View the table structure of the tb_emp data table. The SQL statement and running results are as follows.

mysql> DESC tb_emp;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   |     | NULL    |       |
| name   | varchar(30) | YES  |     | NULL    |       |
| deptId | int(11)     | YES  |     | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.14 sec)
Copy after login

Example:

Modify the data table tb_emp and set the field id as the primary key. The SQL statement and running results are as follows.

mysql> ALTER TABLE tb_emp
    -> ADD PRIMARY KEY(id);
Query OK, 0 rows affected (0.94 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> DESC tb_emp;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(30) | YES  |     | NULL    |       |
| deptId | int(11)     | YES  |     | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.12 sec)
Copy after login

Normally, when you want to set the primary key constraint of a field in the table when modifying the table, you must ensure that the values ​​in the field set as the primary key constraint cannot have duplicates and must be non-empty. . Otherwise, the primary key constraint cannot be set.

Related recommendations: php training

The above is the detailed content of How to change column name as 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