Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: How to set the Primary Key on phpMyAdmin?

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!

PS. I haven't input any values or relate any other tables on this table yet.

The screen cap

like image 267
just_a_newbie Avatar asked Oct 05 '13 13:10

just_a_newbie


People also ask

How do you set a primary key attribute in MySQL?

In MySQL, a primary key is created using either a CREATE TABLE statement or an ALTER TABLE statement. You use the ALTER TABLE statement in MySQL to drop, disable or enable a primary key.

How do I add a primary key to an existing table in MySQL?

When the table does not have a primary key, this statement is used to add the primary key to the column of an existing table. Following are the syntax of the ALTER TABLE statement to create a primary key in MySQL: ALTER TABLE table_name ADD PRIMARY KEY(column_list);

Can I change primary key MySQL?

To change the primary key of a table, delete the existing key using a DROP clause in an ALTER TABLE statement and add the new primary key. Note You must be logged in to the database using a database name before you can add a primary key or conduct any other referential integrity (RI) operation.


1 Answers

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.

like image 120
Marc Delisle Avatar answered Sep 30 '22 03:09

Marc Delisle