-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_integrity_constrain.sql
More file actions
27 lines (16 loc) · 1.14 KB
/
unique_integrity_constrain.sql
File metadata and controls
27 lines (16 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use temp_db;
show tables;
-- unique integrity constraint simply means that values in a column must be unique
create table stu_2(stu_id int UNIQUE, stu_name varchar(15));
create table stu_3(stu_id int, stu_name varchar(15), unique(stu_id)); -- here we have provided the constraint at column level
-- here it means stu_id combinaton must be unique what if i have written
create table stu_4(stu_id int, stu_name varchar(15), unique(stu_id, stu_name));
-- it means combination of id and name must be unique
-- like 1, sanoj is different than 1, deepak
-- here stu_id must be unique
insert into stu_2 values(1, 'SANOJ'); -- this will not give any error
insert into stu_2 values(NULL, 'KUMAR'); -- this will not give any error because NULL is a unique value till now
insert into stu_2 values(1, 'DEEPAK'); -- will give error because 1 is already in the column (saying duplicate entry)
insert into stu_2 values(2, 'SANOJ'); -- this will work because we have condition on id not name
insert into stu_2 values(NULL, 'KUMAR'); -- gives error because NULL is already present
select * from stu_2; -- so multple null values can be inserted while defining unique constraint