![]() ![]() To change the data types of multiple columns in a single statement, you use multiple ALTER COLUMN clauses like this: ALTER TABLE table_nameĪLTER COLUMN column_name1 TYPE new_data_type,ĪLTER COLUMN column_name2 TYPE new_data_type, The SET DATA TYPE and TYPE are equivalent. Third, supply the new data type for the column after the TYPE keyword.Second, specify the name of the column that you want to change the data type after the ALTER COLUMN clause.First, specify the name of the table to which the column you want to change after the ALTER TABLE keywords.Let’s examine the statement in a greater detail: To change the data type of a column, you use the ALTER TABLE statement as follows: ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type Code language: CSS ( css ) In this tutorial, you have learned how to use the PostgreSQL RENAME COLUMN clause in the ALTER TABLE statement to rename a column.Summary: this tutorial shows you step by step how to change the data type of a column by using the ALTER TABLE statement. These statements rename two columns name and phone of the customers table to customer_name and contact_phone respectively: ALTER TABLE customers 3) Using multiple RENAME COLUMN to rename multiple columns example JOIN customer_groups g ON g.id = c.group_id Code language: Shell Session ( shell )Īs you can see in the view definition, the name column has been changed to group_name. +-+-+-+-Ĭ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 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. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |