当前位置:网站首页>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);边栏推荐
- How to write good code - Defensive Programming Guide
- February 16, 2022 Daily: graph neural network self training method under distribution and migration
- MES production equipment manufacturing execution system software
- SQL injection vulnerability (MySQL and MSSQL features)
- Step size of ode45 and reltol abstol
- Nielseniq found that 60% of the re launched products had poor returns
- Is it safe to open an ETF account online? What are the steps?
- Sword finger offer II 105 Maximum area of the island
- Unity3d extended toolbar
- Software construction scheme of smart factory collaborative management and control application system
猜你喜欢

Penetration practice vulnhub range Nemesis

Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target

Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open

How to write good code - Defensive Programming Guide

Enter wechat applet

Fix the black screen caused by iPhone system failure

Extract the compressed package file and retrieve the password

Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #

Mujoco's biped robot Darwin model

Samba basic usage
随机推荐
证券开户安全么,有没有什么样的危险呢
Review Net 20th anniversary development and 51aspx growth
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
Subnet division and summary
EasyCVR设备录像出现无法播放现象的问题修复
JS how to convert a string with a delimiter into an n-dimensional array
Good looking UI mall source code has been scanned, no back door, no encryption
Unity3d extended toolbar
Radhat builds intranet Yum source server
February 16, 2022 Daily: graph neural network self training method under distribution and migration
Is it safe to open a securities account? Is there any danger
Mujoco XML modeling
Reflective XSS vulnerability
Smart factory digital management system software platform
The reviewboard has 500 errors when submitting a review. Solutions
Flex layout
Fix the problem that easycvr device video cannot be played
Wechat applet blind box - docking wechat payment
ZABBIX alarm execute remote command
Check log4j problems using stain analysis