课程作业
1.创建表的时候定义列级约束
-- 创建表的时候为表定义列级约束:在列级定义了primary key约束(id列)和not null约束(name列)。 omm=# omm=# drop table if exists test; NOTICE: table "test" does not exist, skipping DROP TABLE omm=# create table test( omm(# id bigint primary key, omm(# name varchar(50) not null, omm(# omm(# ); age int NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE omm=# omm=# --插入数据 insert into test values(1,'user1',50); INSERT 0 1 omm=# --查看数据 omm=# select * from test; id | name | age ----+-------+----- 1 | user1 | 50 (1 row) omm=# --查看约束 omm=# \d test Table "public.test" Column | Type | Modifiers --------+-----------------------+----------- id | bigint | not null name | character varying(50) | not null age | integer | Indexes: "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default omm=#
复制
2.创建表的时候定义表级约束
omm=# drop table if exists test001; NOTICE: table "test001" does not exist, skipping DROP TABLE omm=# create table test001( omm(# id bigint, omm(# name varchar(50) not null, -- 创建列级not null约束 omm(# age int, omm(# primary key(id) -- 创建表级约束 omm(# ); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test001_pkey" for table "test001" CREATE TABLE omm=# insert into test001 values(1,'user1',50); INSERT 0 1 omm=# select * from test001; id | name | age ----+-------+----- 1 | user1 | 50 (1 row) omm=# \d test001 Table "public.test001" Column | Type | Modifiers --------+-----------------------+----------- id | bigint | not null name | character varying(50) | not null age | integer | Indexes: "test001_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default omm=#
复制
3.为表的属性定义默认值
omm=# drop table if exists test002; NOTICE: table "test002" does not exist, skipping DROP TABLE omm=# create table test002( omm(# id bigint, omm(# name varchar(28) not null, omm(# ); age int default 20, -- 为该列定义默认值为20 omm(# primary key(id) omm(# insert into test002(id,name) values(1,'user1'); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test002_pkey" for table "test002" CREATE TABLE INSERT 0 1 omm=# insert into test002(id,name) values(2,'user2'); INSERT 0 1 omm=# omm=# select * from test002; (2 rows) omm=# id | name | age ----+-------+----- 1 | user1 | 20 2 | user2 | 20
复制
4.如果在创建表的时候,没有为某列定义默认值,缺省的默认值是空值null
-- 未定义age列的默认值,如果插入数据时未提供该列的值,则将默认插入空值null omm=# drop table if exists test; DROP TABLE omm=# create table test( omm(# id bigint, omm(# omm(# name varchar(50) not null, age int, omm(# primary key(id) omm(# ); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE omm=# insert into test(id,name) values(1,'user1'); INSERT 0 1 omm=# select * from test; id | name | age ----+-------+----- 1 | user1 | (1 row) omm=#
复制
5.创建表时使用自增数据类型
omm=# drop table if exists invoice; NOTICE: table "invoice" does not exist, skipping DROP TABLE --创建一个带有serial数据类型的测试表invoice: omm=# create table invoice(invoicenum serial NOT NULL,name varchar(20)); NOTICE: CREATE TABLE will create implicit sequence "invoice_invoicenum_seq" for serial column "invoice.invoicenum" omm=# CREATE TABLE omm=# insert into invoice(name) values('user1'); INSERT 0 1 omm=# insert into invoice(name) values('user2'); INSERT 0 1 omm=# insert into invoice(name) values('user3'); INSERT 0 1 --可以看到每插入一条记录到表invoice后,列invoicenum的值会自增1。 omm=# select * from invoice; invoicenum | name ------------+------- 1 | user1 2 | user2 3 | user3 (3 rows) omm=#
复制
6.使用现有的表创建新表
--执行下面的SQL语句,将创建新表,并且会将旧表的数据拷贝给新表: omm=# DROP TABLE if exists newtestwithdata; NOTICE: table "newtestwithdata" does not exist, skipping omm=# DROP TABLE CREATE TABLE newtestwithdata AS SELECT * FROM invoice; INSERT 0 3 omm=# SELECT * FROM newtestwithdata; (3 rows) omm=# invoicenum | name ------------+------- 1 | user1 2 | user2 3 | user3 --执行下面的SQL语句,创建和旧表的表结构相同的新表,但是不会将旧表的数据拷贝给新表: omm=# DROP TABLE if exists testnewwithoutdata; NOTICE: table "testnewwithoutdata" does not exist, skipping DROP TABLE omm=# CREATE TABLE testnewwithoutdata AS SELECT * FROM invoice WHERE 1=2; INSERT 0 0 omm=# SELECT * FROM testnewwithoutdata; invoicenum | name ------------+------ (0 rows) omm=#
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论

2年前

评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
595次阅读
2025-04-03 15:21:16
内蒙古公司成功完成新一代BOSS云原生系统割接上线
openGauss
199次阅读
2025-03-24 09:40:40
第4期 openGauss 中级认证OGCP直播班招生中!3月30日开课
墨天轮小教习
161次阅读
2025-03-17 15:48:40
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
143次阅读
2025-04-01 12:27:03
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
123次阅读
2025-04-01 10:30:07
从数据库源码比较 PostgreSql和OpenGauss的启动过程
maozicb
84次阅读
2025-03-24 15:55:04
一文快速上手openGauss
进击的CJR
80次阅读
2025-03-26 16:12:54
openGauss HASH JOIN原理
lbsswhu
61次阅读
2025-03-18 10:45:01
openGauss 学习之路:集群部署实战探索
openGauss
54次阅读
2025-03-21 10:34:13
openGauss问题记录:开启备机归档且备机stop情况下,执行gs_probackup失败
zym
45次阅读
2025-03-18 19:06:13