当前位置:网站首页>The constraints of the database learning table
The constraints of the database learning table
2022-08-10 06:34:00 【sakewww】
表的约束
1引入
真正约束字段的是数据类型,但是数据类型约束很单一,需要有一些额外的约束,更好的保证数据的合法性,从业务逻辑角度保证数据的正确性.比如有一个字段是email,要求是唯一的.
MySQL是和数据打交道的,MySQLMust try to ensure that the data do not appear偏差
2空属性-not null
C/C++:NULL,0,\0 Number is the same!
MySQL:NULL 为不存在(空) !=0 !=“”
3默认值–default
**默认值:**某一种数据会经常性的出现某个具体的值,可以在一开始就指定好,在需要真实数据的时候,用户可以选择性的使用默认值.
Empty attributes and default value
4列描述-comment
列描述:comment,没有实际含义,专门用来描述字段,会根据表创建语句保存,用来给程序员或DBA来进行了解.
5zerofill–(在前面用0Auto-complete must be digits)
6主键–primary key
主键:primary key用来唯一的约束该字段里面的数据,不能重复,不能为空,一张表中最多只能有一个主键;主键所在的列通常是整数类型
Data in database is not the only purpose,Also convenient to extract
一般而言,Request records in a database table,There is a specific unique identification,标定唯一性!
当表创建好以后但是没有主键的时候,可以再次追加主键
alter table 表名 add primary key(字段列表)
When you need into the key that a column,Duplicate values,You add the primary key is not enough,Must be repeated to add
删除主键
alter table 表名 drop primary key;
复合主键
在创建表的时候,在所有字段之后,使用primary key(主键字段列表)来创建主键,如果有多个字段作为主键,可以使用复合主键.
7自增长-auto_increment
auto_increment:当对应的字段,不给值,会自动的被系统触发,系统会从当前字段中已经有的最大值+1操作,得到一个新的不同的值.通常和主键搭配使用,作为逻辑主键.
自增长的特点:
任何一个字段要做自增长,前提是本身是一个索引(key一栏有值)
自增长字段必须是整数
一张表最多只能有一个自增长
auto-incrementGeneral and primary key to match,Can also and other(唯一键)搭配
8唯一键-unique
not null+unique=primary key
一张表中有往往有很多字段需要唯一性,数据不能重复,但是一张表中只能有一个主键:唯一键就可以解决表中有多个字段需要唯一性约束的问题.
唯一键的本质和主键差不多,唯一键允许为空,而且可以多个为空,空字段不做唯一性比较.
主键和唯一键有什么区别?
Primary key and unique key is not conflict!Can have a primary key in a table,Also can have the only key at the same time
A primary key is not set the primary key attribute,You only as the primary key,But the corresponding property is selected as the primary key!
9外键-foreign key (字段名) references 主表(列)
外键用于定义主表和从表之间的关系:外键约束主要定义在从表上,主表则必须是有主键约束或unique约束.当定义外键后,要求外键列数据必须在主表的主键列存在或为null.
foreign key (字段名) references 主表(列)
外键 vs 外键约束
A foreign key is used to implement the relationships between table and table field,In fact only ourselves know,But as long as it is to operate,就可能出现错误,
For the sake of the relationship between the strong constraint table,Need to add a foreign key constraint,mysqlWill help us to the logical relationship between maintained table.
边栏推荐
- JS中初始化对象为null和空对象的区别
- C语言文件操作
- 全网可达,实现备份
- Hypervisor, KVM, QEMU总结
- MySQL's InnoDB engine (6)
- The difference between initializing objects as null and empty objects in JS
- 2022 Henan Mengxin League Game (5): University of Information Engineering K - Matrix Generation
- Why do games need hot updates
- UnityShader入门精要-立方体纹理、反射、折射、菲涅尔反射
- Confluence可以连接数据库但是在下一步就报错了
猜你喜欢
Talking about 3 Common Shadow Rendering Techniques in Games (3): Shadow Mapping
OSPF的dr和bdr
COLMAP+OpenMVS实现物体三维重建mesh模型
神经网络可视化有3D版本了,美到沦陷 已开源
About MongoDb query Decimal128 to BigDecimal problem
如何正确理解线程机制中常见的I/O模型,各自主要用来解决什么问题?
强化学习_03_表格方法实践(CartPole-v0 And MontoCarlo)
共享静态IP与独享静态ip有何区别
MySQL's InnoDB engine (6)
第11章 数据库的设计规范【2.索引及调优篇】【MySQL高级】
随机推荐
Quickly grasp game resources in one hour and remote hot update
求职
Chapter 12 Other Database Tuning Strategies [2. Index and Tuning] [MySQL Advanced]
MVCC详解
order by注入与limit注入,以及宽字节注入
H3C文档NAT专题
Unity扩展编辑器EditorWindow 小玩意(一)
761. Special Binary Sequences
椭圆曲线离散对数问题以及求解
Simplest character device driver
UE 游戏模式
Unity资源热更新--资源管理、Addressable
各位大佬,oracle11g,cdc2.2,flink1.13.6,单表增量同步。在没新增数据的情
排序二叉树代码
UnityShader入门精要-立方体纹理、反射、折射、菲涅尔反射
npm搭建私服,上传下载包
关于研究鼠标绘制平滑曲线的阶段总结
求问各位大佬,FLink SQL读取source的时候去指定水位线的时间字段,如果指定的这个字段中格
动态代理-cglib
Everyone, the default configuration of oracle cdc occasionally takes 30 seconds to capture data. How to optimize this?