Você está na página 1de 6

DB2 for z/OS

Fundamentals
Lab Instruction










Copyright IBM Corporation 2009. All rights reserved. 1

Lab 5 Create Key Constraints
Objectives
Learn to use SPUFI
Learn to create primary keys
Learn to create foreign keys

Content
Create different types of keys:
Primary keys
Foreign keys
Query catalog tables for key related information

Description
This lab teaches students how to use SPUFI to create DB2 key constraints. Students
need to log-on to TSO, go into ISPF then DB2I, and use the SPUFI menu to create key
constraints.

Bibliography
[1] DB2 Universal Database for z/OS SQL Reference, March 2004, SC18-7426-00

Copyright IBM Corporation 2009. All rights reserved. 2
5.1 Create Key Constraints
In this unit, we are going to create a few key constraints on the existing tables to ensure data
integrity. The picture below shows the relationship between these tables, which is referential
structures.











Figure 5-1 Referential Structure

The detailed description of the key constraints is as follows:

Primary Keys:
Table Name Primary Key Column(s) Constraint Name
GRADE CLASS, STUDENT PK_GRADE
CLASS CLASSNO PK_CLASS
COURSE COURSENO PK_COURSE
STUDENT STUNO PK_STUDENT
INSTRUCTOR INSTNO PK_INSTRUCTOR

Foreign Keys:
Parent Table Name Child
Table
Name
Foreign Key
Column(s)
Constraint Name Delete Rule
CLASS GRADE CLASS FK_CLR RESTRICT
STUDENT GRADE STUDENT FK_SR RESTRICT
COURSE CLASS COURSENO FK_COCL RESTRICT
INSTRUCTOR CLASS INSTRUCTOR FK_ICL RESTRICT

STUDENT STUNO
GRADE CLASS STUDENT COURSE COURSENO
CLASS CLASSNO COURSENO INSTNO
INSTRUCTOR INSTNO
Copyright IBM Corporation 2009. All rights reserved. 3
5.1.1 Define Primary Keys:


[Hint] ALTER TABLE GRADE
ADD CONSTRAINT PK_GRADE
PRIMARY KEY (CLASS, STUDENT);

ALTER TABLE CLASS
ADD CONSTRAINT PK_CLASS
PRIMARY KEY (CLASSNO);

ALTER TABLE COURSE
ADD CONSTRAINT PK_COURSE
PRIMARY KEY (COURSENO);

ALTER TABLE STUDENT
ADD CONSTRAINT PK_STUDENT
PRIMARY KEY (STUNO);

ALTER TABLE INSTRUCTOR
ADD CONSTRAINT PK_INSTRUCTOR
PRIMARY KEY (INSTNO);

5.1.2 Define Foreign Keys:


[Hint] ALTER TABLE GRADE
ADD CONSTRAINT FK_CLR
FOREIGN KEY (CLASS) REFERENCES CLASS
ON DELETE RESTRICT;

ALTER TABLE GRADE
ADD CONSTRAINT FK_SR
FOREIGN KEY (STUDENT) REFERENCES STUDENT
ON DELETE RESTRICT;

Copyright IBM Corporation 2009. All rights reserved. 4
ALTER TABLE CLASS
ADD CONSTRAINT FK_COCL
FOREIGN KEY (COURSENO) REFERENCES COURSE
ON DELETE RESTRICT;

ALTER TABLE CLASS
ADD CONSTRAINT FK_ICL
FOREIGN KEY (INSTRUCTOR) REFERENCES INSTRUCTOR
ON DELETE RESTRICT;


5.2 Query Catalog Tables
5.2.1 Query the SYSTABCONST, SYSKEYCOLUSE, SYSRELS, and
SYSFOREIGNKEYS catalog tables


[Hint] SELECT CONSTNAME, TBNAME, IXNAME, COLCOUNT
FROM SYSIBM.SYSTABCONST
WHERE TBCREATOR = USER;

SELECT CONSTNAME, TBNAME, COLNAME, COLSEQ, COLNO
FROM SYSIBM.SYSKEYCOLUSE
WHERE TBCREATOR = USER;

SELECT REFTBNAME, TBNAME, RELNAME, COLCOUNT, DELETERULE
FROM SYSIBM.SYSRELS
WHERE CREATOR = USER;

SELECT CREATOR, TBNAME, RELNAME, COLNAME, COLNO, COLSEQ
FROM SYSIBM.SYSFOREIGNKEYS
WHERE CREATOR = USER;


Observe the contents in these catalog tables and answer the following questions:
1. Which kind of constraint (primary / foreign) is recorded in the SYSTABCONST table?

Copyright IBM Corporation 2009. All rights reserved. 5
Copyright IBM Corporation 2009. All rights reserved. 6

2. Do these constraints from question 1 require indexes?


3. Which kind of constraint (primary / foreign) is recorded in the SYSRELS table?


4. Do these constraints from question 1 require indexes?


5. Which table contains information on the columns used for primary keys?


6. Which table contains information on the columns used for foreign keys?



[ANSWER]
1. Primary
2. Not necessarily
3. Foreign
4. Yes
5. SYSIBM.SYSKEYCOLUSE
6. SYSIBM.SYSFOREIGNKEYS

Você também pode gostar