openGauss每日一练第14天
学习表管理2
学习表的约束、表的默认值、自增类型等技术,完成课后作业
创建表t1并定义列级约束
在omm用户下通过gsql -r连接数据库后创建表并设置列级约束,使用元命令\d <tablename>查看列约束信息
omm=# create table t1(id bigint primary key,name char(100)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1" CREATE TABLE omm=# \d t1 Table "testsm.t1" Column | Type | Modifiers --------+----------------+----------- id | bigint | not null name | character(100) |
重建t1表并定义表记约束
omm=# drop table if exists t1;
DROP TABLE
omm=# create table t1(
omm(# id bigint,
omm(# name varchar(50) not null,
omm(# age int,
omm(# primary key(id)
omm(# );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "t1"
CREATE TABLE
此时primary key的id为表级的约束
重建t1表为age列定义默认值
omm=# drop table if exists t1;
DROP TABLE
omm=# create table test002(
omm(# id bigint,
omm(# name varchar(28) not null,
omm(# age int default 20,
omm(# primary key(id)
omm(# );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t1_pkey" for table "test002"
CREATE TABLE
我们为age年龄表定义了默认值20,在插入数据时age为空的情况下,会被设置为20
创建invoice表并携带自增数据列invoicenum
omm=# create table invoice(invoicenum serial NOT NULL); NOTICE: CREATE TABLE will create implicit sequence "invoice_invoicenum_seq" for serial column "invoice.invoicenum" CREATE TABLE
serial代表就是自增数据类型,每插入数据时,invoicenum都会自增加一
使用t1表创建t2表
omm=# create table t2 as select * from t1; INSERT 0 0 omm=# \d t2 Table "testsm.t2" Column | Type | Modifiers --------+-----------------------+----------- id | bigint | name | character varying(50) | age | integer
可以看到通过t1表创建出来的t2表它们的结构都一样。
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




