Sunteți pe pagina 1din 14

FATIMA JINNAH WOMEN UNIVERSITY

Department Of Software Engineering

LAB 10

Summary

Items Description
Course Title Database System
Lab Title Including Constraints
Duration 3 Hours
Operating System Windows Operating System/Oracle 11g
/Tool/Language
Objective To get familiar with constraints created at the time of table
creation and after table creation, enable and disable constraints

Including Constraints
In this lab, we will explore constraints created at the time of table creation and after the table
creation, enable and disable constraints.

Including Constraints

Constraints enforce rules at the table level. It prevent the deletion of a table if there are
dependencies.
The following constraint types are valid:
o NOT NULL
o UNIQUE
o PRIMARY KEY
o FOREIGN KEY
o CHECK

Instructor Manual/Database Systems BSE 1


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

All constraints are stored in the data dictionary. Constraints can be defined at the time of table
creation or after the table has been created. One can view the constraints defined for the specific
table by looking at the USER_CONSTRAINT data dictionary table. For naming the constraint, it
follows the standard object naming rules. If you do not name your constraint, the Oracle server
generates a name with the format SYS_Cn, where n is a integer so that the constraint name is
unique.

CREATE TABLE [schema.]table


(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);

Constraint can be created at the table level.


column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),

Constraint can also be created at the column level.

column [CONSTRAINT constraint_name] constraint_type,

Primary Key Constraint:

Primary key is a key that uniquely identifies each row.

Example:
In the example below, constraint is created at the column level, when column is created or declared
then at that time, constraint is also mentioned.

Instructor Manual/Database Systems BSE 2


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

Not Null Constraint:

Not null constraint assures that the value in the attribute should not be null.

Example:

In the query, when table is created then constraint are created at the table level, primary key is
mentioned but not with the column. Also note that job_id is declared as not null, it means that null
value is not allowed in this column. When inserting the values, the user must enter the values.

UNIQUE Constraint:

UNIQUE key constraint assures that the values in the field must be unique.

Example:

The query below defines the not null and unique constraint. The unique constraint defines that
values of email id should be unique. The UNIQUE key constraint can be defined at the column
level as well as the table level.

Foreign Key Constraint:

Foreign key constraint can be defined at the table level or column level. The keywords that are used
in the query are FOREIGN KEY which defines the column in the child table at the table-constraint
level, where as REFERENCES Identifies the table and column in the parent table
Instructor Manual/Database Systems BSE 3
FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

Example:

The query creates the foreign key of deptno in the employee table, whereas the primary key deptno
exists in the deptt table.

Check Constraint:

Check constraint defines the condition that each row must satisfy.

Example:
In the example below, the check constraint is implement in the salary attribute where condition is
placed that salary is greater than 0.

Example Including All Constraints:

Instructor Manual/Database Systems BSE 4


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

View Constraint:

To view the constraints along with the table name, retrieve the information from
USER_CONSTRAINTS.

Instructor Manual/Database Systems BSE 5


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

One can also view the constraint associated with column name.

One can also search the constraint name along with the constraint type.

Adding Constraint:

One can use the ALTER TABLE statement, but not modify its structure. One can also enable or
disable constraint.

Instructor Manual/Database Systems BSE 6


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

One can define the NOT NULL constraint only if the table is empty or if the column has a value
for every row. In order to add the NOT NULL constraint to the existing column by using the
modify clause in the ALTER TABLE statement.

Example:

In the query below, primary key constraint is added to the column “ID” in the table “hire_dates1”

The example added the not null constraint to the column “ID” to the table “hire_dates1” by using
the modify clause of ALTER TABLE statement.

Instructor Manual/Database Systems BSE 7


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

Dropping Constraint:

To drop the constraints, first identify the constraint name. Then use the ALTER TABLE statement
with the drop clause. The Cascade option of the drop clause causes any dependent constraints to be
dropped.

Example:

The example drops the check constraint emp_min_sal from table “emp”

The following query removes the primary key constraint on the “deptt” table and drops the
associated foreign key constraint on the associated table.

Disabling Constraint:

One can disable the constraint without dropping it or re-creating it with by using the ALTER
TABLE statement or CREATE TABLE with the DISABLE clause. It deactivates an integrity
constraint. In order to disable dependent integrity constraints, apply the cascade options.

Example:
The query disable the constraint id_hiredates_pk that belongs to table hiredates.

Enabling Constraint:

Enabling constraints include activate an integrity constraint currently disabled in the table definition
by using the enable clause in the ALTER TABLE or CREATE TABLE statement. Enabling a
primary key constraint that was disabled with the CASCADE option does not enable any foreign
keys that are dependent upon primary key.

Example:

The query enables the constraint id_hiredates_pk that was disabled in the last query.
Instructor Manual/Database Systems BSE 8
FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

Cascading Constraint:

The CASCADE CONSTRAINTS clause is used along with the DROP COLUMN clause. The
CASCADE CONSTRAINTS clause drops all referential integrity constraint that refers to the
primary and unique keys defined on the dropped columns. It drops all multicolumn constraints
defined on the dropped column.

LAB TASKS

1. Add a table level primary key constraint to the “Employee_Detail” table on the ID column.
The constraint should be named at creation. Name the constraint my_emp_id_pk.

2. Create a PRIMARY KEY constraint to the “DEPT_DETAIL” table using the ID column.
The constraint should be named at creation. Name the constraint my_deptid_pk.

3. Add a column DEPT_ID to the “Employee_Detail” table. Add a foreign key reference on
the “Employee_Detail” table that ensures that the employee is not assigned to a nonexistent
department. Name the constraint my_emp_dept_id_fk.

Instructor Manual/Database Systems BSE 9


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

4. Conform that the constraints were added by querying the USER_CONSTRAINTS view.
Note the types and names of the constraints.
5. Display the object names and types from USER_OBJECTS data dictionary view for the
“Employee_Detail” and “Dept_Detail” tables. Notice that the new tables and a new index
were created.
6. Modify the “Employee_Detail” table. Add a COMMISION column of NUMBER data type,
precision 2, scale 2. Add a constraint to the commission column that ensures that a
commission value is greater than zero.

Task 01:
Add a table level primary key constraint to the “Employee_Detail” table on the ID
column. The constraint should be named at creation. Name the constraint
my_emp_id_pk.

Task 02:
Create a PRIMARY KEY constraint to the “DEPT_DETAIL” table using the ID
column. The constraint should be named at creation. Name the constraint
my_deptid_pk.

Task 03:
Add a column DEPT_ID to the “Employee_Detail” table. Add a foreign key reference
on the “Employee_Detail” table that ensures that the employee is not assigned to a
nonexistent department. Name the constraint my_emp_dept_id_fk.

Instructor Manual/Database Systems BSE 10


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

Task 04:
Conform that the constraints were added by querying the USER_CONSTRAINTS
view.
Note the types and names of the constraints.

Task 05:
Display the object names and types from USER_OBJECTS data dictionary view for
the “Employee_Detail” and “Dept_Detail” tables. Notice that the new tables and a new
index were created.

Instructor Manual/Database Systems BSE 11


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

Task 06:
Modify the “Employee_Detail” table. Add a COMMISION column of NUMBER data
type, precision 2, scale 2. Add a constraint to the commission column that ensures that
a commission value is greater than zero.

Instructor Manual/Database Systems BSE 12


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

Instructor Manual/Database Systems BSE 13


FATIMA JINNAH WOMEN UNIVERSITY
Department Of Software Engineering

References:

Introduction to Oracle, Latest Edition

http://www.tutorialspoint.com/sql_certificate/using_ddl_statements.htm

Instructor Manual/Database Systems BSE 14

S-ar putea să vă placă și