前言:
最近参加了由opengauss、墨天轮、鲲鹏社区一起推出的活动《每日一练 opengauss 3.0.0 数据库在线实训课程》,共21天,墨天轮提供实操环境,特此记录学习笔记。
活动详情:https://www.modb.pro/db/551619
主题
学习表的约束、表的默认值、自增类型等技术
学习笔记
1.创建表的时候定义列级约束
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
2.创建表的时候定义表级约束
drop table if exists test001;
create table test001(
id bigint,
name varchar(50) not null,
age int,
primary key(id)
);
insert into test001 values(1,'user1',50);
select * from test001;
\d test001
3.为表的属性定义默认值
drop table if exists test002;
create table test002(
id bigint,
name varchar(28) not null,
age int default 20,
primary key(id)
);
insert into test002(id,name) values(1,'user1');
insert into test002(id,name) values(2,'user2');
select * from test002;
4.如果在创建表的时候,没有为某列定义默认值,缺省的默认值是空值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;
5.建表时使用自增数据类型
商品的序号通常按顺序递增。这种情况可以使用serial数据类型。最简单方法直接使用serial数据类型。
drop table if exists invoice;
create table invoice(invoicenum serial NOT NULL,name varchar(20));
insert into invoice(name) values('user1');
insert into invoice(name) values('user2');
insert into invoice(name) values('user3');
select * from invoice;
6.使用现有的表创建新表
DROP TABLE if exists newtestwithdata;
CREATE TABLE newtestwithdata AS SELECT * FROM invoice;
SELECT * FROM newtestwithdata;
DROP TABLE if exists testnewwithoutdata;
CREATE TABLE testnewwithoutdata AS SELECT * FROM invoice WHERE 1=2;
SELECT * FROM testnewwithoutdata;
课后作业
1.创建表的时候定义列级约束
|
omm-# drop table if exists khzy1;
WARNING: Session unused timeout.
FATAL: terminating connection due to administrator command
could not send data to server: Broken pipe
The connection to the server was lost. Attempting reset: Succeeded.
omm=# create table KHZY1(id bigint primary key, name varchar(50) not null,age int);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index “khzy1_pkey” for table “khzy1”
CREATE TABLE
omm=# insert into khzy1 values(1,‘user1’,46);
INSERT 0 1
omm=# select * from khzy1;
id | name | age
-~—±------±----
1 | user1 | 46
(1 row)
omm=# \d khzy1
Table “public.khzy1”
Column | Type | Modifiers
--------±----------------------±----------
id | bigint | not null
name | character varying(50) | not null
age | integer |
Indexes:
“khzy1_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
2.创建表的时候定义表级约束
|
omm=# drop table if exists khzy2;
NOTICE: table “khzy2” does not exist, skipping
DROP TABLE
omm=# create table khzy2(id bigint,name varchar(50) not null, age int, primary key(id) );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index “khzy2_pkey” for table “khzy2”
CREATE TABLE
omm=# insert into khzy2 values(1,‘user1’,46);
INSERT 0 1
omm=# select * from khzy2;
id | name | age
----±------±----
1 | user1 | 46
(1 row)
omm=# \d khzy2
omm=# Table “public.khzy2”
Column | Type | Modifiers
--------±----------------------±----------
id | bigint | not null
name | character varying(50) | not null
age | integer |
Indexes:
“khzy2_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
3.为表的属性定义默认值
|
omm=# drop table if exists khzy3;
NOTICE: table “khzy3” does not exist, skipping
DROP TABLE
omm=# create table khzy3(id bigint,name varchar(28) not null,age int default 46, primary key(id));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index “khzy3_pkey” for table “khzy3”
CREATE TABLE
omm=# insert into khzy3(id,name) values(1,‘user1’);
INSERT 0 1
omm=# insert into khzy3(id,name) values(2,‘user2’);
INSERT 0 1
omm=# select * from khzy3;
(2 rows)
omm=# id | name | age
----±------±----
1 | user1 | 46
2 | user2 | 46
omm=# \d khzy3
Table “public.khzy3”
Column | Type | Modifiers
--------±----------------------±-----------
age | integer | default 46
Indexes:
“khzy3_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=# id | bigint | not null
name | character varying(28) | not null
omm=#
|
4.如果在创建表的时候,没有为某列定义默认值,缺省的默认值是空值null
|
omm=# drop table if exists khzy4;
NOTICE: table “khzy4” does not exist, skipping
DROP TABLE
omm=# create table khzy4( id bigint,name varchar(50) not null,age int, primary key(id));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index “khzy4_pkey” for table “khzy4”
CREATE TABLE
omm=# insert into khzy4(id,name) values(1,‘user1’);
INSERT 0 1
omm=# select * from khzy4;
omm=# id | name | age
----±------±----
1 | user1 |
(1 row)
omm=# \d khzy4
Table “public.khzy4”
Column | Type | Modifiers
--------±----------------------±----------
id | bigint | not null
name | character varying(50) | not null
age | integer |
Indexes:
“khzy4_pkey” PRIMARY KEY, btree (id) TABLESPACE pg_default
omm=#
|
5.创建表时使用自增数据类型
|
omm=# drop table if exists khzy5;
NOTICE: table “khzy5” does not exist, skipping
DROP TABLE
omm=# create table khzy5(id serial NOT NULL,name varchar(20));
NOTICE: CREATE TABLE will create implicit sequence “khzy5_id_seq” for serial column “khzy5.id”
CREATE TABLE
omm=# insert into khzy5(name) values(‘user1’);
INSERT 0 1
omm=# insert into khzy5(name) values(‘user2’);
INSERT 0 1
omm=# insert into khzy5(name) values(‘user3’);
INSERT 0 1
omm=#
omm=# select * from khzy5;
id | name
----±------
1 | user1
2 | user2
3 | user3
(3 rows)
omm=# \d khzy5
Table “public.khzy5”
Column | Type | Modifiers
--------±----------------------±---------------------------------------------------
id | integer | not null default nextval(‘khzy5_id_seq’::regclass)
name | character varying(20) |
omm=#
|
6.使用现有的表创建新表
|
omm=# drop table if exists khzy61;
DROP TABLE
omm=# create table khzy61 as select * from khzy5;
INSERT 0 3
omm=# select * from khzy61;
id | name
----±------
1 | user1
2 | user2
3 | user3
(3 rows)
omm=# drop table if exists khzy62;
NOTICE: table “khzy62” does not exist, skipping
DROP TABLE
omm=# create table khzy62 as select * from khzy5 where 1=2;
INSERT 0 0
omm=# select * from khzy62;
id | name
----±-----
(0 rows)
omm=#
|
总结
通过第14天的学习,了解了表的约束、表的默认值、自增类型等技术。