The PostgreSQL ALTER TABLE order is utilized to add, erase or adjust sections in a current table.
You would likewise utilize ALTER TABLE order to add and drop different imperatives on a current table.
Syntax
The fundamental grammar of ALTER TABLE to add another segment in a current table is as per the following −
ALTER TABLE table_name ADD column_name datatype;
The fundamental grammar of ALTER TABLE to DROP COLUMN in a current table is as per the following −
ALTER TABLE table_name DROP COLUMN column_name;
The essential sentence structure of ALTER TABLE to change the DATA TYPE of a section in a table is as per the following −
ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;
The essential linguistic structure of ALTER TABLE to add a NOT NULL requirement to a section in a table is as per the following −
ALTER TABLE table_name MODIFY column_name datatype NOT NULL;
The essential language structure of ALTER TABLE to ADD UNIQUE CONSTRAINT to a table is as per the following −
ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...);
The essential language structure of ALTER TABLE to ADD CHECK CONSTRAINT to a table is as per the following −
ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION);
The essential punctuation of ALTER TABLE to ADD PRIMARY KEY requirement to a table is as per the following −
ALTER TABLE table_name
ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...);
The essential language structure of ALTER TABLE to DROP CONSTRAINT from a table is as per the following −
ALTER TABLE table_name
DROP CONSTRAINT MyUniqueConstraint;
In the event that you are utilizing MySQL, the code is as per the following −
ALTER TABLE table_name
DROP INDEX MyUniqueConstraint;
The essential punctuation of ALTER TABLE to DROP PRIMARY KEY requirement from a table is as per the following −
ALTER TABLE table_name
DROP CONSTRAINT MyPrimaryKey;
In the event that you are utilizing MySQL, the code is as per the following −
ALTER TABLE table_name
DROP PRIMARY KEY;
Example
Consider our COMPANY table has the accompanying records −
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
Coming up next is the guide to ADD another section in a current table −
testdb=# ALTER TABLE COMPANY ADD GENDER char(1);
Presently, COMPANY table is changed and the accompanying would be the yield from SELECT proclamation −
id | name | age | address | salary | gender
----+-------+-----+-------------+--------+--------
1 | Paul | 32 | California | 20000 |
2 | Allen | 25 | Texas | 15000 |
3 | Teddy | 23 | Norway | 20000 |
4 | Mark | 25 | Rich-Mond | 65000 |
5 | David | 27 | Texas | 85000 |
6 | Kim | 22 | South-Hall | 45000 |
7 | James | 24 | Houston | 10000 |
(7 rows)
Coming up next is the guide to DROP sex section from existing table −
testdb=# ALTER TABLE COMPANY DROP GENDER;
Presently, COMPANY table is changed and the accompanying would be the yield from SELECT articulation −
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000