当前位置:网站首页>MySQL basic knowledge summary
MySQL basic knowledge summary
2022-07-26 11:13:00 【qq_ five hundred and forty-seven million twenty-six thousand on】
1.SQL Sentences are mainly divided into four categories :DDL,DML,DQL,DCL
- Data definition language (DDL): Main operation database as well as surface Of create,drop,alter Wait for the operation ;
- Data manipulation language (DML): Mainly for Table data Conduct insert,update,delete Wait for the operation ;
- Data query language (DQL): Used to get data from a table , Usually the most commonly used is reserved words SELECT, And often with FROM Clause 、WHERE Clauses make up the query SQL Query statement ;
- Data control language (DCL): Main control user or User group Access to database objects , Usually by GRANT and REVOKE To control permissions ;
2.SQL Five common constraints :Primary Key Primary key constraint ,Foreign Key Foreign key constraints ,Unique Unique constraint ,Default Default constraint ,Not Null Non empty constraint ;
3.MySQL Three paradigms and anti paradigms :
Candidate keywords : Also known as houxuanma , Uniquely identify a row of data , Its true subset cannot be a candidate keyword , A table can have multiple candidate keywords .
Key words : Also called primary key , Main code , The selected candidate keywords used to distinguish other rows , A table has only one primary key .
Part of it depends on :(A,B)->C,D, Such as A->C, be C Part of it depends on A.
Transitive dependency :A->B->C, be C Transitive dependency A.
First normal form : Ensure that each column in the data table ( Field ) The atomicity of . That is to say, every field in the data table is the smallest data unit that cannot be subdivided , The first paradigm .
Second normal form : On the basis of the first paradigm , Any non key field cannot partially depend on any candidate keyword ( That is, it must be completely dependent on ). for example :

In this table goods_name and goods_company Two fields make up a (goods_name,goods_company) Candidate keywords .
But there are two dependencies in this table , As a result, non key fields depend on some candidate keywords , Therefore, the table does not meet the second normal form .
(price,weight) --> (goods_name)
(company_phone) -->(goods_company)Third normal form : On the basis of the second paradigm , Any non critical fields You cannot rely on any candidate keyword . for example :

The candidate keywords of this table are (goods_name).
The transfer dependency of this table is :(goods_name)–> (sort)–> (sort_desc)
in other words (sort_desc) Classification describes this non key field for candidate keywords (goods_name) There are transitive dependencies , Not directly related , It's indirect , So it doesn't satisfy the third paradigm .BC normal form : On the basis of the third paradigm , Any field Can't rely on any candidate keyword . That is, the candidate keyword or some of its fields cannot be transferred and depend on other candidate keywords . Compared with the third paradigm , One is “ Any non critical fields ”, One is “ Any field ”, Obviously more strict . for example :

Assume that the contact person can only be employed by one manufacturer , Each supplier can provide a variety of products . There are two choices of keywords :
(company,goods_id)–> (company_contact,goods_num)
(company_contact,goods_id) --> (company,goods_num)
But there is another relationship : As a result, some fields of the two candidate keywords are interdependent . So not satisfied BC normal form .
(company_contact)–> (company)Anti normalization : Denormalization refers to improving the query performance of a database by adding redundant or duplicate data . That is to add fields of other tables to one table , It eliminates the problem caused by the association query between multiple tables IO Consume . In fact, it costs more data storage space in exchange for shorter query time .
- Field type selection principle :
1. Compare the data ( Query criteria ,join Conditions and sorting ) In operation , For the same data, character processing is often slower than digital processing .
2. In the database , Data processing is in pages , The smaller the length of the column , Good for performance . - char And varchar How to choose :
1. If the length of the data stored in the column is almost the same , We should consider using char, Otherwise, we should consider using varchar.
2. If the maximum length of this column is less than 50Byte, It is generally considered to use char, If this column is rarely used , Based on saving space and reducing I/O The consideration of , Or you can choose to use varchar.
3. Generally, the maximum length of the column exceeds 50Byte It's best to use varchar. - decimal And float How to choose :
1.decimal For storing accurate data , and float Can only be used to store imprecise data . Therefore, it is necessary to choose decimal type .
2. because float The cost of storage space is generally higher than decimal Small ,( Accurate to 7 Bit only 4 Bytes , Accurate to 15 Bit only 8 Bytes ) Therefore, those with low accuracy requirements can be selected float type . - How to choose the time type :
1. Use int To store the advantages and disadvantages of time :
advantage : Field length ratio datetime Small .
shortcoming : Inconvenient to use , Function conversion is required ; It can only be stored in 2038-1-19 11:14:07
2. Consider according to the granularity of time storage , Yes, it only needs to be stored for years , You can choose year, Just store the date , You can choose date And so on . - Avoid using foreign key constraints :
1. It will reduce the efficiency of data import .
2. Increased maintenance costs . - Avoid using triggers :
1. It will reduce the efficiency of data import .
2. Increase the complexity of business logic . - Do not use reserved fields :
1. Unable to know exactly the type of reserved field .
2. It is impossible to know exactly what needs to be stored in the reserved field .
3. Increase the cost of later maintenance .
边栏推荐
- Pyqt5 rapid development and practice Chapter 1 understanding pyqt5
- The assignment of member pointer defined in C structure and the use of structure pointer as member function parameter
- pytest conftest.py和fixture的配合使用
- easyui02
- Capture ZABBIX performance monitoring chart with selenium
- Access rights - private, public, protected
- 配置文件以rc结尾什么意思
- easyui03
- 1748. Sum of unique elements
- Esxi6.5 patch update
猜你喜欢
随机推荐
Bash shell学习笔记(四)
脉冲波形的产生与变换
承认吧 投新能源就是为了安全感
During the interview, how did the interviewer evaluate the level of rust engineers?
@The real difference and usage between validated and @valid
看源码之LinkedList
Bash shell learning notes (VII)
Shape matching Halcon template
BLE之ATT请求
Logging learning final edition - configured different levels of log printing colors
线程之间的几种通信方式
ThreadPoolExecutor是怎样执行任务的
[学习进度]5月
Bash shell学习笔记(七)
C notes
Linkedblockingqueue of novice source code
The assignment of member pointer defined in C structure and the use of structure pointer as member function parameter
Simple use of MySQL database
mysql数据库进阶
logging基本使用









