![](/img/trans.png)
[英]MySQL - assign values of AUTO_INCREMENT to another column in a table
[英]add auto_increment to column of NULL values - MySQL
我有一張這樣的桌子:
company_id | name
----------------------
NULL | google
NULL | amazon
company_id 中的所有值都是 NULL,我想用 AUTO_INCREMENTED INT 從 24,000 開始替換這些值。 但是,當我嘗試
alter table table_name modify column company_id int AUTO_INCREMENT, add primary key;
它拋出錯誤Query 1 ERROR: 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 '' at line 1
Query 1 ERROR: 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 '' at line 1
alter table `table_name` modify column `company_id` int AUTO_INCREMENT=24000, add primary key;
任何幫助,將不勝感激!
最好只添加一個從某個值開始的自動增量列。 如果您想要 go 更新路線,那么這里有一個對 MySQL 8+ 友好的選項:
UPDATE yourTable t1
INNER JOIN
(
SELECT name, ROW_NUMBER() OVER (ORDER BY name) rn
FROM yourTable
) t2
ON t2.name = t1.name
SET
company_id = rn + 23999
alter table test AUTO_INCREMENT=24000,
modify column company_id int AUTO_INCREMENT,
add primary key (company_id);
首先確保您沒有現有的 PRIMARY KEY,否則添加“第二個”將失敗。
至於你的問題,語法不正確,你想做:
alter table table_name modify column company_id int PRIMARY KEY AUTO_INCREMENT;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.