前言:
最近参加了由opengauss、墨天轮、鲲鹏社区一起推出的活动《每日一练 opengauss 3.0.0 数据库在线实训课程》,共21天,墨天轮提供实操环境,特此记录学习笔记。
活动详情:https://www.modb.pro/db/551619
主题
学习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.为表添加字段
\d test
alter table test add column sex Boolean;
\d test
3.删除表中的已有字段
alter table test drop column sex ;
\d test
4.删除表的已有约束
alter table test drop constraint test_pkey;
\d test
select * from pg_constraint where conname like 'test_pkey';
5.为表添加约束
alter table test add constraint test_pkey primary key(id);
select * from pg_constraint where conname like 'test_pkey';
\d test
6.修改表字段的默认值
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.修改表的名字
ALTER TABLE test RENAME TO mytest;
\d mytest
10.删除表
DROP TABLE mytest;
课后作业
1.创建表,为表添加字段
|
root@modb:~#
root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type “help” for help.
omm=# CREATE TABLESPACE khzy_tbs1 RELATIVE LOCATION ‘tablespace1/khzy_ts1’;
CREATE TABLESPACE
omm=# CREATE DATABASE khzy WITH TABLESPACE = khzy_tbs1;
CREATE DATABASE
omm=# drop table if exists test;
NOTICE: table “test” does not exist, skipping
DROP TABLE
omm=# create table khzy1(id bigint,name varchar(50) not null,age int default 20,primary key(id));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index “khzy1_pkey” for table “khzy1”
CREATE TABLE
omm=# \d khzy1
Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# alter table khzy1 add column sex boolean;
ALTER TABLE
omm=# \d khzy1
Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
sex | boolean |
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
2.删除表中的已有字段
|
omm=# alter table khzy1 drop column sex;
ALTER TABLE
omm=# \d khzy1
Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
3.删除表的已有约束、添加约束
|
omm=# alter table khzy1 drop constraint khzy1_pkey;
ALTER TABLE
omm=# \d khzy1
omm=# Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
omm=# select * from pg_constraint where conname like ‘khzy1_pkey’;
conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conin
did | confrelid | confupdtype | confdeltype | confmatchtype | conislocal | coninhcount | connoinherit | cons
oft | conopt | conkey | confkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | coninclu
ding
---------±-------------±--------±--------------±------------±-------------±---------±---------±-----
----±----------±------------±------------±--------------±-----------±------------±-------------±----
----±-------±-------±--------±----------±----------±----------±----------±-------±-------±--------
-~----
(0 rows)
omm=# alter table khzy1 add constraint khzy1_pkey primary key(id);
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index “khzy1_pkey” for table “khzy1”
ALTER TABLE
omm=# select * from pg_constraint where conname like ‘khzy1_pkey’;
conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | co
nindid | confrelid | confupdtype | confdeltype | confmatchtype | conislocal | coninhcount | connoinherit | c
onsoft | conopt | conkey | confkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | conin
cluding
------------±-------------±--------±--------------±------------±-------------±---------±---------±–
-------±----------±------------±------------±--------------±-----------±------------±-------------±-
-------±-------±-------±--------±----------±----------±----------±----------±-------±-------±-----
-~-------
khzy1_pkey | 2200 | p | f | f | t | 16391 | 0 |
16397 | 0 | | | | t | 0 | t | f
| f | {1} | | | | | | | |
(1 row)
omm=# \d khzy1
omm=# Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 20
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
4.修改表字段的默认值
|
omm=# alter table khzy1 alter column age set default 45;
ALTER TABLE
omm=# \d khzy1
Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | integer | default 45
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
5.修改表字段的数据类型
|
omm=# alter table khzy1 alter column age type bigint;
ALTER TABLE
omm=# \d khzy1
Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
age | bigint | default 45
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
6.修改表字段的名字
|
omm=# alter table khzy1 rename column age to stuage;
ALTER TABLE
omm=# \d khzy1
Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
stuage | bigint | default 45
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
7.修改表的名字
|
omm=# alter table khzy1 rename to mykhzy1;
ALTER TABLE
omm=# \d mykhzy1
Table “public.mykhzy1”
Column | Type | Modifiers
--------±----------------------±-----------
id | bigint | not null
name | character varying(50) | not null
stuage | bigint | default 45
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
8.删除表
|
omm=# drop table mykhzy1;
DROP TABLE
|
总结
通过第16天的学习,主要学习的表的建立,字段的添加与删除,约束的添加与删除等。