学习目标
学习openGuass数据库中如何对表进行修改
课程学习
1.测试准备
--首先创建一张测试表。 drop table if exists test; create table test( id bigint, name varchar(50) not null, age int default 20, primary key(id) );
复制
2.为表添加字段
--查看表test的信息 \d test --为表test新增一列,列名为sex,数据类型为Boolean: alter table test add column sex Boolean; --执行下面gsql命令,查看表test的信息 \d test
复制
3.删除表中的已有字段
--执行下面的SQL语句,删除刚刚添加的列sex: alter table test drop column sex ; --执行下面gsql命令,再次查看表test的信息 \d test
复制
### 4.删除表的已有约束
复制
–表test上有一个名叫test_pkey的PRIMARY KEY约束。执行下面的SQL语句,删除这个约束:
alter table test drop constraint test_pkey;
–执行下面的gsql命令,再次查看表test的信息:
\d test
--或直接查看约束是否被删除 select * from pg_constraint where conname like 'test_pkey';
复制
5.为表添加约束
--执行下面的SQL命令,为表test添加刚刚删除的主键约束: alter table test add constraint test_pkey primary key(id); --再次查看表test的信息: select * from pg_constraint where conname like 'test_pkey'; \d test
复制
6.修改表字段的默认值
--执行下面的SQL语句,将age的默认值变更为25 alter table test alter column age set default 25; \d test
复制
7.修改表字段的数据类型
alter table test ALTER COLUMN age TYPE bigint; \d test
复制
8.修改表字段的名字
ALTER TABLE test RENAME COLUMN age TO stuage; \d test
复制
9.修改表的名字
--修改表的名字。执行下面的SQL语句,将表test的名字变更为mytest: ALTER TABLE test RENAME TO mytest; \d mytest
复制
10.删除表
DROP TABLE mytest;
复制
课程作业
1.创建表,为表添加字段
su -omm gsql -r drop table if exists test1; create table test1( id bigint, name varchar(50) not null, age int default 20, primary key(id) ); \d test1 alter table test1 add column sex Boolean; \d test1
复制
omm=# drop table if exists test1;
NOTICE: table “test1” does not exist, skipping
DROP TABLE
omm=# create table test1(
omm(# id bigint,
omm(# name varchar(50) not null,
omm(# age int default 20,
omm(# primary key(id)
omm(# );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index “test1_pkey” for table “test1”
CREATE TABLE
omm=# \d test1
Table “public.test1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
“test1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# alter table test1 add column sex Boolean;
ALTER TABLE
omm=# \d test1
Table “public.test1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
sex | boolean |
Indexes:
“test1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
2.删除表中的已有字段
alter table test1 drop column sex; \d test1
复制
omm=# alter table test1 drop column sex;
ALTER TABLE
omm=# \d test1
Table “public.test1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
“test1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
3.删除表的已有约束、添加约束
alter table test1 drop constraint test1_pkey; \d test1
复制
omm=# alter table test1 drop constraint test1_pkey;
ALTER TABLE
omm=# \d test1
Table “public.test1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
4.修改表字段的默认值
select * from pg_constraint where conname like ‘test1_pkey’;
\d test1
omm=# select * from pg_constraint where conname like ‘test1_pkey’;
conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conindid | confrelid
| confupdtype | confdeltype | confmatchtype | conislocal | coninhcount | connoinherit | consoft | conopt | conkey | confkey |
conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | conincluding
------------±-------------±--------±--------------±------------±-------------±---------±---------±---------±---------
-±------------±------------±--------------±-----------±------------±-------------±--------±-------±-------±--------+
-----------±----------±----------±----------±-------±-------±-------------
test1_pkey | 2200 | p | f | f | t | 16403 | 0 | 16409 | 0
| | | | t | 0 | t | f | f | {1} | |
| | | | | |
(1 row)
omm=#
omm=# \d test1
Table “public.test1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
5.修改表字段的数据类型
alter table test1 alter COLUMN age type bigint; \d test1
复制
omm=# alter table test1 alter COLUMN age type bigint;
ALTER TABLE
omm=#
omm=# \d test1
Table “public.test1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | bigint | default 20
Indexes:
“test1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
6.修改表字段的名字
alter table test1 rename COLUMN age to stuage; \d test1
复制
omm=# alter table test1 rename COLUMN age to stuage;
ALTER TABLE
omm=# \d test1
Table “public.test1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
stuage | bigint | default 20
Indexes:
“test1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
7.修改表的名字
alter table test1 rename TO mytest; \d mytest
复制
omm=# alter table test1 rename TO mytest;
ALTER TABLE
omm=# \d mytest
Table “public.mytest”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
stuage | bigint | default 20
Indexes:
“test1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
8.删除表
drop table mytest ;
复制
omm=# drop table mytest ;
DROP TABLE
评论

