Java实训笔记(八)之MYSQL篇 今天首先进行函数的讲解,函数主要分为: 1、 聚合函数 2、 字符串函数 3、 日期函数 4、 数学函数 聚合函数:由一组值计算后产生一个值。 常用函数有: 函数名 函数意义 Count() 统计非空行数 Max()、min() 计算最大数、
Java实训笔记(八)之MYSQL篇
今天首先进行函数的讲解,函数主要分为:
1、 聚合函数
2、 字符串函数
3、 日期函数
4、 数学函数
聚合函数:由一组值计算后产生一个值。
常用函数有:
函数名 |
函数意义 |
Count() |
统计非空行数 |
Max()、min() |
计算最大数、计算最小数 |
Avg() |
求平均 |
Sum() |
求和 |
在函数中还可以使用distinct来表示不重复数据。
问题:计算每门课程的参加考试的人数?(假定score来表示成绩表,此表中有sid表示学号、cid表示课程号、grade表示成绩)
解决问题方法:
1、统计人数使用count;
2、每门课程需要使用分组(group by)
答案:
Select count(*) from score where grade is not null group by cid;
字符串函数:在mysql字符串中第一个字符的编号从1开始。
常用函数有:
函数名 |
函数意义 |
Length()、bit_length() |
求字符串长度、二进制字符串的长度 |
Concat() |
字符串连接 |
Ascii() |
求平均 |
Char |
求和 |
Ltrim() |
去除左边空格 |
Rtrim() |
去除右边空格 |
Left() |
从左边开始取字符 |
Right() |
从右边取开始取字符 |
Instr() |
查找位置 |
Substring() |
取子串 |
Rerverse() |
字符串反转 |
Bin() |
二进制 |
Oct() |
八进制 |
Hex() |
十六进制 |
Locate() |
得到字符的出现的位置 |
Repeat() |
字符重复 |
Format() |
格式字符串 |
日期函数:用来处理日期的函数
1、 ADDDATE(date,INTERVAL expr type) ADDDATE(expr,days) (ADDDATE()就是DATE_ADD()的同义词)
Type值 |
type 值 |
MINUTE_SECOND |
MICROSECOND |
HOUR_MICROSECOND |
SECOND |
HOUR_SECOND |
MINUTE |
HOUR_MINUTE |
HOUR |
DAY_MICROSECOND |
DAY |
DAY_SECOND |
WEEK |
DAY_MINUTE |
MONTH |
DAY_HOUR |
QUARTER |
YEAR_MONTH |
YEAR |
MINUTE_MICROSECOND |
SECOND_MICROSECOND |
例如:推迟一个月
Select adddate(curdate(),interval 1 month);(curdate()获得当前日期,now()获得当前日期和时间,curtime()获得当前时间)
2、 Datediff:两者日期之间的差值
3、 DAY() 或者dayofmonth()
4、 Dayname():
5、 Dayofweek
6、 Dayofyear
7、 Extract():用来提取日期中某一个部分;
问题:统计本周内的营业额.(销售表sale(sid,sname,sdate,count,number));
解决问题方法:如何确定本周,在这里就要用到extract函数用来取出日期中的周数即可。
代码:select sum(count*number) from sale
Where extract(week from curdate()) = extract(week from sdate);
数学函数
Random():用来产生随机数。
多表查询:
实现多表查询主要通过连接和子查询来实现。
连接的方式:连接分为内连接和外连接,外连接分为做外连接和右外连接。
示意图:(通过学生信息表和成绩表,两张表有一个公共字段叫做学号)
代码示例:
三张示例表的结构:学生信息表stdinfo(sid pk,sname,sex ,address),成绩表 score(sid ,cid ,grade,pk(sid,cid)),课程表 course(cid pk,cname)注明:pk表示主键
问题:查询学生的考试成绩。
解决问题方法:根据以上信息需要用到几张表,然后在看获得的信息是否是两张表的共同的值。
代码:select sname,cid,grade from stdinfo s inner join score sc on s.sid = sc.sid;
问题:把对应的课程名称显示出来.
问题:显示所有学生的考试信息。
解决问题的方法:在这里需要用到内连接和左外连接。
子查询:在语句中含有select查询,称为子查询,子查询之外的称为外部查询,先计算子查询,然后在计算外部查询。
语法:select 列名 from 表名 where 。。(子查询)
外部查询可以是update或者delete,他们都有一个共同特点就是都含有where子句,在where中通过运算符与子查询连接,可用的运算符为比较运算符和in。
问题:查找和川大出版社在同一个城市的作者。
解决问题方法:首先要查出川大出版社所在的城市,然后在根据这个城市查找作者。
表结构:author(aid,author_name,sex,birth,city)
Publisher(pid ,publisher_name,city)
代码:select author_name,sex birth ,city from author where
City in (select city from publisher where publisher_name=’川大出版社’);
连接实现方式:
Select author_name,sex,birth,city from author inner join (select city from publisher where publisher_name=’川大出版社’) c on author.city = c .city;

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.