Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: set existing column as Primary Key in MySQL

I have a database with 3 columns:

id, name, somethingelse   

This table has no index set and i am getting "No index defined!" in phpmyadmin
id is a 7digit alphanumeric value, unique to each row.
I want to set Drugid to be the primarykey/index (i dont know the difference if there is one)
Please explain in detail as i am new to this.
Thank you.

like image 961
krasatos Avatar asked Mar 14 '12 13:03

krasatos


People also ask

How do I change the primary key of a column?

We can also define the primary key after making the table, but make sure the column has the NOT NULL constraint on it. The syntax for declaring primary key after defining the table: Syntax: Alter table table_name add primary key (column_name);

How do I make two columns a primary key in MySQL?

How can we set PRIMARY KEY on multiple columns of an existing MySQL table? We can set PRIMARY KEY constraint on multiple columns of an existing table by using ADD keyword along with ALTER TABLE statement.

Can primary key be set of columns?

Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).


2 Answers

Either run in SQL:

ALTER TABLE tableName   ADD PRIMARY KEY (id)           ---or Drugid, whichever you want it to be PK 

or use the PHPMyAdmin interface (Table Structure)

like image 200
ypercubeᵀᴹ Avatar answered Oct 16 '22 02:10

ypercubeᵀᴹ


ALTER TABLE your_table ADD PRIMARY KEY (Drugid); 
like image 39
juergen d Avatar answered Oct 16 '22 01:10

juergen d