当前位置:网站首页>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);边栏推荐
- Is the software of futures pioneer formal and safe? Which futures company is safer to choose?
- Domestic spot silver should be understood
- Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
- Can hero sports go public against the wind?
- 聊聊项目经理最爱使用的工具
- Petrv2: a unified framework for 3D perception of multi camera images
- Draw drawing process of UI drawing process
- Relationship between sensor size, pixel, dpi resolution, inch and millimeter
- Win10+vs2019 Community Edition compiling OpenSSL
- Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
猜你喜欢

Gameframework eating guide

Enter wechat applet

Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?
![[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON](/img/83/9bd9ce7608ebfe7207ac008b9e8ab1.png)
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON

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

Check log4j problems using stain analysis
![Htt [ripro network disk link detection plug-in] currently supports four common network disks](/img/e4/c06b94243cdbe391b8b2124ff30789.jpg)
Htt [ripro network disk link detection plug-in] currently supports four common network disks

An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press

Thinkphp6 - CMS multi wechat management system source code

This is the latest opportunity of the London bank trend
随机推荐
Bug of QQ browser article comment: the commentator is wrong
Data warehouse (3) star model and dimension modeling of data warehouse modeling
New 95 community system whole station source code
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
Wechat applet blind box - docking wechat payment
Nielseniq found that 60% of the re launched products had poor returns
Record 3 - the state machine realizes key control and measures the number of external pulses
Function, condition, regular expression
Redis master-slave realizes 10 second check and recovery
目前炒期货在哪里开户最正规安全?怎么期货开户?
Product service, operation characteristics
Apache iceberg source code analysis: schema evolution
How to write good code - Defensive Programming Guide
golang中的select详解
Euler function: find the number of numbers less than or equal to N and coprime with n
【Try to Hack】vulnhub DC4
Terms related to K line
Length of learning and changing
Mujoco's biped robot Darwin model
SQL injection vulnerability (MySQL and MSSQL features)