Example 1
Widen the description column from VARCHAR(50) to VARCHAR(200)
The column is widened from VARCHAR(50) to VARCHAR(200). Existing rows (id 1 and 2) are unaffected — widening never truncates data. A new row with an 88-character description is then inserted successfully. On PostgreSQL, TYPE VARCHAR(200) is equivalent to TYPE CHARACTER VARYING(200); a USING clause is not needed here because the new type is a superset of the old one.
CREATE TABLE products (id INT, description VARCHAR(50));
INSERT INTO
products
VALUES
(1, 'Short description'),
(2, 'Another item');ALTER TABLE products MODIFY COLUMN description VARCHAR(200);
INSERT INTO
products
VALUES
(
3,
'This is a much longer description that would not have fit in fifty characters previously'
);
SELECT
id,
description
FROM
products
ORDER BY
id;| id | description |
|---|---|
| 1 | Short description |
| 2 | Another item |
| 3 | This is a much longer description that would not have fit in fifty characters previously |
CREATE TABLE products (id INT, description VARCHAR(50));
INSERT INTO
products
VALUES
(1, 'Short description'),
(2, 'Another item');ALTER TABLE products
ALTER COLUMN description VARCHAR(200);
INSERT INTO
products
VALUES
(
3,
'This is a much longer description that would not have fit in fifty characters previously'
);
SELECT
id,
description
FROM
products
ORDER BY
id;| id | description |
|---|---|
| 1 | Short description |
| 2 | Another item |
| 3 | This is a much longer description that would not have fit in fifty characters previously |
CREATE TABLE products (id INT, description VARCHAR(50));
INSERT INTO
products
VALUES
(1, 'Short description'),
(2, 'Another item');ALTER TABLE products
ALTER COLUMN description TYPE VARCHAR(200);
INSERT INTO
products
VALUES
(
3,
'This is a much longer description that would not have fit in fifty characters previously'
);
SELECT
id,
description
FROM
products
ORDER BY
id;| id | description |
|---|---|
| 1 | Short description |
| 2 | Another item |
| 3 | This is a much longer description that would not have fit in fifty characters previously |
MySQL/MariaDB use MODIFY COLUMN. SQL Server uses ALTER COLUMN. PostgreSQL uses ALTER COLUMN … TYPE. Results are identical across supported engines.