JIYIK CN >

Current Location:Home > Learning > DATABASE > PostgreSQL >

Single query to rename and change column type in PostgreSQL

Author:JIYIK Last Updated:2025/04/11 Views:

This article describes how to rename a column and change its type in PostgreSQL using only a single query.

Renaming and changing column types in MySQL

In MySQL, if you want to change the column type and rename it, you can use a simple statement as shown below.

alter table TAB change id identity varchar(50);

The preceding statement includes the table TABand its columns IDas INT.

create table TAB(
    id int not null,
    name varchar(50) not null
);

How does this work? SQLCompared to other queries, the SELECT statement CHANGEis MySQLan extension provided only by ; it uses ALTERthe SELECT statement and follows this convention.

ALTER TABLE tbl_name [alter_option [, alter_option] ...] [partition_options]

For that alter_option, you can use:

CHANGE [COLUMN] old_col_name new_col_name column_definition
        [FIRST | AFTER col_name]

This syntax helps you use the INSERT statement efficiently CHANGE. You first enter tablethe name and then the INSERT for the column you want to change CHANGE.

There is one column_definitionwhere you can put the updated data type for this column.

Now that we have looked at this MySQLextension in detail, let's see how we can do the same thing in PostgreSQL.

ALTERSimple table query to change data type and column name in PostgreSQL

There is no single ALTERstatement in PostgreSQL that does such an operation.

ALTER TABLE cat
	ALTER COLUMN id TYPE varchar(50)
	RENAME id TO identity;

The above is wrong and will return an error. The best approach is to use multiple ALTERstatements.

ALTER TABLE cat
	ALTER COLUMN id TYPE varchar(50);
ALTER TABLE cat
	RENAME id TO identity;

Why? Because PostgreSQL has no ALTERnotation for specifying multiple operations in a single statement.

PostgreSQL documentation

You can see that each ALTERstatement supports only one operation at a time. Therefore, if you ALTERcall it for a column after a statement ACTION, you can only use ADD, , ALTERor DROPcolumns.

You must call ALTERthe statement again to RENAMEdelete the columns.

Create a user-specific function in PostgreSQL to execute two queries simultaneously

However, if you want to compile both operations at the same time, you can create a function that tends to do this.

create or replace function alter_change_extension(new_type varchar(50), new_name varchar(50))
	returns void
	language plpgsql
	as
$$
begin
if new_type = 'varchar(50)' then
	ALTER TABLE cat ALTER COLUMN id TYPE varchar(50);
end if;
if new_name = 'identity' then
	ALTER TABLE cat RENAME id TO identity;
end if;
end;
$$;

Here, you declare two variables for TYPEand for the columns you want to change NEW NAME. You make IFstatements to check if the values ​​exist, and then if they are true, you go ahead and change the correct ALTERcolumn.

You can use similar IFstatements for specific case scenarios and make your functions dynamic.

This approach is not very efficient because you might be passing a unique string each time, and adding a lot of IFstatements will make things worse. However, you only need one query to do this.

select alter_change_extension('varchar(50)', 'identity');

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Terminate the PostgreSQL connection

Publish Date:2025/04/11 Views:199 Category:PostgreSQL

In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep

Joining columns using Select in PostgreSQL

Publish Date:2025/04/11 Views:176 Category:PostgreSQL

MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi

Using CASE in PostgreSQL

Publish Date:2025/04/11 Views:124 Category:PostgreSQL

This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,

Using NOT IN with subqueries in PostgreSQL

Publish Date:2025/04/11 Views:93 Category:PostgreSQL

NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr

Using variables in PostgreSQL

Publish Date:2025/04/11 Views:171 Category:PostgreSQL

This article will demonstrate how we can declare and assign values ​​to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y

Connect to PostgreSQL using a password

Publish Date:2025/04/11 Views:171 Category:PostgreSQL

This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm

Deleting a database in PostgreSQL via PSQL

Publish Date:2025/04/11 Views:166 Category:PostgreSQL

There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t

Using the database in PostgreSQL

Publish Date:2025/04/11 Views:132 Category:PostgreSQL

This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab

PostgreSQL replace string

Publish Date:2025/04/11 Views:91 Category:PostgreSQL

This article discusses how to use PostgreSQL replace() functions to replace strings. Use the PostgreSQL replace() function to replace strings PostgreSQL replace() functions have the following arguments, which are all text types: replace (st

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial