We have changed tablespace from default to my_test. The below example shows that change in the tablespace of the stud2 table. Alter table stud2 SET SCHEMA my_schema Įxample #5 – Alter the table to change the tablespace of a table We have changed the schema public to my_schema. The below example shows that change in the schema of the stud2 table. Alter table student1 DROP COLUMN test_stud Įxample #4 – Alter table to change the schema of the table We have dropped the test_stud column from the student1 table. Alter table student1 ADD test_stud varchar īelow example shows that alter the table to drop the column. We have added the test_stud column to the student1 table. Alter table stud1 RENAME TO student1 Įxample #2 – Alter table to add a new columnīelow example shows that alter the table to add the new column. We have to change the stud1 table name to student1. \d+ stud1 Įxample #1 – Alter table to change the name of the tableīelow example shows that alter a table to change the name of the table. We have using stud1 and stud2 tables to describe examples of the alter table statement.īelow is the table description of stud1 and stud2 table. In the second example, we have altered the table using the username as Postgres, after using this user, we have to drop the column name of stud_id from the student table.Īlter table is used to do the following action on the table are as follows.īelow is an example of the alter table statement in PostgreSQL are as follows. In the above first example, we have used the user as db_test, this user doesn’t have the privileges of table owner or superuser, so it will issue an error while executing the alter table statement. The below example shows that we need to have privileges of the owner of the table or superuser privileges. The ALTER TABLE command in PostgreSQL modifies the structure of a table. How to ALTER TABLE statement work in PostgreSQL?īelow is the working of the alter table statement in PostgreSQL. Drop constraint – The DROP CONSTRAINT command is used in conjunction with the ALTER TABLE statement to remove a constraint from a table.Set schema – The SET SCHEMA command, used with the ALTER TABLE statement, sets a new schema for a table.Set tablespace – We use the SET TABLESPACE command in the ALTER TABLE statement to assign a new tablespace to a table.Modify – We use the MODIFY keyword to modify a column and add a NOT NULL constraint in PostgreSQL.The data type of the column –This is defined as the data type which was we have allocated to the column.Column name – The column name is the name of the column that we have changed using the ALTER TABLE statement.Add column –We can add a column of the table by using add column keyword with alter table statement. The new name of the table –This is the newly created name of the table which was we have modified with alter table statement.Rename to – We use the RENAME TO keyword to change the name of a table. We can use the rename keyword to change the table name.Name of the table –This is defined as the table name we used to change the table structure.Alter table –This statement is used to change the table structure in PostgreSQL.Alter table to change the tablespace Alter table name_of_table set tablespace name_of_tablespace Parametersīelow is the parameter description syntax of the alter table statement in PostgreSQL. Alter table to drop constraint Alter table name_of_table drop constraint name_of_constraint ĩ. Alter column to add a not-null constraint Alter table name_of_table modify name_of_column data_type not null Ĩ. Alter table to rename the column Alter table name_of_table rename column old_column_name to new_column_name ħ. Alter table to set a new schema Alter table name_of_table SET SCHEMA new_schema_name Ħ. Alter table to change the data type of the column Alter table name_of_tableALTER COLUMN name_of_column type data_type_of_column ĥ. Alter table to drop column Alter table name_of_table DROP name_of_column Ĥ. Alter table to add a column Alter table name_of_table ADD name_of_column data_type_of_column ģ. Alter table name_of_table RENAME TO new_name_of_table Ģ.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |