5.5. Modifying Tables
5.5.修改表
When you create a table and you realize that you made a mistake, or the requirements of the application
change, you can drop the table and create it again. But this is not a convenient option if the table is
already filled with data, or if the table is referenced by other database objects (for instance a foreign key
constraint). Therefore PostgreSQL provides a family of commands to make modifications to existing
tables. Note that this is conceptually distinct from altering the data contained in the table: here we are
interested in altering the definition, or structure, of the table.
当你创建了一张错误的表,或者应用程序的需求改变了,你可以删掉该表,并重建它。但是当表中有数据或者该表被其他表引用(例如,外键索引)的时候,这样操作并不太方便。因此,PostgreSQL提供了一系列命令以修改表。请注意,从概念上讲,这与改变表中的数据不同:此处,我们的关注点在于修改表的定义或结构。
You can:
• Add columns
• Remove columns
• Add constraints
• Remove constraints
• Change default values
• Change column data types
• Rename columns
• Rename tables
All these actions are performed using the ALTER TABLE command, whose reference page contains
details beyond those given here.
你可以:
-
添加列
-
删除列
-
添加约束
-
删除约束
-
改变默认值
-
改变列的数据类型
-
重命名列
-
重命名表
以上所有的操作均通过
ALTER TABLE命令实现,此命令的参考页提供了比本章更详细的相关信息。