当前位置:网站首页>MySQL: Integrity Constraints and Table Design Principles
MySQL: Integrity Constraints and Table Design Principles
2022-08-04 10:01:00 【_Sauron】
Article table of contents
Integrity constraints
- Primary key constraint: primary key
- Auto-increment key constraint: auto_increment
- Unique key constraint: unique
- Not null constraint: not null
- Default value constraint: default
- Foreign key constraint: foreign key
Only one primary key can be created in a table, but there can be multiple unique keys.
Example of usage:
CREATE TABLE user(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
nickname varchar(50) UNIQUE NOT NULL,
age TINYINT UNSIGNED NOT NULL DEFAULT 18,
sex ENUM('male','female'));
Relational database table design
1. One to One
As shown in the figure, these are two tables, one for basic user information and one for identity information.
If you want to associate the two tables, you need to add a field to the identity information table
2. One-to-many
For example, to make an e-commerce system
- User User
- Product Product
- Order Order
Analysis:
Users and products: no relationship
Users and orders: one-to-many relationship
products and orders: many-to-many relationship
One-to-many relationship: Add a column to the order sub-table to associate the primary key of the parent table (the field representing the user id).But in this way, it is obvious that data redundancy, especially with hundreds or thousands of orders, will lead to large batches of modifications.
3. Many-to-many
In order to solve the problem of data redundancy, an intermediate table can be created.
边栏推荐
猜你喜欢
Win11文件资源管理器找不到选项卡怎么办?
无代码平台描述文字入门教程
双指针方法
什么是元宇宙?
After four years of outsourcing, the autumn recruits finally landed
EastWave应用:自动计算光子晶体透反率
v-model原理,在“radio”、“checkbox”、“select”、修饰符
sqlilabs less-40
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
Qt:小的任务管理器(task)
随机推荐
SVG 的 path 属性绘制图形
[Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1
KubeDNS 和 CoreDNS
请问同一个oracle cdc表,如果flink job重新提交,是会全量读取一遍源数据还是增量呢?
渗透——信息收集
LeetCode 6. Z 字形变换 找规律
MindSpore:损失函数问题
HTB-Nibbles
视频加密怎么播放_win播放器加密视频
架构设计杂谈
移动端 开源低代码工具 beeware 和 kivy
【C补充】指向指针或函数的指针
二叉树与堆
Techwiz OLED:OLED器件的发光效率
低代码是开发的未来吗?浅谈低代码开发平台的发展现状及未来趋势
无代码平台单项选择入门教程
参数优化文档介绍
Producer and Consumer Problems in Concurrent Programming
多了<audio controls=
蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路