Aim:-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.
To verify whether the primary key constraint is functional ,reissue the same INSERT INTO
statement .the result is the following error:
create table emp with its primary as emp_no referencing the foreign key branch_no in the branch
table.
UNIQUE KEY
Create table customer such that the contents of the column cust_no is unique across the entire
column.
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
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:
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.
CHECK
• 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.
• 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.
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.
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.
1 row created.
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.
1 row created.
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:
Table created.
1 row created.
NOT NULL:
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:
Table created.
DEFAULT:
Table created.
SQL> Alter table mcust
2 add status varchar(1) default 'A';
Table altered.
CONCLUSION: All conditions for integrity constraints are studied and implemented.