--简单查询 --投影查询 /* 简单查询关键字说明: all :指定显示所有的记录,包括重复行。all是默认设置。 distinct :指定显示所有的记录,但不包括重复行。 top n [percent]:指定从结果中返回前n行,或者前n%的数据记录 * : 表示所有记录 */ --选择一个表
--简单查询
--投影查询
/*
简单查询关键字说明:
all :指定显示所有的记录,包括重复行。all是默认设置。
distinct :指定显示所有的记录,但不包括重复行。
top n [percent]:指定从结果中返回前n行,或者前n%的数据记录
* : 表示所有记录
*/
--选择一个表中指定的列
--查询学生表中“姓名”,“年龄”
select Sname , Sage from Student
--查询学生表中的所有记录
select * from Student --用*表示所有的列
--查询学生表中所有学生的姓名,,去掉重复行
select distinct Sname from Student
/*
用distinct关键字可以过滤掉查询结果中重复行
*/
--查询学生表中前三行的记录
select top 3 * from Student
/*
用top n 可以指定查询表中的前n行记录
*/
--查询学生表中前50%的记录
select top 50 percent * from Student
/*
用top n percent可以指定查询表中前n%的记录
*/
--修改查询结果中的列标题
/*
修改查询列名的常用方法
方法一:在列表达式后面直接给出列名
方法二:用as关键字来连接列表达式和指定的列名
*/
--将查询结果中的Sname给成“姓名”,Sno改成“学号”,Sage改成“年龄”
--方法一:
select Sno 学号,Sname 姓名,Sage 年龄 from Student
--方法二:
select Sno as 学号,Sname as 姓名,Sage as 年龄 from Student
--计算列值
select 100-Sage as 寿命 from Student
--选择查询
/*
选择查询语法:
select select_list from table_list
where search_condition
//其中有多种语句可以做为条件表达式
分别是“关系表达式”、“逻辑表达式”、“between语句”、“in语句”、“like语句”、“is [not] null语句”、“复合语句”
*/
--使用“关系表达式”做为查询条件。。。。。。
select * from SC
--查询所有成绩大于等于90分的记录
select * from SC Student where Grade>=90
--使用“逻辑表达式”做为查询条件。。。。。。
/*
SQL 中的逻辑表达式:
not :非
and :与
or :或
*/
select * from Student
--在学生表中查询年龄是19岁的男学生
select * from Student where Sage = 19 and Ssex = '男'
--在学生表中查询年龄是19或20岁的学生
select * from Student where Sage = 19 or Sage = 20
--在学生表中查询年龄不是19岁的学生
select * from Student where not Sage = 19
--使用between关键之做为查询条件。。。。。。。
/*
between 语法:
表达式 [not] between 表达式1 and 表达式2
使用between关键字可以方便的控制查询结果数据的范围
注意使用between形成搜索范围是一个“闭区间”
*/
--查询所有年龄"大于等于"18岁且"小于等于"20岁的学生
select * from Student where Sage between 18 and 20
--查询所有年龄不在18到19岁之间的学生
select * from Student where Sage not between 18 and 19
--使用in(属于)关键字做为条件表达式......。。。。。
/*
同between关键字一样,in关键字的引入也是为了更加方便的限制检索数据的范围
*/
/*
in关键字的语法如下:
表达式 [not] in (表达式1,表达式2,...)
*/
select * from Student
--查询所有年龄为18和19岁的学生
select * from Student where Sage in (18,19)
--使用like关键字语句做为条件语句。。。。。。
/*
like 关键字搜索与指定模式匹配的字符串
*/
/*
通配符介绍:
% :包括零个或多个字符的任意字符串
_ : 任何单个字符
[]: 代表指定范围内的单个字符,[]中可以是单个字符(如[acef]),也可以是字符范围(如[a-f])
[^]: 表示不在指定范围内的单个字符,[^]中可以是单个字符(如[^abef]),也可以是字符范围[^a-f]
*/
/*
通配符的示例
like 'AB%' 返回以AB开始的任意字符串
like 'Ab%' 返回Ab开始的任意字符串
like '%abc'返回以abc结尾的任意字符串
like '%abc%'返回包含abc的任意字符串
like '_ab'返回以ab结尾的任意三个字符的字符串
like '[ACK]%'返回以A、C或K开始的任意字符串
like '[A-T]ing' 返回四个字符的字符串,以ing结尾,其首字母的范围是A到T
like 'M[^c]%' 返回以M开始且第二个字符不是c的任意长度的字符串
*/
select * from Student
--查询所有姓王的学生
select * from Student where Sname like '王%'
insert into Student(Sno,Sname,Sage,Ssex,Sdept)
values ('008','张四',20,'男','sc')
--查询所有名字中带四字的学生
select * from Student where Sname like '%四%'
//
--使用isnull(是否为空)查询。。。。。。
/*
说明:在where语句中不能使用比较运算符来进行控制判断,只能使用空值表达式来判断某个表达式是否为空值
语法如下:
表达式 is null
或
表达式 is not null
*/
--查询所有学生姓名为空的学生。。。。。。。。。
select * from Student where Sname is not null
--使用复合条件查询。。。。。。。。。。。。。。
/*
使用复合语句的时候需要使用逻辑运算符把多个条件语句合并
and
or
not
每个单独的条件语句可以使用()小括号括起来
*/
//聚合函数(求记录数据的处理结果)
--聚合函数 (求记录数据的处理结果)
/*
聚合函数是在SQL Server 中已经定义好了的一些列函数
注意:这些函数处理的是一个数据集合,而不是一行单独的记录
*/
/*
sum()返回一个数字列或计算列的总和
avg()返回一盒数字列或计算列的平均值
min()返回最小值
max()返回最大值
count() 返回一个数据列中数据项数
count(*) 返回找到的行数
*/
select * from SC
--求SC表中成绩的平均值
select AVG(Grade ) as 成绩平均值 from SC
--求SC表中成绩的总和
select sum(Grade ) as 成绩平均值 from SC
--求SC表中的项数
select count(Grade ) as 记录条数 from SC
select COUNT(*) as 记录条数 from SC

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.