19

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?

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

The screen cap

Dharman
  • 30,962
  • 25
  • 85
  • 135
just_a_newbie
  • 375
  • 1
  • 3
  • 8

4 Answers4

21

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.

Marc Delisle
  • 8,879
  • 3
  • 29
  • 29
  • Thank you for telling me. I know how to do it now. Another question arises, what does Index mean and do? – just_a_newbie Oct 08 '13 at 16:17
  • That would be a new thread. This is an answer http://stackoverflow.com/questions/707874/differences-between-index-primary-unique-fulltext-in-mysql but normally you'd want to check for the answer on stack overflow and then if it doesn't exist, post it as a separate question for others to find. – AppsIntheParkNYC Jan 01 '15 at 23:38
  • Making "ask to create the index" a little less vague: In phpmyadmin 4.8.x, in table Structure view, right below the structure grid there are clickable items for Primary, Unique, and Index to assign the checkbox'ed fields to those functions. Or further down the page under the Indexes display, there's a feature "Create an index on" and you choose N columns. The adjacent Go button invokes a dialog to fill in further information including Primary/Unique/Index and more choices. It also allow previewing the ALTER sql that it proposed to run. – gwideman Mar 09 '19 at 06:55
3

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.

Mihai
  • 26,325
  • 7
  • 66
  • 81
1

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

PHPLover
  • 1
  • 51
  • 158
  • 311
1

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