ALTER TABLE causes auto_increment resulting key 'PRIMARY'
修改表為主鍵的自動增長值時,報出以下錯誤:
mysql> ALTER TABLE YOON CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT ADD PRIMARY KEY (id);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD PRIMARY KEY (id)' at line 1
解決:
將ID值為0的那條記錄或其他大於0且不重複的資料;
查詢重複資料:
mysql> select id,count(*) as count from yoon group by id having count > 1;
+------+-------+
| id | count |
+------+-------+
| 1 | 2 |
+------+-------+
1 row in set (0.00 sec)
mysql> select * from yoon where id =1;
+------+------+
| id | name |
+------+------+
| 1 | AAAA |
| 1 | DDDD |
+------+------+
2 rows in set (0.00 sec)
mysql> delete from yoon where name='DDDD';
Query OK, 1 row affected (0.00 sec)
新增表為主鍵的自動增長值時,依舊報出以下錯誤:
mysql> ALTER TABLE YOON CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT ADD PRIMARY KEY (id);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD PRIMARY KEY (id)' at line 1
原因:
很多人都忽略了NULL值:
mysql> select * from yoon where id is null;
+------+------+
| id | name |
+------+------+
| NULL | EEEE |
+------+------+
1 row in set (0.00 sec)
mysql> delete from yoon where id is null;
Query OK, 1 row affected (0.01 sec)
mysql> alter table yoon change column id id int(11) not null auto_increment,add primary key(id);
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
or
alter table yoon modify id int(11) not null auto_increment primary key;
mysql> ALTER TABLE YOON CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT ADD PRIMARY KEY (id);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD PRIMARY KEY (id)' at line 1
解決:
將ID值為0的那條記錄或其他大於0且不重複的資料;
查詢重複資料:
mysql> select id,count(*) as count from yoon group by id having count > 1;
+------+-------+
| id | count |
+------+-------+
| 1 | 2 |
+------+-------+
1 row in set (0.00 sec)
mysql> select * from yoon where id =1;
+------+------+
| id | name |
+------+------+
| 1 | AAAA |
| 1 | DDDD |
+------+------+
2 rows in set (0.00 sec)
mysql> delete from yoon where name='DDDD';
Query OK, 1 row affected (0.00 sec)
新增表為主鍵的自動增長值時,依舊報出以下錯誤:
mysql> ALTER TABLE YOON CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT ADD PRIMARY KEY (id);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD PRIMARY KEY (id)' at line 1
原因:
很多人都忽略了NULL值:
mysql> select * from yoon where id is null;
+------+------+
| id | name |
+------+------+
| NULL | EEEE |
+------+------+
1 row in set (0.00 sec)
mysql> delete from yoon where id is null;
Query OK, 1 row affected (0.01 sec)
mysql> alter table yoon change column id id int(11) not null auto_increment,add primary key(id);
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
or
alter table yoon modify id int(11) not null auto_increment primary key;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-1773672/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql primary key procedureSQL
- Alter table for ORACLEOracle
- alter table move 和 alter table shrink space的區別
- mysql中key 、primary key 、unique key 與index區別MySqlIndex
- mysql的ALTER TABLE命令MySql
- oracle alter table詳解Oracle
- alter table using indexIndex
- With KEY & With Table KEY 的使用
- SQL Server Primary Key ConstraintsSQLServerAI
- v$lock之alter table drop column與alter table set unused column區別系列五
- “alter database switchover to xx“過程不當導致的primary-primary 雙主問題Database
- unique index與primary key的區別Index
- oracle資料庫primary key和unique key的異同Oracle資料庫
- alter table語法增補(一)
- ALTER TABLE MOVE | SHRINK SPACE區別
- 簡單分析MySQL中的primary key功能MySql
- ALTER TABLE MOVE和SHRINK SPACE區別
- 關於primary key和foreign key的問題處理
- alter table move 與shrink space的區別
- alter table move跟shrink space的區別
- alter table engine=memory ERROR 1114Error
- alter table nologging /*+APPEND PARALLEL(n)*/APPParallel
- Oracle 11g alter table move與shrink spaceOracle
- alter table move跟shrink space的區別(轉)
- 表、索引遷移表空間alter table move索引
- alter table列管理的一些區別
- oracle 中 ALTER TABLE ADD default 的明確Oracle
- oracle 10g__alter table shrink space compactOracle 10g
- oracle10g_alter table_測試3Oracle
- ORA-02429: cannot drop index used for enforcement of unique/primary keyIndex
- alter table table_name move ; 在自身表空間move是如何操作的?
- MySQL oak-online-alter-table工具使用初探MySql
- alter table modify constraint_disable_enable_novalidateAI
- 資料庫審計(create/alter/drop table、user、tablespace)資料庫
- ERROR 1062 (23000): Duplicate entry for key 'PRIMARY'Error
- 如何證明INNODB輔助索引葉子結點KEY值相同的按照PRIMARY KEY排序索引排序
- MySQL-ALTER TABLE命令學習[20180503]MySql
- 測試alter table storage及dbms_space_admin包