1 DDL 数据定义语言(create drop alter)
-- 2 DML 数据操作语言(insert delete update)
-- 3 DQL 数据查询语言(select)
-- 4 DCL 数据控制语言(revoke ,GRANT)
2 表 table,主要用于存放数据
-- 语法: create table 表名( 字段1,字段2,...);
3 插入数据 insert into
insert into 表名(字段1,字段2...) values(值1,值2,....);
-- 注意如果字段全插入,可以省略字段的描述
-- 除数值类型外,其余类型数值需加单引号
-- 4 查询语句
-- 语法: select 查询字段 from 表名;
select * from Emp;-- *表示所有的表头字段
select name , salary from Emp;-- 查询部分字符数据
select name , salary , job from Emp;
-- 待条件的查询
where '条件是'
-- 张无忌的薪水
select salary
from Emp
where name = '张无忌';
-- 5 特殊的条件查询
5.1 范围查询 ,给定的条件是一个数值范围
关键字 between A and B,包含A和B的
-- 查询薪水在7000~10000之间的员工姓名
-- 练习1:查询编号为1007号员工的信息(所有字段)
select name from emp
where salary >=7000 and salary <=10000;
select name from emp
where salary between 7000 and 10000;
5.2 并列查询 , 在同一个列上给出不同的条件
关键字 in(值1,值2,...)
-- 查询10号部门和20部门的员工信息
select * from emp
where dept = 10 or dept = 20;
select * from emp
where dept in (20,30);
5.3 模糊查询,给定的条件是不确切的
like 是模糊查询的关键字
%,用在模糊查询中表示空字符1个或者多个字符
_,用于表示一个任意字符
select name from emp
where name like '张%';
5.4 空值查询,给定的条件和空值有关
is null 为空 is not null
-- 查询哪些员工没有奖金
select name from emp
where comm is null;
特殊的查询
1 去重 DISTINCT,放在select之后
-- 例: 查询员工所有的部门编号(不重复)
select DISTINCT dept from emp;
2 排序
order by 字段 [asc/desc] ,默认是升序
查询语句:
范围查询:在最后面加上between A and B
并列查询:即是查询时需要同时范围多个字段的数据
方式:在最后面加上in(A,B,C,D)
模糊查询
在最后面加上:like 条件
_是代表有一个字符
%代表是有0个或多个字符
空值查询:
在后面加上is null 则代表为空
is not null 代表不为空
去重:在select后面加上distinct则会有去重的效果
排序:表名后面加上order by 字段 asc 升序
表名后面加上order by 字段 desc 降序
注意默认为升序
3 支持算术运算
例:查询张无忌的年薪
select name , salary*12 from Emp where name = '张无忌';
查询所有人的月收入
select name ,salary+comm from Emp; -- 这时如果相加的值中存在一个空值则相加的和会变成空值
select name, salary ,comm,salary+IFNULL(comm,0) from Emp;
使用ifnull函数可以检测comm中的值是否为空,如果是则用后面的0代替。