4036dbms constraints
4036dbms constraints
They are the data integrity rules/restrictions which allows only valid
data into tables.
Through constraints we can fulfil the business requirements
We provide constraints on tables and even on views.
Constraint keys:
1. Primary key
2. Unique
3. Not null
4. Check
5. Ref(log)
6. Default
7. Foreign key
1. Primary key:
It acts as both (unique +not null) which means it won’t allow
duplicate and null values
Implicitly an unique index et defined on primary key columns
These should be only one primary key for an entire table
A P.K can hold maximum of 32 columns(i.e unique index
limitation)
Materialized view get defined only on tables, which are aving
primary key’s
Generally we call primary key table as master table/parent
table.
2. Unique key:
It allows only unique (null) and values (won’t allow duplicates)
Not null values are allowed through unique constraint
For unique key also an implicit unique index get defined
An unique key can hold maximum of 32 columns
3) Not Null: It won’t allow null values, but allows duplicate values.
Note:
Not null constraint are allowed only in columns levels
Views won’t allow not null constraints
4) Check:
To restrict/enforce other than standard integrity rules
(Primary key (unique + not null) we use check constraints.
Check constraint throws an error only when condition becomes
false, won’t throw for ‘true and null’
5) Default:
It takes default values (if user won’t provide any value to a
columns then default provides default values to columns).
It won’t allow sub queries and user defined functions.
6) Foreign key:
It’s a reference integrity constraint (RIC), if ever you provide
any value into the foreign key columns before begin inserted
that value will be referred through F.K with primary/unique
column
F.K allows null values for flexibility and allows duplicates
PK and FK columns names could be different but data types
should be same/compatible, size should also be same.
System defined column level (CL)
System defined table level (TL)
User defined column level
User defined table level.
Mixed method:
SQL>create table con10 (sno number (5) primary key, loc varchar2
(10),name varchar2(10), constraint un5(loc));
Composite key:
It holds more than one column (PK +FK +unique). Whichever the number of
columns PK is having FK also s to contain same number of columns.
Eg:-
C1 C2
10 X
10 X
10 Y
10 Null
10 Null
In the above table last two rows are unique or same values then null also treat as
same at that time it treats as duplicate record, so won’t allow.
In the above example null values become equal when all of the non-null values are
same.
Defining constraints on existing table by using alter command:
Note: It is not possible to add Not Null constraint rather we modify it from Null to
Not Null and Not Null to Null by using one alter we can use ‘n’ number of ‘adds’.
SQL>create table ctab1 sno number(5) primary key using index <index_nme>;
Add:
Modify:
Rename:
Syn: Alter table <table_name> rename constraint oldname to newname;
SQL>create table ct(sno number(5) constraint kp10 primary key, name
varchar2(10) constraint un10 unique);
SQL>alter table ct rename constraint kp10 to kp11;
Note: we cannot alter more than one column at a time.
Constraint states:
Delete rules:
Event Action
Deleting the parent records On deleting (while defining FK)
Dropping parent column and table Cascade constraint
Dropping/disabling PK when it is Cascade
relation with FK
If you want to delete the PK record you can not delete because table is in
relation with FK and you have child records so, you can not delete, first you
need to delete child records, these is a chance to delete the records by using
‘on delete cascade’.
If tables are in rlation (PK with FK) you can’t delete PK column and PK record
and PK table and PK until unless deleting the CT, but we have the chance to
drop and delete by using one table.
1. Initially immediate(default)
2. Initially deferrable
If constraint checking takes place at the individual statements i.e called
‘initially immediate’ which is default.
But if constraint checking takes place at the time of transaction is called
‘transaction specific’ we do this with ‘initially deferrable’.
Disadvantages of constraints:
Constraints can’t handle varying data, but by using trigger we can handle
varying data.
Constraint can check the old data but trigger’s can’t check the existing
data.(it checks only incoming data)
Constraints are more useful than trigger.
Constraints can give guarantee for centralized data.
Tables regarding with constraints:
we use following query to find out constraint name and table name once
we know the column name.
Constraint types:
Primary key P
Unique U
Foreign key R
Check C
Not null C