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

openGauss每日一练第 16 天

原创 陶笑 2022-12-10
280

学习目标

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

课程作业

1.创建表,为表添加字段

newdb1=# create table test(id bigint,name varchar(50) not null,age int default 20,primary key(id)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE newdb1=# \d test Table "testsm.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 newtbs1 newdb1=# alter table test add column sex Boolean; ALTER TABLE newdb1=# \d test Table "testsm.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 newtbs1

2.删除表中的已有字段

newdb1=# alter table test drop column sex ; ALTER TABLE newdb1=# \d test Table "testsm.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 newtbs1

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

--删除主键约束 newdb1=# \d test Table "testsm.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 newtbs1 newdb1=# alter table test drop constraint test_pkey; ALTER TABLE newdb1=# \d test Table "testsm.test" Column | Type | Modifiers --------+-----------------------+------------ id | bigint | not null name | character varying(50) | not null age | integer | default 20 --添加主键约束 newdb1=# 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 newdb1=# \d test Table "testsm.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 newtbs1 newdb1=#

4.修改表字段的默认值

newdb1=# \d test Table "testsm.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 newtbs1 newdb1=# alter table test alter column age set default 25; ALTER TABLE newdb1=# \d test Table "testsm.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 newtbs1

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

newdb1=# \d test Table "testsm.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 newtbs1 newdb1=# alter table test ALTER COLUMN age TYPE bigint; ALTER TABLE newdb1=# \d test Table "testsm.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 newtbs1

6.修改表字段的名字

newdb1=# ALTER TABLE test RENAME COLUMN age TO stuage; ALTER TABLE newdb1=# \d test Table "testsm.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 newtbs1

7.修改表的名字

newdb1=# ALTER TABLE test RENAME TO mytest; ALTER TABLE newdb1=# \d test Did not find any relation named "test". newdb1=# \d mytest Table "testsm.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 newtbs1

8.删除表

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

评论

目录
  • 学习目标
  • 课程作业
    • 1.创建表,为表添加字段
    • 2.删除表中的已有字段
    • 3.删除表的已有约束、添加约束
    • 4.修改表字段的默认值
    • 5.修改表字段的数据类型
    • 6.修改表字段的名字
    • 7.修改表的名字
    • 8.删除表