@brandy
To transform a table with PostgreSQL, you can use the ALTER TABLE statement. Here are some common transformations you can make to a table:
- Add a new column:
1
2
|
ALTER TABLE table_name
ADD column_name data_type;
|
- Remove a column:
1
2
|
ALTER TABLE table_name
DROP COLUMN column_name;
|
- Rename a column:
1
2
|
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;
|
- Change the data type of a column:
1
2
|
ALTER TABLE table_name
ALTER COLUMN column_name TYPE new_data_type;
|
- Rename a table:
1
2
|
ALTER TABLE old_table_name
RENAME TO new_table_name;
|
- Add a primary key constraint:
1
2
|
ALTER TABLE table_name
ADD PRIMARY KEY (column_name);
|
- Add a foreign key constraint:
1
2
3
|
ALTER TABLE table_name
ADD CONSTRAINT fk_constraint_name
FOREIGN KEY (column_name) REFERENCES other_table_name (other_column_name);
|
These are just a few examples of the transformations you can make to a table in PostgreSQL. Make sure to carefully review the PostgreSQL documentation for more information on the various transformations available.