Page 1 of 1

How can I alter a table to include a primary key in MySQL?

Posted: Mon Mar 19, 2012 4:20 pm
by APD1993
Hi,

I want to be able to add a primary key to a table through altering the table. At the moment, I am using coding such as:

USE test3;
ALTER table users ->ADD primary key (uname);


But I get told: No database selected

Does anyone know how to fix this?

Any help is appreciated :)

Re: How can I alter a table to include a primary key in MySQ

Posted: Mon Mar 19, 2012 4:26 pm
by social_experiment
Are you using php to do this? If so can you paste the code you are using

Re: How can I alter a table to include a primary key in MySQ

Posted: Mon Mar 19, 2012 4:55 pm
by APD1993
How come I need to give PHP for a MySQL question? :)

Re: How can I alter a table to include a primary key in MySQ

Posted: Mon Mar 19, 2012 5:23 pm
by Celauran
APD1993 wrote:How come I need to give PHP for a MySQL question? :)
Why are you posting an SQL question in the PHP forum, then?

Code: Select all

ALTER TABLE table_name ADD PRIMARY KEY(`field_name`)

Re: How can I alter a table to include a primary key in MySQ

Posted: Tue Mar 20, 2012 6:14 am
by social_experiment
@APD1993 Your innitial question, although descriptive about the problem you are having, was vague about how you are using the code. Are you entering it via phpmyadmin, via a php script? No database selected means you didn't select a database; whatever method you are using you first have to select a database :|