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

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

原创 田灬禾 2022-12-10
392

打卡第十六天,表修改操作

https://docs.opengauss.org/zh/docs/3.1.0/docs/Developerguide/ALTER-TABLE.html

新增表字段:

alter table test add column sex Boolean;

删除表字段

alter table test drop column sex ;

删除表约束

alter table test drop constraint test_pkey;

表添加约束

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

修改表字段的默认值(无法用modify修改)

alter table test alter column age set default 25;

修改表字段的数据类型(也可以用modify修改)

alter table test ALTER COLUMN age TYPE bigint;

修改表字段名

ALTER TABLE test RENAME COLUMN age TO stuage; 

修改表名

ALTER TABLE test RENAME TO mytest;


课程作业

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

NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "tt_pkey" for table "tt"
CREATE TABLE
omm=# alter table tt add column col varchar(20);
ALTER TABLE
omm=# \d tt
             Table "public.tt"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | not null
 col    | character varying(20) | 
Indexes:
    "tt_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

omm=# 


2.删除表中的已有字段

omm=# alter table tt drop column col;
ALTER TABLE
omm=# 


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

omm=# alter table tt drop constraint tt_pkey;
ALTER TABLE
omm=# select * from pg_constraint where conname='tt_pkey';
---------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+--------
---+-------------+-------------+---------------+------------+-------------+--------------+---------+--------+--------+---
------+-----------+-----------+-----------+-----------+--------+--------+--------------
(0 rows)

 conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conindid | confrel
id | confupdtype | confdeltype | confmatchtype | conislocal | coninhcount | connoinherit | consoft | conopt | conkey | co
nfkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | conincluding 
omm=# alter table tt add constraint tt_pkey primary key(id);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "tt_pkey" for table "tt"
ALTER TABLE
omm=# select * from pg_constraint where conname='tt_pkey';
 conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conindid | confrel
id | confupdtype | confdeltype | confmatchtype | conislocal | coninhcount | connoinherit | consoft | conopt | conkey | co
nfkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | conincluding 
---------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+--------
---+-------------+-------------+---------------+------------+-------------+--------------+---------+--------+--------+---
------+-----------+-----------+-----------+-----------+--------+--------+--------------
 tt_pkey |         2200 | p       | f             | f           | t            |    16389 |        0 |    16394 |        
 0 |             |             |               | t          |           0 | t            | f       | f      | {1}    |   
      |           |           |           |           |        |        | 
(1 row)

omm=# 


4.修改表字段的默认值

omm=# \d tt
                        Table "public.tt"
 Column |         Type          |            Modifiers            
--------+-----------------------+---------------------------------
 id     | integer               | not null
 col    | character varying(20) | default 'xx'::character varying
Indexes:
    "tt_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

omm=# alter table tt alter COLUMN col set default 'AA';
ALTER TABLE
omm=# \d tt 
                        Table "public.tt"
 Column |         Type          |            Modifiers            
--------+-----------------------+---------------------------------
 id     | integer               | not null
 col    | character varying(20) | default 'AA'::character varying
Indexes:
    "tt_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

omm=# 


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

omm=# alter table tt modify id bigint;
ALTER TABLE
omm=# \d tt
                        Table "public.tt"
 Column |         Type          |            Modifiers            
--------+-----------------------+---------------------------------
 id     | bigint                | not null
 col    | character varying(20) | default 'AA'::character varying
Indexes:
    "tt_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

omm=# 


6.修改表字段的名字

omm=# alter table tt rename col to name;
ALTER TABLE
omm=# \d tt
                        Table "public.tt"
 Column |         Type          |            Modifiers            
--------+-----------------------+---------------------------------
 id     | bigint                | not null
 name   | character varying(20) | default 'AA'::character varying
Indexes:
    "tt_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default


7.修改表的名字

omm=# alter table tt rename to t;
ALTER TABLE


8.删除表

omm=# drop table t;
DROP TABLE





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

评论