学习目标
学习表的约束、表的默认值、自增类型等技术
课程学习
1.创建表的时候定义列级约束
-- 创建表的时候为表定义列级约束:在列级定义了primary key约束(id列)和not null约束(name列)。 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 test2.创建表的时候定义表级约束
--创建表时定义表级约束 执行下面的SQL语句,在创建表的时候为表定义表级约束: #这里在表列级定义了primary key约束(id列),在列级定义了not null约束(name列)。 drop table if exists test001; create table test001( id bigint, name varchar(50) not null, -- 创建列级not null约束 age int, primary key(id) -- 创建表级约束 ); insert into test001 values(1,'user1',50); select * from test001; \d test0013.为表的属性定义默认值
--执行下面的语句,在创建表的时候为表的某个列定义默认值: drop table if exists test002; create table test002( id bigint, name varchar(28) not null, age int default 20, -- 为该列定义默认值为20 primary key(id) ); --下面的SQL insert语句,在向表test插入数据时,没有提供age列的值: insert into test002(id,name) values(1,'user1'); insert into test002(id,name) values(2,'user2'); select * from test002;4.如果在创建表的时候,没有为某列定义默认值,缺省的默认值是空值null。
-- 未定义age列的默认值,如果插入数据时未提供该列的值,则将默认插入空值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;
- 创建表时使用自增数据类型
商品的编号通常按顺序递增。这种情况可以使用serial数据类型。最简单方法直接使用serial数据类型。-创建一个带有serial数据类型的测试表invoice: drop table if exists invoice; create table invoice(invoicenum serial NOT NULL,name varchar(20)); --为表invoice插入3条记录,并查看插入数据后的表的数据: insert into invoice(name) values('user1'); insert into invoice(name) values('user2'); insert into invoice(name) values('user3'); --可以看到每插入一条记录到表invoice后,列invoicenum的值会自增1。 select * from invoice;6.使用现有的表创建新表
--执行下面的SQL语句,将创建新表,并且会将旧表的数据拷贝给新表: DROP TABLE if exists newtestwithdata; CREATE TABLE newtestwithdata AS SELECT * FROM invoice; SELECT * FROM newtestwithdata; --执行下面的SQL语句,创建和旧表的表结构相同的新表,但是不会将旧表的数据拷贝给新表: DROP TABLE if exists testnewwithoutdata; CREATE TABLE testnewwithoutdata AS SELECT * FROM invoice WHERE 1=2; SELECT * FROM testnewwithoutdata;课程作业
1.创建表的时候定义列级约束
```sql
root@modb:~# su - omm omm@modb:~$ gsql -r gsql ((openGauss 3.0.0 build 02c14696) compiled at 2022-04-01 18:12:00 commit 0 last mr ) Non-SSL connection (SSL connection is recommended when requiring high-security) Type "help" for help. omm=# creae tablespace apptbs relative location 'tablespace/apptbs' tablespace apptbs relative locatioomm=# create tablespace apptbs relative location 'tablespace/apptbs'; CREATE TABLESPACE omm=# create database appdb with tablespace=apptbs; CREATE DATABASE omm=# create user user1 identified by 'kunpeng@1234'; NOTICE: The encrypted password contains MD5 ciphertext, which is not secure. CREATE ROLE omm=# alter user user1 sysadmin; ALTER ROLE omm=# create table test( id integer primary key, name char(20) not null, age int); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE omm=# insert into test values (1,'tom',20); INSERT 0 1 omm=# select * from test; (1 row) omm=# id | name | age ----+----------------------+----- 1 | tom | 20 omm=# \d test Table "public.test" Column | Type | Modifiers --------+---------------+----------- id | integer | not null name | character(20) | not null age | integer | Indexes: "test_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default
```
2.创建表的时候定义表级约束
```sql omm=# create table test1 (id int,name char(30) not null, age int, primary key(i NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test1_pkey" for table "test1" CREATE TABLE omm=# insert into test1 values (2,'jim',33); INSERT 0 1 omm=# select * from test1; id | name | age ----+--------------------------------+----- 2 | jim | 33 (1 row) omm=# \d test1 omm=# Table "public.test1" Column | Type | Modifiers --------+---------------+----------- id | integer | not null name | character(30) | not null age | integer | Indexes: "test1_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default ```
3.为表的属性定义默认值
```sql omm=# create table test2 (id int ,name char(30) not null,age int default 20,primary key(id)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test2_pkey" for table "test2" CREATE TABLE omm=# insert into test2(id ,name) values (3,'jik'); INSERT 0 1 omm=# select * from test2; id | name | age ----+--------------------------------+----- 3 | jik | 20 (1 row) omm=# \d test2 Table "public.test2" Column | Type | Modifiers --------+---------------+------------ id | integer | not null name | character(30) | not null age | integer | default 20 Indexes: "test2_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default ```
4.如果在创建表的时候,没有为某列定义默认值,缺省的默认值是空值null
```sql omm=# create table test3 (id int ,name char(39) not null,age int ,primary key(id)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test3_pkey" for table "test3" CREATE TABLE omm=# insert into test3(id ,name) values (4,'tomm'); INSERT 0 1
omm=# select * from test3; id | name | age ----+-----------------------------------------+----- 4 | tomm | (1 row) omm=# \d test3 Table "public.test3" Column | Type | Modifiers --------+---------------+----------- id | integer | not null name | character(39) | not null age | integer | Indexes: "test3_pkey" PRIMARY KEY, btree (id) TABLESPACE pg_default ```
5.创建表时使用自增数据类型
```sql omm=# create table test4( id serial not null,name char(30)); NOTICE: CREATE TABLE will create implicit sequence "test4_id_seq" for serial column "test4.id" CREATE TABLE omm=# insert into test4(name)values('tom1'); INSERT 0 1 omm=# insert into test4(name)values('tom2'); INSERT 0 1 omm=# insert into test4(name)values('tom3'); INSERT 0 1 omm=# select * from test4; 2 | tom2 3 | tom3 (3 rows) omm=# id | name ----+-------------------------------- 1 | tom1 omm=# omm=# \d test4; Table "public.test4" Column | Type | Modifiers --------+---------------+---------------------------------------------------- id | integer | not null default nextval('test4_id_seq'::regclass) name | character(30) | ```
6.使用现有的表创建新表
```sql omm=# create table test5 as select * from test4; INSERT 0 3 omm=# select * from test5; id | name ----+-------------------------------- 1 | tom1 2 | tom2 3 | tom3 (3 rows) omm=# \d test5; Table "public.test5" Column | Type | Modifiers --------+---------------+----------- id | integer | name | character(30) | ```




