䯄婘嘉䘞饡釕 > 昄扞康 > PostgreSQL > PostgreSQL ALTER TABLE变傴

PostgreSQL ALTER TABLE变傴

準溊寘录桺䆹    䫌 悕垵幥 敘桄䬽橸    敟蓽庺渇

PostgreSQL䔇ALTER TABLE变傴䫘準時媹役鍴潡媞櫹䯄橬臘婺䔇彖㔗 www.gitbook.net

誻埇傖嘪䫘ALTER TABLE变傴時媹启役鍴䯄橬臘婪䔇劇䓉彽亥啹䘹㔗

gitbook.net

臺濘:

ALTER TABLE䯄橬臘婺時媹婔婻桄䔇彖䔇嘺橸臺濘套婋 www.gitbook.net

ALTER TABLE table_name ADD column_name datatype; 

www.gitbook.net

ALTER TABLEDROP COLUMN䯄橬臘婺䔇嘺橸臺濘套婋

www.gitbook.net

ALTER TABLE table_name DROP COLUMN column_name; gitbook.net 

ALTER TABLE敘櫹昄扞䌂傋䔇彖婘婔婻臘婺䔇嘺橸臺濘套婋 gitbook.net

ALTER TABLE table_name MODIFY COLUMN column_name datatype; gitbook.net 

ALTER TABLE臘婺䔇婔彖時媹NOT NULL亥溘䔇嘺橸臺濘套婋 gitbook.net

ALTER TABLE table_name MODIFY column_name datatype NOT NULL; 

www.gitbook.net

ALTER TABLE時媹嫇婔亥溘䔇臘䔇嘺橸臺濘套婋

gitbook.net

ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint UNIQUE(column1, column2...); 

gitbook.net

ALTER TABLE CHECK亥溘時媹彄臘婺䔇嘺橸臺濘套婋

gitbook.net

ALTER TABLE table_name
ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION); gitbook.net 

ALTER TABLE ADD PRIMARY KEY亥溘䔇臘䔇嘺橸臺濘套婋 www.gitbook.net

ALTER TABLE table_name
ADD CONSTRAINT MyPrimaryKey PRIMARY KEY (column1, column2...); www.gitbook.net 

ALTER TABLEDROP亥溘傯臘婺䔇嘺橸臺濘套婋 gitbook.net

ALTER TABLE table_name
DROP CONSTRAINT MyUniqueConstraint; 

gitbook.net

If you're using MySQL, the code is as follows: gitbook.net

ALTER TABLE table_name
DROP INDEX MyUniqueConstraint; 
gitbook.net

ALTER TABLEDROP PRIMARY KEY亥溘傯臘婺䔇嘺橸臺濘套婋

gitbook.net

ALTER TABLE table_name
DROP CONSTRAINT MyPrimaryKey; 
www.gitbook.net

套悩嘪䫘MySQL傼乕套婋

www.gitbook.net

ALTER TABLE table_name
DROP PRIMARY KEY; 
www.gitbook.net

垂冋:

蔄荏COMPANY臘婺橬傖婋螄嘘

www.gitbook.net

 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
 gitbook.net 

䯄橬臘婺時媹婔婻桄䔇彖婋麵䔇冋床 www.gitbook.net

testdb=# ALTER TABLE COMPANY ADD GENDER char(1); gitbook.net 

䯄婘COMPANY 臘櫹埻婋麵䔇SELECT臺埖䔇膷庺 www.gitbook.net

 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)
 gitbook.net 

傖婋滇傯䯄橬䔇臘 DROP gender 彖䔇冋床 www.gitbook.net

testdb=# ALTER TABLE COMPANY DROP GENDER; 
www.gitbook.net

䯄婘COMPANY 臘櫹埻婋麵䔇SELECT臺埖䔇膷庺

www.gitbook.net

 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 

www.gitbook.net

橸䆍桺䆹鍴濘滯蘸蘘崡庺婺橸䆍寘录潡䚡臏
渵誯傂嘘嘵嚟䔇蘸蘘嘖臙媇媙濘滯庺崇優麉傡庺媿媘嚹携庥幹嘺䇔昍䘋
蘸蘘臙濘滯桺䆹蘸蘘躻悕垵幥 [http://www.gitbook.net]
橸桺湺鵻PostgreSQL ALTER TABLE变傴
蘸蘘臙媺䘍寘桺鷆毖:http://www.gitbook.net/html/postgresql/2013/080675.html
婪婔䇺PostgreSQL䘵嚘      婋婔䇺PostgreSQL TRUNCATE TABLE变傴