学习目标
学习openGuass数据库中如何对表进行修改
课程作业
提前说明:最近几次作业是表的基础操作,暂不截图,有特殊注意的地方再输出图片。
1.创建表,为表添加字段
create table test(id bigint,name varchar(50) not null,age int default 20,primary key(id));
查看表test的信息
\d test
为表test新增一列,列名为sex,数据类型为Boolean:
alter table test add column sex Boolean;
执行下面gsql命令,查看表test的信息
\d test
2.删除表中的已有字段
删除刚刚添加的列sex
alter table test drop column sex ;
查看表test的信息
\d test
3.删除表的已有约束、添加约束
删除test表约束 test_pkey
alter table test drop constraint test_pkey;
查看表test的信息
\d test
select * from pg_constraint where conname like 'test_pkey';
表test添加刚刚删除的主键约束
alter table test add constraint test_pkey primary key(id);
再次查看表test的信息
select * from pg_constraint where conname like 'test_pkey';
\d test
4.修改表字段的默认值
将age的默认值变更为30
alter table test alter column age set default 30;
\d test
5.修改表字段的数据类型
alter table test alter column age type bigint;
\d test
6.修改表字段的名字
alter table test rename column age to stuage;
\d test
7.修改表的名字
将表test的名字变更为mytest
alter table test rename to test_old;
\d test_old
8.删除表
drop table test_old cascade;




