Docsity

SQL Integrity Constraints: Not Null, Foreign Key, and Check Constraints, Lecture notes of Database Management Systems (DBMS)

Toggle sidebar
bg1
Sql Untegrity constraints
integrity constraints are used to apply bussiness rules for the database tables.
the constraaints available in SQL are
1. not null
2. unique
3. Foreign Key
4. check
Not Null-> Ensures that a column cannot have a NULL values.
uniQue -> Ensures that all values in a column are different.
Primary key-> A combination of not null and unique.uniquely identifies each row in a table.
Foreign Key-> uniquely identifies a row/record in another Table.
check-> ensures that all avalyes in a column specifies a speciic condition.
Sql Untegrity constraints
integrity constraints are used to apply bussiness rules for the database tables.
the constraaints available in SQL are
1. not null
2. unique
3. Foreign Key
4. check
Not Null-> Ensures that a column have a NULL values.
uniQue -> Ensures that all values in a column are different.
Primary key-> A combination of not null and unique.uniquely identifies each row in a table.
Foreign Key-> uniquely identifies a row/record in amnother Table.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download SQL Integrity Constraints: Not Null, Foreign Key, and Check Constraints and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are

  1. not null
  2. unique
  3. Foreign Key
  4. check Not Null-> Ensures that a column cannot have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in another Table. check-> ensures that all avalyes in a column specifies a speciic condition. Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are
  5. not null
  6. unique
  7. Foreign Key
  8. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in amnother Table.

check-> ensures that all avalyes in a column specifies a speciic condition.Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are

  1. not null
  2. unique
  3. Foreign Key
  4. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in amnother Table. check-> ensures that all avalyes in a column specifies a speciic condition. Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are
  5. not null
  6. unique
  7. Foreign Key
  8. check

the constraaints available in SQL are

  1. not null
  2. unique
  3. Foreign Key
  4. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in amnother Table. check-> ensures that all avalyes in a column specifies a speciic condition. Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are
  5. not null
  6. unique
  7. Foreign Key
  8. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in amnother Table. check-> ensures that all avalyes in a column specifies a speciic condition.

Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are

  1. not null
  2. unique
  3. Foreign Key
  4. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in amnother Table. check-> ensures that all avalyes in a column specifies a speciic condition. Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are
  5. not null
  6. unique
  7. Foreign Key
  8. check

the constraaints available in SQL are

  1. not null
  2. unique
  3. Foreign Key
  4. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in amnother Table. check-> ensures that all avalyes in a column specifies a speciic condition. Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are
  5. not null
  6. unique
  7. Foreign Key
  8. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in amnother Table. check-> ensures that all avalyes in a column specifies a speciic condition.

Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are

  1. not null
  2. unique
  3. Foreign Key
  4. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different. Primary key-> A combination of not null and unique.uniquely identifies each row in a table. Foreign Key-> uniquely identifies a row/record in amnother Table. check-> ensures that all avalyes in a column specifies a speciic condition. Sql Untegrity constraints integrity constraints are used to apply bussiness rules for the database tables. the constraaints available in SQL are
  5. not null
  6. unique
  7. Foreign Key
  8. check Not Null-> Ensures that a column have a NULL values. uniQue -> Ensures that all values in a column are different.

SQL> insert into Persons1 values (10,NULL,'saini',20); insert into Persons1 values (10,NULL,'saini',20)

ERROR at line 1: ORA-01400: cannot insert NULL into ("MYDB"."PERSONS1"."LASTNAME") SQL> create table Persons2 ( ID int NOT NULL UNIQUE,LastName varchar(20) NOT NUL L, FirstName varchar (20),Age int); Table created. SQL> insert into Persons2 values (10,NULL,'saini',20); insert into Persons2 values (10,NULL,'saini',20)

ERROR at line 1: ORA-01400: cannot insert NULL into ("MYDB"."PERSONS2"."LASTNAME") SQL> insert into Persons2 values (10,'saini','chanchu',20); 1 row created. SQL> insert into Persons2 values (10,'gadwal','chanchu',20);

insert into Persons2 values (10,'gadwal','chanchu',20)

ERROR at line 1: ORA-00001: unique constraint (MYDB.SYS_C004045) violated SQL> create table Persons3( ID int NOT NULL, LastName varchar(21) NOT NULL, Fir stName varchar(21), Age int Check(Age>=18)); Table created. SQL> insert into Persons3 values (10,'saini','chanchu',20); 1 row created. SQL> insert into Persons3 values (10,'saini','chanchu',10); insert into Persons3 values (10,'saini','chanchu',10)

ERROR at line 1: ORA-02290: check constraint (MYDB.SYS_C004048) violated SQL> create table customers( ID int , Name varchar(20) NOT NUll, Age int , 2 address char(25), salary DeciMal(18,2), PRIMARY KEY(ID)); Table created.

hiredate date,

sal number(7,2),

comm number(7,2),

deptno number(2,0),

constraint pk_emp primary key (empno),

constraint fk_deptno foreign key (deptno) references dept (deptno)

create table dept(

deptno number(2,0),

dname varchar2(14),

loc varchar2(13),

constraint pk_dept primary key (deptno)

create table emp(

empno number(4,0),

ename varchar2(10),

job varchar2(9),

mgr number(4,0),

hiredate date,

sal number(7,2),

comm number(7,2),

deptno number(2,0),

constraint pk_emp primary key (empno),

constraint fk_deptno foreign key (deptno) references dept (deptno)

SQL> select * from emp where SAL>=2000 and SAL<=4000; EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7566 JONES MANAGER 7839 02-APR-81 2975 20 EMPNO ENAME JOB MGR HIREDATE SAL COMM


EMPNO ENAME JOB MGR HIREDATE SAL COMM

DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300

7521 WARD SALESMAN 7698 22-FEB-81 1250 500

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400

EMPNO ENAME JOB MGR HIREDATE SAL COMM

DEPTNO

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0

SQL> select * from emp where ENAME LIKE 'A%'; EMPNO ENAME JOB MGR HIREDATE SAL COMM

DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300

7876 ADAMS CLERK 7788 23-MAY-87 1100

SQL> select ENAME || ' == , == ' || job From emp; ENAME||'==,=='||JOB


KING == , == PRESIDENT BLAKE == , == MANAGER CLARK == , == MANAGER JONES == , == MANAGER SCOTT == , == ANALYST FORD == , == ANALYST SMITH == , == CLERK ALLEN == , == SALESMAN WARD == , == SALESMAN MARTIN == , == SALESMAN TURNER == , == SALESMAN

MILLER CLERK

SQL> select ENAME ,JOB FROM EMP WHERE JOB='CLERK' or JOB='MANAGER' or JOB='SALES MAN'; ENAME JOB


BLAKE MANAGER CLARK MANAGER JONES MANAGER SMITH CLERK ALLEN SALESMAN WARD SALESMAN MARTIN SALESMAN TURNER SALESMAN ADAMS CLERK JAMES CLERK MILLER CLERK 11 rows selected. SQL> select * from emp where JOB NOT IN('CLERK','SALESMAN','MANAGER'); EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO

7839 KING PRESIDENT 17-NOV-81 5000

7788 SCOTT ANALYST 7566 19-APR-87 3000

7902 FORD ANALYST 7566 03-DEC-81 3000

SQL>