实操作业
目标
学习表的约束、表的默认值、自增类型等技术
1.创建表的时候定义列级约束
drop table if exists t1;
create table t1(
id int primary key, --定义主键约束
name varchar(10) not null --定义非空约束
);
insert into t1 values (9466,'张三');
select * from t1;
enmdb=> \d+ t1
Table "public.t1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
id | integer | not null | plain | |
name | character varying(10) | not null | extended | |
Indexes:
"t1_pkey" PRIMARY KEY, btree (id) TABLESPACE tmatbs_test
Has OIDs: no
Options: orientation=row, compression=no复制
2.创建表的时候定义表级约束
drop table if exists t11;
create table t11(
id int ,
name varchar(10) not null, --定义非空约束
primary key(id) --创建表级约束
);
insert into t11 values (94666,'张三');
select * from t11;
enmdb=> \d+ t11
Table "public.t11"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
id | integer | not null | plain | |
name | character varying(10) | not null | extended | |
Indexes:
"t11_pkey" PRIMARY KEY, btree (id) TABLESPACE tmatbs_test
Has OIDs: no
Options: orientation=row, compression=no复制
3.为表的属性定义默认值
drop table if exists t2;
create table t2(
id int4 ,
name varchar(10) not null, --定义列级非空约束
age int default 18, --定义列级默认值
primary key(id) --创建表级约束
);
insert into t2 values (94666666,'张三');
enmdb=> select * from t2;
id | name | age
----------+--------+-----
94666666 | 张三 | 18
(1 row)
enmdb=> \d+ t2
Table "public.t2"
Column | Type | Modifiers | Storage | Stats target | Description
enmdb=> --------+-----------------------+------------+----------+--------------+-------------
id | integer | not null | plain | |
name | character varying(10) | not null | extended | |
age | integer | default 18 | plain | |
Indexes:
"t2_pkey" PRIMARY KEY, btree (id) TABLESPACE tmatbs_test
Has OIDs: no
Options: orientation=row, compression=no复制
4.如果在创建表的时候,没有为某列定义默认值,缺省的默认值是空值null
drop table if exists t22;
create table t22(
id int4 ,
name varchar(10) not null, --定义列级非空约束
age int , --定义列级默认值
primary key(id) --创建表级约束
);
insert into t22 values (946946946,'张三');
enmdb=> select * from t22;
id | name | age
-----------+--------+-----
946946946 | 张三 |
(1 row)复制
5.创建表时使用自增数据类型
drop table if exists t3;
create table t3(
stu_id serial,
name varchar(10) not null, --定义列级非空约束
age int , --定义列级默认值
primary key(stu_id) --创建表级约束
);
insert into t3(name,age) values ('张三',20);
insert into t3(name,age) values ('张三2',22);
insert into t3(name,age) values ('张三3',23);
enmdb=> select * from t3;
stu_id | name | age
--------+---------+-----
1 | 张三 | 20
2 | 张三2 | 22
3 | 张三3 | 23
(3 rows)
--注:此处的serial类型默认是从1开始自增,如果想自定义自增的初始值,可以自定义序列,然后将自定义的序列应用到需要的字段即可复制
6.使用现有的表创建新表
--复制表结构及数据,
create table t33 as select * from t3;
enmdb=> \d t3
Table "public.t3"
Column | Type | Modifiers
--------+-----------------------+-----------------------------------------------------
stu_id | integer | not null default nextval('t3_stu_id_seq'::regclass)
name | character varying(10) | not null
age | integer |
Indexes:
"t3_pkey" PRIMARY KEY, btree (stu_id) TABLESPACE tmatbs_test
enmdb=> \d t33
Table "public.t33"
Column | Type | Modifiers
--------+-----------------------+-----------
stu_id | integer |
name | character varying(10) |
age | integer |
--复制表结构,不复制表数据
create table t333 as select * from t3 where 1=2;
--注:使用此语法建表,不保留原表上定义的任何对象(如约束、索引等)复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
21天剩下没多少了,坚持坚持,一起加油打卡
2年前

评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
555次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
308次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
201次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
172次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
162次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
151次阅读
2025-04-30 16:39:58
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
96次阅读
2025-04-18 10:49:53
Postgresql数据库单个Page最多存储多少行数据
maozicb
94次阅读
2025-04-23 16:02:19
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
66次阅读
2025-04-17 10:41:47
RISC-V 首迎 openGauss 7.0.0-RC1 全量版适配!数据库核心功能完整落地开源架构
openGauss
49次阅读
2025-04-16 10:33:59