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

openGuass每日一练第16天|学习心得体会

原创 小严 2022-12-09
700

课程学习内容

软件版本openGauss 3.0.0

学习openGuass数据库中如何对表进行修改

数据库实操内容

环境准备

–首先创建一张测试表。

drop table if exists test; create table test(

​ id bigint,

​ name varchar(50) not null,

​ age int default 20,

​ primary key(id)

​ );

omm=# drop table if exists test; NOTICE: table "test" does not exist, skipping DROP TABLE omm=# create table test( 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 "test_pkey" for table "test" CREATE TABLE omm=#

为表添加字段

–查看表test的信息

\d test

omm=# \d test 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=#

–为表test新增一列,列名为sex,数据类型为Boolean:

alter table test add column sex Boolean;

omm=# alter table test add column sex Boolean; ALTER TABLE omm=#

–执行下面gsql命令,查看表test的信息

\d test

omm=# \d test Table "public.test" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null age | integer | default 20 sex | boolean | Indexes: "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default omm=#

删除表中的已有字段

–执行下面的SQL语句,删除刚刚添加的列sex:

alter table test drop column sex ;

omm=# alter table test drop column sex ; ALTER TABLE

–执行下面gsql命令,再次查看表test的信息

\d test

omm=# \d test 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=#

删除表的已有约束

–表test上有一个名叫test_pkey的PRIMARY KEY约束。执行下面的SQL语句,删除这个约束:

alter table test drop constraint test_pkey;

omm=# alter table test drop constraint test_pkey; ALTER TABLE

–执行下面的gsql命令,再次查看表test的信息:

\d test

omm=# \d test Table "public.test" 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 ‘test_pkey’;

omm=# select * from pg_constraint where conname like 'test_pkey'; conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conindid | con frelid | confupdtype | confdeltype | confmatchtype | conislocal | coninhcount | connoinherit | consoft | conopt | con key | confkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | conincluding ---------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+---- -------+-------------+-------------+---------------+------------+-------------+--------------+---------+--------+---- ----+---------+-----------+-----------+-----------+-----------+--------+--------+-------------- (0 rows) omm=#

为表添加约束

–执行下面的SQL命令,为表test添加刚刚删除的主键约束:

alter table test add constraint test_pkey primary key(id);

omm=# alter table test add constraint test_pkey primary key(id); NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "test_pkey" for table "test" ALTER TABLE

–再次查看表test的信息:

select * from pg_constraint where conname like ‘test_pkey’;

\d test

omm=# select * from pg_constraint where conname like 'test_pkey'; -----------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+-- ---------+-------------+-------------+---------------+------------+-------------+--------------+---------+--------+-- ------+---------+-----------+-----------+-----------+-----------+--------+--------+-------------- test_pkey | 2200 | p | f | f | t | 16389 | 0 | 16395 | 0 | | | | t | 0 | t | f | f | { 1} | | | | | | | | (1 row) conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conindid | c onfrelid | confupdtype | confdeltype | confmatchtype | conislocal | coninhcount | connoinherit | consoft | conopt | c onkey | confkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | conincluding 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=#

修改表字段的默认值

–执行下面的SQL语句,将age的默认值变更为25

alter table test alter column age set default 25;

\d test

omm=# alter table test alter column age set default 25; ALTER TABLE omm=# \d test age | integer | default 25 Indexes: "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default omm=# Table "public.test" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null omm=#

修改表字段的数据类型

alter table test ALTER COLUMN age TYPE bigint;

\d test

omm=# alter table test ALTER COLUMN age TYPE bigint; ALTER TABLE omm=# \d test Table "public.test" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null age | bigint | default 25 Indexes: "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default omm=#

修改表字段的名字

ALTER TABLE test RENAME COLUMN age TO stuage;

\d test

omm=# ALTER TABLE test RENAME COLUMN age TO stuage; ALTER TABLE omm=# \d test Table "public.test" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null omm=# name | character varying(50) | not null stuage | bigint | default 25 Indexes: "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default omm=#

修改表的名字

–修改表的名字。执行下面的SQL语句,将表test的名字变更为mytest:

ALTER TABLE test RENAME TO mytest;

\d mytest

omm=# ALTER TABLE test 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 25 Indexes: "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default omm=#

删除表

DROP TABLE mytest;

omm=# DROP TABLE mytest; DROP TABLE omm=#

课程作业

创建表,为表添加字段

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 omm=# 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 omm=#

删除表中的已有字段

omm=# alter table test1 drop column sex ; ALTER TABLE omm=#

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

omm=# alter table test1 drop constraint test1_pkey; ALTER TABLE omm=# omm=# \d test1 Table "public.test1" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null age | integer | default 20 omm=# omm=# select * from pg_constraint where conname like 'test1_pkey'; conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conindid | con frelid | confupdtype | confdeltype | confmatchtype | conislocal | coninhcount | connoinherit | consoft | conopt | con key | confkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | conincluding ---------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+---- -------+-------------+-------------+---------------+------------+-------------+--------------+---------+--------+---- ----+---------+-----------+-----------+-----------+-----------+--------+--------+-------------- (0 rows) omm=#

修改表字段的默认值

omm=# alter table test1 alter column age set default 25; ALTER TABLE omm=# \d test1 Table "public.test1" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null age | integer | default 25 omm=#

修改表字段的数据类型

omm=# alter table test1 ALTER COLUMN age TYPE bigint; ALTER TABLE omm=#

修改表字段的名字

omm=# ALTER TABLE test1 RENAME COLUMN age TO stuage; ALTER TABLE omm=#

修改表的名字

omm=# ALTER TABLE test1 RENAME TO testok; ALTER TABLE omm=# \d testok Table "public.testok" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null stuage | bigint | default 25 omm=#

删除表

omm=# DROP TABLE testok; DROP TABLE omm=#

课程学习体会

 通过本次的实训操作,掌握数据库的表进行修改,添加字段、修改约束条件、修改默认值等操作。

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

评论