1. MySQL's basic statement that is so simple that it can't be simpler:
- information_schema object information column information, user information, permission information, character set
- Permission information for the mysql user
- test test
- cluster cluster information
①DDL
Data Definition Language create drop alter
②DML
data manipulation language insert delete update select
③DCL
Data Control Language grant revoke
1).DDL(create drop alter)
(1. Create database create database Cy1703;
(2. Query the database show databases;
(3. Delete the database drop databases Cy1703;
1. Build a table
create TABLE Stu(
id varchar(20) primary key,
name varchar(20) not null,
age int default null,
sex varchar(8),
score float );
2. Delete the table
drop TABLE tablename;
3. Query table structure
show Create table tablename;
4. Table modification alter
1. Modify the field properties
alter table Stu MODIFY sex varchar(10);
2. Add fields
alter table Stu ADD height2 float not NULL AFter sex;
3. Delete a field
alter table Stu DROP height2;
4. Modify the field name
alter table Stu CHANGE sex mysex enum("man","woman");
5. Modify the table name
alter table Stu RENAME student;
2).DML( insert|load|replace delete update select)
1. Data Insertion
insert into Stu value("zhangsan",10,"man",98.6);
insert into stu(name,mysex) value("lisi","man");
insert into stu value("zhangsan",10,"man",78.1),
("lisi",10,"man",98.6),
("lisi",10,"man",59.4),
("lisi",10,"man",65.5);
2. Deletion of data
1. Delete all data in the table
delete from stu;
2. Delete eligible data
delete from stu where name = "lisi";
3. Modification of data
update stu SET score = 76 where name = "zhangsan";
4. Data query
- select * from Stu;//Query all
- select name,age,mysex,score from Stu;
1. Deduplication distinct
select distinct name from stu;
2. Sort order by asc desc
group by
select name,score from stu
order by score desc;
select name, Sum(score) all_score from stu
group by name
having Sum(score) > 100;
3. Equivalent query
1. Basic information sheet
id name zhangsan
2. Payroll
id examples
select mysalary from salary a,
(select id infoid from info where name = "zhangsan") b
where a.id = b.infoid;
select info.id,name,mysalary from info,salary
where info.id = salary.id;
create table info(
id int primary key,
name varchar(20) not null
);
insert into info value(1,"zhangsan"),
(2,"lisi"),
(3,"wangwu"),
(4,"maliu");
insert into salary value(1,5000),
(1,7800),
(3,6000),
(3,6000),
(3,6100);
create table salary(
id int,
examples float);
4. Link query
1).Left join
select b.name,a.mysalary from salary a
left join (select id,name from info where name= "zhangsan") b
on a.id = b.id
where b.name is not null;
2). Right join
select b.name,a.mysalary from salary a
right join (select id,name from info where name in("lisi","zhangsan")) b
on a.id = b.id
where a.mysalary is not null;
3). Inner join
inner
5. Subqueries
select mysalary from salary
where id in (select id from info where name = "zhangsan");
6. Joint query
union union all
create table money
(
id int,
mymoney float
);
insert into money value(1,2000),
(3,5000);
select id,mysalary from salary where id = 1
union all
select id,mymoney from money where id = 1;
insert into salary value(1,2000);
select sum(mysalary) + sum(mymoney)
from salary,money
where salary.id = 1 and money.id = 1;
3) .DCL
grant grants a user a permission on a database or table
revoke recovery permission
all all all
1.root ==> user1 ==> user2 ==> user3
Second, MySQL data types :
Integer types: BIT, BOOL, TINY INT, SMALL INT, MEDIUM INT, INT, BIG INT
Floating point type: FLOAT, DOUBLE, DECIMAL
String types: CHAR, VARCHAR, TINY TEXT, TEXT, MEDIUM TEXT, LONGTEXT, TINY BLOB, BLOB, MEDIUM BLOB, LONG BLOB
1. Integer
MySQL data types |
Meaning (signed) |
tinyint(m) |
1 byte range (-128~127) |
smallint(m) |
2 bytes range (-32768~32767) |
mediumint(m) |
3 bytes range (-8388608~8388607) |
int(m) |
4 bytes range (-2147483648~2147483647) |
bigint(m) |
8 bytes range (+-9.22*10 to the 18th power) |
2. Floating point
MySQL data types |
meaning |
float(m,d) |
Single-precision floating-point type 8-bit precision (4 bytes) m total number, d decimal places |
double(m,d) |
Double-precision floating-point type 16-bit precision (8 bytes) m total number, d decimal places |
Suppose a field is defined as float(6,3), if you insert a number 123.45678, the actual database stores 123.457, but the total number is based on the actual number, that is, 6 digits. The integer part can be up to 3 bits, if the number 12.123456 is inserted, it will store 12.1234, and if 12.12 is inserted, it will store 12.1200.
3. Fixed-point number
Floating-point types store approximate values in the database, while fixed-point types store exact values in the database.
decimal(m,d) The parameter m<65 is the total number, d<30 and d<m are the decimal places.
4. String
MySQL data types |
meaning |
char(n) |
Fixed length, up to 255 characters |
varchar(n) |
Fixed length, up to 65535 characters |
tinytext |
Variable length, up to 255 characters |
text |
Variable length, up to 65535 characters |
mediumtext |
Variable length, up to 2 to the 24th power - 1 character |
longtext |
Variable length, up to 2 to the 32nd power - 1 character |
①char and varchar:
- char(n) If the number of stored characters is less than n, it will be filled with spaces, and the spaces will be removed when querying. Therefore, the string stored in char type cannot have spaces at the end, and varchar is not limited to this.
- char(n) fixed length, char(4) will occupy 4 bytes no matter how many characters are stored, varchar is the actual number of characters stored + 1 byte (n<=255) or 2 words section (n>255), so varchar(4), storing 3 characters will occupy 4 bytes.
- String retrieval speed of type char is faster than that of type varchar.
②varchar and text:
1. varchar can specify n, text cannot words
Festival.
2. The text type cannot have a default value.
3. varchar can directly create an index, and text creates an index to specify how many characters before it is created. The varchar query is faster than the text, and the index of the text does not seem to work when the index is created.
5. Binary data (_Blob)
1. The storage methods of _BLOB and _text are different. _TEXT is stored in text mode, and English storage is case-sensitive, while _Blob is stored in binary mode and is case-insensitive.
2. The data stored in _BLOB can only be read as a whole.
3. _TEXT can specify the character set, _BLO does not need to specify the character set.
3. Common functions in MySQL:
①conv(N,from_base,to_base); The first parameter is the character to be converted, the second is the base of the character, and the third is the base to be converted
BIN(N), OCT(N), HEX(N) return N in binary, octal, hexadecimal
②case-when-then
…
case-when-then
end ,
case field name when value then value replacer
end can be followed by a replacer for cases other than those mentioned in case, or it can be empty.
例如:case when 2>1 then 8 else 10 end;
case 101 when bin(5) then 1 else 0 end;
③Cast ( type of field name as conversion )
where type can be:
CHAR[(N)] Character
type DATE Date type
DATETIME Date and time type
DECIMAL float type
SIGNED int
TIME Time type
④round(x,y) returns the rounded value of the parameter x with y decimal places
⑤mod(x,y) returns the modulus (remainder) of x/y
⑥substr(string string,num start,num length);
string is a string; start is the starting position; length is the length.
⑦insert(str,x,y,instr)
Replace the string str starting from the xth position, the substring y characters long with the string instr, and return the result
⑧length(str) returns the number of characters in the string str
⑨concat(str1,str2,str3,...) string concatenation function
⑩ABS(x) function returns the absolute value of x
sign(x) returns the value representing the sign of the number x
⑪ln(x) returns the natural logarithm of x
log(x,y) returns the base y logarithm of x
sqrt(x) returns the square root of a number
⑫floor(x) returns the largest integer value less than x
ceiling(x) returns the smallest integer value greater than x
⑬lcase(str) or lower(str) returns the result of changing all characters in the string str to lowercase
ucase(str) or upper(str) returns the result of converting all characters in the string str to uppercase
⑭avg(col) returns the average value of the specified column
⑮count(col) returns the number of non-null values in the specified column
⑯min(col) returns the minimum value of the specified column
⑰max(col) returns the maximum value of the specified column
⑱sum(col) returns the sum of all values of the specified column
⑲left(str, x) returns the leftmost x characters in the string str
right(str,x) returns the rightmost x characters in the string str
⑳reverse(str) returns the result of reversing the string str
Fourth, difficult questions:
1) What is the difference between on and where? Use on when connecting two tables. When using left jion, the difference between on and where conditions is as follows:
①The on condition is the condition used when generating the temporary table. It will return the records in the left table regardless of whether the condition in on is true.
②The where condition is to filter the temporary table after the temporary table is generated. At this time, there is no meaning of left join (the records of the left table must be returned), and all the conditions are filtered out if the conditions are not true.
2) The difference between INNER JOIN, LEFT JOIN, and RIGHT JOIN
Both tables of INNER JOIN have corresponding data at the same time, that is, missing data on either side will not be displayed.
LEFT JOIN will read all the data in the left data table, even if there is no corresponding data in the right table.
RIGHT JOIN will read all the data in the right data table, even if there is no corresponding data in the left table.
3) The usage of having in mysql (the difference from where)
1. The SQL standard requires that HAVING must refer to a column in the GROUP BY clause or a column used in the total function.
2. The HAVING clause must be located after GROUP BY and before ORDER BY.
3. If the HAVING clause references an ambiguous column, a warning will appear.
4. The HAVING clause can refer to the total function, but the WHERE clause cannot.
5. Do not use HAVING for entries that should be used in the WHERE clause.
6. The aggregation function cannot be connected to the where, so the having is used for use.
(The aggregation function is performed for the result set, but the where condition is not run after the result set is queried, so the main function is placed in the where statement, and an error will occur)
7. The group by clause is also used in conjunction with the where conditional statement. When combined, where comes first and group by comes after.
4) When join, where, group by, having, order by appear in a query statement at the same time, the execution order and writing order are:
1. First connect the data source after from (if there is join, execute the post-on condition first, and then connect to the data source).
2. Execute where xx to filter the entire table data and return the first result set.
3. Use group by for the previous result set and return the second result set.
4. Execute select xx for each group of data in the previous result set, execute it several times for several groups, and return the third result set.
5. Perform having xx to filter the previous result set and return the fourth result set.
6. Execute order by for the previous result set to sort.
7. Output the results
5. Follow-up:
Next time I will lead you to database mastery (from 0.1 to 99.9)