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

openGauss每日一练第 16 天 | 表管理4

原创 七七 2022-12-09
846

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

课程作业
1.创建表,为表添加字段

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.

omm=# create table t1(id int);
omm=# CREATE TABLE

omm=# \d t1
      Table "public.t1"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | 

omm=# alter table t1 add column age int;
ALTER TABLE
omm=# \d t1 
omm=#       Table "public.t1"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | 
 age    | integer | 

2.删除表中的已有字段

omm=# alter table t1 drop column age;
ALTER TABLE
omm=# \d t1
      Table "public.t1"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | 

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

omm=#  create table t2(id int ,name varchar(10) not null,age int default 20,primary key(id));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "t2_pkey" for table "t2"
CREATE TABLE
omm=# \d t2
              Table "public.t2"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | integer               | not null
 name   | character varying(10) | not null
 age    | integer               | default 20
Indexes:
    "t2_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default


omm=# alter table t2 drop constraint t2_pkey;
ALTER TABLE
omm=# \d t2 
 age    | integer               | default 20

omm=#               Table "public.t2"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | integer               | not null
 name   | character varying(10) | not null

omm=# select * from pg_constraint where conname like 't2_pkey';
 conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | 
contypid | conindid | confrelid | confupdtype | confdeltype | confmatchtype | conislocal | c
oninhcount | connoinherit | consoft | conopt | conkey | confkey | conpfeqop | conppeqop | co
nffeqop | conexclop | conbin | consrc | conincluding 
---------+--------------+---------+---------------+-------------+--------------+----------+-
---------+----------+-----------+-------------+-------------+---------------+------------+--
-----------+--------------+---------+--------+--------+---------+-----------+-----------+---
--------+-----------+--------+--------+--------------
(0 rows)

omm=# alter table t2 add constraint t2_pkey primary key(id);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "t2_pkey" for table "t2"
ALTER TABLE
omm=# \d t2
              Table "public.t2"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | integer               | not null
 name   | character varying(10) | not null
 age    | integer               | default 20
Indexes:
    "t2_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

omm=# select * from pg_constraint where conname like 't2_pkey';
 conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | 
contypid | conindid | confrelid | confupdtype | confdeltype | confmatchtype | conislocal | c
oninhcount | connoinherit | consoft | conopt | conkey | confkey | conpfeqop | conppeqop | co
nffeqop | conexclop | conbin | consrc | conincluding 
---------+--------------+---------+---------------+-------------+--------------+----------+-
---------+----------+-----------+-------------+-------------+---------------+------------+--
-----------+--------------+---------+--------+--------+---------+-----------+-----------+---
--------+-----------+--------+--------+--------------
 t2_pkey |         2200 | p       | f             | f           | t            |    16392 | 
       0 |    16398 |         0 |             |             |               | t          |  
         0 | t            | f       | f      | {1}    |         |           |           |   
        |           |        |        | 
(1 row)


4.修改表字段的默认值

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

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

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

6.修改表字段的名字

omm=# alter table t2 rename column age to stuage;
ALTER TABLE
omm=# \d t2 
              Table "public.t2"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | integer               | not null
 name   | character varying(10) | not null
 stuage | bigint                | default 20
Indexes:
    "t2_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

7.修改表的名字

omm=# alter table t2 rename to t3
omm-# ;
ALTER TABLE
omm=# \d t3
 name   | character varying(10) | not null
 stuage | bigint                | default 20
Indexes:
    "t2_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

omm=#               Table "public.t3"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 id     | integer               | not null

8.删除表

omm=# drop table t3;
DROP TABLE

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

评论