MySQL:如何在 phpMyAdmin 上设置主键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19198397/
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
MySQL: How to set the Primary Key on phpMyAdmin?
提问by just_a_newbie
Help! First time using phpMyAdmin. The column that I intend to set as a primary key is of TEXT in phpMyAdmin, it gave me an error message, the primary key can't be set! And I don't want to change it to INT. How can I solve this? Million thanks!
帮助!第一次使用 phpMyAdmin。我想设置为主键的列是phpMyAdmin中的TEXT,提示错误,不能设置主键!而且我不想将其更改为 INT。我该如何解决这个问题?万感谢!
PS. I haven't input any values or relate any other tables on this table yet.
附注。我还没有在这个表上输入任何值或关联任何其他表。
回答by Marc Delisle
You can set a primary key on a text column. In phpMyAdmin, display the Structure of your table, click on Indexes, then ask to create the index on one column. Then choose PRIMARY, pick your TEXT column, but you have to put a length big enough so that its unique.
您可以在文本列上设置主键。在 phpMyAdmin 中,显示表的结构,单击索引,然后要求在某一列上创建索引。然后选择 PRIMARY,选择您的 TEXT 列,但您必须放置足够大的长度以使其唯一。
回答by Mihai
MySQL can index the first x characters of a column,but a TEXT type is of variable length so mysql cant assure the uniqueness of the column.If you still want text column,use VARCHAR.
MySQL可以索引列的前x个字符,但是TEXT类型是可变长度的,所以mysql不能保证该列的唯一性。如果你还想要text列,使用VARCHAR。
回答by saurabh2226
You can view the INDEXES column below where you find a default PRIMARY KEY is set. If it is not set or you want to set any other variable as a PRIMARY KEY then , there is a dialog box below to create an index which asks for a column number ,either way you can create a new one or edit an existing one.The existing one shows up a edit button whee you can go and edit it and you're done save it and you are ready to go
您可以查看下方的 INDEXES 列,您可以在其中找到设置了默认 PRIMARY KEY 的位置。如果未设置它或者您想将任何其他变量设置为 PRIMARY KEY 那么,下面有一个对话框来创建一个要求列号的索引,您可以通过任何一种方式创建一个新的或编辑一个现有的。现有的会显示一个编辑按钮,你可以去编辑它,你就完成了保存它,你就可以开始了
回答by PHPLover
You can't set the field having data-type "text". Only because of that thing you are getting this error. Try to change the data-type with int
您不能设置数据类型为“文本”的字段。只是因为那件事,您才会收到此错误。尝试使用 int 更改数据类型