This article brings you relevant knowledge about the mysql database. It mainly organizes some basic knowledge points of the database, including indexes, syntax order, execution order, stored procedures and other related issues. I hope everyone has to help.
Recommended learning: mysql tutorial
Atomicity
)Consistency
)Isolation
)Durability
)SQL
The standard defines four types of isolation: (The following isolation is from low to high, and concurrency is from high to low)
select
cannot be modified, so as to avoid inconsistent reading before and after a transaction. But there is no way to control phantom reading, because other transactions cannot change the selected data at this time, but they can add data; Summary: The four levels are gradually enhanced, each level solves the problem. The higher the transaction level, the worse the performance.
Isolation level Dirty read Non‐repeatable Phantom read
Read uncommitted possible possible possible
Read committed Not possible possible Perhaps
Repeatable read read) impossible impossible possible
Serializable impossible impossible impossible
##Summary: Uncommitted reads will cause dirty reads—> Committed reads solve dirty reads , but it will cause non-repeatable reading -> Repeatable reading solves the problem of inconsistent reading results, but causes phantom reading (not before, but now) -> Serializable solves phantom reading, but adds a lot of range locks , may cause lock timeout;
(1) Ordinary index (it There are no restrictions.)
(2) Unique index (the value of the index column must be unique, but null values are allowed.)
(3) Primary key index (a special unique index that does not allow null values) . Generally, the primary key index is created at the same time when creating the table.)
(4) Combined index
(5) Clustered index constructs a B-tree according to the primary key of each table, and the leaf node stores the entire The rows of the table record data, so the leaf nodes of the clustered index become data pages.
(6) Non-clustered index (auxiliary index) (the page node does not store an entire row of records).
(1) If there is an or in the condition, even if there is a condition with an index, it will not be used (use or as little as possible);
(2) Like query starts with %, for example, SELECT * FROM mytable WHEREt Name like'�min';
(3) If the column type is a string, it must be enclosed in quotation marks in the condition, otherwise it will not Will use index;
MyISAM
,InnoDB
,Memory
Three commonly used MySQL
Engine Type Comparison:
三 myisam index InnoDB index Memonry index
B-Tree index supports support for support
Hash index does not support it without support
R-Tree Index Supported Not supported Not supported
Full‐text index Not supported Not supported yet Not supported
Because when using a binary tree , because the depth of the binary tree is too large, I/O reading and writing are too frequent, which in turn leads to low query efficiency. Therefore, using a multi-way tree structure, various operations on the B-tree can keep the height of the B-tree low.
B-tree is also called balanced multi-path search tree. The characteristics of an m-order B-tree are as follows:
That is, when the following keywords exist in sql, they must maintain this order:
select[distinct ], from, join (such as left join), on, where, group
by, having, union, order by, limit;
That is, the sql is executed in the following order during execution:
from, on, join, where, group by, having, select, distinct, union, order by
group by and aggregation Functions are used together,
For example:
select a.Customer,sum(a.OrderPrice) from orders a where a.Customer=’Bush’ or a.Customer = ‘Adams’ group by a.Customer;
Realize multi-table query (inner join)
select u.uname,a.addr from lm_user u inner join lm_addr a on u.uid = a.uid;
The same can be achieved by using select from where
select u.uname,a.addr from lm_user u, lm_addr a where u.uid = a.uid;
delimiter $$ create procedure procedure_bill() comment '查询所有销售情况' begin select billid, tx_time, amt from lm_bill; end $$ delimiter ;
Call stored procedure
call procedure_bill();
View stored procedure
show procedure status like 'procedure_bill';
In the database, if the relationship between the two tables is a many-to-many relationship, such as: "student table and course schedule", a student can choose multiple courses, and a course can also be selected by multiple students. Select; According to the design principles of the database, a third association table should be formed.
Step 1: Create three data tables Student, Course, Stu_Cour
/**学生表*/ CREATE TABLE Student ( stu_id INT AUTO_INCREMENT, NAME VARCHAR(30), age INT , class VARCHAR(50), address VARCHAR(100), PRIMARY KEY(stu_id) ) /*学生课程表*/ CREATE TABLE Course( cour_id INT AUTO_INCREMENT, NAME VARCHAR(50), CODE VARCHAR(30), PRIMARY KEY(cour_id) ) /**学生课程关联表*/ CREATE TABLE Stu_Cour( sc_id INT AUTO_INCREMENT, stu_id INT , cour_id INT, PRIMARY KEY(sc_id) )
Step 2: Add foreign keys to the Stu_Cour related table
/*添加外键约束*/ ALTER TABLE Stu_Cour ADD CONSTRAINT stu_FK1 FOREIGN KEY(stu_id) REFERENCES Student(stu_id); ALTER TABLE Stu_Cour ADD CONSTRAINT cour_FK2 FOREIGN KEY(cour_id) REFERENCES Course(cour_id);
Complete the creation!
alter table table name add constraint FK_ID foreign key (your foreign key field name) REFERENCES appearance Table name (primary key field name of the corresponding table)
;Example: alter table tb_active add constraint FK_ID foreign key(user_id) REFERENCES tb_user(id);
When you access the database, whether it is manual access or program access, you do not read and write the database file directly, but access the database through the database engine. document.
Take a relational database as an example. Send a SQL statement to the database engine. The database engine interprets the SQL statement, extracts the data you need and returns it to you. Therefore, to visitors, the database engine is the interpreter of SQL statements.
Main difference:
is non-transactionally safe. And
InnoDB is transaction-safe;
lock granularity is table-level locks, while
InnoDB supports row-level locks;
supports full-text indexing, while
InnoDB does not support full-text indexing.
is relatively simple, so it is better in efficiency.
InnoDB, small applications can consider using
MYISAM;
The table is saved in the form of a file and used in cross-platform data transfer
MYISAM Storage will save a lot of trouble;
table is more secure than
MYISAM table, which can ensure that the data is not In case of loss, switch the non-transaction table to the transaction table;
MYISAM
manages non-transactional tables, which provides high-speed storage and retrieval, as well as full-text search capabilities. If the application needs to perform a large number of select
queries, then MYISAM
is a better choice. InnoDB
is used for transaction processing applications and has numerous features, including ACID
transaction support. If your application needs to perform a large number of insert
or update
operations, you should use innodb
, which can improve the performance of multi-user concurrent operations. Currently there are 6 paradigms in relational databases: first normal form {1NF}, second normal form {2NF}, third normal form {3NF}, Bath —Code normal form {BCNF}, fourth normal form {4NF}, fifth normal form {5NF, also known as perfect normal form}. The paradigm that meets the minimum requirements is the first normal form. On the basis of the first normal form, the one that further meets more standard requirements is called the second normal form {2NF}, and the other paradigms follow suit. Generally speaking, the database only needs to satisfy the third normal form (3NF).
Paradigm:
Recommended learning: mysql video tutorial
The above is the detailed content of MySQL database basic knowledge point reserve (organized summary). For more information, please follow other related articles on the PHP Chinese website!