当前位置:网站首页>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);边栏推荐
- MFC obtains local IP (used more in network communication)
- How to use JMeter function and mockjs function in metersphere interface test
- Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
- Source code of new campus errand / campus task platform on mutual station
- Can hero sports go public against the wind?
- Develop those things: add playback address authentication to easycvr platform
- Definition of rotation axis in mujoco
- [beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
- Radhat builds intranet Yum source server
- 網上股票開戶安全嗎?是否可靠?
猜你喜欢
![[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)](/img/e8/f43f5583e330fbc0cb6c0188711707.jpg)
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)

Mujoco model learning record

【Try to Hack】vulnhub DC4

2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers

Penetration practice vulnhub range Keyring

How to use JMeter function and mockjs function in metersphere interface test

Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week

Intelligent operation and maintenance practice: banking business process and single transaction tracking

Good looking UI mall source code has been scanned, no back door, no encryption

Check log4j problems using stain analysis
随机推荐
Source code of new campus errand / campus task platform on mutual station
DRF --- response rewrite
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
Detailed explanation of select in golang
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
聊聊项目经理最爱使用的工具
Detailed explanation of ArrayList expansion
Classpath classpath
How to use JMeter function and mockjs function in metersphere interface test
Replace UUID, nanoid is faster and safer!
[C supplement] [string] display the schedule of a month by date
Irradiance, Joule energy, exercise habits
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
Leetcode problem solving series -- continuous positive sequence with sum as s (sliding window)
Development cost of smart factory management system software platform
Is it safe to open an ETF account online? What are the steps?
MES production equipment manufacturing execution system software
EasyCVR设备录像出现无法播放现象的问题修复