学习目标
学习openGuass数据库中如何对表进行修改
课程学习
1.测试准备
2.为表添加字段
3.删除表中的已有字段
4.删除表的已有约束
5.为表添加约束
6.修改表字段的默认值
7.修改表字段的数据类型
8.修改表字段的名字
9.修改表的名字
10.删除表
课程作业
1.创建表,为表添加字段
/*
--创建一张测试表test16
drop table if exists test16;
create table test16(
id bigint,
name varchar(50) not null,
age int default 20,
primary key(id)
);
--查看表test的信息
\d test16
--为表test16新增一列,列名为sex,数据类型为Boolean:
alter table test16 add column sex Boolean;
--执行下面gsql命令,查看表test16的信息
\d test16
*/
omm=# --创建一张测试表test16
omm=# drop table if exists test16;
DROP TABLE
omm=# create table test16(
omm(# omm(# id bigint,
omm(# name varchar(50) not null,
omm(# age int default 20,
primary key(id)
omm(# );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test16_pkey" for table "test16"
CREATE TABLE
omm=#
omm=# --查看表test的信息
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
omm=# --为表test16新增一列,列名为sex,数据类型为Boolean:
omm=# alter table test16 add column sex Boolean;
ALTER TABLE
omm=#
omm=# --查看表test16的信息
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
sex | boolean |
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
2.删除表中的已有字段
/*
--先查看列定义
\d test16
--删除刚刚添加的列sex:
alter table test16 drop column sex ;
--再次查看表test16的信息
\d test16
*/
omm=# --先查看列定义
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
sex | boolean |
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# omm=# --删除刚刚添加的列sex:
alter table test16 drop column sex ;
ALTER TABLE
omm=#
omm=# --再次查看表test16的信息
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
3.删除表的已有约束、添加约束
/*
\d test16
--表test16上有一个名叫test16_pkey的PRIMARY KEY约束。尝试删除这个约束:
alter table test16 drop constraint test16_pkey;
--再查看表test16的信息:
\d test16
--或直接查看约束是否被删除
select * from pg_constraint where conname like 'test16_pkey';
*/
omm=# alter table test16 drop constraint test16_pkey;
ALTER TABLE
omm=#
omm=# --再查看表test16的信息:
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
omm=#
omm=# --或直接查看约束是否被删除
omm=# select * from pg_constraint where conname like 'test16_pkey';
(No rows)
/*
--为表test16重新添加刚删除的主键约束:
alter table test16 add constraint test16_pkey primary key(id);
--再次查看表test16的信息:
select * from pg_constraint where conname like 'test16_pkey';
\d test16
*/
omm=# select * from pg_constraint where conname like 'test16_pkey';
(No rows)
omm=# alter table test16 add constraint test16_pkey primary key(id);
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "test16_pkey" for table "test16"
ALTER TABLE
omm=#
omm=# --再次查看表test16的信息:
omm-# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# select * from pg_constraint where conname like 'test16_pkey';
-[ RECORD 1 ]-+------------
conname | test16_pkey
connamespace | 2200
contype | p
condeferrable | f
condeferred | f
convalidated | t
conrelid | 16467
contypid | 0
conindid | 16473
confrelid | 0
confupdtype |
confdeltype |
confmatchtype |
conislocal | t
coninhcount | 0
connoinherit | t
consoft | f
conopt | f
conkey | {1}
confkey |
conpfeqop |
conppeqop |
conffeqop |
conexclop |
conbin |
consrc |
conincluding |
4.修改表字段的默认值
/*
\d test16
--将表test16中age的默认值变更为29
alter table test16 alter column age set default 25;
\d test16
*/
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# omm=# --将表test16中age的默认值变更为25
alter table test16 alter column age set default 29;
ALTER TABLE
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 29
Indexes:
omm=# "test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
5.修改表字段的数据类型
/*
\d test16
alter table test16 ALTER COLUMN age TYPE bigint;
\d test16
*/
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 29
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# alter table test16 ALTER COLUMN age TYPE bigint;
ALTER TABLE
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | bigint | default 29
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
6.修改表字段的名字
/*
\d test16
ALTER TABLE test16 RENAME COLUMN age TO nianling;
\d test16
*/
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
--------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
age | bigint | default 29
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# ALTER TABLE test16 RENAME COLUMN age TO nianling;
ALTER TABLE
omm=# \d test16
Table "public.test16"
Column | Type | Modifiers
----------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
nianling | bigint | default 29
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
7.修改表的名字
/*
--修改表的名字。将表test16的名字变更为test0:
ALTER TABLE test16 RENAME TO test0;
\d test0
*/
omm=# omm=# ALTER TABLE test16 RENAME TO test0;
ALTER TABLE
omm=# \d test0
Table "public.test0"
Column | Type | Modifiers
----------+-----------------------+------------
id | bigint | not null
name | character varying(50) | not null
nianling | bigint | default 29
Indexes:
"test16_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
8.删除表
/*
DROP TABLE test0;
\d test0
*/
omm=# \d test0
Did not find any relation named "test0".
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




