- NOT NULL
- UNIQUE
- DEFAULT
- CHECK
- Key Constraints – PRIMARY KEY, FOREIGN KEY
Example:
SQL> create table abc(sno number(2) not null,sname varchar(11));
Table created.
SQL> insert into abc values(11,'raj');
1 row created.
SQL> insert into abc(sname) values('raj');
insert into abc(sname) values('raj')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."ABC"."SNO")
UNIQUE:
Example:
SQL> create table abc(sno number(2) unique,sname varchar(11));
Table created.
SQL> insert into abc values(11,'raj');
1 row created.
SQL> insert into abc values(11,'raj');
insert into abc values(11,'raj')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C004058) violated
SQL> create table abc(sno number(2) unique,sname varchar(22),city varchar(11) default('vsp'));
Table created.
SQL> insert into abc values(11,'raj','hyd');
1 row created.
SQL> insert into abc values(11,'raj',default);
insert into abc values(11,'raj',default)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C004060) violated
SQL> insert into abc values(12,'raj',default);
1 row created.
SQL> insert into abc(sno,sname) values(13,'rasdf');
1 row created.
SQL> select * from abc;
SNO SNAME CITY
---------- ---------------------- -----------
11 raj hyd
12 raj vsp
13 rasdf vsp
Table created.
SQL> insert into abc values(11,'raj','vsp');
1 row created.
SQL> insert into abc values(22,'raj','vizag');
insert into abc values(22,'raj','vizag')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C004061) violated
1.Primary key :A key that is used for unique identification of each row in a table is known as primary key of the database.
Example :
Table created.
SQL> insert into std values(11,'raju');
1 row created.
SQL> insert into std values(22,'rani');
1 row created.
SQL> select * from std;
SNO SNAME
---------- -----------
11 raju
22 rani
SQL> insert into std values(11,'ramesh');
insert into std values(11,'ramesh')
*
ERROR at line 1:
ORA-00001: unique constraint (RK.PK_STD) violated
2. Foreign Key :Foreign keys are the columns of a table that points to the primary key of another table. They act as a cross-reference between tables.
Example :
---------- ----------- ------- ----------
11 raju mpc 520
22 rani bipc 420
33 suresh mpc 520
44 ramesh bipc 420
55 sita mpc 520
SQL> Create table faculty (fid number(3) primary key, fname varchar2(15), sno number(2), foreign key (sno) references std(sno));
FID FNAME SNO
---------- --------------- ----------
111 rk 11
222 pk 44
----------
11
22
33
44
55