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

openGauss每日一练第16天

原创 Scott_jin 2022-12-09
207

openGauss的逻辑架构 表管理

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

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

omm=# \d testtab01
omm=#  id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20

--表上添加字段                                             ^
omm=# alter table testtab01 add column sex boolean;
ALTER TABLE
omm=# \d testtab01
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20
 sex    | boolean               | 


2.删除表中的已有字段

omm=# \d testtab01
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20
 sex    | boolean               | 

omm=# alter table testtab01 drop column sex;
ALTER TABLE
omm=# \d testtab01
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20

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

omm=# \d+ testtab01
                              Table "public.testtab01"
 Column |         Type          | Modifiers  | Storage  | Stats target | Description 
--------+-----------------------+------------+----------+--------------+-------------
 id     | bigint                | not null   | plain    |              | 
 name   | character varying(50) | not null   | extended |              | 
 age    | integer               | default 20 | plain    |              | 
Indexes:
    "testtab01_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no

--删除表上的约束主键
omm=# alter table testtab01 drop constraint testtab01_pkey;
ALTER TABLE
omm=# \d+ testtab01
                              Table "public.testtab01"
 Column |         Type          | Modifiers  | Storage  | Stats target | Description 
--------+-----------------------+------------+----------+--------------+-------------
 id     | bigint                | not null   | plain    |              | 
 name   | character varying(50) | not null   | extended |              | 
 age    | integer               | default 20 | plain    |              | 
Has OIDs: no
Options: orientation=row, compression=no

--添加约束主键
omm=# alter table testtab01 add constraint testtab01_pkey primary key (id);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "testtab01_pkey" for table "testtab01"
ALTER TABLE

4.修改表字段的默认值

omm=# alter table testtab01 alter column age set default 25;
ALTER TABLE

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

omm=# alter table testtab01 alter column age type bigint;
ALTER TABLE

6.修改表字段的名字

omm=# alter table testtab01 rename column age to stuage;
ALTER TABLE

7.修改表的名字

omm=# alter table testtab01 rename to mytest ;
ALTER TABLE

8.删除表

omm=# drop table mytest ; 
DROP TABLE

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

评论