Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of changing column name to primary key by mysql

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the method of changing the column name to the primary key of mysql. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Mysql changes the column name to the primary key by using the "ALTER TABLE data table name ADD PRIMARY KEY (field name / column name);" statement; the field / column set as the primary key constraint ensures that the value cannot be duplicated and is non-empty.

The full name of primary key (PRIMARY KEY) is "primary key constraint", which is the most frequently used constraint in MySQL. In general, in order to make it easier for DBMS to find records in the table faster, a primary key is set in the table.

Primary keys are divided into single-field primary keys and multi-field joint primary keys. This section will explain the creation, modification, and deletion of these two primary key constraints.

(recommended tutorial: mysql video tutorial)

When using primary keys, you should pay attention to the following points:

Only one primary key can be defined per table.

The primary key value must uniquely identify each row in the table and cannot be NULL, that is, there can be no 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 federated primary key field table.

The federated primary key cannot contain unnecessary extra fields. When a field of the federated primary key is deleted, the federated primary key is incorrect if the primary key formed by the remaining fields still satisfies the principle of uniqueness. This is the principle of minimization.

Add a primary key constraint when modifying a table

Primary key constraints can not only be created while the table is created, but can also be added when the table is modified. It is important to note, however, that null values are not allowed in fields that are set as primary key constraints.

The syntax format for adding primary key constraints when modifying a datasheet is as follows:

ALTER TABLE ADD PRIMARY KEY ()

View the table structure, SQL statements, and run results of the tb_emp data table as shown below.

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)

Example:

Modify the data table tb_emp and set the field id as the primary key. The SQL statement and run result 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: 0mysql > 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)

In general, when you set a primary key constraint for a field in a table when you modify a table, make sure that the values in the fields that are set as primary key constraints cannot be duplicated and that they are not empty. Otherwise, the primary key constraint cannot be set.

This is the end of the method for mysql to change the column name to the primary key. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report