

To rename a particular table in PostgreSQL, use the RENAME TO clause in conjunction with the ALTER TABLE statement: ALTER TABLE tab_name
Postgresql rename table how to#
This blog post will teach you how to rename a table in Postgres through practical examples. A Postgres table can be renamed using the ALTER TABLE command. For instance, the CREATE TABLE statement creates a table, the DROP TABLE deletes a table, and the ALTER TABLE updates an existing table. To drop or delete a foreign key contraint we use the DROP CONSTRAINT command.įor example, if we want to drop the foreign key constraint employeeId_comments_fk from the comments table we will run the following command.PostgreSQL provides different commands/statements to perform the different operations on the Postgres tables.
Postgresql rename table update#
Now, our comments table will look like the following.ĬONSTRAINT "commentId_comments_pk" PRIMARY KEY ("commentId"),ĬONSTRAINT "employeeId_comments_fk" FOREIGN KEY ("employeeId") REFERENCES employee("employeeId") ON UPDATE CASCADE ON DELETE CASCADE We have ON DELETE CASCADE ON UPDATE CASCADE which means if the employeeId in the employee table is updated or deleted then it will be reflected to the comments table. Now we will add the foreign key constraint. It will be the foreign key and will refer to the employeeId column of the employee table. Let's add a new column employeeId to the comments table. In the CREATE Table tutorial we created the comments table which looks like the following at the moment.ĬONSTRAINT "commentId_comments_pk" PRIMARY KEY ("commentId") To add a foreign key to a table we use the ADD CONSTRAINT. In the following example we are dropping the idx_firstName_employee index from the firstname column. To drop an index from a column we use the DROP INDEX command. In the following example we are adding an index to the firstname column. We can add index to table columns to speed up search. To drop a unique key constraint we use the DROP CONSTRAINT command.įor example, if we want to drop the UNIQUE constraint from the email column by the symbol email_employee_unique we will run the following command. Now, our table will look like the following.ĬONSTRAINT email_employee_unique UNIQUE (email), In the following example we are adding a new column email to the employee table.Īdd constraint "email_employee_unique" unique("email") To ensure that a column contains distinct values we add the UNIQUE constraint. Now we will drop the temp column from the table. Right now the table looks like the following. I added the temp column using the following command. Let the temp column be of boolean data type. Let's say we have a temp column in the employee table and we want to drop it. To drop a column from a table we use the DROP COLUMN command. Let's rename the dateOfBirth column back to birthday and modify the column type from TIMESTAMP to DATE. Let's rename the birthday column to dateOfBirth. To rename a column in PostgreSQL we use the RENAME COLUMN command. Let's modify the birthday column that we added to the employee table by changing its type from DATE to TIMESTAMP. To modify column in PostgreSQL we use the ALTER COLUMN command. Now our table will look like the following. Let's add a new column birthday of date data type. This is how our employee table looks now.ĬONSTRAINT "employeeId_employee_pk" PRIMARY KEY ("employeeId") In the following tutorial we will work with the employee table that we created in the CREATE Table tutorial. In this tutorial we will cover the following. We use the ALTER TABLE table_name command in PostgreSQL to alter tables.

In this tutorial we will learn to alter table in PostgreSQL.
