openGauss每日一练第16天 | openGauss逻辑结构:表管理4
表在数据库是很重要的,数据以表的形式存储在数据库中。对表的管理,比如创建表,修改表(修改表名称、修改表字段名称、表新增字段、表删除字段、修改表字段数据类型、修改表字段默认值、修改表字段约束等),删除表等。
1、创建表,为表添加字段
su - omm
gsql -r
--创建t1表
omm=# create table t1(id int not null);
CREATE TABLE
--t1表新增ename字段
omm=# alter table t1 add ename varchar(50);
ALTER TABLE
omm=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer | not null
ename | character varying(50) |
omm=#
2、删除表中的已有字段
--删除表t1的ename字段
omm=# alter table t1 drop column ename;
ALTER TABLE
omm=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+---------+-----------
id | integer | not null
--一个表最少有1个字段
omm=# alter table t1 drop column id;
ERROR: must have at least one column
3、删除表的已有约束、添加约束
--表t1添加主键约束
omm=# alter table t1 add constraint pk_t1_id primary key(id);
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "pk_t1_id" for table "t1"
ALTER TABLE
omm=# alter table t1 modify sex not null;
ALTER TABLE
omm=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+---------+-----------
id | integer | not null
age | integer |
sex | boolean | not null
Indexes:
"pk_t1_id" PRIMARY KEY, btree (id) TABLESPACE pg_default
--删除t1表sex的not null约束
omm=# alter table t1 modify sex null;
ALTER TABLE
omm=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+---------+-----------
id | integer | not null
age | integer |
sex | boolean |
Indexes:
"pk_t1_id" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# \d test
omm=# Table "public.test"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
"test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# \x
Expanded display is on.
--通过数据字典查看指定约束test_pkey
omm=# select * from pg_constraint where conname like 'test_pkey';
-[ RECORD 1 ]-+----------
conname | test_pkey
connamespace | 2200
contype | p
condeferrable | f
condeferred | f
convalidated | t
conrelid | 16402
contypid | 0
conindid | 16406
confrelid | 0
confupdtype |
confdeltype |
confmatchtype |
conislocal | t
conopt | f
conkey | {1}
confkey |
conpfeqop |
conppeqop |
--More--coninhcount | 0
connoinherit | t
consoft | f
conffeqop |
conexclop |
conbin |
consrc |
conincluding |
4、修改表字段的默认值
omm=# alter table t1 alter column age set default 20;
ALTER TABLE
omm=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+---------+------------
id | integer | not null
age | integer | default 20
sex | boolean |
Indexes:
"pk_t1_id" PRIMARY KEY, btree (id) TABLESPACE pg_default
--修改表t1中age字段的默认值
omm=# alter table t1 alter column age set default 18;
ALTER TABLE
omm=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+---------+------------
id | integer | not null
age | integer | default 18
sex | boolean |
Indexes:
"pk_t1_id" PRIMARY KEY, btree (id) TABLESPACE pg_default
5、修改表字段的数据类型
--把表t1中id字段数据类型修改程bigint
omm=# alter table t1 modify id bigint;
ALTER TABLE
omm=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+---------+------------
id | bigint | not null
age | integer | default 18
sex | boolean |
Indexes:
"pk_t1_id" PRIMARY KEY, btree (id) TABLESPACE pg_default
6、修改表字段的名字
--修改表t2中sex字段名为xb
omm=# alter table t2 rename sex to xb;
ALTER TABLE
omm=# \d t2
Table "public.t2"
Column | Type | Modifiers
--------+---------+------------
id | bigint | not null
age | integer | default 18
xb | boolean |
Indexes:
"pk_t1_id" PRIMARY KEY, btree (id) TABLESPACE pg_default
7、修改表的名字
--修改表t1名称为t2
omm=# alter table t1 rename to t2;
ALTER TABLE
omm=# \d
List of relations
Schema | Name | Type | Owner | Storage
--------+------+-------+-------+----------------------------------
public | t2 | table | omm | {orientation=row,compression=no}
(1 row)
8、删除表
--删除t2表
omm=# drop table t2;
DROP TABLE
omm=# \d
No relations found.
omm=#
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




