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

openGauss 每日一练第 14 天 |openGauss 逻辑结构:表管理 2

956

学习目标

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

前面每日一练链接

openGauss 每日一练第 1 天 | openGauss 数据库状态查看
openGauss 每日一练第 2 天 | 学习 gsql 命令行的使用
openGauss 每日一练第 3 天 | openGauss 数据库状态查看
openGauss 每日一练第 4 天 | openGauss 中一个数据库可以被多个用户访问
openGauss 每日一练第 5 天 | openGauss 中一个用户可以访问多个数据库
openGauss 每日一练第 6 天 | openGauss 中用户一次只能连接到一个数据库
openGauss 每日一练第 7 天 | openGauss 中一个数据库中可以创建多个模式
openGauss 每日一练第 8 天 | openGauss 中一个数据库可以存储在多个表空间中
openGauss 每日一练第 9 天 | openGauss 中一个表空间可以存储多个数据库
openGauss 每日一练第 10 天 | openGauss 逻辑结构:表空间管理
openGauss 每日一练第 11 天 | openGauss 逻辑结构:数据库管理
openGauss 每日一练第 12 天 | openGauss 逻辑结构:模式管理
openGauss 每日一练第 13 天 |openGauss 逻辑结构:表管理 1

课程学习

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

--创建表的时候为表定义列级约束:在列级定义了 primary key 约束(id 列)和not null 约束(name 列)。

drop table if exists test;
create table test(
id bigint primary key, 
name varchar(50) not null,
age  int
);
--插入数据
  insert into test values(1,'user1',50);
--查看数据
 select * from test;
--查看约束
\d test 

图片.png

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

--创建表时定义表级约束  执行下面的 SQL 语句,在创建表的时候为表定义表级约束:
#这里在表列级定义了 primary key 约束(id列),在列级定义了 not nul 约束(name列)。
drop table if exists test001;
create table test001(
      id bigint,
      name varchar(50) not null,  -- 创建列级 not null 约束
      age  int,
      primary key(id) -- 创建表级约束
     );
insert into test001 values(1,'user1',50);
select * from test001;
 \d test001

图片.png

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

--执行下面的语句,在创建表的时候为表的某个列定义默认值:
drop table if exists test002;
create table test002(
      id bigint,
      name varchar(28) not null,
      age  int default 20,  --  为该列定义默认值为20
      primary key(id)
);
--下面的SQL insert语句,在向表test插入数据时,没有提供age列的值:
insert into test002(id,name) values(1,'user1');
insert into test002(id,name) values(2,'user2');

  select * from test002;

图片.png

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

-- 未定义 age 列的默认值,如果插入数据时未提供该列的值,则将默认插入空值 null

drop table if exists test;

 create table test(
       id bigint,
        name varchar(50) not null,
      age  int,                                
       primary key(id)
);

 insert into test(id,name) values(1,'user1');
 select * from test;

图片.png

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

商品发票的编号通常按顺序递增。这种情况可以使用 serial 数据类型。最简单方法直接使用 serial 数据类型。

--创建一个带有 serial 数据类型的测试表 invoice:
drop table if exists invoice;
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"
--为表 invoice 插入 3 条记录,并查看插入数据后的表的数据:
insert into invoice(name) values('user1');
insert into invoice(name) values('user2');
insert into invoice(name) values('user3');

--可以看到每插入一条记录到表invoice后,列invoicenum的值会自增1。
select * from invoice;

图片.png

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

--执行下面的SQL语句,将创建新表,并且会将旧表的数据拷贝给新表:
 DROP TABLE if exists newtestwithdata;
 CREATE TABLE newtestwithdata AS SELECT * FROM invoice;
 SELECT * FROM newtestwithdata;

--执行下面的 SQL 语句,创建和旧表的表结构相同的新表,但是不会将旧表的数据拷贝给新表:
 DROP TABLE if exists testnewwithoutdata;
 CREATE TABLE testnewwithoutdata AS SELECT * FROM invoice WHERE 1=2;
 SELECT * FROM testnewwithoutdata;

图片.png

课程作业

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

create table t1(id int primary key,
name varchar(40) not null);
insert into t1 values(1,'jiekexu');
select * from t1;
\d t1


omm=# \d t1
             Table "public.t1"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | not null
 name   | character varying(40) | not null
Indexes:
    "t1_pkey" PRIMARY KEY, btree (id) TABLESPACE jieke_tbs

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

create table t2(id int,
name varchar(40) not null,
primary key(id));

insert into t2 values(2,'jiekexu');
select * from t2;

图片.png

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

create table t3(id int primary key,
name varchar(40) not null,
addr varchar(40) default 'beijing'
);

insert into t3 values(3,'jiekexu');
select * from t3;

图片.png

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

create table t4(id bigint,
name varchar(50) not null,
age  int,                                
primary key(id)
);

insert into t4(id,name) values(4,'jiekexu');
select * from t4;

图片.png

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

create table t5(seqno serial NOT NULL,name varchar(20));

insert into t5(name) values('jiekexu1');
insert into t5(name) values('jiekexu2');
insert into t5(name) values('jiekexu3');

--可以看到每插入一条记录到表 t5 后,列 seqno 的值会自增1。
select * from t5;

图片.png

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

create table t6 as select * from t5;

select * from t6;

create table t7 as select * from t5 where 3=2;
select * from t7;

omm=# \d t6
             Table "public.t6"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 seqno  | integer               | 
 name   | character varying(20) | 

omm=# \d t7
             Table "public.t7"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 seqno  | integer               | 
 name   | character varying(20) | 

omm=# \d
                                   List of relations
 Schema |          Name          |   Type   | Owner |             Storage              
--------+------------------------+----------+-------+----------------------------------
 public | invoice                | table    | omm   | {orientation=row,compression=no}
 public | invoice_invoicenum_seq | sequence | omm   | 
 public | newtestwithdata        | table    | omm   | {orientation=row,compression=no}
 public | t1                     | table    | omm   | {orientation=row,compression=no}
 public | t2                     | table    | omm   | {orientation=row,compression=no}
 public | t3                     | table    | omm   | {orientation=row,compression=no}
 public | t4                     | table    | omm   | {orientation=row,compression=no}
 public | t5                     | table    | omm   | {orientation=row,compression=no}
 public | t5_seqno_seq           | sequence | omm   | 
 public | t6                     | table    | omm   | {orientation=row,compression=no}
 public | t7                     | table    | omm   | {orientation=row,compression=no}
 public | test                   | table    | omm   | {orientation=row,compression=no}
 public | test001                | table    | omm   | {orientation=row,compression=no}
 public | test002                | table    | omm   | {orientation=row,compression=no}
 public | testnewwithoutdata     | table    | omm   | {orientation=row,compression=no}
(15 rows)

图片.png

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

评论