SoFunction
Updated on 2025-04-08

Summary of common commands for modifying table fields in PostgreSQL database

1. Modify the table field data type

When you need to modify the table field data type, you can use the following command:

ALTER TABLE table_name ALTER COLUMN column_name SET DATA TYPE new_data_type;

in,table_nameFor the table name to be modified,column_nameFor the field name to be modified,new_data_typeThe newly set data type.

For example, liststudentsFields inageModify from integer type to floating point type:

ALTER TABLE students ALTER COLUMN age SET DATA TYPE FLOAT;

2. Modify the table field name

When you need to modify the table field name, you can use the following command:

ALTER TABLE table_name RENAME COLUMN old_name TO new_name;

in,table_nameFor the table name to be modified,old_nameis the original field name,new_nameis the new field name.

For example, liststudentsFields inyearModify toenrollment_year

ALTER TABLE students RENAME COLUMN year TO enrollment_year;

3. Delete the table field

When you need to delete a table field, you can use the following command:

ALTER TABLE table_name DROP COLUMN column_name;

in,table_nameFor the table name to be modified,column_nameFor the field name to be deleted.

For example, liststudentsFields inaddressdelete:

ALTER TABLE students DROP COLUMN address;

The above is a complete guide to modifying commonly used commands in PostgreSQL table fields. I hope it will be helpful to you.

This is the article about the summary of commonly used commands for modifying table fields in PostgreSQL database. For more related contents of PostgreSQL table fields, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!