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. 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:
|
>>: Develop upload component function based on React-Dropzone (example demonstration)
Disclaimer: Since the project requires the use of...
To improve processing power and concurrency, Web ...
Table of contents 01 What is Kubernetes? 02 The d...
Table of contents Introduction: Installation of e...
Take 3 consecutive days as an example, using the ...
Copy code The code is as follows: <div id=&quo...
Writing a Dockerfile Configure yum source cd /tmp...
1. Implementation ideas The purpose of interface ...
1. Create the tomcat installation path mkdir /usr...
Table of contents Preface 1. What is scalability?...
1. Download the required kernel version 2. Upload...
Table of contents Preface 🍹Preparation 🍲vue3 usag...
Preface The file system is responsible for organi...
By default, the width and height of the header ar...
Table of contents 1. Introduction 2. Usage Statel...