Page 1 of 1

cannot define primary key in phpMyadmin

PostPosted: 23. November 2015 18:25
by jerrittpace
I started a database without defining a primary key, and in the ID entry, the first entry of the column, '0' was entered as an input a few times.

Now, I believe because a primary key is not defined, I have the following message at the top of the "Browse" window in phpMyAdmin:

"Current selection edosot contain a u nnique column. Grid edit, checkbox, Edit, Copy and Delete features are not available. Documentation"

When I try to define the ID entry as the primary variable, I get an error message
"Duplicate entry '0' for key primary"

To be honest, I am going to go ahead and just delete and reenter the table, but I would like to know what can be done to be able to amend this in the future.

Thanks for your help!!

Re: cannot define primary key in phpMyadmin

PostPosted: 23. November 2015 18:36
by Nobbie
jerrittpace wrote:To be honest, I am going to go ahead and just delete and reenter the table, but I would like to know what can be done to be able to amend this in the future.


Define a primary key and do not enter the same value twice. Whatelse?

An SQL Table without a primary key is like a car without a steering wheel. You may do so, but its useless.

Re: cannot define primary key in phpMyadmin

PostPosted: 23. November 2015 19:08
by jerrittpace
Yeah I know, I need a primary key, but phpMyadmin will not let me change the ID entry, the first entry, to a primary key, because it already has values entered as '0', which are duplicate values that a primary key cannot hold. Then, it will not let me delete values because there is no primary key defined.

I already just deleted the table and reentered it with a primary key, but I was curious about the problem I was having, that I could not change a table entry to a primary key basically because the entry was already filled with identical values and then the values could not be deleted without a primary key defined.

Re: cannot define primary key in phpMyadmin

PostPosted: 23. November 2015 19:32
by Nobbie
jerrittpace wrote:Yeah I know, I need a primary key, but phpMyadmin will not let me change the ID entry, the first entry, to a primary key, because it already has values entered as '0', which are duplicate values that a primary key cannot hold. Then, it will not let me delete values because there is no primary key defined.


YES!! Exactly - a table without a primary key is a pain. You learned it. Good so! As I told you: like a car without steering wheel. You cannot do anything with your table, because you cannot identify the rows.

Re: cannot define primary key in phpMyadmin

PostPosted: 23. November 2015 20:46
by Altrea
For the future: If you need to add a primary key to a not indexed table, there is a SQL command:
Code: Select all
ALTER TABLE `tablename` ADD `pk` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST

Re: cannot define primary key in phpMyadmin

PostPosted: 23. November 2015 21:43
by jerrittpace
Yeah, I learned my lesson.

Thanks you guys for your help!!