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

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

原创 SongF 2022-12-09
248

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

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

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

2.删除表中的已有字段

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

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

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

4.修改表字段的默认值

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

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

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

6.修改表字段的名字

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

7.修改表的名字

omm=# alter table customer rename TO customer_new;
ALTER TABLE

omm=# \d
                            List of relations
 Schema |     Name     | Type  | Owner |             Storage              
--------+--------------+-------+-------+----------------------------------
 public | customer_new | table | omm   | {orientation=row,compression=no}
 public | test         | table | omm   | {orientation=row,compression=no}
(2 rows)

8.删除表

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

评论