Optimization of data tables in MySQL database, analysis of foreign keys and usage of three paradigms

Optimization of data tables in MySQL database, analysis of foreign keys and usage of three paradigms

This article uses examples to illustrate the optimization of data tables in MySQL databases, the use of foreign keys and three paradigms. Share with you for your reference, the details are as follows:

Data table optimization

Optimize the product information table

1. Create a product category table:

create table if not exists goods_cates( 
id int unsigned primary key auto_increment, 
name varchar(40) not null 
);

2. Write the product category into the product category table:

Note: You do not need to add values ​​when inserting query results into another table

insert into goods_cates(name) 
(select cate_name from goods group by cate_name); 

3. Update the product category table to the product table:

Connect the product table to the category table, and use the category table id to replace the product table category content

update (goods_cates inner join goods on goods_cates.name=goods.cate_name) 
set goods.cate_name=goods_cates.id;

4. Modify the table structure:

Rename the product category name field cate_name in the product table to cate_id

alter table goods change cate_name cate_id int unsigned not null;

Foreign Keys

1. Use of foreign keys:

Add a foreign key constraint and insert the data line to determine whether cate_id exists in the id in goods_cates. If not, the data insertion fails.

alter table table name 1 add foreign key (field name 1) references table name 2 (field name 2); 

(Setting a foreign key can prevent illegal data entry)

foreign key (field name 1) references table name 2 (field name 2); 

(You can also set the foreign key directly when creating the table, provided that the table to be connected exists)

2. Disadvantages of foreign keys:

Although foreign keys can effectively prevent the entry of illegal data, they will greatly reduce the update efficiency of the table. Therefore, in actual development, foreign key constraints are generally not applied, and the legitimacy of the data can be judged at the transaction level.

3. Deletion of foreign keys:

Display the statement to create the table

show create table goods; 

Delete the foreign key after getting its name:

alter table goods drop foreign key goods_ibfk_1;

Three Paradigms

1. First Normal Form (1NF): emphasizes the atomicity of columns, that is, columns cannot be divided into other columns.
2. Second Normal Form (2NF): Based on 1NF, the table must have a primary key, and non-primary key fields must be completely dependent on the primary key, rather than only part of the primary key.
3. Third Normal Form (3NF): Based on 2NF, non-primary key columns must be directly dependent on the primary key, and there cannot be transitive dependencies.

A database that conforms to the third normal form is a more reasonable database.

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL Series Database Design Three Paradigm Tutorial Examples
  • Detailed explanation of MySQL database paradigm
  • Mysql database design three paradigm examples analysis
  • Summary of MySQL database normalization design theory

<<:  Detailed explanation of the functions of -I (uppercase i), -L (uppercase l), and -l (lowercase l) when compiling programs with g++ under Linux

>>:  Develop upload component function based on React-Dropzone (example demonstration)

Recommend

A very detailed tutorial on installing rocketmq under Docker Desktop

Install Docker Desktop Download address: Docker D...

How to use Webstorm and Chrome to debug Vue projects

Table of contents Preface 1. Create a new Vue pro...

Pitfalls based on MySQL default sorting rules

The default varchar type in MySQL is case insensi...

Native js to implement 2048 game

2048 mini game, for your reference, the specific ...

jQuery implements breathing carousel

This article shares the specific code of jQuery t...

MySQL optimization: how to write high-quality SQL statements

Preface There are a lot of information and method...

Detailed explanation of Linux tee command usage

The tee command is mainly used to output to stand...

Summary of the use of Vue computed properties and listeners

1. Computed properties and listeners 1.1 Computed...

Markup Language - Image Replacement

Click here to return to the 123WORDPRESS.COM HTML ...

Solution to the problem "Table mysql.plugin doesn't exist" when deploying MySQL

Today I deployed the free-installation version of...

Linux bridge method steps to bridge two VirtualBox virtual networks

This article originated from my complaints about ...

Detailed explanation of common methods of JavaScript String

Table of contents 1. charAt grammar parameter ind...

iview implements dynamic form and custom verification time period overlap

Dynamically adding form items iview's dynamic...

MySQL 8.0.20 compressed version installation tutorial with pictures and text

1. MySQL download address; http://ftp.ntu.edu.tw/...