###课程回顾
- 数据库相关SQL
- 查询所有 show databases;
- 创建 create database db1 character set utf8/gbk;
- 查询详情 show create database db1;
- 删除数据库 drop database db1;
- 使用数据库 use db1;
- 表相关SQL
- 创建表 create table t1(name varchar(10),age int)charset=utf8;
- 查询所有 show tables;
- 查询详情 show create table t1;
- 表字段 desc t1;
- 删除表 drop table t1;
- 修改表名 rename table t1 to t2;
- 添加字段 alter table t1 add 字段名 类型 first/after xxx;
- 删除字段 alter table t1 drop 字段名;
- 修改字段 alter table t1 change 原名 新名 新类型;
- 数据相关SQL
- 插入数据 insert into t1(name,age) values(xxx,xxx),(xxx,xxx);
中文问题 如果存在中文问题 执行 set names gbk; - 查询数据 select 字段信息 from t1 where 条件;
- 修改数据 update t1 set xxx=xxx where 条件;
- 删除数据 delete from t1 where 条件;
- 数据类型
- 整数 int(m) 和 bigint(m) m代表显示长度 需要结合zerofill关键字使用
- 浮点数 double(m,d) m代表总长度 d代表小数长度 超高精度浮点数decimal
- 字符串: char:不可变长度 执行效率高 255 varchar 可变长度 节省空间,建议保存255以下 实际最大长度65535 text 可变长度 建议保存大于255 并且65535以内
- 日期: date年月日 time时分秒 datetime默认值为null 最大9999-12-31 timestamp: 默认值为系统时间,最大 2038-1-19
- 导入*.sql 文件
source 路径; - is null 和 is not null
- 去重 distinct
- 比较运算符 > < >= <= = !=和<>
- and和or
- in(x,y,z)
- between x and y 包含x和y
###综合练习 - 查询有上级领导并且是3号部门的员工信息
select * from emp where mgr is not null and deptno=3; - 查询2号部门工资在1000到2000之间的员工姓名 工资和部门编号
select ename,sal,deptno from emp where deptno=2 and sal between 1000 and 2000; - 查询1号部门工资为800和1600的员工信息
select * from emp where deptno=1 and sal in(800,1600); - 查询1号和2号部门工资高于2000的员工信息
select * from emp where sal>2000 and deptno in(1,2); - 查询员工表中出现的部门编号有哪几个
select distinct deptno from emp;
###练习: - 创建数据库mydb2 字符集utf8 并使用
create database mydb2 character set utf8;
use mydb2;