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

边栏推荐
猜你喜欢

Producer and Consumer Problems in Concurrent Programming

双重for循环案例以及while循环和do while循环案例
![cannot import name 'import_string' from 'werkzeug' [bug solution]](/img/ee/c91ec761eb637260d92980a2838a92.png)
cannot import name 'import_string' from 'werkzeug' [bug solution]

Win11不识别蓝牙适配器的解决方法

蜜芽CEO刘楠:垂直电商黄金时代已落幕 坚定转型品牌之路

LVS+Keepalived群集部署

leetcode经典例题——49.字母异位词分组
![Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]](/img/9c/b4ebe608cf639b8348adc1f1cc71c8.png)
Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]

leetcode每天5题-Day06

使用ClickHouse分析COS的清单和访问日志
随机推荐
无代码平台多项选择入门教程
陈春花发布声明,这场流量狂欢该到了收尾的时候
ps如何换背景颜色,自学ps软件photoshop2022,3种不同的方式笔记记录
Since his 97, I roll but he...
用匿名函数定义函数_c语言最先执行的函数是
【C补充】指针相关知识点收集01
I am 37 this year, and I was rushed by a big factory to...
开源一夏 | 查询分页不只有limit,这四种分页方法值得掌握
HCIP 第十七天
微信小程序自定义组件-城市选择「建议收藏」
云计算适合什么企业_当前全球云计算处于发展
Layer 3 Switch/Router OSPF Configuration Details [Huawei eNSP Experiment]
sqlilabs less-38~39
Detailed explanation of telnet remote login aaa mode [Huawei eNSP]
IDEA 自动导入的配置(Auto import)
Get the number of cpu cores
Libtomcrypt AES 加密及解密
Shell编程的条件语句
学习在php中分析switch与ifelse的执行效率
多了<audio controls=