SQL Constraints: - : Syntax
SQL Constraints: - : Syntax
---------------
Constraints are used to limit the type of data that can go into a table. This ensures the
Constraints can be specified when the table is created with the CREATE TABLE statement, or
after the table is created with the ALTER TABLE statement.
Constraints can be column level or table level. Column level constraints apply to a column,
COLUMN LEVEL:
------------------------
In this method we are defining constraints in individual columns i.e whenever we are defining
column than only we are specifying constraints type.
Syntax:
---------
);
TABLE LEVEL:
-----------------
In this method we are defining constraints after declaring all column , In this method we are defining
constraints on group of column.
------------------------------------
SYNTAX:
column_name2 <data_type>(size)
);
or
column_name2 <data_type>(size)
);
Table dropped.
Table created.
values(10);
1 row created.
values(null);
values(null)
ERROR at line 2:
values(10);
1 row created.
-------------------------------------------------------
unique identification number in the format of sys_c this is called server define constraint
name, Instead of server define constraint name we can also create our own constraint name by
below syntax.
SYNTAX:
);
------------------------------------------------------
);
o/p:ERROR at line 3:
sno1 number(10) ,
sno3 number(10) ,
);
O/P:
ERROR at line 6:
UNIQUE CONSTRAINT:
-----------------------------
SQL>
);
Table created.
1 row created.
values(20);
1 row created.
values(20);
ERROR at line 1:
values(30);
1 row created.
values(null);
1 row created.
values(null);
--------------------------------------------------------------------------
sno2 number(10) ,
sno4 number(10),
);
values(1,2,3,4,5);
o/p: inserted
values(8 ,2,3,6,7);
op:
ERROR at line 1:
values(1,2,4);
sno2 number(10) ,
);
values(1,2,3);
o/p: inserted
values(1,2,4);
o/p:ERROR at line 1:
sno1 number(10),
sno2 number(10),
sno3 number(10),
-----------------------------
sno1 number(10),
sno2 number(10),
sno3 number(10),
);
values(1,2,3);
values(1,3,3)
values(2,3,3);
PASSOUT_YEAR NUMBER(5),
NAME VARCHAR2(10)
----------------------------------------------------
If we declared the constraints as "deferred", they are only checked at the commit point.
sno2 number(10),
sno3 number(10)
);
values(1,2,3);
values(1,3,3) ;
values(2,3,3) ;
values(2,3,3) ;
values(2,2,3);
values(3,2,3);
values(4,2,3);
sno1 number(10) ,
sno2 number(10),
sno3 number(10),
);
------------------------------------
Primary key won’t allow NULL and duplicate to my table. So this is combination of NOT NULL
and UNIQUE.
The same column or combination columns cannot design both as primary key and unique
key.
);
Table created.
values(10);
values(20);
values(20);
ERROR at line 1:
values(null);
values(null)
ERROR at line 2:
**PRIMARY KEY AND UNIQUE WE CAN DECLARE IN TABLE LEVEL BUT NOT NULL WE CAN’T DECLARE
TABLE LEVEL.
snonumber(10),
name varchar2(10),
primary key(sno)
);
Table created.
Sno number(10),
name varchar2(10),
unique(sno)
);
Table created.
snonumber(10),
name varchar2(10),
not null(sno)
);
ERROR at line 5:
ERROR at line 1:
name varchar2(10)
);
id number(10) ,
name varchar2(10)
);
NO
-----------------------
id number(10) ,
name varchar2(10),
);
Table created.
Name Varchar2(20)
);
Table created.
Values(10,'raj');
Values(20,'king');
Values(30,'scott');
ID NAME
---------- ----------
20 king
30 scott
SQL> commit;
Commit complete.
1 row created.
ERROR at line 1:
found
Values(40,'rajesh');
1 row created.
Values(40,'rajesh');
1 row created.
---------- ----------
10 raj
20 king
30 scott
40 rajesh
ID NAME
---------- ----------
10 raj
40 rajesh
where id=10;
ERROR at line 1:
where id=10;
ERROR at line 1:
found
where id=20;
ERROR at line 1:
found
where id=10;
1 row deleted.
where id=10;
1 row deleted.
ID NAME
---------- ----------
30 scott
40 rajesh
ID NAME
---------- ----------
40 rajesh
16 rows selected.
60 SALES BBSR
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 SHIPPING KOLKATA
6 rows selected.
sampidnumber(10)
sampname varchar2(10),
sampidfknumber(10)
);
Table created.
16 rows selected.
2 values(1234,'mukesh','clerk',2345,sysdate,60000,900,80);
ERROR at line 1:
2 values(1234,'mukesh','clerk',2345,sysdate,60000,900,30);
1 row created.
ERROR at line 2:
SQL> rollback;
Rollback complete.
ERROR at line 2:
17 rows selected.
2 whereempno=1234;
1 row deleted.
Table altered.
2 values(1234,'mukesh','clerk',2345,sysdate,60000,900,30);
ERROR at line 1:
2 values(2345,'mukesh','clerk',7977,sysdate,60000,900,30);
1 row created.
2 values(3456,'rakeshh','clerk',2345,sysdate,60000,900,30);
1 row created.
SQL> rollback;
Rollback complete.
DEFERRABLE CONSTRAINT:
--------- --------------------------------
mastid number(10)
mastname varchar2(10)
);
(mastid,mastname,mastdate)
values(11,'MASTER11','11-oct-07');
(mastid,mastname,mastdate)
values(12,'MASTER12','12-oct-07');
(mastid,mastname,mastdate)
values(13,'MASTER13','13-oct-07');
(mastid,mastname,mastdate)
values(11,'MASTER11','11-oct-07');
Creating FK on UNIQUE
--------------------
);
Adding Constraint:
snonumber(10)
);
Dropping Constraint:
----------------------------------------
sampidnumber(2)
sampname varchar2(15),
sampdate date
);
sampidnumber(2)
sampname varchar2(15),
sampdate date,
sampidfknumber(2)
);
sampidnumber(2)
sampname varchar2(15),
sampdate date,
sampidfknumber(2)
on delete cascade
);
begin
(sampid,sampname,sampdate)
values(1,'sample1',sysdate);
(sampid,sampname,sampdate)
values(2,'sample2',sysdate);
(sampid,sampname,sampdate)
values(3,'sample3',sysdate);
values(4,'sample04',sysdate);
(sampid,sampname,sampdate)
values(5,'sample5',sysdate);
(sampid,sampname,sampdate)
values(6,'sample6',sysdate);
commit;
begin
(sampid,sampname,sampdate,sampidfk)
values(11,'sample7',sysdate,3 );
(sampid,sampname,sampdate,sampidfk)
values(12,'sample8',sysdate,3 );
(sampid,sampname,sampdate,sampidfk)
values(13,'sample9',sysdate,2);
(sampid,sampname,sampdate,sampidfk)
values(14,'sample9',sysdate,2 );
commit;
end;
begin
(sampid,sampname,sampdate,sampidfk)
values(21,'sample10',sysdate,4 );
(sampid,sampname,sampdate,sampidfk)
values(22,'sample11',sysdate,4 );
(sampid,sampname,sampdate,sampidfk)
values(23,'sample12',sysdate,5);
(sampid,sampname,sampdate,sampidfk)
values(24,'sample13',sysdate,5);
(sampid,sampname,sampdate,sampidfk)
values(25,'sample14',sysdate,6);
commit;
end;
where sampid=2;
where sampid=3;