Example: Adding a Foreign Key to a Column-Partitioned Table - Analytics Database - Teradata Vantage

SQL Data Definition Language Syntax and Examples

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Teradata Vantage
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2024-10-04
dita:mapPath
jco1628111346878.ditamap
dita:ditavalPath
qkf1628213546010.ditaval
dita:id
mdr1472255012272
lifecycle
latest
Product Category
Teradata Vantage™

Suppose that after defining tables p and c, you decide to add a foreign key relationship between these tables, with column-partitioned table c referencing table p using a table-level foreign key constraint.

Table c has the following definition.

     CREATE TABLE c (
       c1  INTEGER, 
       cb  INTEGER, 
       cd1 DATE, 
       cc2 CHARACTER(30))
     PARTITION BY (COLUMN,
                   RANGE_N(cd1 BETWEEN DATE '2006-01-01' 
                               AND     DATE '2020-12-31'
                               EACH INTERVAL '1' MONTH));

Table p has the following definition.

     CREATE TABLE p (
       p1  INTEGER, 
       pb  INTEGER NOT NULL UNIQUE, 
       pc1 CHARACTER(10))
     PRIMARY INDEX (p1);

You submit the following ALTER TABLE statement to add the desired table-level foreign key constraint to table c, with column c.cb referencing column p.pb.

     ALTER TABLE c ADD FOREIGN KEY (cb) REFERENCES p (pb);

Because this foreign key only defines a relationship between a single column in the referencing and the referenced tables, you could just as well have defined it as a column-level constraint like the following.

     ALTER TABLE c ADD cb REFERENCES p (pb);

These ALTER TABLE statements both produce the following error table definition.

     CREATE TABLE c_0 (
       c1  INTEGER, 
       cb  INTEGER, 
       cd1 DATE, 
       cd2 CHARACTER(30))
     NO PRIMARY INDEX;