In addition, we create a view based on the books and publishers tables as follows: CREATE VIEW book_info The following statements create the three tables: CREATE TABLE publishers (Ĭode language: SQL (Structured Query Language) ( sql ) Each book is assigned to a category and each category can have many books. In this diagram, each book has only one publisher and each publisher can publish many books. We will create three tables: books, categories, and publishers for the demonstration. Let’s look at some examples to see how the ALTER TABLE DROP COLUMN statement works. This is possible in PostgreSQL, but not possible according to SQL standard. ![]() If a table has one column, you can use drop it using the ALTER TABLE DROP COLUMN statement. Notice that you need to add a comma ( ,) after each DROP COLUMN clause. Code language: SQL (Structured Query Language) ( sql ) If you want to drop multiple columns of a table in a single command, you use multiple DROP COLUMN clause like this: ALTER TABLE table_name In this form, if you remove a column that does not exist, PostgreSQL will issue a notice instead of an error. To remove a column only if it exists, you can add the IF EXISTS option as follows: ALTER TABLE table_nameĭROP COLUMN IF EXISTS column_name Code language: SQL (Structured Query Language) ( sql ) ![]() If you remove a column that does not exist, PostgreSQL will issue an error. In this case, you need to add the CASCADE option to the DROP COLUMN clause to drop the column and all of its dependent objects: ALTER TABLE table_nameĭROP COLUMN column_name CASCADE Code language: SQL (Structured Query Language) ( sql ) If the column that you want to remove is used in other database objects such as views, triggers, stored procedures, etc., you cannot drop the column because other objects are depending on it. When you remove a column from a table, PostgreSQL will automatically remove all of the indexes and constraints that involved the dropped column. To drop a column of a table, you use the DROP COLUMN clause in the ALTER TABLE statement as follows: ALTER TABLE table_nameĭROP COLUMN column_name Code language: SQL (Structured Query Language) ( sql ) Introduction to PostgreSQL DROP COLUMN clause Summary: this tutorial shows you how to use the PostgreSQL DROP COLUMN clause in the ALTER TABLE statement to remove one or more columns of a table.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |