Table of contents
Common functions
The concept and characteristics of functions:
Consistent with the concept of functions in java and js
Features: database functions must have return values (one row and one column)
Several function types
1. Date function
now() gets the current date;
Example: select now();
day() gets the day part of the specified date;
Example: select day(now());
month() Gets the month part of the specified date;
Example: select month(now());
year() gets the month part of the specified date;
Example: select year(now());
date_format() Converts the date to a string in the specified format;
Example: select date_format(now(), '%Y-%m-%d %H:%i:%s ');
str_to_date(): Convert a date in a specific format to a date;
example: select str_to_date('2017-01-06 10:20:30','%Y-%m-%d %H:%i:% s');
Comprehensive example:
1)直接按年份计算学生年龄
SELECT t.sname, YEAR(NOW()) - YEAR(t.birthday) FROM t_student t
2)按照出生日期来算,当前月日 < 出生年月的月日则(说明月份还没到),年龄减一
SELECT sid,sname,birthday,YEAR(NOW())-YEAR(birthday) '年龄',
YEAR(NOW())-YEAR(birthday) + IF(CONVERT(DATE_FORMAT(NOW(),'%m%d'),SIGNED)-CONVERT(DATE_FORMAT(birthday,'%m%d'),SIGNED)<0,-1,0) '真实年龄'
FROM t_student;
3)查询本月过生日的学生信息
SELECT * FROM t_student t WHERE MONTH(NOW()) - MONTH(t.birthday) = 0;
2) The example running results are as follows:
2. String function
upper() Converts to uppercase characters
Example: select upper('faafafa')
lower() Converts to lowercase characters
Example: Sselect lower('FEFEFF')
replace() Searches and replaces substrings in a string
Example: select replace( 'www.google.net','w','n')
substring() last substring starting at a position with a specific length
Example:
select substring('abcdefghijk', 1, 3)
select substring((' abcdefghijk', 4);
select substring(('abcdefghijk', -3);
trim() remove leading and trailing spaces
Example: select trim(' fdfdfdfd ');
length()
Example to get the string length: select length('abcdef') ;
3. Digital functions
floor() rounds down
Example: select floor(123.8934);
ceil(
) rounds up Example: select ceil(123.8934)
round() rounds
down Example: select round(123.8934, 2);
4. Aggregate function
Features: It is commonly used together with group by, or it can be used alone. For example, if you need to filter, you can use the having clause
SUM to sum
COUNT to count the number of records
AVG to find the average value,
MAX to find the maximum value ,
MIN to find the minimum value .
merge
Keyword:
union puts all query results together and removes the same records
union all combines all the query results together without removing the same recordsThe premise of merging: the number of columns in the result set is the same
. Scenario: in the project statistical report module, it is used to merge data
select 'abc', 123
union
selet 'def',456
select 'abc', 123
union
select 'abc', 123
select sid fromt_score where cid = 1
union
select sid from t_score where cid = 2
# 注意此处去掉了重复的值,可以与下面的语句执行结果比较
select sid fromt_score where cid = 1
union all
select sid from t_score where cid = 2
select 'abc', 123
union all
select 'def', 456
select 'abc', 123
union all
select 'abc', 123
view index
view
Concept and Features:
A view is a kind of virtual table , which is a table derived from one or more tables in the database. The
database stores the definition of the view, but does not store the data of the view, and the data is still stored in the original table.
When using a view to query data, the database will obtain data from the original table
(note: materialized views are not included here, currently mysql does not support materialized views by default)
View role:1) Simplify operations
2) Increase data security
3) Improve the logical independence of tables
Basic syntax:create view view name as select statement
example :create view stu_score_statistics as select t1.sid, t1.sname, t1.ssex, t2.courses, t3.total total_score from t_student t1 left join (select sid, count(*) courses from t_score group by sid) t2 on t1.sid=t2.sid left join (select sid, sum(score) total from t_score group by sid) t3 on t1.sid=t3.sid
index
concept:
The index is composed of one or more columns in the database table, and its role is to improve the query speed of the data in the table.
It can be understood as the role of the directory of the book
Advantages and disadvantagesAdvantages : For medium or large tables, proper use of indexes can significantly improve query performance
Disadvantages : Increases the work of index maintenance, making insertion, modification, and deletion operations slower
Classification:Ordinary index (basic index, the purpose is to improve query performance)
Unique index (in addition to improving query performance, it can also avoid duplication of column values)
Primary key index (special unique index, a table can only have one primary key, and no Null value)
combined index (index generated by combining multiple columns, you need to pay attention to the index order)
full-text index (used to support full-text search (FULLTEXT))
Index maintenance:Create
syntax
CREATE [UNIQUE|FULLTEXT] INDEX index name ON table name (field name [(length)][ASC|DESC])
Example
CREATE INDEX sname_inx ON t_student(sname);
Modify
syntax
ALTER TABLE table name ADD [UNIQUE|FULLTEXT] INDEX index name (field name[(length)][ASC|DESC])
example
ALTER TABLE t_student ADD INDEX birthday_inx(birthday);
delete
syntax
DROP INDEX index name ON table name
example
DROP INDEX birthday_inx ON t_student;
Common error codes
1075
has an auto-increment key, but it is not that it is not set as the primary key.
1142 The
operation is rejected, generally because there is no permission.
1064
Generally, there is a syntax error, such as keyword error, missing spaces, missing after the sql statement in the sql script; and other reasons
1048
columns Cannot be null
1055
Not in GROUP BY
1265
The format of the saved data is different from the definition
1366
Data encoding
1451
Foreign key constraint violation