SQL:在 MySQL 中将现有列设置为主键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9702766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:32:12  来源:igfitidea点击:

SQL: set existing column as Primary Key in MySQL

mysqlsqlindexingprimary-key

提问by krasatos

I have a database with 3 columns:

我有一个包含 3 列的数据库:

id, name, somethingelse  

This table has no index set and i am getting "No index defined!" in phpmyadmin
id is a 7digit alphanumeric value, unique to each row.
I want to set Drugid to be the primarykey/index (i dont know the difference if there is one)
Please explain in detail as i am new to this.
Thank you.

该表没有设置索引,我收到“未定义索引!” 在 phpmyadmin 中,
id 是一个 7 位字母数字值,每一行都是唯一的。
我想将 Drugid 设置为主键/索引(如果有一个我不知道有什么区别)
请详细解释,因为我是新手。
谢谢你。

回答by ypercube??

Either run in SQL:

要么在 SQL 中运行:

ALTER TABLE tableName
  ADD PRIMARY KEY (id)           ---or Drugid, whichever you want it to be PK

or use the PHPMyAdmin interface (Table Structure)

或使用 PHPMyAdmin 界面(表结构)

回答by juergen d

ALTER TABLE your_table
ADD PRIMARY KEY (Drugid);

回答by Ortiga

If you want to do it with phpmyadmin interface:

如果您想使用 phpmyadmin 界面执行此操作:

Select the table -> Go to structure tab -> On the row corresponding to the column you want, click on the icon with a key

选择表格 -> 转到结构选项卡 -> 在您想要的列对应的行上,单击带有键的图标

回答by Ravi Kamesh

alter table table_name
add constraint myprimarykey primary key(column);

reference : http://www.w3schools.com/sql/sql_primarykey.asp

参考:http: //www.w3schools.com/sql/sql_primarykey.asp

回答by S K TANDON

Go to localhost/phpmyadmin and press enter key. Now select:

转到 localhost/phpmyadmin 并按回车键。现在选择:

database --> table_name --->Structure --->Action  ---> Primary -->click on Primary