The expression after the USING keyword can be as simple as column_name::new_data_type such as price::numeric or as complex as a custom function. In case the cast fails, PostgreSQL will issue an error and recommends you provide the USING clause with an expression for the data conversion. If you omit the USING clause, PostgreSQL will cast the values to the new ones implicitly. The USING clause specifies an expression that allows you to convert the old values to the new ones. PostgreSQL allows you to convert the values of a column to the new ones while changing its data type by adding a USING clause as follows: ALTER TABLE table_nameĪLTER COLUMN column_name TYPE new_data_type USING expression Ĭode language: SQL (Structured Query Language) ( sql ) In this syntax, you add a comma ( ,) after each ALTER COLUMN clause. Code language: SQL (Structured Query Language) ( sql ) 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 ) Summary: this tutorial shows you step by step how to change the data type of a column by using the ALTER TABLE statement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |