当前位置:网站首页>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.

边栏推荐
- 《福格行为模型》:如何养成好习惯?
- HTB-Sense
- No module named 'flask_misaka' has been resolved [BUG solution]
- LeetCode 6. Z 字形变换 找规律
- Multimedia and Internet of Things technology make the version "live" 129 vinyl records "Centennial Voice"
- 江西发布紧急通知:全面开展涉校涉生安全隐患大排查
- Detailed explanation of switch link aggregation [Huawei eNSP]
- Inheritance and the static keyword
- How to restore the Youxuan database with only data files
- 黑马瑞吉外卖之员工账号的禁用和启用以及编辑修改
猜你喜欢
随机推荐
学习在php中分析switch与ifelse的执行效率
LVS+Keepalived群集部署
无代码平台附件上传入门教程
HTB-Nibbles
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
无代码平台单项选择入门教程
如何直击固定资产管理的难题?
Ansible deployment scripts - pro available without pit
rk3399-339 usb设备复合 总体流程
陈春花发布声明,这场流量狂欢该到了收尾的时候
sqlilabs less-38~39
字符串相关题目
LeetCode581+621+207
LeetCode 54. 螺旋矩阵 蛇形矩阵式输出字符串
【C补充】指针相关知识点收集01
关于DSP驱动外挂flash
HCIP 第十八天
I am 37 this year, and I was rushed by a big factory to...
ps抠图怎么抠出来,自学ps软件photoshop2022,ps怎么抠出想要的部分-笔记记录
低代码是开发的未来吗?浅谈低代码开发平台的发展现状及未来趋势









