先创建一个student表,后面例子以student表为样例:
CREATE TABLE students(
student_id integer UNIQUE NOT NULL, --学号,主键
hobby text, --爱好
age integer, --年龄
height integer --身高
);
ALTER TABLE 命令用于添加,修改,删除一张已经存在表的列
语法
用 ALTER TABLE 在一张已存在的表上添加列的语法如下:
ALTER TABLE table_name ADD column_name datatype;
--example, 给student表增加性别一列
ALTER TABLE student add gender character varying(10);
在一张已存在的表上 DROP COLUMN(删除列),语法如下:
ALTER TABLE table_name DROP COLUMN column_name;
--删除身高
alter table students drop column height;
修改表中某列的 DATA TYPE(数据类型),语法如下:
ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;
--将student表的身高integer类型改为numeric类型
alter table students
alter column height type numeric(2,2);
参考链接:/postgresql/