b409 ri trig
TRANSCRIPT
-
7/27/2019 b409 Ri Trig
1/34
Module 9: Referential Integrity and Triggers
After completing this module, you will be able to:
Describe the concept and benefits of referential integrity.
Explain the concepts behind relationships that occur amongtables.
Use the INSERT and UPDATE statements to make changes to
a table that has references to other tables.
Describe the access rights you need to create and replacetriggers.
-
7/27/2019 b409 Ri Trig
2/34
Referential Integrity
Reasons to implement RI
Data integrity and consistency
Increases development productivityusers dont have to code SQL
statements to enforce referential constraints
Optimized performance Teradata chooses the most efficient method
to enforce the referential constraints
User applications may rely on Referential Integrity for their functionality
Relationships among tables based on the definition of:
A primary key - referenced column
A foreign key - referencing column
Referenced columns must be defined as:
Unique columns
Not Null columns
Prevents database corruption when performing INSERT, UPDATEs, and
DELETEs.
A row cannot exist with a non-null value for a referencing column if an equalvalue does not exist in a referenced column.
-
7/27/2019 b409 Ri Trig
3/34
Referential Model
Employee Dept Emp_Mgr Job Last First Salary
Number Number Number Code Name Name Amount
PK FK FK FK
100001 1001 ? 3000 DeBosse Ibee 200000.00
100797 1048 100791 3017 Myers Ruth 41000.00
100002 1001 100001 3001 Smith Steve 110000.00
100471 1028 100442 3017 Roberts Rich 40000.00
100389 1023 100381 3018 Ball Jason 64000.00
Employee
Job Job
Code Description
PK
3000 President
3001 Senior Mgmt
3017 Analyst L2
3018 Analyst L1
3045 Assembler L4
Job
Dept Dept Dept_Mgr Budget
Number Name Number Amount
PK FK
1048 Design SW 100791 1000000.00
1050 Design Services 100811 1000000.00
1028 Engineering SW 100441 3000000.00
1023 Engineering Disk 100381 2000000.00
1001 Leadership Team 10000
1 5000000.00
Department
Employee Area Phone
Number Code Number Extension
PK
FK
100001 937 5100001 1001
100001 937 4100001 1001
100389 858 4852815 815
100797 770 9082445 445
100797 310 5554512 ?
Employee_Phone
-
7/27/2019 b409 Ri Trig
4/34
Implementing Referential Integrity
Two different approaches to establishing RI and populating tables.
First Approach (recommended):
1. Create the tables and define the Primary Keys.
2. Populate the tables.
3. Create the Foreign Key references.
Second approach:
1. Create the tables and define the Primary Keys.
2. Create the Foreign Key references.
3. Populate the tables.
From previous example: The Department table has a nullable foreign key reference to the Employee table.
The Job table references no other table.
The Employee table has foreign key reference to the Department and Job tables.
Compress is not allowed on either the referencing or referenced columns. The
data types must be the same.
-
7/27/2019 b409 Ri Trig
5/34
Circular References
Employee DeptNumber Number
PK1 FK2
: :
100341 1022
Employee
Dept Dept Dept_MgrNumber Name Number
PK2 FK1
: : :
1022 Engineering CPU 100341
Department
INSERT INTO Employee (Employee_Number, Dept_Number, . . .)
VALUES (100341, NULL, . . .);
INSERT INTO Department (Dept_Number, Dept_Name, Dept_Mgr_Number)
VALUES (1022, 'Engineering CPU', 100341);
UPDATE Employee SET Dept_Number = 1022 WHERE Employee_Number = 100341;
To Insert
rows:
DELETE Employee WHERE Employee_Number= 100341; (error, referencing column)
UPDATE Department SET Dept_Mgr_Number = NULL WHERE Dept_Number = 1022 ;
DELETE Employee WHERE Employee_Number= 100341; (success)
To Delete
rows:
Note: FK1 and FK2 must allow NULLs for these operations to work.
-
7/27/2019 b409 Ri Trig
6/34
Referential Integrity Example
CREATE SET TABLE Employee
( employee_number INTEGER NOT NULL PRIMARY KEY,dept_number INTEGER
,emp_mgr_number INTEGER
,job_code INTEGER
,last_name CHAR(20)
,first_name VARCHAR(20)
,salary_amount DECIMAL(10,2) ) ;
CREATE SET TABLE Department
( dept_number INTEGER NOT NULL PRIMARY KEY
,dept_name CHAR(20) NOT NULL UNIQUE
,dept_mgr_number INTEGER
,budget_amount DECIMAL (10,2) ) ;
CREATE SET TABLE Job
( job_code INTEGER NOT NULL PRIMARY KEY,job_desc CHAR(20) NOT NULL UNIQUE ) ;
CREATE SET TABLE Emp_Phone
( employee_number INTEGER NOT NULL
,area_code SMALLINT NOT NULL
,phone_number INTEGER NOT NULL
,extension INTEGER
, PRIMARY KEY (employee_number, area_code, phone_number) )PRIMARY INDEX (employee_number);
Create the tablesspecifying the
Primary Keys.
Before creating the
References
constraints,
populate the tableswith user data.
-
7/27/2019 b409 Ri Trig
7/34
Referential Integrity Example (cont.)
ALTER TABLE EmployeeADD CONSTRAINT emp_dept_ref
FOREIGN KEY (dept_number) REFERENCES
Department (dept_number);
ALTER TABLE Employee
ADD CONSTRAINT emp_job_ref
FOREIGN KEY (job_code) REFERENCES
Job (job_code);
ALTER TABLE Employee
ADD CONSTRAINT emp_mgr_ref
FOREIGN KEY (emp_mgr_number) REFERENCES
Employee (employee_number);
ALTER TABLE DepartmentADD CONSTRAINT dept_mgr_ref
FOREIGN KEY (dept_mgr_number) REFERENCES
Employee (employee_number);
ALTER TABLE Emp_Phone
ADD CONSTRAINT phone_emp_ref
FOREIGN KEY (employee_number) REFERENCES
Employee (employee_number);
After populating the tableswith data, ALTER the tables
to create the References
constraints.
Notes:
The Employee table will have3 Reference Index subtables
created for it.
Error tables will be created
identifying rows where a FK
value does not exist in the
PK column(s).
Employee_0
Employee_4
Employee_8
Department_0
Emp_Phone_0
-
7/27/2019 b409 Ri Trig
8/34
Referential Integrity Example (cont.)
The tables (previous example) are shown below with the necessary indexesneeded to support this RI example.
EMPLOYEE
EMP
EMPLOYEE DEPT MGR JOB LAST
NUMBER NUMBER NUMBER CODE NAME
PK FK FK FKUPI
DEPARTMENT
DEPT
DEPT DEPT MGR BUDGETNUMBER NAME NUMBER AMOUNT
PK FK
UPI USI
JOB
JOB JOB
CODE DESC
PKUPI USI
EMP_PHONE
EMPLOYEE AREA PHONENUMBER CODE NUMBER EXTENSION
PK
FK
NUPI
USI
-
7/27/2019 b409 Ri Trig
9/34
Referential Integrity Views
View Description
DBC.All_RI_Children Information about all tables in child-parent order.
DBC.All_RI_Parents Information about all tables in parent-child order.
DBC.RI_Distinct_Children Reduces subset (eliminates duplicates) - information
about all tables in child-parent order.
DBC.RI_Distinct_Parents Reduces subset (eliminates duplicates) - information
about all tables in parent-child order.
DBC.RI_Child_Tables Provides internal table IDs of all tables in child-parentorder.
DBC.RI_Parent_Tables Provides internal table IDs of all tables in parent-child
order.
Examples are only provided of the first two views.
-
7/27/2019 b409 Ri Trig
10/34
DBC.All_RI_Children View
Provides information about all tables in child-parent order.
SELECT Indexid (FORMAT z9) AS ID
,IndexName
,ChildTable
,ChildKeyColumn
,ParentTable
,ParentKeyColumn
FROM DBC.ALL_RI_Children
WHERE ChildDB = USERORDER BY 3, 4 ;
Example Results:
Example:
List all of the referential
integrity constraints in
the child database.
DBC.All_RI_Children
IndexID IndexName ChildDB ChildTable
ChildKeyColumn ParentDB ParentTable ParentKeyColumn
InconsistencyFlag CreatorName CreateTimeStamp
ID IndexName ChildTable ChildKeyColumn ParentTable ParentKeyColumn
0 dept_mgr_ref Department dept_mgr_number Employee employee_number
0 emp_dept_ref Employee dept_number Department dept_number
8 emp_mgr_ref Employee emp_mgr_number Employee employee_number
4 emp_job_ref Employee job_code Job job_code0 phone_emp_ref Emp_Phone employee_number Employee employee_number
-
7/27/2019 b409 Ri Trig
11/34
DBC.All_RI_Parents View
Provides information about all tables in parent-child order.
SELECT Indexid (FORMAT 'z9') AS ID
,IndexName
,ParentTable
,ParentKeyColumn
,ChildTable
,ChildKeyColumn
FROM DBC.ALL_RI_Parents
WHERE ParentDB = USERORDER BY 3, 4 ;
Example Results:
Example:
List all of the referential
integrity constraints in
the parent database.
DBC.All_RI_Parents
IndexID IndexName ParentDB ParentTable
ParentKeyColumn ChildDB ChildTable ChildKeyColumn
InconsistencyFlag CreatorName CreateTimeStamp
ID IndexName ParentTable ParentKeyColumn ChildTable ChildKeyColumn
0 emp_dept_ref Department dept_number Employee dept_number
0 phone_emp_ref Employee employee_number Emp_Phone employee_number
0 dept_mgr_ref Employee employee_number Department dept_mgr_number
8 emp_mgr_ref Employee employee_number Employee emp_mgr_number4 emp_job_ref Job job_code Employee job_code
-
7/27/2019 b409 Ri Trig
12/34
Additional RI Views
These two additional views can be used to reduce duplicate rows for multi-column
foreign keys.
DBC.RI_DISTINCT_Children
IndexID IndexName ChildDB ChildTable ParentDB
ParentTable InconsistencyFlag CreatorName CreateTimeStamp
DBC.RI_DISTINCT_Parents
IndexID IndexName ParentDB ParentTable ChildDBChildTable InconsistencyFlag CreatorName CreateTimeStamp
These two additional views can be used if internal Table ID information is needed.
DBC.RI_Child_Tables
IndexID IndexName ChildDbID ChildTID ChildKeyFIDParentDbID ParentTID ParentKeyFID InconsistencyFlag CreatorName
CreateTimeStamp
DBC.RI_Parent_Tables
IndexID IndexName ParentDbID ParentTID ParentKeyFID
ChildDbID ChildTID ChildKeyFID InconsistencyFlag CreatorNameCreateTimeStamp
-
7/27/2019 b409 Ri Trig
13/34
Using the DBC.Tables View
SELECT TableName,TableKind
,ParentCount
,ChildCount
FROM DBC.Tables
WHERE DatabaseName = 'TFACT'
AND TableKind = 'T'ORDER BY 2, 1 ;
Example Results:
Example:List the tables objects
in the database TFACT
and identify parent
and child counts.
TableName TableKind ParentCount ChildCount
Department T 1 1
Employee T 3 3
Emp_Phone T 1 0
Job T 0 1Salary_Log T 0 0
The DBC.Tables view (shown previously) can be used to list parent and child
counts for tables. ParentCount how many foreign keys does a table have or how many parents does
the table reference?
ChildCount how many foreign keys reference this table or how many children does
the table have?
-
7/27/2019 b409 Ri Trig
14/34
Referential Integrity States
The status of a Referential integrity constraint is classified as follows:
Unresolved reference constraints-the FK exists, but the PK does not.
Creating a table with a Foreign Key before creating the table with Parent
Key (Primary Key).
Restoring a table with a Foreign Key and the Parent Key (Primary Key) table
does not exist or hasn't been restored.
Inconsistent reference constraint- both the FK and the PK exist, but the
constraint is marked as inconsistent.
When either the child or parent table is restored, the reference constraint
for the child table is marked as inconsistent. no inserts, updates, deletes or table changes allowed
Invalid Rows- both the FK and the PK exist and are considered
consistent, but specific foreign key values are identified as invalid.
Typically occurs when the reference constraints are created on already
populated tables or
Following a REVALIDATE REFERENCES command after a restore of eitherthe child or parent table.
-
7/27/2019 b409 Ri Trig
15/34
Unresolved Reference Constraints
The DBC.Databases2 view provides information about unresolved reference
constraints.
Unresolved reference constraints are caused by:
Creating a table with a Foreign Key before creating the table with Parent Key
(Primary Key).
Restoring a table with a Foreign Key and the Parent Key (Primary Key) table does not
exist or hasn't been restored.
DatabaseName DatabaseID UnresolvedRICountDBC.Databases2
Example: List all databases with
unresolved references.
DatabaseName DatabaseID UnresolvedRICount
PD 0000FE03 2
Results:
Restore the Parent Table to resolve the
references constraint, but the referencesconstraint is marked as inconsistent.
SELECT DatabaseName
,DatabaseId
,UnresolvedRICount
FROM DBC.Databases2
WHERE UnresolvedRICount > 0;
-
7/27/2019 b409 Ri Trig
16/34
Inconsistent Reference Constraints
When either the child or parent table is restored, the reference constraint is
marked as inconsistent.The DBC.All_RI_Children View can be used to identify tables with inconsistent
references following a restore operation.
IndexID IndexName ChildDB ChildTable
ChildKeyColumn ParentDB ParentTable ParentKeyColumn
InconsistencyFlag CreatorName CreateTimeStamp
ID IndexName ChildTable ChildKeyColumn ParentTable ParentKeyColumn ICF
0 dept_mgr_ref Department dept_mgr_number Employee employee_number Y
0 emp_dept_ref Employee dept_number Department dept_number Y
8 emp_mgr_ref Employee emp_mgr_number Employee employee_number Y
4 emp_job_ref Employee job_code Job job_code Y0 phone_emp_ref Emp_Phone employee_number Employee employee_number Y
DBC.All_RI_Children
Example: SELECT IndexID (FORMAT 'z9') AS ID,IndexName
,ChildTable ,ChildKeyColumn
,ParentTable ,ParentKeyColumn
,InconsistencyFlag AS ICF
FROM DBC.ALL_RI_ChildrenWHERE ChildDB = 'PD'
ORDER BY 3, 4 ;
Results:
-
7/27/2019 b409 Ri Trig
17/34
Handling Inconsistent References
When a table has an inconsistent reference, changes are not allowed to the
child key table (no inserts, updates, or deletes).
Options to handle inconsistent references.
DROP INCONSISTENT REFERENCES. The FK Constraints are now removed
from the table. Use the ALTER TABLE command to create new References
constraint(s).
Use the REVALIDATEREFERENCES command (ARC facility).
If a references constraint is created or revalidated, the foreign key values are
validated against PK values. FK values (missing from the PK) are marked as
invalid in the RI subtable and an error table is created (e.g., EMPLOYEE_0).
Warning: Proceed with caution!
ALTER TABLE child_table DROP INCONSISTENT REFERENCES;
FK Constraints are now removed from the table and the table may be updated withinconsistent foreign key values before the References constraint is created.
-
7/27/2019 b409 Ri Trig
18/34
What is a Trigger?
A Trigger may be defined as:
One or more stored SQL statementsassociated with a table
An event driven procedure attached
to a table
An object in a database, like tables,
views and macros
CREATE
DROP
SHOW
ALTER
RENAME
REPLACE
HELP
TRIGGERS
Triggers may not be used in conjunction
with:
The FastLoad utility
The MultiLoad utility
Updateable Cursors
DELETE DATABASE or DELETE USER cause all triggers to be dropped.
Privileges are required to create and drop triggers.
Any of the following SQL statements may be applied to triggers:
GRANT
REVOKE
CREATE
DROP TRIGGER
-
7/27/2019 b409 Ri Trig
19/34
Access Rights and Triggers
Example: CREATE TRIGGER trigger1
AFTER UPDATE OF (col1) ON table1 FOR EACH ROWWHEN NEW col1 > 100
INSERT INTO log_table VALUES .
Access Rights to Create Triggers
CREATE TRIGGER privilege on the subject table or the database.
SELECT privilege on any column referenced in a WHEN clause or a triggered SQL
statement subquery.
INSERT, UPDATE, or DELETE privileges on the triggered SQL statement target table,
depending on the triggered SQL statement.
Access Rights to Replace Triggers DROP TRIGGER privilege on the subject table or the database. The exception is
when you use the REPLACE TRIGGER statement when no target trigger exists and
you instead create a new trigger.
SELECT privilege on any column referenced in a WHEN clause or a triggered SQL
statement subquery.
INSERT, UPDATE, or DELETE privileges on the triggered SQL statement target table,depending on the triggered SQL statement.
-
7/27/2019 b409 Ri Trig
20/34
Trigger Example
CREATE SET TABLE Employee
(Name CHAR(15),Deptid INTEGER,
Salary DECIMAL(10,2),
Job_Title CHAR(15))
PRIMARY INDEX (Name);
CREATE SET TABLE Salarylog
(UserName CHAR(30),EmpName CHAR(30),
OldSalary DECIMAL(10,2),
NewSalary DECIMAL(10,2))
PRIMARY INDEX (UserName);
Create a trigger that places a new row into the Salarylog table whenever there is a salary
increase greater than 10%.
CREATE TRIGGER RaiseTrig
AFTER UPDATE OF (Salary) ON Employee
REFERENCING OLD AS OldRow NEW AS NewRow
FOR EACH ROW
WHEN ((NewRow.Salary - OldRow.Salary)/OldRow.Salary > 0.10)
( INSERT INTO SalaryLogVALUES (USER, NewRow.Name, OldRow.Salary, NewRow.Salary); );
-
7/27/2019 b409 Ri Trig
21/34
Trigger Example (cont.)
SELECT * FROM Employee; SELECT * FROM Salarylog;
Name Deptid Salary Job_Title *** Query completed. 0 row(s) found. 4
========= ====== ======= ========= field(s) returned.carol 3333 32000.00 directordebbie 4444 40000.00 presidentlarry 2222 22000.00 instructor
joe 2222 30000.00 instructorallan 1111 20000.00 consultant
UPDATE Employee SET salary = salary * 1.25 WHERE name = 'allan';UPDATE Employee SET salary = salary * 1.05 WHERE name = 'debbie';
UPDATE Employee SET salary = 50000 WHERE name = 'carol';
SELECT * FROM Employee;
Name Deptid Salary Job_Title
========= ====== ======= =========carol 3333 50000.00 director
debbie 4444 42000.00 presidentlarry 2222 22000.00 instructor
joe 2222 30000.00 instructorallan 1111 25000.00 consultant
SELECT * FROM Salarylog;
UserName EmpName OldSalary NewSalary
========= =========== ======== =========
PAYADMIN allan 20000.00 25000.00PAYADMIN carol 32000.00 50000.00
-
7/27/2019 b409 Ri Trig
22/34
Referential Integrity and Triggers Summary
Referential integrity is a concept of relationships between tables based on
the definition of a primary key and a foreign key in the tables.
Referential integrity prevents database corruption when application users
execute INSERT, UPDATE and DELETE statements.
When referential integrity is applied, columns within a referencing tableare foreign keys for columns in another referenced table. You must define
referenced columns as either:
Primary key columns, or UNIQUE columns
NOT NULL
To create or replace triggers, you need specific privileges.
-
7/27/2019 b409 Ri Trig
23/34
Review Questions
1. Number the following steps 1 to 3 based on the recommendations of this module.
___ Create references___ Create data tables
___ Populate the tables
2. Answer the following statements about Primary and Foreign Keys as true or false.
___ A PK value can be set to NULL as long as there are no FK values referencing
that PK value.
___ A PK value can be set to a different value as long as there are no FK values
referencing that PK value.
___ A FK value can be set to NULL.
___ A PK can be implemented as a NOT NULL NUPI.
___ A FK can only be implemented on indexed columns.
___ A PK is always implemented as a UNIQUE INDEX.
3. A trigger executes (fires) when either an ______, ______, or _______ statement modifies
a specified column or columns in a table.
-
7/27/2019 b409 Ri Trig
24/34
-
7/27/2019 b409 Ri Trig
25/34
Lab Exercises
Lab Exercise 9-1
Purpose
In this lab, you will use BTEQ or (Teradata SQL Assistant) to establish References constraints between
4 populated tables and view the associated data dictionary entries.
What you need
Populated PD tables and empty tables in your database
Tasks1. Use INSERT/SELECT to place all rows from the populated PD tables into your empty tables. Verify the
number of rows in your tables.
PD.Employee to populate Employee Count = _______
PD.Department to populate Department Count = _______
PD.Job to populate Job Count = _______
PD.Emp_Phone to populate Emp_Phone Count = _______
2. Use the GRANT statement to GRANT yourself the REFERENCES access rights on the tables.
-
7/27/2019 b409 Ri Trig
26/34
Lab Exercises
Lab Exercise 9-1 (cont.)
Tasks3. Create a References constraint between the Employee.Dept_Number column and the
Department.Dept_Number column.
What is the name of the Employee RI error table? _______________
How many rows are in this table? ______
Which department is not represented in the department table? _______
4. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References
constraint.
What is the IndexID of this constraint? _______
-
7/27/2019 b409 Ri Trig
27/34
Lab Exercises
Lab Exercise 9-1 (cont.)
Tasks5. Create the References constraint between the Employee.Job_code column and the Job.Job_Code
column.
What is the name of the Employee RI error table? _______________
How many rows are in this table? ______
Which job code is not represented in the job table? _______
6. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References
constraint.
What is the IndexID of this constraint? ________
7. Create the References constraint between the Employee.Emp_Mgr_Number column and the
Employee.Employee_Number column.
What is the name of the Employee RI error table? _______________
How many rows are in this table? ______
Which employee does not have a manager (Emp_Mgr_Number is 0 or NULL)? _______
8. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References
constraint.
What is the IndexID of this constraint? _______
-
7/27/2019 b409 Ri Trig
28/34
Lab Exercises
Lab Exercise 9-1 (cont.)
Tasks9. Create a References constraint between the Department.Dept_Mgr_Number column and the
Employee.Employee_Number column.
What is the name of the Department RI error table? _______________
How many rows are in this table? ______
10. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this Referencesconstraint.
What is the IndexID of this constraint? _______
11. Create a References constraint between the Emp_Phone.Employee_Number column and the
Employee.Employee_Number column.
What is the name of the Emp_Phone RI error table? _______________How many rows are in this table? ______
12. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References
constraint.
What is the IndexID of this constraint? _______
-
7/27/2019 b409 Ri Trig
29/34
Lab Solutions for Lab 9-1
Lab Exercise 9-1
1. Use INSERT/SELECT to place all rows from the populated PD tables into your empty tables. Verify the number ofrows in your tables.
INSERT INTO Employee SELECT * FROM PD.Employee;
SELECT COUNT(*) FROM Employee; Count = 1000
INSERT INTO Department SELECT * FROM PD.Department;
SELECT COUNT(*) FROM Department; Count = 60
INSERT INTO Job SELECT * FROM PD.Job;
SELECT COUNT(*) FROM Job; Count = 66
INSERT INTO Emp_Phone SELECT * FROM PD.Emp_Phone
SELECT COUNT(*) FROM Emp_Phone; Count = 2000
2. Use the GRANT statement to GRANT yourself the REFERENCES access rights on the tables.
GRANT REFERENCES ON Employee TO tljc20;
GRANT REFERENCES ON Department TO tljc20;
GRANT REFERENCES ON Job TO tljc20;
-
7/27/2019 b409 Ri Trig
30/34
Lab Solutions for Lab 9-1
Lab Exercise 9-1 (cont.)
3. Create a References constraint between the Employee.Dept_Number column and the Department.Dept_Numbercolumn.
ALTER TABLE Employee ADD CONSTRAINT emp_dept_ref
FOREIGN KEY (dept_number) REFERENCES
Department (dept_number) ;
What is the name of the Employee RI error table? EMPLOYEE_0
How many rows are in this table? 1Which department is not represented in the department table? 1000
4. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References constraint.
What is the IndexID of this constraint? 0
SELECT Indexid (FORMAT 'z9') AS ID,IndexName
,ChildTable,ChildKeyColumn,ParentTable,ParentKeyColumn
FROM DBC.ALL_RI_ChildrenWHERE ChildDB = USERORDER BY 3, 4 ;
ID IndexName ChildTable ChildKeyColumn ParentTable ParentKeyColumn0 emp_dept_ref Employee dept_number Department dept_number
-
7/27/2019 b409 Ri Trig
31/34
Lab Solutions for Lab 9-1
Lab Exercise 9-1 (cont.)
5. Create a References constraint between the Employee.Job_code column and the Job.Job_Code column.
ALTER TABLE Employee ADD CONSTRAINT emp_job_ref
FOREIGN KEY (job_code) REFERENCES Job (job_code);
What is the name of the Employee RI error table? EMPLOYEE_4
How many rows are in this table? 1
Which job code is not represented in the job table? 3000
6. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References constraint.
What is the IndexID of this constraint? 4
SELECT Indexid (FORMAT 'z9') AS ID,IndexName,ChildTable
,ChildKeyColumn,ParentTable,ParentKeyColumn
FROM DBC.ALL_RI_ChildrenWHERE ChildDB = USERORDER BY 3, 4 ;
ID IndexName ChildTable ChildKeyColumn ParentTable ParentKeyColumn
0 emp_dept_ref Employee dept_number Department dept_number
4 emp_job_ref Employee job_code Job job_code
-
7/27/2019 b409 Ri Trig
32/34
Lab Solutions for Lab 9-1
Lab Exercise 9-1 (cont.)
7. Create a References constraint between the Employee.Emp_Mgr_Number column and theEmployee.Employee_Number column.
ALTER TABLE Employee ADD CONSTRAINT emp_mgr_ref
FOREIGN KEY (emp_mgr_number) REFERENCES Employee (employee_number);
What is the name of the Employee RI error table? EMPLOYEE_8
How many rows are in this table? 1
Which employee does not have a manager (Emp_Mgr_Number is 0 or NULL)? 100001
SELECT * FROM Employee WHERE emp_mgr_number = 0:
8. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References constraint.
What is the IndexID of this constraint? 8
SELECT Indexid (FORMAT 'z9') AS ID ,IndexName
,ChildTable ,ChildKeyColumn,ParentTable ,ParentKeyColumn
FROM DBC.ALL_RI_Children
WHERE ChildDB = USER
ORDER BY 3, 4 ;
ID IndexName ChildTable ChildKeyColumn ParentTable ParentKeyColumn
0 emp_dept_ref Employee dept_number Department dept_number
8 emp_mgr_ref Employee emp_mgr_number Employee employee_number4 emp_job_ref Employee job_code Job job_code
-
7/27/2019 b409 Ri Trig
33/34
Lab Solutions for Lab 9-1
Lab Exercise 9-1 (cont.)
9. Create a References constraint between the Department.Dept_Mgr_Number column and theEmployee.Employee_Number column.
ALTER TABLE Department ADD CONSTRAINT dept_mgr_ref
FOREIGN KEY (dept_mgr_number) REFERENCES Employee (employee_number);
What is the name of the Department RI error table? DEPARTMENT_0
How many rows are in this table? 0
10. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References constraint.
What is the IndexID of this constraint? 0
SELECT Indexid (FORMAT 'z9') AS ID ,IndexName
,ChildTable ,ChildKeyColumn
,ParentTable ,ParentKeyColumn
FROM DBC.ALL_RI_Children
WHERE ChildDB = USERORDER BY 3, 4 ;
ID IndexName ChildTable ChildKeyColumn ParentTable ParentKeyColumn
0 dept_mgr_ref Department dept_mgr_number Employee employee_number
0 emp_dept_ref Employee dept_number Department dept_number
8 emp_mgr_ref Employee emp_mgr_number Employee employee_number4 emp_job_ref Employee job_code Job job_code
-
7/27/2019 b409 Ri Trig
34/34
Lab Exercise 9-1 (cont.)
11. Create a References constraint between the Emp_Phone.Employee_Number column and theEmployee.Employee_Number column.
ALTER TABLE Emp_Phone ADD CONSTRAINT phone_emp_ref
FOREIGN KEY (employee_number) REFERENCES Employee (employee_number);
What is the name of the Emp_Phone RI error table? EMP_PHONE_0
How many rows are in this table? 0
12. Use the DBC.All_RI_Children view (qualify the ChildDB to your database) and verify this References constraint.
What is the IndexID of this constraint? 0
SELECT Indexid (FORMAT 'z9') AS ID ,IndexName
,ChildTable ,ChildKeyColumn
,ParentTable ,ParentKeyColumn
FROM DBC.ALL_RI_ChildrenWHERE ChildDB = USER
ORDER BY 3, 4 ;
ID IndexName ChildTable ChildKeyColumn ParentTable ParentKeyColumn
0 dept_mgr_ref Department dept_mgr_number Employee employee_number
0 emp_dept_ref Employee dept_number Department dept_number
8 emp_mgr_ref Employee emp_mgr_number Employee employee_number
4 emp job ref Employee job code Job job code
Lab Solutions for Lab 9-1