0% found this document useful (0 votes)
19 views15 pages

Lab 08

SQL

Uploaded by

melaibaabbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

Lab 08

SQL

Uploaded by

melaibaabbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Database Systems Lab

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> CREATE TABLE employee(


2 empno NUMBER(4),
3 ename VARCHAR2(10) NOT NULL,
4 job VARCHAR2(9),
5 mgr NUMBER(4),
6 hiredate DATE,
7 sal NUMBER(7,2),
8 comm NUMBER(7,2),
9 deptno NUMBER(7,2) NOT NULL);
The NOT NULL Constraint
• Add to the existing column

SQL> ALTER TABLE employee MODIFY


2 empno NUMBER(4) constraint emp_cons
3 NOT NULL
The NOT NULL Constraint
• Dropping the constraint

SQL> ALTER TABLE employee DROP


2 constraint emp_cons
The PRIMARY KEY Constraint
• Defined at either the table level or the column level

SQL> CREATE TABLE department(


2 deptno NUMBER(2),
3 dname VARCHAR2(14),
4 loc VARCHAR2(13),
5 CONSTRAINT dept_dname_uk UNIQUE (dname),
6 CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));
The FOREIGN KEY Constraint
• Defined at either the table level or the column
level
SQL> CREATE TABLE employee(
2 empno NUMBER(4),
3 ename VARCHAR2(10) NOT NULL,
4 job VARCHAR2(9),
5 mgr NUMBER(4),
6 hiredate DATE,
7 sal NUMBER(7,2),
8 comm NUMBER(7,2),
9 deptno NUMBER(7,2) NOT NULL,
10 CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno)
11 REFERENCES dept (deptno));
The CHECK Constraint

– Defines a condition that each row must satisfy


– Expressions that are not allowed:

..., deptno NUMBER(2),


CONSTRAINT emp_deptno_ck
CHECK (DEPTNO BETWEEN 10 AND 99),...
Adding a Constraint
• Add a FOREIGN KEY constraint to the EMP
table indicating that a manager must already
exist as a valid employee in the EMP table.

SQL> ALTER TABLE employee


2 ADD CONSTRAINT emp_mgr_fk
3 FOREIGN KEY(mgr) REFERENCES employee(empno);
Table altered.
Dropping a Constraint
– Remove the manager constraint from the
EMP table.
SQL>
SQL> ALTER
ALTER TABLE
TABLE employee
employee
22 DROP
DROP CONSTRAINT
CONSTRAINT emp_mgr_fk;
emp_mgr_fk;
Table
Table altered.
altered.
• Remove the PRIMARY KEY constraint on the DEPT table and drop
the associated FOREIGN KEY constraint on the EMP.DEPTNO
column.

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.

– A UNIQUE or PRIMARY KEY index is


automatically created if you enable a
UNIQUE key or PRIMARY KEY constraint.

You might also like