我在 Idaq 托管包上使用 Webmin 来管理我的 MySQL 数据库。我从以前的主机导入了数据库,现在发现我无法更改特定表中的任何数据,因为出现错误“无法编辑此表中的数据,因为它没有主键。”
在 php 脚本中,我可以更改数据,但确实需要从控制面板更改它。
当我尝试创建主键字段时,出现以下错误“无法保存字段:SQL alter table userbase
add referenceNumber
smallint not null auto_increment failed : Incorrect表定义,auto列只能有一个,必须定义为key"
我很清楚任何表都应该包含一个主键,这是我的错误,但是有人可以提供一些帮助吗?
最佳答案
因为你没有同时将它设置为key所以它没有通过并且它必须被定义为key
部分,把key放在最后。
使用
ALTER TABLE tableName ADD autoIncrementColumn MEDIUMINT NOT NULL AUTO_INCREMENT KEY
Johan 在评论中提供的额外信息
@Mick, To add to PEZ's answer, if you do not provide a primary key, MySQL adds a hidden primary integer autoincrement key. In order to override the hidden key, the alter table statement must specify the addition of a new field and the assignment of the new PK in the same line, otherwise the hidden PK will block the alteration.
关于mysql - 在没有主键的情况下编辑 MySQL 数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7191480/