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 Third, specify the new name for the column after the TO keyword.Second, provide name of the column that you want to rename after the RENAME COLUMN keywords.First, specify the name of the table that contains the column which you want to rename after the ALTER TABLE clause.RENAME COLUMN column_name TO new_column_name Code language: SQL (Structured Query Language) ( sql ) To rename a column of a table, you use the ALTER TABLE statement with RENAME COLUMN clause as follows: ALTER TABLE table_name Introduction to PostgreSQL RENAME COLUMN clause Summary: in this tutorial, you will learn how to use the PostgreSQL RENAME COLUMN clause in the ALTER TABLE statement to rename one or more columns of a table.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |