如何向 MySQL 表添加主键?

2021-11-20 00:00:00 mysql

这是我尝试过但失败的方法:

This is what I tried but it fails:

alter table goods add column `id` int(10) unsigned primary AUTO_INCREMENT;

有人给个提示吗?

推荐答案

添加列后,可以随时添加主键:

After adding the column, you can always add the primary key:

ALTER TABLE 商品ADD PRIMARY KEY(id)

至于为什么你的脚本不起作用,你需要指定PRIMARY KEY,而不仅仅是PRIMARY这个词:

As to why your script wasn't working, you need to specify PRIMARY KEY, not just the word PRIMARY:

alter table goods add column `id` int(10) unsigned primary KEY AUTO_INCREMENT;

相关文章