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

Friday, 16 February 2024

Changing a column Datatype length in MARIADB

In MariaDB, you can modify the length of a column's datatype using the ALTER TABLE statement. Below are examples demonstrating how to change the length of columns for various datatypes:

1. Changing VARCHAR length:

Suppose you have a table named MyTable with a column named MyVarcharColumn of datatype VARCHAR(50), and you want to change its length to VARCHAR(100).

ALTER TABLE MyTable

MODIFY COLUMN MyVarcharColumn VARCHAR(100);

2. Changing CHAR length:

Suppose you have a table named MyTable with a column named MyCharColumn of datatype CHAR(10), and you want to change its length to CHAR(20).

ALTER TABLE MyTable

MODIFY COLUMN MyCharColumn CHAR(20);

3. Changing TEXT length:

Suppose you have a table named MyTable with a column named MyTextColumn of datatype TEXT, and you want to change its length to TEXT(5000).

ALTER TABLE MyTable

MODIFY COLUMN MyTextColumn TEXT(5000);

4. Changing numeric types precision/scale:

Suppose you have a table named MyTable with a column named MyDecimalColumn of datatype DECIMAL(10,2), and you want to change its precision to DECIMAL(12,2).

ALTER TABLE MyTable

MODIFY COLUMN MyDecimalColumn DECIMAL(12,2);

5. Changing datetime types:

Suppose you have a table named MyTable with a column named MyDatetimeColumn of datatype DATETIME, and you want to change its datatype to DATETIME(3).

ALTER TABLE MyTable

MODIFY COLUMN MyDatetimeColumn DATETIME(3);

These examples demonstrate how to modify the length or precision of columns for various datatypes using the ALTER TABLE statement in MariaDB. Make sure to review and handle any potential data loss or truncation issues before modifying the column datatype length or precision.

No comments:

Post a Comment

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