当前位置: 首页 > news >正文

爱站网源码如何建立自己的网络销售

爱站网源码,如何建立自己的网络销售,做语音聊天网站要多少钱,国际网站 建设文章目录 openGauss学习笔记-26 openGauss 高级数据管理-约束26.1 NOT NULL约束26.2 UNIQUE约束26.3 PRIMARY KEY26.4 FOREIGN KEY26.5 CHECK约束 openGauss学习笔记-26 openGauss 高级数据管理-约束 约束子句用于声明约束,新行或者更新的行必须满足这些约束才能成…

文章目录

    • openGauss学习笔记-26 openGauss 高级数据管理-约束
      • 26.1 NOT NULL约束
      • 26.2 UNIQUE约束
      • 26.3 PRIMARY KEY
      • 26.4 FOREIGN KEY
      • 26.5 CHECK约束

openGauss学习笔记-26 openGauss 高级数据管理-约束

约束子句用于声明约束,新行或者更新的行必须满足这些约束才能成功插入或更新。如果存在违反约束的数据行为,行为会被约束终止。

约束可以在创建表时规定(通过 CREATE TABLE 语句),或者在表创建之后规定(通过 ALTER TABLE 语句)。

约束可以是列级或表级。列级约束仅适用于列,表级约束被应用到整个表。

openGauss中常用的约束如下:

  • NOT NULL:指示某列不能存储NULL值。
  • UNIQUE:确保某列的值都是唯一的。
  • PRIMARY KEY:NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
  • FOREIGN KEY: 保证一个表中的数据匹配另一个表中的值的参照完整性。
  • CHECK: 保证列中的值符合指定的条件。

26.1 NOT NULL约束

创建表时,如果不指定约束,默认值为NULL,即允许列插入空值。如果您不想某列存在NULL值,那么需要在该列上定义NOT NULL约束,指定在该列上的值不允许存在NULL值。插入数据时,如果该列存在NULL值,则会报错,插入失败。

NULL与没有数据是不一样的,它代表着未知的数据。

例如,创建表staff,共有5个字段,其中NAME,ID设置不接受空值。

openGauss=# CREATE TABLE staff(ID             INT      NOT NULL,NAME           char(8)    NOT NULL,AGE            INT     ,ADDRESS        CHAR(50),SALARY         REAL
);

给表staff插入数据。当ID字段插入空值时,数据库返回报错。

openGauss=# INSERT INTO staff  VALUES (1,'lily',28);
INSERT 0 1
openGauss=# INSERT INTO staff (NAME,AGE) VALUES ('JUCE',28);
ERROR:  null value in column "id" violates not-null constraint
DETAIL:  Failing row contains (null, JUCE    , 28, null, null).

26.2 UNIQUE约束

UNIQUE约束表示表里的一个字段或多个字段的组合必须在全表范围内唯一。

对于唯一约束,NULL被认为是互不相等的。

例如,创建表staff1,表包含5个字段,其中AGE设置为UNIQUE,因此不能添加两条有相同年龄的记录。

openGauss=# CREATE TABLE staff1(ID             INT      NOT NULL,NAME           char(8)    NOT NULL,AGE            INT   NOT NULL  UNIQUE  ,ADDRESS        CHAR(50),SALARY         REAL
);

给表staff1表插入数据。当字段AGE插入两条一样的数据时,数据库返回报错。

openGauss=# INSERT INTO staff1  VALUES (1,'lily',28);
INSERT 0 1
openGauss=# INSERT INTO staff1 VALUES (2, 'JUCE',28);
ERROR:  duplicate key value violates unique constraint "staff1_age_key"
DETAIL:  Key (age)=(28) already exists.

26.3 PRIMARY KEY

PRIMARY KEY为主键,是数据表中每一条记录的唯一标识。主键约束声明表中的一个或者多个字段只能包含唯一的非NULL值。

主键是非空约束和唯一约束的组合。一个表只能声明一个主键。

例如,创建表staff2,其中ID为主键。

openGauss=# CREATE TABLE staff2(ID             INT     PRIMARY KEY     ,NAME           TEXT    NOT NULL,AGE            INT     NOT NULL,ADDRESS        CHAR(50),SALARY         REAL
);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "staff2_pkey" for table "staff2"
CREATE TABLE

26.4 FOREIGN KEY

FOREIGN KEY即外键约束,指定列(或一组列)中的值必须匹配另一个表的某一行中出现的值。通常一个表中的FOREIGN KEY指向另一个表中的 UNIQUE KEY(唯一约束的键),即维护了两个相关表之间的引用完整性。

例如,创建表staff3,包含5个字段。

openGauss=# CREATE TABLE staff3(ID             INT    PRIMARY KEY  NOT NULL,NAME           TEXT    NOT NULL,AGE            INT     NOT NULL,ADDRESS        CHAR(50),SALARY         REAL
);

创建一张DEPARTMENT表,并添加3个字段,其中EMP_ID为外键,参照staff3的ID字段:

openGauss=# CREATE TABLE DEPARTMENT(ID INT PRIMARY KEY      NOT NULL,DEPT           CHAR(50) NOT NULL,EMP_ID         INT      references staff3(ID)
);

FOREIGN Key在MySQL兼容性下,外键可以关联非唯一性索引。即一个表中的FOREIGN Key指向另一个表中的 Non-unique KEY(非唯一约束的键)。 注: 如果在MySQL兼容性下,定义外键指定ON UPDATE | DELETE CASCADE时,在非唯一性索引中,非唯一索引字段存在多个元组时,只要dml其中一行数据,则会触发外键表里关联的字段全部修改。但如果字段为NULL时,则不触发外键关联的字段做对应的修改。

MySQL兼容性需要安装dolphin插件才可生效。

openGauss=# create table t1(id int, name varchar);
CREATE TABLE
openGauss=# create table t2(id int, a_id int);
CREATE TABLE
-- create non-unique index on table t1.
openGauss=# create index a_index_1 on t1(id);
CREATE INDEX
-- create foreign key on non-unique index
openGauss=# alter table t2 add constraint t2_fk foreign key (a_id) references t1(id);
ALTER TABLE
openGauss=# \d t1Table "public.t1"Column |       Type        | Modifiers 
--------+-------------------+-----------id     | integer           | name   | character varying | 
Indexes:"a_index_1" btree (id) TABLESPACE pg_default
Referenced by:TABLE "t2" CONSTRAINT "t2_fk" FOREIGN KEY (a_id) REFERENCES t1(id)openGauss=# \d t2Table "public.t2"Column |  Type   | Modifiers 
--------+---------+-----------id     | integer | a_id   | integer | 
Foreign-key constraints:"t2_fk" FOREIGN KEY (a_id) REFERENCES t1(id)openGauss=# insert into t1 values(1,'a'),(2,'b');
INSERT 0 2
openGauss=# select * from t1;id | name 
----+------1 | a2 | b
(2 rows)openGauss=# insert into t2 values(1,1);
INSERT 0 1
openGauss=# select * from t2;id | a_id 
----+------1 |    1
(1 row)openGauss=# insert into t2 values(1,3);
INSERT 0 1
ERROR:  insert or update on table "t2" violates foreign key constraint "t2_fk"
DETAIL:  Key (a_id)=(3) is not present in table "t1".
openGauss=# select * from t2;id | a_id 
----+------1 |    1
(1 row)openGauss=# alter table t2 drop constraint t2_fk;
ALTER TABLE
openGauss=# alter table t2 add constraint t2_fk foreign key (a_id) references t1(id) on update cascade;
ALTER TABLE
openGauss=# select * from t1;id | name 
----+------1 | a2 | b
(2 rows)openGauss=# insert into t1 values(1,'s');
INSERT 0 1
openGauss=# select * from t1;id | name 
----+------1 | a2 | b1 | s
(3 rows)openGauss=# insert into t2 values(2,1);
INSERT 0 1
openGauss=# select * from t2;id | a_id 
----+------1 |    12 |    1
(2 rows)openGauss=# update t1 set id = 11 where name = 'a';
UPDATE 1
openGauss=# select * from t1;id | name 
----+------2 | b1 | s11 | a
(3 rows)openGauss=# select * from t2;id | a_id 
----+------1 |   112 |   11
(2 rows)openGauss=# update t1 set id =1 where name = 'a';
UPDATE 1
openGauss=# alter table t2 drop constraint t2_fk;
ALTER TABLE
openGauss=# alter table t2 add constraint t2_fk foreign key (a_id) references t1(id) on delete cascade;
ALTER TABLE
openGauss=# select * from t1;id | name 
----+------2 | b1 | s1 | a
(3 rows)openGauss=# select * from t2;id | a_id 
----+------1 |    12 |    1
(2 rows)openGauss=# delete from t1 where name = 's';
DELETE 1
openGauss=# select * from t1;id | name 
----+------2 | b1 | a
(2 rows)openGauss=# select * from t2;id | a_id 
----+------
(0 rows)

26.5 CHECK约束

CHECK约束声明一个布尔表达式,每次要插入的新行或者要更新的行的新值必须使表达式结果为真或未知才能成功,否则会抛出一个异常并且不会修改数据库。

声明为字段约束的检查约束应该只引用该字段的数值,而在表约束里出现的表达式可以引用多个字段。expression表达式中,如果存在“<>NULL”或“!=NULL”,这种写法是无效的,需要写成“is NOT NULL”。

例如,创建表staff4,对字段SALARY新增CHECK约束,确保插入此列数值大于0。

openGauss=# CREATE TABLE staff4(ID INT PRIMARY KEY     NOT NULL,NAME           TEXT    NOT NULL,AGE            INT     NOT NULL,ADDRESS        CHAR(50),SALARY         REAL    CHECK(SALARY > 0)
);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "staff4_pkey" for table "staff4"
CREATE TABLE

给表staff4插入数据。当字段SALARY插入数据不大于0时,数据库返回报错。

openGauss=# INSERT INTO staff4(ID,NAME,AGE,SALARY) VALUES (2, 'JUCE',16,0);
ERROR:  new row for relation "staff4" violates check constraint "staff4_salary_check"
DETAIL:  N/A

👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!

图片


文章转载自:
http://thermoelectrometer.bpcf.cn
http://pleochromatic.bpcf.cn
http://heterotrophe.bpcf.cn
http://incorporated.bpcf.cn
http://devotional.bpcf.cn
http://aquiform.bpcf.cn
http://apostasy.bpcf.cn
http://flakily.bpcf.cn
http://legitimatize.bpcf.cn
http://senegal.bpcf.cn
http://offspeed.bpcf.cn
http://cyclometer.bpcf.cn
http://tharm.bpcf.cn
http://phosphoglucomutase.bpcf.cn
http://solarism.bpcf.cn
http://trivet.bpcf.cn
http://loxodromy.bpcf.cn
http://vxd.bpcf.cn
http://keten.bpcf.cn
http://obverse.bpcf.cn
http://thunderclap.bpcf.cn
http://autokinetic.bpcf.cn
http://anlage.bpcf.cn
http://somersault.bpcf.cn
http://subside.bpcf.cn
http://hofei.bpcf.cn
http://inherently.bpcf.cn
http://liechtensteiner.bpcf.cn
http://lacunaris.bpcf.cn
http://aduncal.bpcf.cn
http://lp.bpcf.cn
http://amniotic.bpcf.cn
http://hazy.bpcf.cn
http://jammer.bpcf.cn
http://haulier.bpcf.cn
http://regretful.bpcf.cn
http://morphological.bpcf.cn
http://fumitory.bpcf.cn
http://ionogram.bpcf.cn
http://zymoscope.bpcf.cn
http://insultingly.bpcf.cn
http://tympanic.bpcf.cn
http://iv.bpcf.cn
http://hardicanute.bpcf.cn
http://congresswoman.bpcf.cn
http://hercynian.bpcf.cn
http://beaux.bpcf.cn
http://whys.bpcf.cn
http://otto.bpcf.cn
http://dispute.bpcf.cn
http://blagoveshchensk.bpcf.cn
http://thanks.bpcf.cn
http://fraught.bpcf.cn
http://etherization.bpcf.cn
http://counterboy.bpcf.cn
http://ticktack.bpcf.cn
http://palp.bpcf.cn
http://pira.bpcf.cn
http://rescinnamine.bpcf.cn
http://these.bpcf.cn
http://slight.bpcf.cn
http://cnidoblast.bpcf.cn
http://aminophylline.bpcf.cn
http://ferule.bpcf.cn
http://exophthalmos.bpcf.cn
http://flanerie.bpcf.cn
http://extragovernmental.bpcf.cn
http://dare.bpcf.cn
http://lacily.bpcf.cn
http://haulier.bpcf.cn
http://reload.bpcf.cn
http://savorily.bpcf.cn
http://quasiparticle.bpcf.cn
http://cultivate.bpcf.cn
http://slavey.bpcf.cn
http://kawasaki.bpcf.cn
http://smudginess.bpcf.cn
http://aerocraft.bpcf.cn
http://evaginable.bpcf.cn
http://hairpin.bpcf.cn
http://pocketbook.bpcf.cn
http://subluxation.bpcf.cn
http://decane.bpcf.cn
http://omnitude.bpcf.cn
http://jumbotron.bpcf.cn
http://pearly.bpcf.cn
http://toad.bpcf.cn
http://official.bpcf.cn
http://questor.bpcf.cn
http://augmentation.bpcf.cn
http://polyamide.bpcf.cn
http://ecchymosis.bpcf.cn
http://parfocal.bpcf.cn
http://unfettered.bpcf.cn
http://chipmuck.bpcf.cn
http://podsolization.bpcf.cn
http://bronchogenic.bpcf.cn
http://securities.bpcf.cn
http://greenlandic.bpcf.cn
http://hippie.bpcf.cn
http://www.15wanjia.com/news/97144.html

相关文章:

  • 山东济南最新疫情爆发seo优化推广工程师
  • 网站百度流量怎么做如何快速推广app
  • 网站开发助理工程师win10系统优化工具
  • 重庆哪里可以学习网站建设和维护有什么平台可以推广
  • 手机网站如何做长春模板建站代理
  • vps 网站攻击ip地址it培训学校it培训机构
  • 手机游戏网站模板seo综合优化公司
  • 食品网站建设需求分析网站开发公司排名
  • 网站制作建设有哪些免费宣传平台
  • 要制作自己的网站需要什么惠城网站设计
  • 黑客怎么攻击网站交易链接大全
  • 淮安市建设工程安全监督站网站互联网全媒体广告代理
  • 网站架构方案宁波seo网络推广软件系统
  • 外贸国际站有哪些平台星巴克网络营销案例分析
  • 网络工程毕设做网站今日新闻消息
  • 互联网技术seo 优化公司
  • 邯郸企业网站团队提高工作效率的工具
  • 网站建设服务商有哪些seoheuni
  • 宁波 住房和建设局网站首页公关策划公司
  • 淘宝客做二级域名网站网站注册地址查询
  • 网站诊断案例赣州网站建设公司
  • 网上商店网站设计知乎关键词优化软件
  • 房产政策最新消息广州网站排名优化报价
  • b2b商城网站资深seo顾问
  • 佛山网站建设佛山网站制作济源网络推广
  • 网站建设项目分析报告百度上广告怎么搞上去的
  • 重庆智能网站建设设计seo网络优化专员是什么意思
  • 微信外卖小程序加盟排名优化哪家专业
  • 关于推进政府网站集约化建设的通知品牌网站建设公司
  • wordpress接单修改任务推推蛙贴吧优化