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

openGauss每日一练第14天

原创 周周 2022-12-07
120

学习目标

学习表的约束、表的默认值、自增类型等技术

课程作业

1.创建表的时候定义列级约束

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=# drop table if exists test;
NOTICE:  table "test" does not exist, skipping
DROP TABLE
omm=#   create table test(
   id bigint primary key, 
   name varchar(50) not null,
   age  int
        );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE
omm=#   insert into test values(1,'user1',50);
INSERT 0 1
omm=#select * from test;
 id | name  | age 
----+-------+-----
  1 | user1 |  50
(1 row)
omm=# \d test 
            Table "public.test"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               | 
Indexes:
    "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
复制

2.创建表的时候定义表级约束

omm=# drop table if exists test001;
NOTICE:  table "test001" does not exist, skipping
DROP TABLE
omm=#create table test001(
      id bigint,
name varchar(50) not null,  -- 创建列级not null约束
age  int,
primary key(id) -- 创建表级约束
     );
 NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test001_pkey" for table "test001"
CREATE TABLE
omm=# insert into test001 values(1,'user1',50);
INSERT 0 1
omm=# select * from test001;
 id | name  | age 
----+-------+-----
  1 | user1 |  50
(1 row)

omm=#  \d test001
 age    | integer               | 
Indexes:
    "test001_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

 Table "public.test001"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | bigint                | not null
 name   | character varying(50) | not null
复制

3.为表的属性定义默认值

omm=# drop table if exists test002;
NOTICE:  table "test002" does not exist, skipping
DROP TABLE
omm=# create table test002(
      id bigint,
       name varchar(28) not null,
       age  int default 20,  --  为该列定义默认值为20
       primary key(id)
      );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test002_pkey" for table "test002"
CREATE TABLE
--下面的SQL insert语句,在向表test插入数据时,没有提供age列的值:
omm=#  insert into test002(id,name) values(1,'user1');
INSERT 0 1
omm=# insert into test002(id,name) values(2,'user2');
INSERT 0 1 
omm=#   select * from test002;
 id | name  | age 
----+-------+-----
  1 | user1 |  20
  2 | user2 |  20
(2 rows)
复制

4.如果在创建表的时候,没有为某列定义默认值,缺省的默认值是空值null

omm=# drop table if exists test;
DROP TABLE
omm=#  create table test(
        id bigint,
         name varchar(50) not null,
       age  int,                                
        primary key(id)
      );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test"
CREATE TABLE
omm=#  insert into test(id,name) values(1,'user1');
INSERT 0 1
omm=#  select * from test;
(1 row)

  id | name  | age 
----+-------+-----
  1 | user1 |    
复制

5.创建表时使用自增数据类型

omm=# create table invoice(invoicenum serial NOT NULL,name varchar(20));
NOTICE:  CREATE TABLE will create implicit sequence "invoice_invoicenum_seq" for serial column "invoice.invoicenum"
CREATE TABLE
--为表invoice插入3条记录,并查看插入数据后的表的数据:
omm=#  insert into invoice(name) values('user1');
INSERT 0 1
omm=# insert into invoice(name) values('user2');
 INSERT 0 1
omm=#insert into invoice(name) values('user3');
INSERT 0 1
--可以看到每插入一条记录到表invoice后,列invoicenum的值会自增1。
omm=# select * from invoice;
 invoicenum | name  
------------+-------
          1 | user1
          2 | user2
          3 | user3
(3 rows)
复制

6.使用现有的表创建新表

omm=#  DROP TABLE if exists newtestwithdata;
NOTICE:  table "newtestwithdata" does not exist, skipping
DROP TABLE
omm=#  CREATE TABLE newtestwithdata AS SELECT * FROM invoice;
 INSERT 0 3
omm=# SELECT * FROM newtestwithdata;
 invoicenum | name  
------------+-------
          1 | user1
          2 | user2
          3 | user3
(3 rows)

omm=#  DROP TABLE if exists testnewwithoutdata;
NOTICE:  table "testnewwithoutdata" does not exist, skipping
DROP TABLE
omm=#  CREATE TABLE testnewwithoutdata AS SELECT * FROM invoice WHERE 1=2;
INSERT 0 0
omm=#  SELECT * FROM testnewwithoutdata;
 invoicenum | name 
------------+------
(0 rows)
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论