mysql changing unique key to primary (mariadb)

元气小坏坏 提交于 2021-02-04 21:37:17

问题


I have my table users:

+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| userid             | varchar(30)  | YES  |     | NULL    |       |
| lname              | varchar(40)  | YES  |     | NULL    |       |
| fname              | varchar(20)  | YES  |     | NULL    |       |
| system_id          | varchar(12)  | YES  |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

and I want to change the system_id and the userid to be unique keys.

If I change them via ALTER TABLE users MODIFY userid varchar(30) NOT NULL UNIQUE KEY; I get:

+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| userid             | varchar(30)  | NO   | PRI | NULL    |       |
| lname              | varchar(40)  | YES  |     | NULL    |       |
| fname              | varchar(20)  | YES  |     | NULL    |       |
| system_id          | varchar(12)  | YES  |     | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

and then if I change the system_id ALTER TABLE users MODIFY system_id varchar(30) NOT NULL UNIQUE KEY;

+--------------------+--------------+------+-----+---------+-------+
| Field              | Type         | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| userid             | varchar(30)  | NO   | PRI | NULL    |       |
| lname              | varchar(40)  | YES  |     | NULL    |       |
| fname              | varchar(20)  | YES  |     | NULL    |       |
| system_id          | varchar(12)  | NO   | UNI | NULL    |       |
+--------------------+--------------+------+-----+---------+-------+

Why is MySQL changing my UNIQUE key to PRI when I never specified it to?


回答1:


"A UNIQUE index may be displayed as PRI if it cannot contain NULL values and there is no PRIMARY KEY in the table. A UNIQUE index may display as MUL if several columns form a composite UNIQUE index; although the combination of the columns is unique, each column can still hold multiple occurrences of a given value."

https://dev.mysql.com/doc/refman/5.7/en/show-columns.html

Found my answer.



来源:https://stackoverflow.com/questions/47933646/mysql-changing-unique-key-to-primary-mariadb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!