添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Use ALTER TABLE ALTER COLUMN statement to change data type of column. Here after ALTER TABLE, specify name of table to which you want to modify column, then after ALTER COLUMN, specify name of column to which you want to change data type. After TYPE keyword, specify the new data type of column.
Syntax:
ALTER TABLE [schema_name.]table_name 
ALTER COLUMN <column_name> [SET DATA] TYPE <new_data_type>,
ALTER COLUMN <column_name> [SET DATA] TYPE <new_data_type>
        PostgreSQL allows changing data types of multiple columns by using one ALTER TABLE statement with multiple ALTER COLUMN clauses. 
        It requires to add comma , after each ALTER COLUMN clause.
        Consider that you already have the following employee table.
        PostgreSQL does implicit auto-casting of data from old data type to new data type when you do ALTER COLUM. In some cases, like converting VARCHAR to INT, casting fails and PostgreSQL will give error that column cannot be casted automatically and will ask you to specify manual casting by specifying the USING clause. 
        
Syntax:
ALTER TABLE <table_name>
ALTER COLUMN <column_name> TYPE <new_data_type> USING <expression>;
     

Alter Column Type using pgAdmin

You can change name, type, and NOT NULL constraint for one or more columns in pgAdmin by right clicking on the table name and select 'Properties'. In the popup, go to 'Columns' tab wherein you can edit the columns name, type, length, and set/unset NOT NULL constraint by clicking on the edit icon against the column, as shown below.

Alternatively, you can also change the type of a column in pgAdmin by right clicking on the column and select 'Properties' in the context menu. This will open a popup where you can edit the column properties.

Go to Definition tab and edit the type by selecting appropriate type from the Data type dropdown, as shown below.

We are a team of passionate developers, educators, and technology enthusiasts who, with their combined expertise and experience, create in -depth, comprehensive, and easy to understand tutorials.We focus on a blend of theoretical explanations and practical examples to encourages hands - on learning. Visit About Us page for more information.