The primary key in mysql is used to identify the unique values in table. It don't allow duplicate entries in mysql table. The primary key column should be NOT NULL. It don't allow null values in mysql table. The mysql table can have only one primary key.
How to create primary key in mysql:
The mysql query for create primary key is:
Create table table_name (column_name datatype(length), primary key(column_name))
For example,
create table table1(name varchar(30), address varchar(100), primary key(name))
Where the column 'name' is primary key.
For example,
create table table1(name varchar(30), address varchar(100), primary key(name))
Where the column 'name' is primary key.
How to add primary key to existing table in mysql:
You created table without primary like this:
create table table1(name varchar(30),address varchar(100))
But you need to add primary key in order to remove duplicate entries in mysql. At the time you can add primary key to existing table in mysql. Following mysq query is used for add primary key.
Alter table table_name add primary key(column_name)
For example,
Alter table table1 add primary key(num)
How to remove primary key in mysql:
You created table with primary. But you need not primary key for particular table. Then how can remove primary key from table in mysql. The following mysql query is used to drop the primary key from table in mysql.
alter table table_name drop primary key
For example,
alter table table1 drop primary key
where,
The primary key is removed from table 'table1'. Now you can add duplicate values to your mysql table.
Related Post:
Related Post:
distinct in mysql
Order by in mysql
Group by multiple columns in mysql
How to use order by together with group by in mysql
Difference between delete truncate drop in mysql
how to use order by before group by in mysql
How to reset auto increment initial value in mysql
Order by in mysql
Group by multiple columns in mysql
How to use order by together with group by in mysql
Difference between delete truncate drop in mysql
how to use order by before group by in mysql
How to reset auto increment initial value in mysql