0% found this document useful (0 votes)
43 views9 pages

Aim:-Apply Integrity Constraints For The Specified System

The document discusses various integrity constraints that can be applied to database tables including primary keys, foreign keys, unique keys, null constraints, not null constraints, check constraints, and default constraints. Examples are provided for each constraint type showing how to create tables with the specified constraints and test that the constraints enforce the expected data integrity rules. Primary keys require unique, non-null values. Foreign keys reference a primary key in another table. Unique keys require unique but potentially null values. Check constraints validate column values meet a specified condition. Default constraints supply a default value if one is not provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views9 pages

Aim:-Apply Integrity Constraints For The Specified System

The document discusses various integrity constraints that can be applied to database tables including primary keys, foreign keys, unique keys, null constraints, not null constraints, check constraints, and default constraints. Examples are provided for each constraint type showing how to create tables with the specified constraints and test that the constraints enforce the expected data integrity rules. Primary keys require unique, non-null values. Foreign keys reference a primary key in another table. Unique keys require unique but potentially null values. Check constraints validate column values meet a specified condition. Default constraints supply a default value if one is not provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

EXP.4. Apply Integrity Constraints for the specified system.

Aim:- Apply Integrity Constraints for the specified system.

Theory:
1. PRIMARY KEY CONSTRAINTS
2. FOREIGN KEY CONSTRAINTS
3. UNIQUE KEY CONSTRAINTS
4. NULL CONSTRAINTS
5. NOT NULL CONSTRAINTS
6. CHECK CONSTRAINTS
7. DEFAULT CONSTRAINTS

PRIMARY KEY

Create table customer such that the contents of the column cust_no is unique and not null.

Create table customer


(cust_no int primary key,
fname varchar2(10),
lname varchar2(10),
Occupation varchar2 (20)
);

output: table created.

For testing purpose execute the following INSERT INTO statement:


INSERT INTO CUSTOMER values (1,’smita’,’jawale’,’AP’);

output: 1 row created.

To verify whether the primary key constraint is functional ,reissue the same INSERT INTO
statement .the result is the following error:

INSERT INTO CUSTOMER values (1,’smita’,’jawale’,’AP’);

output: error at line 1: ORA-00001: unique constraint(DBA_BANKSYS.SYS_C00309)


VIOLATED.
FOREIGN KEY

create table emp with its primary as emp_no referencing the foreign key branch_no in the branch
table.

Output: Table created

UNIQUE KEY

Create table customer such that the contents of the column cust_no is unique across the entire
column.

Create table customer


(cust_no int unique,
fname varchar2(10),
lname varchar2(10),
Occupation varchar2 (20)
);

output: table created.

For testing the unique constraint execute the following INSERT INTO statement:
INSERT INTO CUSTOMER values (1,’smita’,’jawale’,’AP’);
INSERT INTO CUSTOMER values (1,’anita’,’shetye’,’APP’);
INSERT INTO CUSTOMER values (2,’mamta’,’joshi’,’AOP’);

output: The first INSERT INTO statement will execute without any errors as shown below
1 row created.

When second INSERT INTO statement is executed an errors occurs as shown below
the result is the following error:
error at line 1: ORA-00001: unique constraint(DBA_BANKSYS.SYS_C00309)
VIOLATED.

When third INSERT INTO statement rectifies this and the result is as shown below
1 row created

use select * and check values.


NULL
create table branch such that the contents of the column bno are unique across the entire column.

Create table branch


( bno int,
bname varchar(10)
);

Output: Table created

Next ,insert two records into this table .

INSERT INTO branch values (100,’null’);

output: 1 row created.

INSERT INTO branch values (100,’’);

output: 1 row created.

The first statement inserts a record with branch name that is null,while the second statement inserts
a record with an empty string as a branch name.
Now ,retrieve all rows with a branch name that is empty string value as follows:
select * from branch where bname=’’;

when this staement is executed ,it is expected to retrieve the row that was inserted above .but
instead ,this statement will not retrieve any records at all.
Now , try retrieving all rows where the branch name contains a null value:

select * from branch where bname is null;

when this statement is executed, both rows are retrieved. This is because oracle has now changed
its rules so that empty strings behave as null values.

NOT NULL

create table branch such that the contents of the column bname are NOT NULL.

Create table branch


( bno int,
bname varchar(10) not null
);

Output: Table created

Next ,insert two records into this table .


INSERT INTO branch values (100,’null’);

output: error at line 1:


ORA-01400: can not insert NULL into (.......)

CHECK

Create table customer with the following check constraints

• data values being inserted into the column cust_no must start with the capital letter C
• data values being inserted into the column fname,lname should be in upper case only.

Create table customer


(cust_no varchar2(20) check (cust_no like ‘c%’),
fname varchar2(10) check(fname=upper(fname)),
lname varchar2(10) check(fname=upper(lname)),,
Occupation varchar2 (20)
);

output: table created.

Create table customer with the following check constraints

• data values being inserted into the column cust_no must start with the capital letter C
• data values being inserted into the column fname,lname should be in upper case only.

Create table customer


(cust_no varchar2(20),
fname varchar2(10) ,
lname varchar2(10) ,
Occupation varchar2 (20)
check (cust_no like ‘c%’),
check(fname=upper(fname)),
check(fname=upper(lname))
);

output: table created.

INSERT INTO CUST VALUES(1,’SMITA’,’JAWALE’,’AP’);

OUTPUT: error in line 1

check constaint violated


INSERT INTO CUST VALUES(C1,’smita’,’jawale’,’AP’);

OUTPUT: error in line 1

check constaint violated

INSERT INTO CUST VALUES(C1,’SMITA’,’JAawale’,’AP’);

OUTPUT: error in line 1

check constaint violated

DEFAULT:

At the time of table creation a default value can be assigned to a column.when record is loaded
into the table,and the column is left empty, the oracle engine will autoatically load thi scolumn
with the defaultvalue specified.

Create table customer


(cust_no varchar2(20),
fname varchar2(10) ,
lname varchar2(10) ,
Occupation varchar2 (20),
cursal int, default 0,
status varchar(1) default ‘A’);
);
output: table created.

PRIMARY KEY:
SQL> create table ksr
2 (
3 cust_no int primary key,
4 fname varchar2(10),
5 lname varchar2(10),
6 occupation varchar2(20)
7 );

Table created.

SQL> insert into ksr values(1,'Kritika','Rao','Student');

1 row created.

SQL> insert into ksr values(1,'Kritika','Rao','Student');


insert into ksr values(1,'Kritika','Rao','Student')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C009492) violated

UNIQUE:
SQL> create table ksr1
2 (
3 cust_no int unique,
4 fname varchar2(10),
5 lname varchar2(10),
6 occupation varchar2(20)
7 );

Table created.

SQL> insert into ksr1 values(2,'Bindu','Rao','Banker');

1 row created.

SQL> insert into ksr1 values(2,'Bindu','Rao','Banker');


insert into ksr1 values(2,'Bindu','Rao','Banker')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C009492) violated

FOREIGN KEY:
SQL> Create table mcust
2 (
3 cust_no int primary key,
4 fname varchar2(10),
5 lname varchar2(10),
6 occupation varchar2(20)
7 );

Table created.
SQL> create table mbranch
2 (
3 mbno int,
4 bname varchar(10),
5 cno int,
6 foreign key(cno) references mcust(cust_no)
7 );

Table created.
SQL> desc mbranch;
Name Null? Type
----------------------------------------- -------- ----------------------------
MBNO NUMBER(38)
BNAME VARCHAR2(10)
CNO NUMBER(38)

NULL:

SQL> create table hostel


2 (
3 hno int,
4 hname varchar(10),
5 haddress varchar(10)
6 );

Table created.

SQL> insert into hostel values(1,'Chennai','null');

1 row created.

NOT NULL:

SQL> create table hostel2


2 (
3 hno int,
4 hname varchar(10),
5 haddress varchar(10) NOT NULL
6 );

Table created.
SQL> insert into hostel2 values(1,'HHHGM','');
insert into hostel2 values(1,'HHHGM','')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."HOSTEL2"."HADDRESS")

CHECK:

SQL> create table banker


2 (
3 b_no varchar2(20),
4 fname varchar(20),
5 lname varchar(20),
6 occupation varchar(20),
7 check(b_no like 'c%'),
8 check ( fname = upper(fname)),
9 check ( fname = upper(lname))
10 );

Table created.

SQL> insert into banker values(1,'KRITIKA','RAO','Banker');


insert into banker values(1,'KRITIKA','RAO','Banker')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C009532) violated

SQL> insert into banker values(C1,'KRITIKA','Rao','Banker');


insert into banker values(C1,'KRITIKA','Rao','Banker')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.SYS_C009532) violated

DEFAULT:

SQL> Create table mcust


2 (
3 cust_no int primary key,
4 fname varchar2(10),
5 lname varchar2(10),
6 occupation varchar2(20)
7 );

Table created.
SQL> Alter table mcust
2 add status varchar(1) default 'A';

Table altered.

SQL> select * from mcust;

CUST_NO FNAME LNAME OCCUPATION CID MNAME S


---------- ---------- ---------- -------------------- ---------- ---------- -
1 Malavika Nair Student A
2 pooj nayaka student 11 A
3 jini null 21 A
4 abc xyz null 33 A
5 abc xyz 23 A
6 a z s 12 A
6 rows selected.

CONCLUSION: All conditions for integrity constraints are studied and implemented.

You might also like