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

openGauss每日一练第16天 | 表的修改

原创 olabll1 2022-12-09
872

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

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

drop table if exists tab1;
create table tab1(
      c1 bigint,
      c2 varchar(30) not null,
      c3  int default 20,
      primary key(c1)
     );
alter table tab1 add column c4 varchar(20) not null;
alter table tab1 add column c5 Boolean;
--查看表test的信息
\d test

1.png

2.删除表中的已有字段

omm=# alter table tab1 drop column c4 ;
ALTER TABLE

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

--表tab1上有一个名叫tab1_pkey的PRIMARY KEY约束。执行下面的SQL语句,删除这个约束:
alter table tab1 drop constraint tab1_pkey;

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

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

--执行下面的SQL命令,为表tab1添加刚刚删除的主键约束:
alter table tab1 add constraint tab1_pkey primary key(c1);

--再次查看表test的信息:
select * from pg_constraint  where conname like 'tab1_pkey';

\d tab1

2.png

4.修改表字段的默认值

omm=# alter table tab1 alter column c3 set default 25;
ALTER TABLE
omm=# \d tab1
             Table "public.tab1"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 c1     | bigint                | not null
 c2     | character varying(30) | not null
 c3     | integer               | default 25
 c5     | boolean               | 
Indexes:
    "tab1_pkey" PRIMARY KEY, btree (c1) TABLESPACE pg_default

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

omm=# alter table tab1 ALTER COLUMN c3 TYPE bigint;
ALTER TABLE
omm=# \d tab1
 c2     | character varying(30) | not null
 c3     | bigint                | default 25
 c5     | boolean               | 
Indexes:
    "tab1_pkey" PRIMARY KEY, btree (c1) TABLESPACE pg_default

             Table "public.tab1"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 c1     | bigint                | not null
omm=# alter table tab1 ALTER COLUMN c3 TYPE number(10);
ALTER TABLE
omm=# alter table tab1 ALTER COLUMN c3 TYPE int;   
ALTER TABLE
omm=# alter table tab1 ALTER COLUMN c3 TYPE varchar(20);
ALTER TABLE
omm=# \d tab1
             Table "public.tab1"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 c1     | bigint                | not null
 c2     | character varying(30) | not null
 c3     | character varying(20) | default 25
 c5     | boolean               | 
Indexes:
    "tab1_pkey" PRIMARY KEY, btree (c1) TABLESPACE pg_default

6.修改表字段的名字

omm=# ALTER TABLE tab1 RENAME COLUMN c3 TO c6;
ALTER TABLE
omm=# \d tab1
             Table "public.tab1"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 c1     | bigint                | not null
 c2     | character varying(30) | not null
 c6     | character varying(20) | default 25
 c5     | boolean               | 
Indexes:
    "tab1_pkey" PRIMARY KEY, btree (c1) TABLESPACE pg_default

7.修改表的名字

omm=# ALTER TABLE tab1 RENAME TO tab2;
ALTER TABLE
omm=# \d tab2
             Table "public.tab2"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
 c1     | bigint                | not null
 c2     | character varying(30) | not null
 c6     | character varying(20) | default 25
 c5     | boolean               | 
Indexes:
    "tab1_pkey" PRIMARY KEY, btree (c1) TABLESPACE pg_default

8.删除表

omm=# DROP TABLE tab2;
DROP TABLE

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

评论