MySQL Essentials

Database: A warehouse that organizes, stores and manages data according to a data structure. It is a collection of large amounts of data stored in a computer for a long time, organized, shared, and unified.
MySQL is a relational database management system (table group, the relationship between the table and the table through the primary key and foreign key).
The first from MySQL data types:
I.
1. Data type
TINYINT Signed: -128 to 127
unsigned: 0-255 bytes occupied. 1
SMALLINT signed: -32768 to 32767
unsigned: 0-65535 share word Section 2
MEDIUMINT Signed: -2^23 to 2^23 -1
Unsigned: 2^24 Occupied byte 3
INT Signed: -2^31 to 2^31 -1 Note: 11-bit
unsigned: 2^ 32 -1 Occupied byte 4
BIGYINT Signed: -2 63 to 2 63-1
Unsigned: 2^64 -1 Occupied byte 8
BOOLEAN is equivalent to TINYINT(1) 0 is FALSE 1 is TRUE Byte 1
FLOAT [(M,D)]: Floating-point type, with a byte count of 4, 32bit, the value range is -3.4E38~3.4E38 (7 effective digits)
DOUBLE[(M,D)]: Double precision real type, The number of bytes included is 8, 64bit, and the value range is -1.7E308~1.7E308 (15 effective digits).
DECIMAL[(M,D)]: Digital type, 128bit, without loss of precision, often used in bank account calculations. (28 effective bits)
Note: MySQL floating-point and fixed-point types can be represented by adding (M, D) after the type name, M represents the total length of the value, and D represents the length after the decimal point

2. String type (commonly used):
CHAR(M) fixed length M field 0<M<255 Note: Disadvantages waste space
VARCHAR(M) variable length M+1 bytes (including an end character) 0<M <65535
TEXT M+2 bytes M<2^16 long text

Second, the creation and deletion of data tables
Create table: create table table name (field setting list);
delete table: drop table table name;
modify table: alter table t1 rename t2
query table: select * from table name;
empty table: delete from table name;
standard table building statement template:
CREATE TABLE IF NOT EXISTS dodoke_user(
id INT,
username VARCHAR(20),
password CHAR(32),
email VARCHAR(50),
age TINYINT,
card CHAR(18),
tel CHAR (11),
salary FLOAT(8,2),
married TINYINT(1),
addr VARCHAR(100),
sex ENUM('male','female','confidential')
)ENGINE=INNODB CHARSET=UTF8;

3. Common MySQL column constraint
primary key constraint (PRIMARY KEY: PK): the only record in the data table is determined, and the primary key constraint cannot be empty. We generally use fields with no business meaning to define the primary key. In MySQL You can use an auto-increment type of primary key or you can use UUID. A table can only have one primary key.
Non-empty constraint (NOT NULL)
default value (DEFAULT)
unique constraint (UNIQUE KEY:UK): multiple fields can be unique in a table
Foreign key constraint (FOREIGN KEY: FK)
auto-increment constraint (AUTO_INCREMENT): only Can be used for numeric columns and used with indexes

4. Data addition, deletion , modification, and checking
.
Addition : a single addition:
Iinsert into table_name[(field1, field2,…)] values(value1, value2, value3)
insert table set field = "value", field = "value", field = "Value"
batch addition:
insert tbl_name (field name,…) values ​​(value,…),(value,…),(value,…);
Copy new addition:
insert into table select * from table2()
Modification:
update s set code='ss6',birthday='1990-10-10' where name='zhangsan'
delete:
delete from table where condition
query:
select * from table
avoid repeated queries: select distinct * from table

5. MySQL statements (conditions, fuzzy, sorting, grouping, connection)
1. WHERE filtering:
after where, we can keep up with the query conditions to be set. So how to describe the query conditions?
Expressions with relational and logical operators;
conditional queries with the BETWEEN AND keyword; conditional queries
with the IS NULL keyword; conditional queries
with the IN keyword; conditional queries
with the LIKE keyword.

BETWEEN AND
SELECT * FROM stu_info WHERE age BETWEEN 16 AND 20
SELECT * FROM stu_info WHERE age >= 16 AND age <=20

IS NULL:
SELECT * FROM stu_info WHERE name IS NULL;
SELECT * FROM stu_info WHERE name = ‘’; – 判断空字符串

IN:
SELECT * FROM stu_info WHERE clazz IN (‘C1’, ‘C2’);

LIKE(模糊查询)
SELECT * FROM stu_info WHERE name LIKE ‘李_强’;
SELECT * FROM stu_info WHERE name LIKE ‘李%’;
SELECT * FROM stu_info WHERE name LIKE ‘%李%’;

2. Sorting and grouping:
ORDER BY default ASC ascending order, descending order is DESC
grouping: GROUP BY
GROUP BY grouping: Put the same value into a group, the final query result will only show one record in the group.
SELECT id, username, age, sex FROM user;
inner join:
SELECT s. code, s. name, c. nameFROM student s INNER JOIN clazz c ON s.clazz_id = c.id
left outer join:
SELECT field name,... FROM tb_name1 LEFT [OUTER] JOIN tb_name2 ON Condition
First display all the records in the left table, and then go to the right table to query the records that meet the conditions. If not, replace the
right outer join with NULL . The opposite is true.

Six (NULL value processing, operators, aggregate functions, date functions, transactions)
NULL value judgment:
select * from user where user_name<=>null;
Operator:
relational operators: >, >=, <, <=, = ,
<=> ; Logical operators: AND(&&), OR(||), NOT(!)
Aggregate functions:
count total
sum sum sum
avg average
max max max
min min min
Example:
SELECT
COUNT(*) AS totalUsers ,
SUM(age) AS sum_age,
MAX(age) AS max_age,
MIN(age) AS min_age,
AVG(age) AS avg_age,
FROM user

Date:
curtime() displays the current time
current_time() displays the current time
now() displays the current date and time
current_timestamp() displays the current date and time
sysdate() displays the current date and time
month ('2017-02-19'); displays the current month 2
Each function can be nested select month(curdate()), monthName( curdate()); curdate(
)//
dayname(now());//returns the day of the week
dayofweek(now());//returns the week Day of the
week , Sunday is 1 and Monday is 2 weeks (now())//returns the number of weeks in the
year year(now()) returns the year
day(now()) returns the day
hour(now()) Returns the hour
minute() returns the minute
second() returns the second
select datediff('2017-01-05','2019-01-05'); calculate the number of days between two dates

The characteristics of the transaction:
ACID:
1. Atomicity: All operations in a transaction are either completed or not completed, and will not end in the middle. If an error occurs during the execution, it will be rolled back to before the start of the transaction The status is the same as not executing.
2. Consistency: Before and after the transaction is executed, if the data is legal. In other words, the data must be in full compliance with the preset rules (correct).
3. Isolation: Multiple transactions are executed concurrently When the internal operation of the transaction is isolated from the internal operations of other transactions (no mutual influence).
4. Persistence: After the transaction is executed, the modification to the database is permanent and will not be affected by subsequent operations.
Note:
Three problems caused by concurrency and their solutions:
Dirty read: dirty data (wrong data) is read.
Solution: "lock" during write operations.
Non-repeatable read: the result of reading data twice in one transaction Inconsistent.
Solution: "Lock" during read operations.
Phantom read: The result set returned by multiple queries in the same transaction is different.
Solution: "Serialize", so there is no concurrency.


Isolation : transaction isolation level in MySQL (manually set the level, adjust concurrency and isolation):
read uncommitted allows to read uncommitted data. The highest concurrency and the lowest isolation will cause dirty read problems.
read committed only allows read commits The data is equivalent to write and lock. Concurrency is reduced and isolation is improved, which can avoid dirty read problems, but there is a non-repeatable read problem. Repeatable
read (default isolation level) reads and writes are locked, concurrency is further reduced, and isolation is further improved. Avoid the problem of non-repeatable reading, but there is a problem of phantom reading.
serializable is strictly executed serially, with the highest degree of isolation and the lowest concurrency, which can avoid the problem of phantom reading.

Guess you like

Origin blog.csdn.net/hgfhjtff/article/details/108027287