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

openGauss 每日一练第16天

原创 酷酷咖啡 2022-12-09
384

学习目标

学习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
  1. 删除表的已有约束
--表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.创建表,为表添加字段

-- 创建表 test01 omm=# create table test01(id bigint,name varchar(50) not null,age int default 0); CREATE TABLE -- 添加sex 字段,类型为Boolean;添加备注字段 remark,类型为 varchar omm=# alter table test01 add column sex boolean, add column remark varchar(200); ALTER TABLE -- 查看表 test01 的详情 omm=# \d test01 Table "public.test01" Column | Type | Modifiers --------+------------------------+----------- id | bigint | name | character varying(50) | not null age | integer | default 0 sex | boolean | remark | character varying(200) |

2.删除表中的已有字段

-- 删除test01 表中的备注字段 omm=# alter table test01 drop column remark; omm=# ALTER TABLE -- 查看 test01 中 remark 字段是否被删除 omm=# \d test01 Table "public.test01" Column | Type | Modifiers --------+-----------------------+----------- id | bigint | name | character varying(50) | not null age | integer | default 0 sex | boolean |

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

-- 为test01 表中的 id 字段添加 主键约束 omm=# alter table test01 add constraint id_pkey primary key(id); NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "id_pkey" for table "test01" ALTER TABLE -- 查看刚添加的 test01 中的约束,打开扩展显示 omm=# \x Expanded display is on. select * from pg_constraint where conname = 'id_pkey'; -[ RECORD 1 ]-+-------- confdeltype | confmatchtype | conislocal | t coninhcount | 0 conname | id_pkey connamespace | 2200 contype | p condeferrable | f condeferred | f convalidated | t conrelid | 16389 contypid | 0 conindid | 16393 confrelid | 0 confupdtype | conkey | {1} confkey | conpfeqop | conppeqop | connoinherit | t consoft | f conopt | f conffeqop | conexclop | conbin | consrc | conincluding | -- 删除test01 的主键约束 omm=# alter table test01 drop constraint id_pkey; ALTER TABLE -- 查看约束是否已被删除 omm=# select * from pg_constraint where conname = 'id_key'; (No rows)

4.修改表字段的默认值

-- 修改 age字段 默认值为18 omm=# alter table test01 alter column age set default 18; ALTER TABLE -- 查看表详情 omm=# \d test01 Table "public.test01" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null age | integer | default 18 sex | boolean |

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

-- 修改id 字段类型为 varchar(32) omm=# alter table test01 alter column id type varchar(32); ALTER TABLE omm=# \d test01 Table "public.test01" Column | Type | Modifiers --------+-----------------------+------------ id | character varying(32) | not null name | character varying(50) | not null age | integer | default 18 sex | boolean

6.修改表字段的名字

-- 修改字段 name 到 myname omm=# alter table test01 rename COLUMN name to myname; ALTER TABLE -- 查看变更后的表的详情 omm=# \d test01 Table "public.test01" Column | Type | Modifiers --------+-----------------------+------------ id | character varying(32) | not null myname | character varying(50) | not null age | integer | default 18 sex | boolean |

7.修改表的名字

-- 将表的名字从 test01 修改为 test02 omm=# alter table test01 rename to test02; ALTER TABLE -- 查看表 ,可以看到已经 修改为 test02 omm=# \dt List of relations Schema | Name | Type | Owner | Storage --------+--------+-------+-------+---------------------------------- public | test02 | table | omm | {orientation=row,compression=no} (1 row)

8.删除表

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

评论