Examples of redefining a table by using the ALT line command
You can use the ALT line command to make changes to your tables. With ALT, you can make both changes that are done by ALTER TABLE statements and changes that cannot be done by ALTER TABLE statements. Because ALT supports more than just ALTER statement changes, the ALT process is referred to as redefining a table.
- Inserting a column
- Updating a column
- Adding a unique key to a table
- Changing a unique key
- Adding a column to a primary key
- Renaming a table
- Adding a partition to a table
- Adding a partition to a table in a partition-by-growth (PBG) table space
- Inserting a partition into a table
- Adding a foreign key
Inserting a column
Procedure
To insert a column:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the table to be changed.
-
On the ALTER Table (ADB27C)
panel, issue
the I line command against the column after which you
want to insert a new column:
-
For the line that contains
INSERT in the Operation
Type column, complete the fields that contain
? as shown to define the new column, and press
Enter:
-
If you want to specify additional attributes for the new column, use the
U line command, and on the ALTER Table (ADB26CTU)
panel, specify the
updated values, and press Enter:
- On the ALTER Table (ADB27C) panel, issue the NEXT command until the ALTER - Build Analyze and Apply Job (ADBPALT) panel is displayed.
- Choose options for building the WSL or batch job to implement the change, and press Enter to generate the job.
Updating a column
Procedure
To update a column:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the table to be changed.
- On the ALTER Table (ADB27C) panel, type over the fields for any column that you want to update.
-
If you want to update column attributes that are not displayed on the ALTER Table (ADB27C)
panel, issue the
U line command against the column:
-
On the ALTER Table (ADB26CTU)
panel, specify
updated values for any attributes, and press Enter:
If you altered a primary key column of a table, an additional primary command, ADDFK, is displayed on the ALTER Table (ADB27C) panel. ADDFK propagates the primary key column updates for the target table to all tables that are affected by the update. If you specify ADDFK, all affected tables are displayed on the Alter Objects (ADB27CA) panel and included in the subsequently generated JCL for the ALTER. Submit that JCL and skip the remaining steps.
- On the ALTER Table (ADB27C) panel, issue the NEXT command until the ALTER - Build Analyze and Apply Job (ADBPALT) panel is displayed.
- Choose options for building the WSL or batch job to implement the change, and press Enter to generate the job.
Adding a unique key to a table
Procedure
To add a unique key to a table:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the table to which you want to add a unique key.
-
On the ALTER Table (ADB27C)
panel, issue
the CONSTRAINTS primary command.
If any constraints exist on the table, the Alter - Unique Constraints (ADBP7CN) panel lists the primary key and unique key constraints.
- If the Alter - Unique Constraints (ADBP7CN) panel is displayed, issue the ADD primary command.
-
On the Create Primary or Unique Key
(ADBP7CTP) panel, specify
the following options for the unique key, and issue the
NEXT command:
- In the Constraint name field, type a name for the constraint.
- In the Type field, specify whether the key is a primary or a unique key.
- For the columns in the table, use the nn line command to specify the relative position of the column in the key.
- If the ALTER Table (ADB27C) panel is not displayed, press PF3 until that panel is displayed.
- On the ALTER Table (ADB27C) panel, issue the NEXT command until the ALTER - Build Analyze and Apply Job (ADBPALT) panel is displayed.
- Choose options for building the WSL or batch job to implement the change, and press Enter to generate the job.
Changing a unique key
Procedure
To change a unique key:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the table for which you want to alter a unique key.
-
On the ALTER Table (ADB27C)
panel, issue
the CONSTRAINTS primary command.
The Alter - Unique Constraints (ADBP7CN) panel lists the primary key and unique key constraints for the table. If this panel is not displayed, no constraints exist on the table.
- Issue the A line command against the constraint that you want to alter.
-
On the resulting panel, either the Alter Primary Key (ADBP7CTP)
panel or Alter Unique Key (ADBP7CTP)
panel, specify the
options that you want to change, and issue the NEXT
command:
- If you are changing a primary key, in the Constraint name field, type a new name.
- Use the nn line command to change the relative position of the column in the key.
- Press PF3 to return to the ALTER Table (ADB27C) panel.
- On the ALTER Table (ADB27C) panel, issue the NEXT command until the ALTER - Build Analyze and Apply Job (ADBPALT) panel is displayed.
- Choose options for building the WSL or batch job to implement the change, and press Enter to generate the job.
Adding a column to a primary key
About this task
When you add a column to a primary key, the underlying index that enforces that constraint needs to be changed at the same time. (This index is called the primary index.) The complete change can be made using a single ALT command.
Procedure
To add a column to a primary key:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the table whose primary key you want to alter, and press Enter.
- On the ALTER Table (ADB27C) panel, issue the CONSTRAINTS primary command, and press Enter.
-
On the Alter - Unique Constraints (ADBP7CN)
panel, issue
the A line command against the primary key that you want to alter, and press
Enter.
The Alter Primary Key (ADBP7CTP) panel displays the current primary key.
In the following example, the key includes columns INT1 and INT2, in that order:
ADBP7CTP --------------- DD1A Alter Primary Key ------------- Row 1 to 5 of 5 Table schema . . . : TS6462 Table name . . . . : PKTAB Constraint name . . : INT1 > Type . . : PRIMARY BUSINESS_TIME WITHOUT OVERLAPS . . . NO (Yes/No) Commands: NEXT Line commands: nn - Sequence R - Remove column sequence Select Column Name Col Seq Col Type Length Scale Nulls Period * * * * * * * ------ ------------------ ------- -------- ----------- ------ ----- ------ NAME 0 CHAR 24 0 N INT1 1 INTEGER 4 0 N INT2 2 INTEGER 4 0 N INT3 0 INTEGER 4 0 N INT4 0 INTEGER 4 0 Y ******************************* END OF DB2 DATA *******************************
- To add a column to the primary key, specify a sequence number in the
Select column, and press Enter.
For example, to add the INT3 column to the primary key, specify 3 next to INT3:
ADBP7CTP --------------- DD1A Alter Primary Key ------------- Row 1 to 5 of 5 Table schema . . . : TS6462 Table name . . . . : PKTAB Constraint name . . : INT1 > Type . . : PRIMARY BUSINESS_TIME WITHOUT OVERLAPS . . . NO (Yes/No) Commands: NEXT Line commands: nn - Sequence R - Remove column sequence Select Column Name Col Seq Col Type Length Scale Nulls Period * * * * * * * ------ ------------------ ------- -------- ----------- ------ ----- ------ NAME 0 CHAR 24 0 N INT1 1 INTEGER 4 0 N INT2 2 INTEGER 4 0 N 3 INT3 0 INTEGER 4 0 N INT4 0 INTEGER 4 0 Y ******************************* END OF DB2 DATA *******************************
After you press Enter, the Seq column is updated:ADBP7CTP --------------- DD1A Alter Primary Key ------------- Row 1 to 5 of 5 Table schema . . . : TS6462 Table name . . . . : PKTAB Constraint name . . : INT1 > Type . . : PRIMARY BUSINESS_TIME WITHOUT OVERLAPS . . . NO (Yes/No) Commands: NEXT Line commands: nn - Sequence R - Remove column sequence Select Column Name Col Seq Col Type Length Scale Nulls Period * * * * * * * ------ ------------------ ------- -------- ----------- ------ ----- ------ NAME 0 CHAR 24 0 N INT1 1 INTEGER 4 0 N INT2 2 INTEGER 4 0 N * INT3 3 INTEGER 4 0 N INT4 0 INTEGER 4 0 Y ******************************* END OF DB2 DATA *******************************
You can also make other changes on this panel, such as changing the name of the constraint or removing columns from the sequence. You can make multiple changes to the table during this ALT session.
- Issue the NEXT command, and press Enter.
- On the Alter - Unique Constraints (ADBP7CN) panel, press PF3 to exit the panel.
-
On the ALTER Table (ADB27C)
panel, issue
the NEXT command and press Enter.
The following warning message is displayed:
Primary index required.
This warning is a reminder that you must also alter the primary index to include the added column.
- On the ALT - Related Objects (ADBP7REL) panel, specify the A line command next to the index to add it to the list of altered objects, and press Enter.
- Press PF3 to exit the panel.
- On the Alter Objects (ADB27CA) panel, specify the A line command next to the index, and press Enter.
-
On the Redefine Index (ADB21XAR)
panel,
specify a sequence number next to the column that you just added to the
primary key, and press Enter.
For example, specify 3 next to INT3:
ADB21XAR --------------- DD1A Redefine Index ---------------- Row 1 to 3 of 5 Commands: NEXT ORIGINAL Line commands: nnn A|D - Sequence & order R - Remove the column I - Include A - Ascending D - Descending RA - Random U - Update expression/XML pattern ? - Show all line commands CREATE INDEX TS6462 . PIDX > ON TS6462.PKTAB Owner . . . . . . > Owner type . . . . . (U/R) Unique . . . . . . YES Where Not Null . . . Cluster . . . . . NO Buffer Pool . . . BP0 Close Rule . . . . . YES Copy Allowed . . NO Piece Size . . . . 4194304 Define . . . . . . . YES Defer . . . . . . Partitioned . . . Padded . . . . . . . Compress . . . . NO Exclude Null Keys NO Select Column Name Col Type Length Scale N ColSeq Ord OldSeq Ord * * * * * * * * * ------ ------------------ -------- ----------- ------ - ------ --- ------ --- INT1 INTEGER 4 0 N 1 A 1 A INT2 INTEGER 4 0 N 2 A 2 A NAME CHAR 24 0 N 3 INT3 INTEGER 4 0 N INT4 INTEGER 4 0 Y
After you press Enter, the ColSeq column is updated. - Issue NEXT until the ALTER - Build Analyze and Apply Job (ADBPALT) panel is displayed:
- Choose options for building the WSL or batch job to implement the change, and press Enter to generate the job.
Renaming a table
Procedure
To rename a table:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the REN line command against the table that you want to rename.
- On the Rename Table (ADB21TR) panel, in the New name field, specify the new name, and press Enter.
- If the Statement Execution Prompt (ADB2PSTM) panel is displayed, enter the appropriate action to execute the RENAME statement.
Adding a partition to a table
Procedure
To add a partition to a table:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the partitioned table to which you want to add a partition.
-
On the ALTER Table (ADB27C)
panel, issue
the ALTPART primary command to add or alter a partition of a table-based
partitioned table, and press Enter.
If the ALTPART command is not listed, the table is not partitioned.
- On the Alter Partitioned Table (ADB27CPV) panel, issue the ADD primary command to add a partition to the end of the table.
- Enter the limit key value for the new partition.
- Issue the NEXT command.
- On the ALTER Table (ADB27C) panel, issue the NEXT command until the ALTER - Build Analyze and Apply Job (ADBPALT) panel is displayed.
- Choose options for building the WSL or batch job to implement the change, and press Enter to generate the job.
Adding a partition to a table in a partition-by-growth (PBG) table space
Procedure
To add a partition to a table in a PBG table space:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the partitioned table to which you want to add a partition.
-
On the ALTER Table (ADB27C)
panel, issue
the ADDPART primary command, and press Enter.
The Partitions field is updated to reflect the change. Specifying a zero (ADDPART 0) resets the number of partitions to the original value.
- Issue the NEXT command until the ALTER - Build Analyze and Apply Job (ADBPALT) panel is displayed.
- Choose options for building the WSL or batch job to implement the change, and press Enter to generate the job.
Inserting a partition into a table
Procedure
To insert a partition into a table:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the partitioned table where you want to insert a partition.
-
On the ALTER Table (ADB27C)
panel, specify
the ALTPART primary command, and press Enter.
If the ALTPART command is not listed, the table is not partitioned.
-
On the Alter Partitioned Table
(ADB27CPV) panel,
issue the INS line command against the partition after which you want to
insert a new partition, as shown in the following example:
ADB27CPV ----------------- DD1A Alter Partitioned Table---- Row 1 to 12 of 12 Command ===> Scroll ===> CSR New schema . : TS5771 New name . . : TB1100 Commands: NEXT ADD ORIGINAL Line commands: INS - Insert Partition Sel Part Limit Key Value (Type a new value to add or alter) Oper. --- ------ -----------------------------------------------------------> ------ 1 '09999999999999999999999999999999' INS 2 '10000000000000000000000000000000' 3 '19999999999999999999999999999999' 4 '20000000000000000000000000000000' 5 '29999999999999999999999999999999' 6 '39999999999999999999999999999999' 7 '49999999999999999999999999999999' 8 '59999999999999999999999999999999' 9 '69999999999999999999999999999999' 10 '79999999999999999999999999999999' 11 '89999999999999999999999999999999' 12 MAXVALUE ******************************* END OF DB2 DATA *******************************
-
Enter the limit key value for the new partition, as shown in the following example:
ADB27CPV ----------------- DD1A Alter Partitioned Table---- Row 1 to 12 of 12 Command ===> Scroll ===> CSR New schema . : TS5771 New name . . : TB1100 Commands: NEXT ADD ORIGINAL Line commands: INS - Insert Partition Sel Part Limit Key Value (Type a new value to add or alter) Oper. --- ------ -----------------------------------------------------------> ------ 1 '09999999999999999999999999999999' * 2 '10000000000000000000000000000000' ? '11000000000000000000000000000000' INSERT 3 '19999999999999999999999999999999' 4 '20000000000000000000000000000000' 5 '29999999999999999999999999999999' 6 '39999999999999999999999999999999' 7 '49999999999999999999999999999999' 8 '59999999999999999999999999999999' 9 '69999999999999999999999999999999' 10 '79999999999999999999999999999999' 11 '89999999999999999999999999999999' 12 MAXVALUE ******************************* END OF DB2 DATA *******************************
Tip: If, after editing the limit key values, you decide that you want to reset all changes to the original values, use the RESET command. - Issue the NEXT command.
- On the ALTER Table (ADB27C) panel, issue the NEXT command until the ALTER - Build Analyze and Apply Job (ADBPALT) panel is displayed.
- Choose options for building the WSL or batch job to implement the change, and press Enter to generate the job.
Adding a foreign key
Procedure
To add a foreign key:
- On the DB2 Administration Menu (ADB2) panel, select option 1.
- On the System Catalog (ADB21) panel, select option T.
- On the Tables, Views, and Aliases (ADB21T) panel, issue the ALT line command against the table to be changed.
- On the ALTER Table (ADB27C) panel, specify the NEXT primary command, and press Enter.
-
On the Alter Objects (ADB27CA)
panel, specify
the CFK (Create foreign key) line command next to the table where you want
to add the foreign key.
ADB27CA n ---------------------- DD1A Alter Objects ---------- Row 1 to 1 of 1 Command ===> Scroll ===> PAGE Commands: NEXT - Generate jobs ADD - Add objects ALTOPT - Change alter options Line commands: A - Alter object D - Delete S - Select object REL - Alter related FK - Add FK-affected tables RI - Add RI-related tables E - Edit view DDL ? - Show all line commands Object Object RI RI FK Sel Qual Name Ty Info 1 Info 2 Rels Add Add Operation * * * * * * * * * --- -------> -----------------> -- -------> -------> ------ --- --- ----------- CFK ADM001 TABLE01 TB KAWDCC KAWSCC 0 NA NA MODIFY ******************************* END OF DB2 DATA *******************************
-
On the Alter Foreign Key Constraint
(ADB21TAF) panel,
specify the name of the constraint and the columns, and press Enter:
ADB21TAF -------------- DD1A Alter Foreign Key Constraint -------------- Command ===> Commands: COLUMNS More: + ALTER TABLE Table schema . . . ADM001 > Table name . . . . TABLE01 > FOREIGN KEY Constraint name . . PRODUCT > (? to look up existing constraints Columns ( . . . . . . PRODNO, PRODNAME > ) REFERENCES Table schema . . . ADM001 > Table name . . . . > (? to look up) ON DELETE . . . . . RESTRICT (RESTRICT, CASCADE, SET NULL, or NO ACTION) ENFORCED . . . . . . YES (Yes/No, default is Yes)
The ALTER TABLE statement is generated. - If the Statement Execution Prompt (ADB2PSTM) panel is displayed, confirm that you want to execute the statement.