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

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

1075

学习目标

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

前面每日一练链接

openGauss 每日一练第 1 天 | openGauss 数据库状态查看
openGauss 每日一练第 2 天 | 学习 gsql 命令行的使用
openGauss 每日一练第 3 天 | openGauss 数据库状态查看
openGauss 每日一练第 4 天 | openGauss 中一个数据库可以被多个用户访问
openGauss 每日一练第 5 天 | openGauss 中一个用户可以访问多个数据库
openGauss 每日一练第 6 天 | openGauss 中用户一次只能连接到一个数据库
openGauss 每日一练第 7 天 | openGauss 中一个数据库中可以创建多个模式
openGauss 每日一练第 8 天 | openGauss 中一个数据库可以存储在多个表空间中
openGauss 每日一练第 9 天 | openGauss 中一个表空间可以存储多个数据库
openGauss 每日一练第 10 天 | openGauss 逻辑结构:表空间管理
openGauss 每日一练第 11 天 | openGauss 逻辑结构:数据库管理
openGauss 每日一练第 12 天 | openGauss 逻辑结构:模式管理
openGauss 每日一练第 13 天 | openGauss 逻辑结构:表管理 1
openGauss 每日一练第 14 天 | openGauss 逻辑结构:表管理 2
openGauss 每日一练第 15 天 | openGauss 逻辑结构:表管理 3

课程学习

#### 1.测试准备

--首先创建一张测试表。
drop table if exists test;
create table test(
      id bigint,
      name varchar(50) not null,
      age  int default 20,
      primary key(id)
     );

2.为表添加字段

--查看表 test 的信息
\d test

--为表 test 新增一列,列名为 sex,数据类型为 Boolean:
alter table test add column sex Boolean;

--执行下面gsql命令,查看表test的信息
\d test

图片.png

3.删除表中的已有字段

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

alter table test drop column sex;

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

4.删除表的已有约束

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

alter table test drop constraint test_pkey;

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

--或直接查看约束是否被删除
select * from pg_constraint  where conname like 'test_pkey';

图片.png

5.为表添加约束

--执行下面的 SQL 命令,为表 test 添加刚刚删除的主键约束:

alter table test add constraint test_pkey primary key(id);

--再次查看表test的信息:

select * from pg_constraint  where conname like 'test_pkey';

\d test

6.修改表字段的默认值

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

alter table test alter column age set default 25;

\d test

图片.png

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

alter table test ALTER COLUMN age TYPE bigint;

\d test

8.修改表字段的名字

ALTER TABLE test RENAME COLUMN age TO stuage;
\d test

图片.png

9.修改表的名字

--修改表的名字。执行下面的 SQL 语句,将表 test 的名字变更为 mytest:
ALTER TABLE test RENAME TO mytest;

\d mytest

10.删除表

DROP TABLE mytest;

图片.png

课程作业

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

create table t(
      id bigint,
      name varchar(50) not null,
      age  int default 30,
      primary key(id)
     );

alter table t add column email varchar(30);

\d t

图片.png

2.删除表中的已有字段

alter table t drop column email;
\d t

3.删除表的已有约束、添加约束

alter table t drop constraint t_pkey;

\dt 

图片.png

4.修改表字段的默认值

omm=# alter table t alter column age set default 40;
ALTER TABLE
omm=# \d t
              Table "public.t"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 40

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

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

omm=# 
omm=# alter table t ALTER COLUMN age TYPE bigint;
ALTER TABLE
omm=# \d t
              Table "public.t"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | bigint                | default 40

6.修改表字段的名字

omm=# ALTER TABLE t RENAME COLUMN age TO nianling;
ALTER TABLE
omm=# \d t
               Table "public.t"
  Column  |         Type          | Modifiers  
----------+-----------------------+------------
 id       | bigint                | not null
 name     | character varying(50) | not null
 nianling | bigint                | default 40

7.修改表的名字

omm=# alter table t rename to mytablet;
ALTER TABLE
omm=# \d mytablet
            Table "public.mytablet"
  Column  |         Type          | Modifiers  
----------+-----------------------+------------
 id       | bigint                | not null
 name     | character varying(50) | not null
 nianling | bigint                | default 40

8.删除表

omm=# drop table mytablet;
DROP TABLE
omm=# \dt 
                               List of relations
 Schema |        Name        | Type  | Owner |             Storage              
--------+--------------------+-------+-------+----------------------------------
 public | invoice            | table | omm   | {orientation=row,compression=no}
 public | newtestwithdata    | table | omm   | {orientation=row,compression=no}
 public | t1                 | table | omm   | {orientation=row,compression=no}
 public | t2                 | table | omm   | {orientation=row,compression=no}
 public | t3                 | table | omm   | {orientation=row,compression=no}
 public | t4                 | table | omm   | {orientation=row,compression=no}
 public | t5                 | table | omm   | {orientation=row,compression=no}
 public | t6                 | table | omm   | {orientation=row,compression=no}
 public | t7                 | table | omm   | {orientation=row,compression=no}
 public | test001            | table | omm   | {orientation=row,compression=no}
 public | test002            | table | omm   | {orientation=row,compression=no}
 public | testnewwithoutdata | table | omm   | {orientation=row,compression=no}
(12 rows)
最后修改时间:2022-12-10 22:13:40
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论