PostgreSQL ALTER tableIn this section, we are going to learn the various commands of PostgreSQL ALTER TABLE for changing the structure of a table. PostgreSQL ALTER TABLE commandWe use PostgreSQL alter table command to change the current table structure. The syntax of the alter table is given below: The below table will show the following ALTER TABLE commands modifications:
In PostgreSQL, we can perform various activities in the alter table command, which is as follows:
Examples of PostgreSQL ALTER TABLEFor our better understanding of the ALTER TABLE command, we will create a new table named Station with the help of Create table command: Output After executing the above command, we will get the below message window: Once we have done creating a Station table, we will perform the following actions on that particular table: Add a columnAfter that we will add a new column named Latitude with the help of below command: Output Once we execute the above command, we will get the below message window: Drop a columnTo delete the Latitude column from the Station table, we will use the below command: Output We will get the below message after executing the above statement: Rename a columnTo rename the St_Name column to Name, we use the below command: Output Once we implement the above command, we will get the below message: We use the SELECT command to check all the above alter operation: Output We will get the below output, after running the above command: Rename a tableTo rename the Station table to Station1, we will use the below command: Output Once we perform the above command, we will get the below message: Change column typeThe below command is used to modify the type of St_City and St_State columns from Char to Varchar in Station1 table: Output After executing the above command, we will get the below message: Add NOT NULL constraintIf we want to add the NOT NULL constraint to St_City column in Station1 table, we will use the below command: Output Once we execute the above command, we will get the below message: Remove NOT NULL constraintIf we want to delete the NOT NULL constraint from the St_City column in the Station1 table, we will run the below command: Output After executing the above command, we will get the below message: Add PRIMARY KEY constraint9If we want to add a primary key constraint in the Station1 table, we will use the below command. Output In the below message window, only one primary key is allowed for a table. Therefore, we will assume that there is no pre-set primary key in the table. SUMMARY
Next TopicPostgreSQL ADD Columns
|