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

边栏推荐
猜你喜欢
随机推荐
Get the number of cpu cores
usb设备复合g_webcam摄像头码流传输功能以及g_serial串口功能
js文字转语音播报
暴力破解ssh/rdp/mysql/smb服务
关于技术学习的6个观点
Could you please talk about how the website is accessed?[Interview questions in the web field]
为企业数字化转型提供服务_数字赋能企业转型
Cloud function to achieve automatic website check-in configuration details [Web function/Nodejs/cookie]
Libtomcrypt AES 加密及解密
[Punctuality Atom STM32 Serial] Chapter 2 STM32 Introduction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
MindSpore:【model_zoo】【resnet】尝试用THOR优化器运行时报cannot import name ‘THOR‘
MindSpore:MindSpore GPU版本安装问题
leetcode经典例题——56.合并区间
物体颜色的来源
[论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss
HCIP 交换实验
MindSpore:【mindinsight】【Profiler】用execution_time推导出来的训练耗时远小于真实的耗时
How to restore the Youxuan database with only data files
关于ARM2440中断源个数的一点想法[通俗易懂]
多媒体和物联网技术让版本“活”起来 129张黑胶唱片“百年留声”









