Add or modify the primary index of the table in Teradata using alter table query

Initially the table might be created without Primary index or Unique primary index. Later we wants to add the primary index to the existing tables.

Add the Unique primary index for the empty table in Teradata

If the table is empty,we can just add the primary index of the table using alter table query.

Syntax to Alter the Unique Primary Index

Example for Alter table Unique Primary Index

The table CUSTOMER is created without primary index Then we have added the Cust_id as the Unique primary index to the table using the alter table query.

Alter the Unique primary index for non-empty table in Teradata

If the table has contains the data, Teradata will not allow to add the unique primary index of the table. We need to copy the data out to another table, volatile or permanent and then we can alter the index of the table.

Step 1 : Copy the table to another table using Create table as Select query

Step 2 : Delete all the records in the table that needs to alter the primary index

Step 3: Alter the Primary index of the table using alter table query.

Step 4: Copy all records from the backup table using the Insert into Select query

Step 5 : Drop the Backup table using drop table query

Recommended Articles