当前位置:网站首页>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);
边栏推荐
- Oracle TRUNC function processing date format
- DRF --- response rewrite
- 目前炒期货在哪里开户最正规安全?怎么期货开户?
- . Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
- Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
- Mujoco XML modeling
- Is the software of futures pioneer formal and safe? Which futures company is safer to choose?
- Key points on February 15, 2022
- LeetCode 148. Sort linked list
- transform. Forward and vector3 Differences in the use of forward
猜你喜欢
Kia recalls some K3 new energy with potential safety hazards
New 95 community system whole station source code
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
This is the latest opportunity of the London bank trend
Check log4j problems using stain analysis
Review Net 20th anniversary development and 51aspx growth
What are the legal risks of NFT brought by stars such as curry and O'Neill?
Set the style of QT property sheet control
Mujoco model learning record
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
随机推荐
Reflective XSS vulnerability
Apk signature process introduction [easy to understand]
Domestic spot silver should be understood
Source code of new campus errand / campus task platform on mutual station
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
证券开户安全么,有没有什么样的危险呢
Terms related to K line
SCP -i private key usage
聊聊项目经理最爱使用的工具
Redis -- data type and operation
Redis master-slave realizes 10 second check and recovery
Enter wechat applet
Apache iceberg source code analysis: schema evolution
线上开通ETF基金账户安全吗?有哪些步骤?
DRF --- response rewrite
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?
Mujoco model learning record
Distributed task queue: Celery usage record