打卡第十四天,表的约束、表的默认值、自增类型操作
- 列级约束\表级约束
在列级定义了primary key约束(id列)和not null约束(name列)
create table test(
id bigint primary key,
name varchar(50) not null,
age int
);表列级定义了primary key约束(id列),在列级定义了not null约束(name列)
create table test001(
id bigint,
name varchar(50) not null, -- 创建列级not null约束
age int,
primary key(id) -- 创建表级约束
);- 表的属性定义默认值
创建表的时候为表的某个列定义默认值
create table test002(
id bigint,
name varchar(28) not null,
age int default 20, -- 为该列定义默认值为20
primary key(id)
);- 自增数据类型
create table invoice(invoicenum serial NOT NULL,name varchar(20));自增列已有某数值时,自增数据会跳过该值。
课程作业
1.创建表的时候定义列级约束
omm=# create table tt (col char(20) primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "tt_pkey" for table "tt"
CREATE TABLE
omm=# 2.创建表的时候定义表级约束
omm=# create table t2 (id int, primary key(id));
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t2_pkey" for table "t2"
CREATE TABLE
omm=# 3.为表的属性定义默认值
omm=# create table t3 (id int default 30);
CREATE TABLE
omm=# 4.如果在创建表的时候,没有为某列定义默认值,缺省的默认值是空值null
omm=# create table t4 (id int,age int);
CREATE TABLE
omm=# insert into t4 values(1);
INSERT 0 1
omm=# select * from t4;
id | age
----+-----
1 |
(1 row)
omm=# 5.创建表时使用自增数据类型
omm=# create table t5(id serial not null,col char(20));
NOTICE: CREATE TABLE will create implicit sequence "t5_id_seq" for serial column "t5.id"
CREATE TABLE
omm=# 6.使用现有的表创建新表
CREATE TABLE t6 AS SELECT * FROM t5 WHERE 1=2;「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




