暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第16天 | openGauss逻辑结构:表管理4

原创 不会游的鱼 2022-12-09
1090

一、学习目标

学习openGuass数据库中如何对表进行修改

二、课程学习

在gsql中学习为表添加字段,删除表中的已有字段,删除表的已有约束,为表添加约束,修改表字段的默认值,修改表字段的数据类型,修改表字段的名字,修改表的名字,删除表等操作。

三、课后作业

首先还是连接数据库,准备环境:

root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

1.创建表,为表添加字段

create table test(
      id bigint,
      name varchar(50) not null,
      age  int default 20,
      primary key(id)
     );
为表test新增一列,列名为sex,数据类型为Boolean:
omm=# alter table test add column sex Boolean;
ALTER TABLE
执行下面gsql命令,查看表test的信息
\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

16_01.PNG

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

16_02.PNG

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
--或直接查看约束是否被删除
omm=# select * from pg_constraint  where conname like 'test_pkey';
 conname | connamespace | contype | condeferrable | condeferred | convalidated 
| conrelid | contypid | conindid | confrelid | confupdtype | confdeltype | conf
matchtype | conislocal | coninhcount | connoinherit | consoft | conopt | conkey
 | confkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | 
conincluding 
---------+--------------+---------+---------------+-------------+--------------
(0 rows)

16_03.PNG

4.修改表字段的默认值

执行下面的SQL语句,将age的默认值变更为25
omm=# alter table test alter column age set default 25;
ALTER TABLE
omm=# \d test
omm=#              Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 25

16_04.PNG

5.修改表字段的数据类型

omm=# alter table test ALTER COLUMN age TYPE bigint;
omm=# ALTER TABLE
\d test
 age    | bigint                | default 25

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

16_05.PNG

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

16_06.PNG

7.修改表的名字

修改表的名字。执行下面的SQL语句,将表test的名字变更为mytest:
ALTER TABLE test RENAME TO mytest;
\d mytest
            Table "public.mytest"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 stuage | bigint                | default 25

16_07.PNG

8.删除表

omm=# DROP TABLE mytest;
DROP TABLE
omm=# \d mytest
Did not find any relation named "mytest".

16_08.PNG

四、心得体会

通过今天学习在gsql中学习了表添加字段,删除表中的已有字段,删除表的已有约束,为表添加约束,修改表字段的默认值,修改表字段的数据类型,修改表字段的名字,修改表的名字,删除表等操作,通过实验进一步熟悉了表的相关操作。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论