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

openGauss每日一练第 14天|表管理2

原创 那纸忧伤 2022-12-12
247

实操作业

目标

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

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进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论

virvle
暂无图片
2年前
评论
暂无图片 0
21天剩下没多少了,坚持坚持,一起加油打卡
2年前
暂无图片 点赞
评论