How to transform table with postgresql?

Member

by brandy , in category: MySQL , 6 months ago

How to transform table with postgresql?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by jerad , 6 months ago

@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:

  1. Add a new column:
1
2
ALTER TABLE table_name
ADD column_name data_type;


  1. Remove a column:
1
2
ALTER TABLE table_name
DROP COLUMN column_name;


  1. Rename a column:
1
2
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;


  1. Change the data type of a column:
1
2
ALTER TABLE table_name
ALTER COLUMN column_name TYPE new_data_type;


  1. Rename a table:
1
2
ALTER TABLE old_table_name
RENAME TO new_table_name;


  1. Add a primary key constraint:
1
2
ALTER TABLE table_name
ADD PRIMARY KEY (column_name);


  1. 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.