Você está na página 1de 24

Oracle 10g SQL

Module 11: Constraints

Copyright 2009 Accenture All Rights Reserved. Accenture, its logo, and Accenture High Performance Delivered are trademarks of Accenture.
Module 11 Objectives
Upon completing this module the learner will be able to:
Explain constraints
Create a table using syntax for constraints
Explain and use PRIMARY KEY, UNIQUE, and FOREIGN KEY
constraints
Describe how to impose constraints by altering the table
Describe how to drop, enable, or disable constraints

Copyright 2009 Accenture All Rights Reserved. Pilot Version 2


Module 11 Agenda
Topic Name Duration
Constraints Syntax and Types 30 min
NOT NULL Constraint and its Violation
UNIQUE Constraint and its Violation
PRIMARY KEY Constraint and its Violation
FOREIGN KEY Constraint and its Violation
CHECK Constraint and its Violation
Working with Constraints 10 min

Copyright 2009 Accenture All Rights Reserved. Pilot Version 3


Constraints
Are used to impose conditions on data in the database at table
level
Prevent the deletion of a table, if there are any dependencies of
data from another table on this table

Copyright 2009 Accenture All Rights Reserved. Pilot Version 4


Constraints: Syntax and Example
Syntax:
CREATE TABLE [schema.] table_name
(column1 data type [DEFAULT expr] [column1_constraint],
column2 data type [DEFAULT expr] [column2_constraint],
column3 data type
...
[table_constraint]);

Example:

Copyright 2009 Accenture All Rights Reserved. Pilot Version 5


UNIQUE Constraint
Prohibits multiple rows in the same column with same value
Allows NULL values
Can be defined either at the column or table level
Example:

Copyright 2009 Accenture All Rights Reserved. Pilot Version 6


UNIQUE Constraint: Violation

Unique constraint
on email (jsmith)
violated

Copyright 2009 Accenture All Rights Reserved. Pilot Version 7


PRIMARY KEY Constraint
It uniquely identifies a record in a table.
It cannot take NOT NULL values.
A table can have only one PRIMARY KEY.
It can be defined either at column level or table level.
Syntax:
CREATE TABLE table_name
(column1 datatype constraint
column2 datatype constaint
...
CONSTRAINT pk_column
PRIMARY KEY (column1, column2, ... column_n)

Copyright 2009 Accenture All Rights Reserved. Pilot Version 8


PRIMARY KEY Constraint: Example

Copyright 2009 Accenture All Rights Reserved. Pilot Version 9


PRIMARY KEY Constraint: Violation

Unique condition
of primary key
(emp_id) violated

Not null condition


of primary key
(emp_id) violated

Copyright 2009 Accenture All Rights Reserved. Pilot Version 10


FOREIGN KEY Constraint
Requires value in one table to match values in another table
Emp Dept
EMP_ID FIRST_NAME LAST_NAME DEPT_ID DEPT_ID DEPT_NAME MGR_ID LOC_ID
1001 Jack Smith 10 10 Services 175 100
1593 Jack Ryan 20 20 IT 256 200
1890 Jenome Taylor 30 30 Controls 300 300
1945 Andy Garcia 20 40 Sales 150 400

Foreign Key Primary Key

Emp.dept_id is the foreign key to dept.dept_id.


Values in emp.dept_id are dependent on the values present in
dept.dept_id.

Copyright 2009 Accenture All Rights Reserved. Pilot Version 11


FOREIGN KEY Constraint: Syntax
CREATE TABLE table_name
(column1 datatype constraint
column2 datatype constaint
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
);

Copyright 2009 Accenture All Rights Reserved. Pilot Version 12


FOREIGN KEY Constraint: Example

Copyright 2009 Accenture All Rights Reserved. Pilot Version 13


FOREIGN KEY Constraint: Violation

Foreign constraint
on dept_id violated

Copyright 2009 Accenture All Rights Reserved. Pilot Version 14


Knowledge Check
1. Is the following statement true/false?

Primary Key constraint is same as UNIQUE constraint.

2. What is the Foreign Key constraint?

Copyright 2009 Accenture All Rights Reserved. Pilot Version 15


Module 11 Agenda
Topic Name Duration
Constraints Syntax and Types 30 min
Working with Constraints 10 min
Add a Constraint Using ALTER
Drop, Enable, and Disable a Constraint

Copyright 2009 Accenture All Rights Reserved. Pilot Version 16


Adding a Constraint: Using ALTER
(1 of 3)
Use ALTER TABLE statement to:
Add or drop a constraint, but cannot modify the table structure
Enable or disable constraints
Add a NOT NULL constraint by using the MODIFY clause
Syntax:
ALTER TABLE table_name
ADD [CONSTRAINT constraint_name] constraint_type (column);

Syntax for adding CHECK constraint:


ALTER TABLE table_name
ADD CONSTRAINT constraint_name CHECK (column_name condition);

Syntax for adding PRIMARY KEY:


ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1,
column2, ... column_n);
Copyright 2009 Accenture All Rights Reserved. Pilot Version 17
Adding a Constraint: Using ALTER
(2 of 3)
Example:

Copyright 2009 Accenture All Rights Reserved. Pilot Version 18


Adding a Constraint: Using ALTER
(3 of 3)
Syntax for FOREIGN KEY:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n);

Example:

Copyright 2009 Accenture All Rights Reserved. Pilot Version 19


Dropping a Constraint
Syntax:
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

Example:

Copyright 2009 Accenture All Rights Reserved. Pilot Version 20


Enable and Disable a Constraint
Syntax:
ALTER TABLE table_name
ENABLE CONSTRAINT constraint_name;

ALTER TABLE table_name


DISABLE CONSTRAINT constraint_name

Example:

Copyright 2009 Accenture All Rights Reserved. Pilot Version 21


Knowledge Check
1. How can you add a constraint when creating table?

2. How can you add constraint after creating table?

3. What is the difference between dropping and disabling a


constraint?

Copyright 2009 Accenture All Rights Reserved. Pilot Version 22


Module 11 Summary
Upon completing this module, you should now be able to:
Explain constraints
Create a table with constraints
Explain and use PRIMARY KEY, UNIQUE, and FOREIGN KEY
constraints
Describe how to impose constraints by altering the table
Describe how to drop, enable, or disable constraints

Copyright 2009 Accenture All Rights Reserved. Pilot Version 23


Questions and Comments
What questions or comments
do you have?

Copyright 2009 Accenture All Rights Reserved. Pilot Version 24

Você também pode gostar