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

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

原创 Shuhao 2022-12-09
223

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

omm@modb:~$ pgql -r
-bash: pgql: command not found
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=# 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=# \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

ALTER TABLE
omm=# create table test(
      id bigint,
      name varchar(50) not null,
      age  int default 20,
      primary key(id)
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

2.删除表中的已有字段

ALTER TABLE
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

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

ALTER TABLE
omm=# \d test
             Table "public.test"
 Column |         Type          | Modifiers  
--------+-----------------------+------------
omm=#  id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | default 20

 
omm=# omm=# 

omm=# select * from pg_constraint  where conname like 'test_pkey';
 conname | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | conty
pid | conindid | confrelid | confupdtype | confdeltype | confmatchtype | conislocal | coninhcount
-----+--------+--------+--------------
(0 rows)

 | connoinherit | consoft | conopt | conkey | confkey | conpfeqop | conppeqop | conffeqop | conex
clop | conbin | consrc | conincluding 
---------+--------------+---------+---------------+-------------+--------------+----------+------
----+----------+-----------+-------------+-------------+---------------+------------+------------
-+--------------+---------+--------+--------+---------+-----------+-----------+-----------+------


NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "test_pkey" for table "test"
omm=# ALTER TABLE

omm=# select * from pg_constraint  where conname like 'test_pkey';
  conname  | connamespace | contype | condeferrable | condeferred | convalidated | conrelid | con
typid | conindid | confrelid | confupdtype | confdeltype | confmatchtype | conislocal | coninhcou
nt | connoinherit | consoft | conopt | conkey | confkey | conpfeqop | conppeqop | conffeqop | con
exclop | 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

4.修改表字段的默认值

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

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

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

6.修改表字段的名字

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

7.修改表的名字

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

8.删除表

DROP TABLE
DROP TABLE mytest;

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

评论