But in the case of bigger tables, using a TRUNCATE TABLE statement is more efficient. In order to remove all rows from a table, you have to use the DELETE statement. Rename the ‘Books’ table to ‘Notebooks’ using the following statementĪLTER TABLE Books RENAME TO Notebooks How to Truncate tables in PostgreSQL?.Let’s create a table ‘Books’ for demonstration. In order to rename multiple tables at a time, you need to execute multiple statements of ALTER TABLE RENAME TO.In order to avoid this situation, you need to add the IF EXISTS parameter as follows: PostgreSQL throws an error if you try to rename the non-existing table.Specify the new name for the table after the RENAME TO clause. How to Rename a table in PostgreSQL?ĪLTER TABLE table_name RENAME TO new_table_name Įxplanation: Define the table name to be renamed after the ALTER TABLE clause. Note: drop cascades to constraint subject_teacher_id_fkey on table subject. PostgreSQL removes the teacher table as well as the constraint in the subject table.In this case, you need to remove all dependent objects first before removing the teacher table or use the CASCADE parameter as follows:.Because the constraint on the subject table depends on the teacher table, PostgreSQL issues an error message. ![]() Create the following tables to understand the DROP TABLE more:įOREIGN KEY (teacher_id) REFERENCES teacher (teacher_id) In order to avoid this error, use the IF EXISTS parameter.Ģ. PostgreSQL throws an error because the MY_TABLE does not exist. The following statement deletes ‘MY_TABLE’ from the database
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |