zl程序教程

您现在的位置是:首页 >  其他

当前栏目

openGauss逻辑结构:表管理2

逻辑 管理 结构 openGauss
2023-09-11 14:19:28 时间

学习目标

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

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

-- 创建表的时候为表定义列级约束:在列级定义了primary key约束(id列)和not null约束(name列)。
[omm@ogdb1 ~]$ gsql -d omm -p 40000 -r
gsql ((openGauss 3.1.0 build 4e931f9a) compiled at 2022-09-29 14:19:24 commit 0 last mr  )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

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

--插入数据
omm=# insert into tab141 values(1,'user1',50);
INSERT 0 1

--查看数据
omm=# select * from tab141;
 id | name  | age
----+-------+-----
  1 | user1 |  50
(1 row)


--查看约束
omm=# \d tab141
           Table "public.tab141"
 Column |         Type          | Modifiers
--------+-----------------------+-----------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               |
Indexes:
    "tab141_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

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

--创建表时定义表级约束  执行下面的SQL语句,在创建表的时候为表定义表级约束:
#这里在表列级定义了primary key约束(id列),在列级定义了not null约束(name列)。
omm=# drop table if exists tab142;
NOTICE:  table "tab142" does not exist, skipping
DROP TABLE
omm=# create table tab142(id bigint,name varchar(50) not null,age int,primary key(id));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "tab142_pkey" for table "tab142"
CREATE TABLE
omm=# insert into tab142 values(1,'user1',50);
INSERT 0 1
omm=# select * from tab142;
 id | name  | age
----+-------+-----
  1 | user1 |  50
(1 row)

omm=#  \d tab142
           Table "public.tab142"
 Column |         Type          | Modifiers
--------+-----------------------+-----------
 id     | bigint                | not null
 name   | character varying(50) | not null
 age    | integer               |
Indexes:
    "tab142_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default

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

--执行下面的语句,在创建表的时候为表的某个列定义默认值:
omm=# drop table if exists tab143;
NOTICE:  table "tab143" does not exist, skipping
DROP TABLE
omm=# create table tab143(id bigint,name varchar(28) not null,age  int default 20,primary key(id));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "tab143_pkey" for table "tab143"
CREATE TABLE
omm=#


--下面的SQL insert语句,在向表test插入数据时,没有提供age列的值:
omm=# insert into tab143(id,name) values(1,'user1');
INSERT 0 1
omm=# insert into tab143(id,name) values(2,'user2');
INSERT 0 1
omm=#
omm=# select * from tab143;
 id | name  | age
----+-------+-----
  1 | user1 |  20
  2 | user2 |  20
(2 rows)

omm=#

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

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

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

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

-创建一个带有serial数据类型的测试表tab145:
omm=# drop table if exists tab145;
NOTICE:  table "tab145" does not exist, skipping
DROP TABLE
omm=# create table tab145(invoicenum serial NOT NULL,name varchar(20));
NOTICE:  CREATE TABLE will create implicit sequence "tab145_invoicenum_seq" for serial column "tab145.invoicenum"
CREATE TABLE
omm=#


--为表tab145插入3条记录,并查看插入数据后的表的数据:
omm=# insert into tab145(name) values('user1');
INSERT 0 1
omm=# insert into tab145(name) values('user2');
INSERT 0 1
omm=# insert into tab145(name) values('user3');
INSERT 0 1
omm=#


--可以看到每插入一条记录到表invoice后,列invoicenum的值会自增1。
omm=# select * from tab145;
 invoicenum | name
------------+-------
          1 | user1
          2 | user2
          3 | user3
(3 rows)

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

--执行下面的SQL语句,将创建新表,并且会将旧表的数据拷贝给新表:
NOTICE:  table "tab1461" does not exist, skipping
DROP TABLE
omm=#  CREATE TABLE tab1461 AS SELECT * FROM tab145;
INSERT 0 3
omm=#  SELECT * FROM tab1461;
 invoicenum | name
------------+-------
          1 | user1
          2 | user2
          3 | user3
(3 rows)

--执行下面的SQL语句,创建和旧表的表结构相同的新表,但是不会将旧表的数据拷贝给新表:
omm=#  DROP TABLE if exists tab1462;
NOTICE:  table "tab1462" does not exist, skipping
DROP TABLE
omm=#  CREATE TABLE tab1462 AS SELECT * FROM tab145 WHERE 1=2;
INSERT 0 0
omm=#  SELECT * FROM tab1462;
 invoicenum | name
------------+------
(0 rows)