当前位置:网站首页>Mysql database design
Mysql database design
2022-07-01 18:17:00 【Xiaoqi DD】
Database design concept
Database design is based on the specific needs of the business system , Combined with our choice of DBMS, Construct the optimal data storage model for this business system .
Establishment of database Table structure as well as The relationship between tables The process of .
Database design steps
Demand analysis ( What is the data ? What attributes does the data have ? What are the characteristics of data and attributes ?)
logic analysis ( adopt ER Figure to model the database logically , There is no need to consider the database management system we choose )
physical design ( According to the characteristics of the database itself, the logical design is transformed into physical design )
Maintenance design (1、 Create a table for new requirements ;2、 Table optimization )
Table relations
1、 one-on-one : Such as user and user details
One to one relationships are mostly used for table splitting , Put the frequently used fields in an entity into a table , Less frequently used fields are placed in another table , For lifting Query performance
Realization way : Add foreign keys on either side , Associate another party's primary key , And set the foreign key to be unique (unique)
Take the user table for example :
create table tb_user_desc (
id int primary key auto_increment,
city varchar(20),
edu varchar(10),
income int,
status char(2),
des varchar(100)
);
create table tb_user (
id int primary key auto_increment,
photo varchar(100),
nickname varchar(50),
age int,
gender char(1),
desc_id int unique,
-- Add foreign keys
CONSTRAINT fk_user_desc FOREIGN KEY(desc_id) REFERENCES tb_user_desc(id)
);
2、 One to many : Such as departments and employees
A Department corresponds to multiple employees , An employee can only correspond to one department .
Realization way : Add a foreign key to more than one party , A primary key that points to a party of one
-- Departmental table
CREATE TABLE tb_dept(
id int primary key auto_increment,
dep_name varchar(20),
addr varchar(20)
);
-- The employee table
CREATE TABLE tb_emp(
id int primary key auto_increment,
name varchar(20),
age int,
dep_id int,
-- Add foreign keys dep_id, relation dept Tabular id Primary key
CONSTRAINT fk_emp_dept FOREIGN KEY(dep_id) REFERENCES tb_dept(id)
);
3、 Many to many : Such as goods and orders
One item corresponds to multiple orders , An order contains multiple items
Realization way : Create a third intermediate table , The middle table contains at least two foreign keys , Associate the primary keys of two parties respectively
Take order table and commodity table as an example
-- The order sheet
CREATE TABLE tb_order(
id int primary key auto_increment,
payment double(10,2),
payment_type TINYINT,
status TINYINT
);
-- Commodity list
CREATE TABLE tb_goods(
id int primary key auto_increment,
title varchar(100),
price double(10,2)
);
-- Intermediate table of order goods
CREATE TABLE tb_order_goods(
id int primary key auto_increment,
order_id int,
goods_id int,
count int
);
-- After building the table , Add foreign keys
alter table tb_order_goods add CONSTRAINT fk_order_id FOREIGN key(order_id) REFERENCES
tb_order(id);
alter table tb_order_goods add CONSTRAINT fk_goods_id FOREIGN key(goods_id) REFERENCES
tb_goods(id);
边栏推荐
- Redis -- data type and operation
- What are the six steps of the software development process? How to draw software development flow chart?
- 证券开户安全么,有没有什么样的危险呢
- Nielseniq found that 60% of the re launched products had poor returns
- New patent applications and transfers
- ArrayList扩容详解
- 2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers
- When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
- Set the style of QT property sheet control
- ACL 2022 | decomposed meta learning small sample named entity recognition
猜你喜欢
Countdownlatch blocking wait for multithreading concurrency
SQL injection vulnerability (MySQL and MSSQL features)
Cassette helicopter and alternating electric field magnetic manometer DPC
Penetration practice vulnhub range Keyring
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
How to write good code - Defensive Programming Guide
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
transform. Forward and vector3 Differences in the use of forward
ACL 2022 | decomposed meta learning small sample named entity recognition
[PHP foundation] realize the connection between PHP and SQL database
随机推荐
Zabbix报警执行远程命令
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
Sword finger offer II 105 Maximum area of the island
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
Distributed task queue: Celery usage record
golang中的select详解
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
Redis -- data type and operation
Kia recalls some K3 new energy with potential safety hazards
The method of real-time tracking the current price of London Silver
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
About selenium element positioning being overwritten
The latest intelligent factory MES management system software solution
Kernel stray cat stray dog pet adoption platform H5 source code
D @ safety and dip1000
Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
New patent applications and transfers
MFC obtains local IP (used more in network communication)
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes