0% found this document useful (0 votes)
5 views10 pages

Week10 - Constraints

Uploaded by

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

Week10 - Constraints

Uploaded by

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

BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

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

Defining Constraints
Constraints are usually created at the same time as the table. Constraints can be added
to a table after its creation and also temporarily disabled. Constraints can be defined at
one of two levels.

Column constraint level

column [CONSTRAINT constraint_name] constraint_type;

Table constraint level

Constraint Level Description

Column References a single column and is


defined within a specification for the
owing column; can define any type of
integrity constraint

Table References one or more columns and is


defined separately from the definitions of
the columns in the table; can define any
constraints except NOT NULL

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

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

Constraint Types

Constraint Description

NOT NULL Specifies that the column cannot contain


a null value

UNIQUE Specifies a column or combination of


columns whose values must be unique for
all rows in the table

PRIMARY KEY Uniquely identifies each row of the table

FOREIGN KEY Established and enforces a foreign key


relationship between the column and a
column of the referenced table

CHECK Specifies a condition that must be true

The NOT NULL Constraint


The NOT NULL constraint ensures that the column contains no null values. Columns
without the NOT NULL constraint can contain null values by default. The NOT NULL
constraint can be specified only at the column level, not at the table level.

You can specify the name of the constraint when you specify the constraint:

... last_name VARCHAR2(25)


CONSTRAINT emp_last_name_nn NOT NULL...
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE
CONSTRAINT emp_hire_date_nn
NOT NULL,
...

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

Unique Constraint
A UNIQUE key integrity constraint requires that every value in a column or set of
columns (key) be unique—that is, no two rows of a table can have duplicate values in a
specified column or set of columns. The column (or set of columns) included in the
definition of the UNIQUE key constraint is called the unique key. If the UNIQUE
constraint comprises more than one column, that group of columns is called a
composite unique key.

UNIQUE constraints allow the input of nulls unless you also define NOT NULL
constraints for the same columns. In fact, any number of rows can include nulls for
columns without NOT NULL constraints because nulls are not considered equal to
anything. A null in a column (or in all columns of a composite UNIQUE key) always
satisfies a UNIQUE constraint.

Defined at either the table level or the column level:

CREATE TABLE employees(


employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
CONSTRAINT emp_email_uk UNIQUE(email));

Primary Key
A PRIMARY KEY constraint creates a primary key for the table. Only one primary key
can be created for each table. The PRIMARY KEY constraint is a column or set of
columns that uniquely identifies each row in a table. This constraint enforces
uniqueness of the column or column combination and ensures that no column that is
part of the primary key can contain a null value. PRIMARY KEY constraints can be
defined at the column level or table level. A composite PRIMARY KEY is created by
using the table-level definition. A table can have only one PRIMARY KEY constraint but
can have several UNIQUE constraints.

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

Defined at either the table level or the column level:

CREATE TABLE departments(


department_id NUMBER(4),
department_name VARCHAR2(30)
CONSTRAINT dept_name_nn NOT NULL,
manager_id NUMBER(6),
location_id NUMBER(4),
CONSTRAINT dept_id_pk PRIMARY KEY(department_id));

Foreign Key
The FOREIGN KEY, or referential integrity constraint, designates a column or
combination of columns as a foreign key and establishes a relationship between a
primary key or a unique key in the same table or a different table. In the example on the
slide, DEPARTMENT_ID has been defined as the foreign key in the EMPLOYEES table
(dependent or child table); it references the DEPARTMENT_ID column of the
DEPARTMENTS table (the referenced or parent table). A foreign key value must match
an existing value in the parent table or be NULL. Foreign keys are based on data values
and are purely logical, not physical, pointers.

FOREIGN KEY constraints can be defined at the column or table constraint level. A
composite foreign key must be created by using the table-level definition. The foreign
key can also be defined at the column level, provided the constraint is based on a single
column. The syntax differs in that the keywords FOREIGN KEY do not appear. For
example:

CREATE TABLE employees


(...
department_id NUMBER(4) CONSTRAINT emp_deptid_fk
REFERENCES departments(department_id),
...
)

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

The foreign key is defined in the child table, and the table containing the referenced
column is the parent table. The foreign key is defined using a combination of the
following keywords:
● FOREIGN KEY is used to define the column in the child table at the table
constraint level.
● REFERENCES identifies the table and column in the parent table.
● ON DELETE CASCADE indicates that when the row in the parent table is
deleted, the dependent rows in the child table will also be deleted.
● ON DELETE SET NULL converts foreign key values to null when the parent
value is removed.

The default behavior is called the restrict rule, which disallows the update or deletion of
referenced data. Without the ON DELETE CASCADE or the ON DELETE SET NULL
options, the row in the parent table cannot be deleted if it is referenced in the child table.
Defined at either the table level or the column level:

CREATE TABLE employees(


employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
hire_date DATE NOT NULL,
...
department_id NUMBER(4),
CONSTRAINT emp_dept_fk FOREIGN KEY (department_id)
REFERENCES departments(department_id),
CONSTRAINT emp_email_uk UNIQUE(email));

FOREIGN KEY Constraint Keywords


● FOREIGN KEY: Defines the column in the child table at the table constraint level
● REFERENCES: Identifies the table and column in the parent table
● ON DELETE CASCADE: Deletes the dependent rows in the child table when a
row in the parent table is deleted.
● ON DELETE SET NULL: Converts dependent foreign key values to null

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

The CHECK Constraint


The CHECK constraint defines a condition that each row must satisfy. The condition
can use the same constructs as query conditions. A single column can have multiple
CHECK constraints which refer to the column in its definition. There is no limit to the
number of CHECK constraints which you can define on a column.
CHECK constraints can be defined at the column level or table level.

CREATE TABLE employees


(...
salary NUMBER(8,2) CONSTRAINT emp_salary_min
CHECK (salary > 0),
...

Modifying Constraints
Use the ALTER TABLE statement to:
● Add or drop a constraint, but not modify its structure
● Add a NOT NULL constraint by using the MODIFY clause

ALTER TABLE table


ADD [CONSTRAINT constraint] type (column);

Add a FOREIGN KEY constraint to the EMPLOYEES table indicating that a manager
must already exist as a valid employee in the EMPLOYEES table.

ALTER TABLE employees


ADD CONSTRAINT emp_manager_fk
FOREIGN KEY(manager_id)
REFERENCES employees(employee_id);

Table altered.

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

Dropping a Constraint
● Remove the manager constraint from the EMPLOYEES table.
● ALTER TABLE employees
● DROP CONSTRAINT emp_manager_fk;
● Remove the PRIMARY KEY constraint on the DEPARTMENTS table and drop
the associated FOREIGN KEY constraint on the
EMPLOYEES.DEPARTMENT_ID column.

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

-- PRACTICE --

1. Add a table-level PRIMARY KEY constraint to the EMP table on the ID column.
The constraint should be named at creation. Name the constraint
my_emp_id_pk. Hint: The constraint is enabled as soon as the ALTER TABLE
command executes successfully.

-- Paste your solution code below --


Code:

2. Create a PRIMARY KEY constraint to the DEPT table using the ID column. The
constraint should be named at creation. Name the constraint my_dept_id_pk.
Hint: The constraint is enabled as soon as the ALTER TABLE command
executes successfully.

-- Paste your solution code below --


Code:

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

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

-- Paste your solution code below --


Code:

4. Confirm that the constraints were added by querying the USER_CONSTRAINTS


view. Note the types and names of the constraints. Save your statement text in a
file called lab10_4.sql.

-- Paste your solution code below --


Code:

Department of Software Engineering, Faculty of Engineering & Technology, UoS.


BS(SWE) Part-II 2nd Semester Database Systems Week 10 - Constraints

5. Modify the EMP table. Add a COMMISSION 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.

-- Paste your solution code below --


Code:

Department of Software Engineering, Faculty of Engineering & Technology, UoS.

You might also like