Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 16 February 2024

Changing a column Datatype length in TERADATA

In Teradata, altering the length of a column's datatype is done using the MODIFY clause within the ALTER TABLE statement. However, Teradata doesn't support directly modifying the length of existing columns. Instead, you have to create a new table with the desired schema and then copy the data from the old table to the new one. Here's how you can change the length of columns with different data types in Teradata:

Let's assume we have a table named employee_info with various columns such as employee_name (VARCHAR), employee_id (INTEGER), hire_date (DATE), and salary (DECIMAL). We'll change the length of the employee_name column and the precision of the salary column.

-- Create a new table with the desired schema

CREATE TABLE employee_info_new (

    employee_name VARCHAR(100),

    employee_id INTEGER,

    hire_date DATE,

    salary DECIMAL(10, 2)

);

-- Copy data from the old table to the new one

INSERT INTO employee_info_new (employee_name, employee_id, hire_date, salary)

SELECT 

    CAST(employee_name AS VARCHAR(100)), 

    employee_id, 

    hire_date, 

    CAST(salary AS DECIMAL(10, 2))

FROM employee_info;

-- Drop the old table

DROP TABLE employee_info;

-- Rename the new table to the original name

ALTER TABLE employee_info_new RENAME TO employee_info;

In this example:

1. We create a new table named employee_info_new with the desired schema, including the modified lengths for the employee_name and salary columns.

2. We use an INSERT INTO ... SELECT statement to copy data from the old table to the new one while performing any necessary data type conversions.

3. We drop the old table employee_info.

4. Finally, we rename the new table employee_info_new to the original table name employee_info.

Keep in mind that this approach requires downtime for the table employee_info during the schema change process. Also, ensure that you have appropriate backups and perform these operations during a maintenance window to avoid disruptions.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.