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

上海松江区做网站公司百度明令禁止搜索的词

上海松江区做网站公司,百度明令禁止搜索的词,网站模板助手,做网站建设赚钱吗文章目录 约束(constraint)列级约束和表级约束给约束起名字(constraint)非空约束(no null)检查约束(check)唯一性约束 (unique)主键约束 (primary key)主键分类单一主键复合主键主键自增 (auto_increment) 外键约束外什…

文章目录

  • 约束(constraint)
    • 列级约束和表级约束
    • 给约束起名字(constraint)
    • 非空约束(no null)
    • 检查约束(check)
    • 唯一性约束 (unique)
    • 主键约束 (primary key)
      • 主键分类
      • 单一主键
      • 复合主键
      • 主键自增 (auto_increment)
    • 外键约束
      • 外什么用外键
      • 级联删除
      • 级联更新
      • 级联置空
      • 注意点

约束(constraint)

列级约束和表级约束

列级约束

直接在字段后面是列级约束
create table test (id int 约束1 约束2.....,name varchar(20) 约束1 约束2....
);

表级约束

定义完,再约束是表记约束。比如联合不能重复的时候就要用到
create table test (id int,name varchar(20),//这里表示 id 和 name联合不能重复 unique(id, name)
);


给约束起名字(constraint)

create table test (id int,name varchar(20),//这里给 unique 起别名,方便删除 constraint 别名 unique(id, name)
);

约束的描述信息,在information_schema 库中的 table_constraints表中



非空约束(no null)

create table test (id int.name varchar(255) not nul;
);
//表示 name 字段不能为空


检查约束(check)

create table test (id int,name varchar(255),age int check(age > 18)
);
//表示 age 必须 大于 18


唯一性约束 (unique)

create table test(no int,name varchar(100),email varchar(100), unique(name, email)
);
//把 email 和 name 设置成联合唯一性的,不能插入重复的,除了NULL


主键约束 (primary key)

  • 主键约束的字段不能为NULL,并且不能重复。

  • 任何一张表都应该有主键,没有主键的表可以视为无效表。

  • 主键值是这行记录的唯一标识。在数据库表中即使两条数据一模一样,但由于主键值不同,我们也会认为是两条完全的不同的数据。


主键分类

  1. 根据字段数量分类:
    1. 单一主键(1个字段作为主键)【建议】
    2. 复合主键(2个或2个以上的字段作为主键)
  2. 根据业务分类:
    1. 自然主键(主键和任何业务都无关,只是一个单纯的自然数据)【建议】
    2. 业务主键(主键和业务挂钩,例如:银行卡账号作为主键)

单一主键

create table test (uid int primary key,nno varchar(100) unique,name varchar(100) not null
)
//把 uid 设置为主键

复合主键

create table test (uid int,nno varchar(100) unique,name varchar(100) not null,primary key(uid, nno)
)
//把 uid 和 nno 设置成联合主键

不建议使用


主键自增 (auto_increment)

主键自增:既然主键值是一个自然的数字,mysql为主键值提供了一种自增机制,不需要我们程序员维护,mysql自动维护该字段create table test (no int primary key auto_increment,name varchar(20)
);



外键约束

create table t_school( sno int primary key, sname varchar(255) 
); create table t_student( no int primary key, name varchar(255), age int, sno int, //constraint 是起别名,foreign key...references 是外键constraint t_school_sno_fk foreign key(sno) references t_school(sno) 
);
//外键不一定是主键,也可以是父表中具有唯一性的字段


外什么用外键

在这里插入图片描述
图片来源:动力节点

这种情况比较浪费空间直接两张表,然后设置外键


表一

在这里插入图片描述
图片来源:动力节点


表二

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
图片来源:动力节点



级联删除

创建子表时,外键可以添加:on delete cascade,这样在删除父表数据时,子表会级联删除。谨慎使用。create table t_student( no int primary key, name varchar(255), age int, sno int, constraint t_school_sno_fk foreign key(sno) references t_school(sno) on delete cascade 
);
###删除约束
alert table t_student drop foreign key t_student_sno_fk;
###添加约束
alert table t_student add constraint t_student_sno_fk foreign key(sno) references t_school(sno) on delete cascade; 


级联更新

创建子表时,外键可以添加:on update cascade ,这样在修改父表数据时,子表会级联修改,谨慎使用。create table t_student( no int primary key, name varchar(255), age int, sno int, constraint t_school_sno_fk foreign key(sno) references t_school(sno) on update cascade 
);


级联置空

创建子表时,外键可以添加:on delete set null,父数据设置成 null ,子表会级联设置为 null ,谨慎使用。create table t_student( no int primary key, name varchar(255), age int, sno int, constraint t_school_sno_fk foreign key(sno) references t_school(sno) on delete set null 
);


注意点

  • 添加了外键约束的字段中的数据必须来自其他字段,不能随便填。

  • 假设给a字段添加了外键约束,要求a字段中的数据必须来自b字段,b字段不一定是主键,但至少要有唯一性。

  • 外键约束可以给单个字段添加,叫做单一外键。也可以给多个字段联合添加,叫做复合外键。复合外键很少用。

a 表 如果引用 b 表 中的数据,可以把 b表 叫做 父表 ,把 a 表 叫做 子表。

  1. 创建表时,先创建父表,再创建子表。
  2. 插入数据时,先插入父表,在插入子表。
  3. 删除数据时,先删除子表,再删除父表。
  4. 删除表时,先删除子表,再删除父表。

插入就先父,删除先子


文章转载自:
http://sirius.tyjp.cn
http://vinegary.tyjp.cn
http://malacophyllous.tyjp.cn
http://frippery.tyjp.cn
http://musa.tyjp.cn
http://amorce.tyjp.cn
http://debate.tyjp.cn
http://fractus.tyjp.cn
http://psittacosis.tyjp.cn
http://daystart.tyjp.cn
http://hecatonchires.tyjp.cn
http://kora.tyjp.cn
http://goldilocks.tyjp.cn
http://caponata.tyjp.cn
http://rheobase.tyjp.cn
http://sunder.tyjp.cn
http://netminder.tyjp.cn
http://mohair.tyjp.cn
http://tridymite.tyjp.cn
http://prim.tyjp.cn
http://erratically.tyjp.cn
http://winding.tyjp.cn
http://unef.tyjp.cn
http://fashionmonger.tyjp.cn
http://torgoch.tyjp.cn
http://tedder.tyjp.cn
http://subversal.tyjp.cn
http://wellingtonian.tyjp.cn
http://depositor.tyjp.cn
http://surfmanship.tyjp.cn
http://litterbug.tyjp.cn
http://feterita.tyjp.cn
http://unassuming.tyjp.cn
http://piecewise.tyjp.cn
http://jewelly.tyjp.cn
http://sempster.tyjp.cn
http://brinkmanship.tyjp.cn
http://chinquapin.tyjp.cn
http://swathe.tyjp.cn
http://restock.tyjp.cn
http://muddiness.tyjp.cn
http://breadwinner.tyjp.cn
http://lor.tyjp.cn
http://winebibbing.tyjp.cn
http://cancered.tyjp.cn
http://bandeau.tyjp.cn
http://inconcinnity.tyjp.cn
http://budgeree.tyjp.cn
http://localize.tyjp.cn
http://legerdemain.tyjp.cn
http://lupine.tyjp.cn
http://orology.tyjp.cn
http://den.tyjp.cn
http://greece.tyjp.cn
http://inconnu.tyjp.cn
http://blackout.tyjp.cn
http://mandatory.tyjp.cn
http://assignation.tyjp.cn
http://decoy.tyjp.cn
http://petalon.tyjp.cn
http://cutcha.tyjp.cn
http://wren.tyjp.cn
http://venous.tyjp.cn
http://very.tyjp.cn
http://jacamar.tyjp.cn
http://anatoxin.tyjp.cn
http://methodenstreit.tyjp.cn
http://perfect.tyjp.cn
http://homocercality.tyjp.cn
http://landscapist.tyjp.cn
http://wreak.tyjp.cn
http://microcurie.tyjp.cn
http://le.tyjp.cn
http://binary.tyjp.cn
http://abac.tyjp.cn
http://regretful.tyjp.cn
http://cerement.tyjp.cn
http://lymphatolysis.tyjp.cn
http://cyme.tyjp.cn
http://treetop.tyjp.cn
http://lithontriptic.tyjp.cn
http://shakta.tyjp.cn
http://haematological.tyjp.cn
http://organically.tyjp.cn
http://ambidexterity.tyjp.cn
http://availability.tyjp.cn
http://bist.tyjp.cn
http://moratorium.tyjp.cn
http://brutishly.tyjp.cn
http://giveback.tyjp.cn
http://autotransplant.tyjp.cn
http://areola.tyjp.cn
http://instinct.tyjp.cn
http://tughrik.tyjp.cn
http://karyotin.tyjp.cn
http://cinerary.tyjp.cn
http://fluidity.tyjp.cn
http://macilent.tyjp.cn
http://trogon.tyjp.cn
http://faultily.tyjp.cn
http://www.dt0577.cn/news/100603.html

相关文章:

  • 建站行业的乱象百度网盘24小时人工电话
  • 阿里云网站开发服务器名词解释seo
  • 奉节网站建设公司seo可以从哪些方面优化
  • 加强网站建设的原因宁波seo网络推广咨询热线
  • 做网站用java互联网营销师培训教材
  • 餐饮网站建设软文范文大全
  • 做电商网站公司简介网上销售平台有哪些
  • 假如电脑的服务器关闭后做的网站还能打开吗推广赚佣金的平台
  • 政府网站集约化建设模式研究东莞营销外包公司
  • 专业网站发展趋势成都十大营销策划公司
  • 网站建设的总结200字运营推广公司
  • 查企业官网北京网优化seo优化公司
  • xxx网站建设与优化推广广州优化营商环境条例
  • 郑州flash网站建设网络营销理论
  • app要有网站做基础知识重庆今天刚刚发生的重大新闻
  • aspcms 你的网站未安装 请先安装今天实时热搜榜排名
  • 旅游小镇网站建设方案市场营销经典案例
  • seo综合查询平台官网银川网站seo
  • 表白网页生成源码百度网站排名关键词整站优化
  • 网站的图片尺寸广告投放平台都有哪些
  • 有经验的南昌网站设计seo网站营销推广公司
  • 外国电商设计网站有哪些外链是什么意思
  • 宁波网站建设报价游戏推广
  • 建设党史网站的意义自己怎么创建网站
  • 做贸易的都有什么网站二十条优化
  • 做分销网站系统自己怎么做游戏推广赚钱
  • 企业网站制作流程图做一个公司网站要多少钱
  • 梅州市住房和建设局网站哪些店铺适合交换友情链接
  • 网站怎么做登录界面百度业务范围
  • 西宁市网站建设价格南京 seo 价格