接着昨天继续学习openGauss,今天是第16天了,还有5天课程就结束了,时间过的真快。今天学习内容是巩固表管理学习内容。
老规矩,先登陆墨天轮为我准备的实训实验室

root@modb:~# su - omm
omm@modb:~$ gsql -r
作业要求
1.创建表,为表添加字段

– 首先创建一张表

omm=# create table test(
omm(#       id bigint,
omm(#       name varchar(50) not null,
omm(#       age  int default 20,
omm(#       primary key(id)
omm(#      );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE

–查看表test的信息

omm=# \d test
             Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20
Indexes:
    "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

–为表test新增一列,列名为sex,数据类型为Boolean:

omm=# alter table test add column sex Boolean;
ALTER TABLE

–执行下面gsql命令,查看表test的信息

omm=# \d test
             Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20
 sex    | boolean               | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
2.删除表中的已有字段

–执行下面的SQL语句,删除刚刚添加的列sex:

omm=# alter table test drop column sex ;
ALTER TABLE

–执行下面gsql命令,再次查看表test的信息

omm=# \d test
             Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20
Indexes:
    "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
3.删除表的已有约束、添加约束

–表test上有一个名叫test_pkey的PRIMARY KEY约束。执行下面的SQL语句,删除这个约束:

omm=# alter table test drop constraint test_pkey;
ALTER TABLE

–执行下面的gsql命令,再次查看表test的信息:

omm=# \d test
             Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20

可以发现,test_pkey 约束已经被删掉了
–执行下面的SQL命令,为表test添加刚刚删除的主键约束:

omm=# alter table test add constraint test_pkey primary key(id);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "test_pkey" for table "test"
ALTER TABLE
4.修改表字段的默认值

–执行下面的SQL语句,将age的默认值变更为25

omm=# alter table test alter column age set default 25;
omm=# ALTER TABLE
omm=# \d test
             Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 25
Indexes:
    "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

可以看见 已经改成了25.

5.修改表字段的数据类型
omm=# alter table test ALTER COLUMN age TYPE bigint;
ALTER TABLE
omm=# \d test
             Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | bigint                | default 25
Indexes:
    "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

可以看见 age 字段类型已经改为了bigint。

6.修改表字段的名字
omm=# ALTER TABLE test RENAME COLUMN age TO stuage;
ALTER TABLE
omm=# \d test
             Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 stuage | bigint                | default 25
Indexes:
    "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

可以看见 age 字段名称已经改为了stuage。

7.修改表的名字

–修改表的名字。执行下面的SQL语句,将表test的名字变更为mytest:

omm=# ALTER TABLE test RENAME TO mytest;
ALTER TABLE
omm=# 
omm=# \d mytest
            Table "public.mytest"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 stuage | bigint                | default 25
Indexes:
    "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

#### 8.删除表

omm=# DROP TABLE mytest;
DROP TABLE
总结

今天学习的命令都很基础,多多练习即可。

Logo

鲲鹏昇腾开发者社区是面向全社会开放的“联接全球计算开发者,聚合华为+生态”的社区,内容涵盖鲲鹏、昇腾资源,帮助开发者快速获取所需的知识、经验、软件、工具、算力,支撑开发者易学、好用、成功,成为核心开发者。

更多推荐