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

openGauss每日一练第16天|表管理

原创 lqkitten 2022-12-09
203

课程学习

1.测试准备

–首先创建一张测试表。

omm=# drop table if exists test;
DROP TABLE
omm=# NOTICE:  table "test" does not exist, skipping
create table test(
omm(#       id bigint,
omm(#       name varchar(50) not null,
omm(# omm(#       age  int default 20,
      primary key(id)
omm(#      );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE

2.为表添加字段

–查看表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

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

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

–执行下面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.删除表中的已有字段

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

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


–执行下面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.删除表的已有约束

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

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

–执行下面的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

–或直接查看约束是否被删除

omm=# select * from pg_constraint  where conname like 'test_pkey';
---------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+-----------+-------------+-------------+---------------
+------------+-------------+--------------+---------+--------+--------+---------+-----------+-----------+-----------+-----------+--------+--------+--------------
(0 rows)

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

5.为表添加约束

–执行下面的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

–再次查看表test的信息:

omm=# select * from pg_constraint  where conname like 'test_pkey';
  conname  | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | contypid | conindid | confrelid | confupdtype | confdeltype | confmatchtyp
e | conislocal | coninhcount | connoinherit | consoft | conopt | conkey | confkey | conpfeqop | conppeqop | conffeqop | conexclop | conbin | consrc | conincluding 
-----------+--------------+---------+---------------+-------------+--------------+----------+----------+----------+-----------+-------------+-------------+-------------
--+------------+-------------+--------------+---------+--------+--------+---------+-----------+-----------+-----------+-----------+--------+--------+--------------
 test_pkey |         2200 | p       | f             | f           | t            |    16389 |        0 |    16395 |         0 |             |             |             
  | t          |           0 | t            | f       | f      | {1}    |         |           |           |           |           |        |        | 
(1 row)


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

6.修改表字段的默认值

–执行下面的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.修改表的名字

–修改表的名字。执行下面的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

课程作业

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

omm=# CREATE TABLE student
omm-# (
omm(#     std_id INT PRIMARY KEY,
omm(#     std_name VARCHAR(20) NOT NULL,
omm(#     std_sex VARCHAR(6),
omm(#     std_birth DATE,
omm(#     std_in DATE NOT NULL,
omm(#     std_address VARCHAR(100)
omm(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "student_pkey" for table "student"
CREATE TABLE
omm=# alter table student add column tele char(11);
ALTER TABLE
omm=# \d student
              Table "public.student"
   Column    |          Type          | Modifiers 
-------------+------------------------+-----------
 std_id      | integer                | not null
 std_name    | character varying(20)  | not null
 std_sex     | character varying(6)   | 
 std_birth   | date                   | 
 std_in      | date                   | not null
 std_address | character varying(100) | 
 tele        | character(11)          | 
Indexes:
    "student_pkey" PRIMARY KEY, btree (std_id) TABLESPACE pg_default

2.删除表中的已有字段

omm=# alter table student drop column std_in;
ALTER TABLE
omm=# \d student
              Table "public.student"
   Column    |          Type          | Modifiers 
-------------+------------------------+-----------
 std_id      | integer                | not null
omm=#  std_name    | character varying(20)  | not null
 std_sex     | character varying(6)   | 
 std_birth   | date                   | 
 std_address | character varying(100) | 
 tele        | character(11)          | 
Indexes:
    "student_pkey" PRIMARY KEY, btree (std_id) TABLESPACE pg_default

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


omm=# alter table student drop constraint student_pkey;
ALTER TABLE
omm=# \d student;
              Table "public.student"
   Column    |          Type          | Modifiers 
-------------+------------------------+-----------
 std_id      | integer                | not null
 std_name    | character varying(20)  | not null
 std_sex     | character varying(6)   | 
 std_birth   | date                   | 
 std_address | character varying(100) | 
 tele        | character(11)          | 

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

omm=# \d student
              Table "public.student"
   Column    |          Type          | Modifiers 
-------------+------------------------+-----------
 std_id      | integer                | not null
 std_name    | character varying(20)  | not null
 std_sex     | character varying(6)   | 
 std_birth   | date                   | 
 std_address | character varying(100) | 
 tele        | character(11)          | 
Indexes:
    "pk_student_id" PRIMARY KEY, btree (std_id) TABLESPACE pg_default

4.修改表字段的默认值

omm=# alter table student alter column tele set default '13988887777';
omm=# ALTER TABLE

omm=# \d student
                      Table "public.student"
 Column    |          Type          |           Modifiers           
-------------+------------------------+-------------------------------
std_id      | integer                | not null
std_name    | character varying(20)  | not null
std_sex     | character varying(6)   | 
std_birth   | date                   | 
std_address | character varying(100) | 
tele        | character(11)          | default '13988887777'::bpchar
Indexes:
  "pk_student_id" PRIMARY KEY, btree (std_id) TABLESPACE pg_default

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

omm=# alter table student alter column tele type varchar2(11);
ALTER TABLE
omm=# \d student
                        Table "public.student"
   Column    |          Type          |           Modifiers           
-------------+------------------------+-------------------------------
 std_id      | integer                | not null
 std_name    | character varying(20)  | not null
 std_sex     | character varying(6)   | 
 std_birth   | date                   | 
 std_address | character varying(100) | 
 tele        | character varying(11)  | default '13988887777'::bpchar
Indexes:
    "pk_student_id" PRIMARY KEY, btree (std_id) TABLESPACE pg_default

6.修改表字段的名字

omm=# alter table student rename column tele to yddh;
ALTER TABLE
omm=# \d student
                        Table "public.student"
   Column    |          Type          |           Modifiers           
-------------+------------------------+-------------------------------
 std_id      | integer                | not null
 std_name    | character varying(20)  | not null
 std_sex     | character varying(6)   | 
 std_birth   | date                   | 
 std_address | character varying(100) | 
 yddh        | character varying(11)  | default '13988887777'::bpchar
Indexes:
    "pk_student_id" PRIMARY KEY, btree (std_id) TABLESPACE pg_default

7.修改表的名字

omm=# alter table student rename to std;
ALTER TABLE
omm=# \dt
                        List of relations
 Schema | Name | Type  | Owner |             Storage              
--------+------+-------+-------+----------------------------------
 public | std  | table | omm   | {orientation=row,compression=no}
(1 row)

8.删除表

omm=# drop table std purge;
omm=# DROP TABLE

omm=# \dt
No relations found.

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

评论