Lab 08
Lab 08
IT[244]
Lab #:08
FACULTY OF CS & IT
UNIVERSITY OF GUJRAT
Adding Constraints?
– Constraints enforce rules at the table level.
– The following constraint types are valid in
Oracle:
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
Constraint Guidelines
– Name a constraint or the Oracle Server
will generate a name by using the SYS_Cn
format.
– Create a constraint:
• At the same time as the table is created
• After the table has been created
– Define a constraint at the column or table
level.
Naming Conventions
– Must begin with a letter
– Can be 1–30 characters long
– Must contain only A–Z, a–z, 0–9, _, $, and
#
– Must not duplicate the name of another
object owned by the same user
– Must not be an Oracle Server reserved
word
The DEFAULT Option
– Specify a default value for a column during an
insert.
…… ename
ename VARCHAR(20)
VARCHAR(20) DEFAULT
DEFAULT ‘ABC’,
‘ABC’, ……
The NOT NULL Constraint
• Defined at the column level
SQL>
SQL> ALTER
ALTER TABLE
TABLE department
department
22 DROP
DROP PRIMARY
PRIMARY KEY
KEY CASCADE;
CASCADE;
Table
Table altered.
altered.
The CASCADE option of the DROP clause causes any dependent constraints
also to be dropped.
Disabling Constraints
– Execute the DISABLE clause of the ALTER
TABLE statement to deactivate an integrity
constraint.
– Apply the CASCADE option to disable
dependent integrity constraints.
SQL>
SQL> ALTER
ALTER TABLE
TABLE employee
employee
22 DISABLE
DISABLE CONSTRAINT
CONSTRAINT emp_empno_pk
emp_empno_pk CASCADE;
CASCADE;
Table
Table altered.
altered.
Enabling Constraints
– Activate an integrity constraint currently
disabled in the table definition by using the
ENABLE clause.
SQL>
SQL> ALTER
ALTER TABLE
TABLE employee
employee
22 ENABLE
ENABLE CONSTRAINT
CONSTRAINT emp_empno_pk;
emp_empno_pk;
Table
Table altered.
altered.