sql - mysql how to use two field as primary key and unique -


in mysql database have data

create table student(user_id int(20) unsigned not null auto_increment, name varchar(20), email varchar(20), primary key(user_id,email)); 

i want use user_id , email field should unique value. add

alter table student add unique unique_index (user_id,email); 

but still accepts entry

mysql> insert student (name, email) values ("a", "aa", "aa"); query ok, 1 row affected (0.06 sec)  mysql> insert student (name, email) values ("a", "aa", "aa"); query ok, 1 row affected (0.06 sec)     select * student; +---------+------+-------+ | user_id | name | email | +---------+------+-------+ |       1 |    | aa    | |       2 |    | aa    | +---------+------+-------+ 2 rows in set (0.00 sec) 

how can make fields(id , email) unique?

you can use these. here every unique index name should different.

create table student(user_id int(20) unsigned not null auto_increment, name varchar(20), email varchar(20), primary key(user_id,email)); 

unique index creation.

alter table student add unique unique_index1 (user_id); alter table student add unique unique_index2 (email); 

inserting data:

insert student (`name`, email) values ("a", "aa"); 

execute below sql gives error.

insert student (`name`, email) values ("a", "aa"); 

thank you.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -