DML:数据操纵语言;
dml操纵表中的数据(记录);
操作分为四种:CURD(增删改查)
CURD:create /update/retrieve
- insert
: 向表中插入数据。
语法: insert [into] <表名> [(列名)]
values (<值列名>),(),()
– 创建一个表
create table tb_class(
classid int primary key auto_increment,
className varchar(20) unique not null,
Intr varchar(20) not NULL default '一个班级'
)
– e.g. 向表中插入数据(形式1)
insert into tb_class
values (10,'java1809','一个班级');
– e.g.向表中插入数据(形式2)
insert into tb_class (Intr,className,classid)
values('这是一个班级','java1909',11);
insert 小结:1.使用inserte一次必须插入一整条完整记录;
2.插入记录要满足表中的完整性约束;
3.用 default 可以代替缺省内容;
4.可以插入多条记录()用 , 间隔开来。
insert into tb_class
values (21,'java0000','666666'),(22,'java1111',DEFAULT)
- select查询语句
语法:
select <列名> |* from<表名>
where 条件语句;
//注意:列名中可以使用表达式如 * 号+ - / 运算符等。
//where语句中的条件查询用 and连接。
select中的where:
- -比较运算符:
<,>,= ,!=, <>
大于 小于 等于 不等于(<>);
- 逻辑运算符
:and or not
与 或 非 ; - between (<>) and ( <>)
; 在连续区间内; - not between (<>) and ( <>)
不在连续区间内
(<>)in ( , , ,) 不连续区间;
班级编号在1,或3,或5的班级
where classid in (1,3,5)
学位是bachelor或者是master的人
where degree in('bachelor','master')
- is null /is not null
是否非空;
null 不能用=比较。 - like ‘ _% ’
模糊查询;
_ :代表任意一个字符;
% :代表任意多个字符.
//举例:sanme like ‘_玉’ ;查找叫X玉的人(名字中有两个字);
/// sname like ‘%玉’;查找叫XXXX玉的人(名字有好几个字)。 - distinct
剔除重复(多用于查找一列内容时使用)
//select distinct degree from tb_student; 列出有多少种学历。 - regexp
匹配正则表达式 。
()regexp’^ 正则式 &’
^ 以‘’‘’‘’开头
&以‘’‘’‘’‘结尾 - 聚合函数(分组函数)
count()求和
sum()求和
avg()平均
max()最大值
min()最小值
!!!:count(*)是其中()内唯一能用 * 的函数。
!!!:where中不能使用分组函数,having (用在 group by之后)可以使用分组函数。 - group by < xx> : 按xxxx分组
///求不同学历的平均工资
> select degree ,count(*),avg(salary) > from tb_student > group by degree;
-
having :(与where一样有过滤作用)
!!!: {where :在group by 之前使用,
having 在group by 之后使用}
//选出不同学历且人数大于1的平均工资(在where中用count函数不可用)select degree ,count(),avg(salary)
from tb_syudent
group by degree
having count()>1; -
order by <列名> | 表达式 [desc | asc]
按xxxx对查询结果排序(一般用在语句group by 之后)
select *from tb_student order by salary desc, birth;
desc: 代表倒叙
asc :代表正序(默认正序)
!!!排序依据可以有多个 用 , 间隔开(先按第一个排序,第一排序不能确定时采用第二排序)
- limit 0 ,5 ;
获得部分查询结果
(比如 : 从第0个找 找 5 个)。