Sie sind auf Seite 1von 17

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : M.Bhagya Lakshmi,
Designation : Lecturer in CCP,
Branch : DCCP
Institution : GMR Polytechnic, Gajwel
Semester : V Semester
Subject name : RDBMS
Subject Code : CCP- 505
Duration : 50 min.
Major Topic : SQL Plus
Sub –Topic : DATA CONSTRAINTS
Teaching Aids : PPT CCP505.57 1
Objectives:

On completion of this period, you would be


able to know
 FOREIGN KEY CONCEPTS
 FOREIGN KEY as a column constraint
 FOREIGN KEY as a table constraint

CCP505.57 2
Recap
 Primary key constraint
 Unique key constraint
 Avoid duplicates in fields
 Composite key can be defined
 Referential integrity

CCP505.57 3
Introduction
 Primary key and unique key constraint
does not allow duplicates in records
 Not null does allow to leave the fields
without entering any value
 Reference is referring to more than one
table
 Establish relation to more than one table.

CCP505.57 4
Foreign Key

 What is foreign key?


 How it works?

CCP505.57 5
Foreign Key Concepts
 Foreign key represent relationships
between tables.
 A foreign key is a column (or a group of
columns) whose values are derived from
the primary key of the same or some
other table.

CCP505.57 6
Existence of primary Key
 Implies that the table is related to the primary
key table from which foreign key is derived
 Foreign key must have a corresponding
primary key value in the primary table to have
a meaning

CCP505.57 7
Foreign key/references constraint
 Rejects an INSERT or UPDATE of a value, if a
corresponding value does not currently exist in
the primary key table.
 Rejects a DELETE, if it would invalidate a
REFERENCES constraint;
 Must reference a PRIMARY KEY or UNIQUE
columns in a primary key table;

CCP505.57 8
Foreign key/references constraint
(contd..)
 Must reference a table, not a view or cluster
 Requires that you own the primary key table,
 Have REFERENCE privilege on it,
 Or have column_level REFERENCE privilege on
the referenced columns in the primary key table;
 Doesn’t restrict how other constraints may
reference the same tables;

CCP505.57 9
Foreign key/references constraint
(contd..)
 Requires that the FOREIGN key column and the
CONSTRAINT columns have matching data
types;
 May reference the same table named in the
CREATE TABLE statement;
 Must not reference the same column more than
once (in a single constraint)

CCP505.57 10
FOREIGN KEY as a column constraint
CREATE TABLE sales_order details
(s_order_no varchar2(6) REFERENCES
sales_orer, product_no varchar2(6),
Qty _ordered number(8), qty_disp number(8),
product_rate number(8,2),
PRIMARY KEY(s_order_no,product_no));

CCP505.57 11
FOREIGN KEY as a table constraint
CREATE TABLE sales_order_details
(s_order_no varchar2(6), product_no varchar2(6),
qty_ordered number(8), qty_disp number(8),
product_rate number(8,2),
PRIMARY KEY(s_order_no, product_no)
FOREIGN KEY(s_order no )REFERENCES
sales_order);

CCP505.57 12
Example
CREATE TABLE emp(empno number(4)
PRIMARY KEY, ename varchar2(15), job
varchar2(15), sal number(9,2), comm
varchar2(9,2), hiredate date, deptno number(2)
REFERENCES dept);

CCP505.57 13
Summary
We have discussed about
 FOREIGN KEY CONCEPTS
FOREIGN KEY as a column constraint
 FOREIGN KEY as a table constraint

CCP505.57 14
Quiz
 ______ key represent relationships
between tables.
c) Primary key
d) Foreign key
e) Both
f) none

CCP505.57 15
Frequently asked questions
 Define primary key.
 Define primary key and foreign key.
 Explain foreign key constraint.

CCP505.57 16
Assignment

1. Create two tables, one table with primary


key and another table with foreign key

CCP505.57 17

Das könnte Ihnen auch gefallen