问题
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