将其他字段设置为自增主键
admin
2023-05-04 10:21:58
0

1、创建测试表,查看表结构

mysql> desc test_autoinc;

+-------+--------------+------+-----+---------+----------------+

| Field | Type         | Null | Key | Default | Extra          |

+-------+--------------+------+-----+---------+----------------+

| id    | int(11)      | NO   | PRI | NULL    | auto_increment |

| c1    | int(11)      | YES  | UNI | NULL    |                |

| c2    | varchar(100) | YES  |     | NULL    |                |

| id_no | int(20)      | NO   |     | NULL    |                |

+-------+--------------+------+-----+---------+----------------+

4 rows in set (0.00 sec)


2、将其他字段设置为自增主键(当前表上已经有自增主键,设置其他字段为自增主键报错)

mysql> 

mysql> alter table test_autoinc modify id_no int(11) auto_increment,add primary key(id_no);

ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

mysql> 

mysql> desc test_autoinc;

+-------+--------------+------+-----+---------+----------------+

| Field | Type         | Null | Key | Default | Extra          |

+-------+--------------+------+-----+---------+----------------+

| id    | int(11)      | NO   | PRI | NULL    | auto_increment |

| c1    | int(11)      | YES  | UNI | NULL    |                |

| c2    | varchar(100) | YES  |     | NULL    |                |

| id_no | int(20)      | NO   |     | NULL    |                |

+-------+--------------+------+-----+---------+----------------+

4 rows in set (0.00 sec)


3、删除表上的约束

mysql> 

mysql> alter table test_autoinc change id id int;

Query OK, 5 rows affected (0.07 sec)

Records: 5  Duplicates: 0  Warnings: 0


mysql> 

mysql> desc test_autoinc;

+-------+--------------+------+-----+---------+-------+

| Field | Type         | Null | Key | Default | Extra |

+-------+--------------+------+-----+---------+-------+

| id    | int(11)      | NO   | PRI | NULL    |       |

| c1    | int(11)      | YES  | UNI | NULL    |       |

| c2    | varchar(100) | YES  |     | NULL    |       |

| id_no | int(20)      | NO   |     | NULL    |       |

+-------+--------------+------+-----+---------+-------+

4 rows in set (0.00 sec)


5、删除表上的主键

mysql> alter table test_autoinc drop primary key;

Query OK, 5 rows affected (0.07 sec)

Records: 5  Duplicates: 0  Warnings: 0


mysql> 

mysql> desc test_autoinc;

+-------+--------------+------+-----+---------+-------+

| Field | Type         | Null | Key | Default | Extra |

+-------+--------------+------+-----+---------+-------+

| id    | int(11)      | NO   |     | NULL    |       |

| c1    | int(11)      | YES  | UNI | NULL    |       |

| c2    | varchar(100) | YES  |     | NULL    |       |

| id_no | int(20)      | NO   |     | NULL    |       |

+-------+--------------+------+-----+---------+-------+

4 rows in set (0.00 sec)


mysql> show variables like 'sql_mode'; 

+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+

| Variable_name | Value                                                                                                                                     |

+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+

| sql_mode      | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |

+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)


mysql> select * from test_autoinc;

+----+------+-------+-------+

| id | c1   | c2    | id_no |

+----+------+-------+-------+

|  6 |    1 | abc   |     0 |

|  8 |    3 | abcdd |     0 |

|  9 |    4 | abcdd |     0 |

| 10 |    5 | abcdd |     0 |

| 11 |    2 | eeee  |     0 |

+----+------+-------+-------+

5 rows in set (0.00 sec)


mysql> update test_autoinc set id_no=1 ;

Query OK, 5 rows affected (0.14 sec)

Rows matched: 5  Changed: 5  Warnings: 0


mysql> 

mysql> select * from test_autoinc;

+----+------+-------+-------+

| id | c1   | c2    | id_no |

+----+------+-------+-------+

|  6 |    1 | abc   |     1 |

|  8 |    3 | abcdd |     1 |

|  9 |    4 | abcdd |     1 |

| 10 |    5 | abcdd |     1 |

| 11 |    2 | eeee  |     1 |

+----+------+-------+-------+

5 rows in set (0.00 sec)


mysql> alter table test_autoinc add primary key(id_no) ,modify id_no int(11) auto_increment;

ERROR 1062 (23000): ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '1' for key 'PRIMARY'

mysql> alter table test_autoinc add primary key(id_no);

ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

mysql> update test_autoinc set id_no=2 where id=7 ;

Query OK, 0 rows affected (0.00 sec)

Rows matched: 0  Changed: 0  Warnings: 0


mysql> update test_autoinc set id_no=3 where id=8 ;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0


mysql> update test_autoinc set id_no=4 where id=9 ;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0


mysql> update test_autoinc set id_no=5 where id=10 ;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0


mysql> update test_autoinc set id_no=6 where id=11 ;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0


mysql> 

mysql> select * from test_autoinc;

+----+------+-------+-------+

| id | c1   | c2    | id_no |

+----+------+-------+-------+

|  6 |    1 | abc   |     1 |

|  8 |    3 | abcdd |     3 |

|  9 |    4 | abcdd |     4 |

| 10 |    5 | abcdd |     5 |

| 11 |    2 | eeee  |     6 |

+----+------+-------+-------+

5 rows in set (0.00 sec)


mysql> 

mysql> alter table test_autoinc add primary key(id_no) ,modify id_no int(11) auto_increment;

Query OK, 5 rows affected (0.27 sec)

Records: 5  Duplicates: 0  Warnings: 0


mysql> 

mysql> show create table test_autoinc\G;

*************************** 1. row ***************************

       Table: test_autoinc

Create Table: CREATE TABLE `test_autoinc` (

  `id` int(11) NOT NULL,

  `c1` int(11) DEFAULT NULL,

  `c2` varchar(100) DEFAULT NULL,

  `id_no` int(11) NOT NULL AUTO_INCREMENT,

  PRIMARY KEY (`id_no`),

  UNIQUE KEY `c1` (`c1`)

) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

1 row in set (0.00 sec)


ERROR: 

No query specified


mysql> 


相关内容

热门资讯

Google在港推出AI代理G... 观点网讯:7月29日,Google在香港推出AI代理Gemini Spark,该代理可全天候在后台运...
5G+165Hz!联想新品官宣... 近两年来,小屏平板一直是比较有趣的一个品类。 不同于大平板注重效率、手机追求万能的特点,拯救者Y70...
原创 马... 马斯克又出来了。这次说2036年钱就没用了。 理由是人工智能加机器人,东西多到你用不完,还拿钱干嘛...
美的集团:我国智能家居互联互通... IT之家 7 月 30 日消息,美的集团今日发布公告,7 月 21 日至 23 日,智能家居互联互通...
华为新款MatePad Pro... 【CNMO科技消息】7月30日,@华为终端 官宣全新旗舰平板华为MatePad Pro。 新款华为...
从实验室到实景验证:6G光子太... 全息通信是第六代移动通信(6G)的典型应用场景之一,旨在通过融合无线传输与全息三维成像技术,实现超大...
从“能对话”到“能干活”——人... 【经济界面】 光明日报记者 刘坤 李晓东 周洪双 人工智能(AI)眼镜化身为个人“助理”,实时进行...
【全新发布】UminiP Pr... (来源:洲明科技)
日本通过“H3火箭8号”发射失... △H3火箭(资料图) 日本文部科学省30日召开关于去年“H3火箭8号”发射失败的会议。会议通过的最终...
互联网巨头抢滩健康硬件,上游芯... 封面新闻记者 付文超 当下,互联网巨头对健康硬件的押注,正在成为产业现实,加速拉动整个产业链步入新一...