Alter data type in postgres12/5/2023 +-+-+-+-Ĭustomer_group | character varying | | extended | Now, you can check whether the change of the name column was cascaded to the customer_data view: test=# \d+ customer_data Ĭolumn | Type | Modifiers | Storage | Description The name column is used in the customer_data view. This example uses the ALTER TABLE RENAME COLUMN statement to rename the name column of the customer_groups table to group_name. The following statement uses the ALTER TABLE RENAME COLUMN statement to rename the email column of the customers table to contact_email: ALTER TABLE customersĬode language: SQL (Structured Query Language) ( sql ) 2) Using RENAME COLUMN to rename a column that has dependent objects example INNER JOIN customer_groups g ON g.id = c.group_id Ĭode language: SQL (Structured Query Language) ( sql ) 1) Using RENAME COLUMN to rename one column example Then, create a new view named customer_data based on the customers and customer_groups tables. CREATE TABLE customer_groups (įOREIGN KEY ( group_id) REFERENCES customer_groups ( id) Setting the sample tablesįirst, create two new tables customers and customer_groups. Let’s take some examples of using the ALTER TABLE RENAME COLUMN to get a better understanding. If you rename a column referenced by other database objects such as views, foreign key constraints, triggers, and stored procedures, PostgreSQL will automatically change the column name in the dependent objects. To rename multiple columns, you need to execute the ALTER TABLE RENAME COLUMN statement multiple times, one column at a time: ALTER TABLE table_nameĬode language: SQL (Structured Query Language) ( sql ) Unfortunately that PostgreSQL does not provide the IF EXISTS option for the RENAME clause. RENAME column_name TO new_column_name Code language: SQL (Structured Query Language) ( sql )įor some reason, if you try to rename a column that does not exist, PostgreSQL will issue an error. The COLUMN keyword in the statement is optional therefore you can omit it like this: ALTER TABLE table_name
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |