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

边栏推荐
- I am 37 this year, and I was rushed by a big factory to...
- 物体颜色的来源
- 二叉树的基础练习
- LVGL's multi-language conversion tool -- a good assistant for font settings
- 学习使用php把stdClass Object转array的方法整理
- 无代码平台多行文字入门教程
- canvas画图时的bug记录
- MySQL binlog都有哪些模式?
- 为企业数字化转型提供服务_数字赋能企业转型
- [Punctuality Atomic STM32 Serial] Chapter 1 Learning Method of the Book Excerpted from [Punctuality Atomic] MiniPro STM32H750 Development Guide_V1.1
猜你喜欢
随机推荐
MindSpore:Batchnorm only support nchw input!
用匿名函数定义函数_c语言最先执行的函数是
无代码平台单项选择入门教程
Detailed explanation of switch link aggregation [Huawei eNSP]
LeetCode中等题之旋转图像
浅聊偏函数
无代码平台多项选择入门教程
leetcode经典例题——56.合并区间
无代码平台附件上传入门教程
移动端 开源低代码工具 beeware 和 kivy
冰蝎工具开发实现动态二进制加密WebShell
在测试集上训练,还能中CVPR?这篇IEEE批判论文是否合理?
各位大佬,请问mysql数据的cdc,能指定存量数据同步的zone为utc 吗
HCIP 第十七天
二叉树的基础练习
ps抠图怎么抠出来,自学ps软件photoshop2022,ps怎么抠出想要的部分-笔记记录
Techwiz OLED:OLED器件的发光效率
无代码平台多行文字入门教程
请问下Flink SQL如何写hologres分区表?我想要每天一个分区
js文字转语音播报









![Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]](/img/9c/b4ebe608cf639b8348adc1f1cc71c8.png)