学习目标
学习openGuass数据库中如何对表进行修改
课程学习
1、创建表
omm=# --首先创建一张测试表。 omm=# drop table if exists test; NOTICE: table "test" does not exist, skipping DROP TABLE omm=# create table test( id bigint, omm(# omm(# name varchar(50) not null, age int default 20, omm(# omm(# primary key(id) omm(# ); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE
复制
2、为表添加字段
omm=# --查看表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=# omm=# omm=# --为表test新增一列,列名为sex,数据类型为Boolean: alter table test add column sex Boolean; ALTER TABLE omm=# omm=# --执行下面gsql命令,查看表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
复制
3、删除表中的已有字段
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=# --执行下面的SQL语句,删除刚刚添加的列sex: omm=# alter table test drop column sex ; ALTER TABLE omm=# omm=# --执行下面gsql命令,再次查看表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
复制
4、删除表的已有约束
omm=# --执行下面的gsql命令,再次查看表test的信息: omm=# \d test Table "public.test" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not nullomm=# age | integer | default 20 omm=# --或直接查看约束是否被删除 omm=# select * from pg_constraint where conname like 'test_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 ---------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+-----------+-------------+------------- +---------------+------------+-------------+--------------+---------+--------+--------+---------+-----------+-----------+-----------+-----------+------- -+--------+-------------- (0 rows)
复制
5、为表添加约束
omm=# --执行下面的SQL命令,为表test添加刚刚删除的主键约束: 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 omm=# omm=# --再次查看表test的信息: omm=# select * from pg_constraint where conname like 'test_pkey'; conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conindid | confrelid | confupdtype | confdeltyp e | confmatchtype | conislocal | coninhcount | connoinherit | consoft | conopt | conkey | confkey | conpfeqop | conppeqop | conffeqop | conexclop | conb in | consrc | conincluding -----------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+-----------+-------------+----------- --+---------------+------------+-------------+--------------+---------+--------+--------+---------+-----------+-----------+-----------+-----------+----- ---+--------+-------------- test_pkey | 2200 | p | f | f | t | 16389 | 0 | 16395 | 0 | | | | t | 0 | t | f | f | {1} | | | | | | | | (1 row)
复制
6、修改表字段的默认值
omm=# --执行下面的SQL语句,将age的默认值变更为25 omm=# alter table test alter column age set default 25; ALTER TABLE omm=# \d test Table "public.test" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null age | integer | default 25 Indexes: "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
复制
7、修改表字段的数据类型
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
复制
8、修改表字段的名字
omm=# ALTER TABLE test RENAME COLUMN age TO stuage; ALTER TABLE omm=# \d test Table "public.test" 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
复制
9、修改表的名字
omm=# --修改表的名字。执行下面的SQL语句,将表test的名字变更为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
复制
10、删除表
omm=# DROP TABLE mytest; DROP TABLE omm=#
复制
学习总结
今天继续学习对表的操作,包含为表加字段,删字段,加约束,删约束,修改表字段名字,修改表字段类型,修改表的名字,删除表,命令还是容易理解的,跟mysql的命令没啥差别,比较简单适合入门。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论

2年前

评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
475次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
303次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
198次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
159次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
152次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
132次阅读
2025-04-30 16:39:58
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
91次阅读
2025-04-18 10:49:53
opengauss使用gs_probackup进行增量备份恢复
进击的CJR
88次阅读
2025-04-09 16:11:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
81次阅读
2025-04-23 16:02:19
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
54次阅读
2025-04-17 10:41:47