常用函数:
version()
user();
curTime()
current_time();
curDate();
current_Date();
length();
trim();
rtrim();
插入数据的两种方式:
insert into...
load data local infile '文件路径' into table 表名;
如果SQL语句写错了, 可以通过 \c 结束它的执行.
进行改删的操作时, 一定一定一定要加条件.
查询:
select distinct species from pet;
//创建score表, 里边有sid, sname, chinese, math, english,
//要求按照语文成绩排序, 语文一样, 按照数学降序, 数学一样, 英语升序.
//order by chinese, math desc, english;
select * from shop where price=(select max(price) from shop); //子查询.
select article, count(*) from shop group by article; //分组查询.
聚合函数:
count()
sum()
max()
min()
avg(); //即使是获取整数列的平均值, 结果也是一个: 小数.
SQL语句的分类:
D, M, C. Q
定义, 操作, 控制, 查询
M: 数据操作语言:
可以结合事务事务.
show variables like '%commit%';
select @@tx_isolation
set session transaction isolation level read uncommitted;
如何备份数据库:
mysqldump -uroot -p123456 test01 > /root/test01.sql //导出, test01是数据库的名字
mysql -uroot -p123456 test01 < /root/test01.sql //导入, test01是数据库的名字
如何备份数据表: //以下方式只备份表结构(字段, 类型, 不包含主键), 还有数据.
数据表pet --> 数据表 petTemp(备份表)
1) 备份表不存在:
create table petTemp select * from pet;
2) 备份表存在:
insert into petTemp select * from pet;
多表查询:
交叉查询:
格式:
select * from A,B; //获取到笛卡尔积. A表的总数据条数 * B表的总数据条数.
连接查询:
内连接: //获取到的表之间的: 交集.
显式内连接:
select * from A inner join B on 条件;
//select * from A join B on 条件;
隐式内连接:
select * from A, B where 条件;
外连接: //某表的全集 + 表的交集.
左外连接: //左表的全集 + 左右表的交集
select * from A left outer join B on 条件;
//select * from A left join B on 条件;
右外连接: //右表的全集 + 左右表的交集.
select * from A right outer join B on 条件;
//select * from A right join B on 条件;
子查询:
概述: 一个SQL语句的查询条件需要依赖另一个SQL语句的查询结果.
示例:
//从商品表中获取最高价的那条数据 从商品表中获取最高价
select * from product where price = (select max(price) from product);
关键字:
in
select * from users where id in (1, 3); //获取id为1, 或者id为3的数据.
any //任意一个
select a_id from A where a_id % 2 != 0; //1, 3
select * from users where id > any(1, 3); //查询users表中所有的编号: id > 1 or id > 3
all //所有, 和any刚好相反.
exists: 根据查询条件有无数据, 决定是否要展示对应的数据. //同生共死.
select * from pet where exists(select * from shop); //shop有数据, 则打印pet表中的数据
//结果: 因为shop有数据, 所以会打印pet表的数据.
select * from pet where exists(select * from users); //users有数据, 则打印pet表中的数据
//结果: 因为users表没有数据, 所以不会打印pet表的数据.
//要求: 表结构一致.
UNION 语句**:用于将不同表中相同列中查询的数据展示出来;(不包括重复数据)
//合并表, 不包括重复数据.
select * from A union select * from B;
**UNION ALL 语句**:用于将不同表中相同列中查询的数据展示出来;(包括重复数据)
//合并表, 包括重复数据.
MySQL中的数据类型:
数值:
tinyint, int, double
日期/时间:
Date; //获取年与日
time; //获取时分秒
datetime: //年月日时分秒, 时间可以自定义
timestamp: //年月日时分秒, 采用当前系统默认时间. //掌握
一个标准的SQL语句的写法:
select * from 表名 where 条件(分组前查询) group by 要分组的列 having 条件(分组条件)
order by 字段[ASC/DESC];
-------------------------------String类中的常用方法--------------------------------------------
判断功能: //首尾中空加判断
boolean startsWith(String s); //判断字符串是否以 指定的字符串 开头
boolean endsWith(String s); //判断字符串是否以 指定的字符串 结尾
boolean contains(String s); //判断字符串是否 包含指定的字符串
boolean isEmpty(); //判断字符串是否为空
boolean equals(String s); //判断字符串 和指定的字符串是否相同, 区分大小写
boolean equalsIgnoreCase(String s); //判断字符串 和指定的字符串是否相同, 忽略大小写
获取功能: //截长取位取元素
String substring(int start, int end); //从字符串中截取 指定索引处的字符串, 包括start位置, 不包括end位置, 包左不包右
String substring(int start); //从字符串中截取 指定索引处的字符串, 从start位置到字符串末尾
int length(); //获取字符串的长度
int indexOf(); //判断字符(串) 在字符串中第一次出现的位置
int lastIndexOf(); //判断字符(串) 在字符串中最后一次出现的位置
char charAt(); //根据给定的索引, 获取对应位置的字符
转换功能: //大小拼串转数组
String toUpperCase(); //把字符串中所有的字母都转换成其对应的大写形式
String toLowerCase(); //把字符串中所有的字母都转换成其对应的小写形式
String concat(); //拼接字符串, 只不过我们一般不使用它, 用+号
char[] toCharArray(); //把字符串转换成其对应的 字符数组
byte[] getBytes(); //把字符串转换成其对应的 字节数组
其他功能: //除空切换字典排
String trim(); //移除字符串两端的空格
String[] split(); //按照指定的字符串 切割字符串
String replace(); //替换
int compareTo(String s); //将字符串按照字典顺序排序.