DEV Community

Cover image for PostgreSQL(Alternations)
s mathavi
s mathavi

Posted on

PostgreSQL(Alternations)

-ALTER TABLE → modify existing table structure.
-Operations: add/drop column, change datatype, rename column/table, add/drop constraints.

Common ALTER TABLE Operations

1. Add a new column

ALTER TABLE Employee
ADD COLUMN email VARCHAR(100);

2. Drop (remove) a column

ALTER TABLE Employee
DROP COLUMN email;

3. Change datatype of a column


ALTER TABLE Employee
ALTER COLUMN salary TYPE NUMERIC(12,2);

4. Rename a column

ALTER TABLE Employee
RENAME COLUMN name TO full_name;

5. Add a constraint

ALTER TABLE Employee
ADD CONSTRAINT salary_positive CHECK (salary > 0);

6. Drop a constraint

ALTER TABLE Employee
DROP CONSTRAINT salary_positive;

7. Rename the table itself

ALTER TABLE Employee
RENAME TO Staff;

This is the basic PostgreSQL...

See you soon in the next blog!....

Top comments (0)