数据库简介以及增删改查
数据驱动测试(ddt-data,Driver,Test)
数据库的概念:
什么是数据:一切真实事物的抽象 把事物的属性和信息抽取出来并记录
数据库系统(DBS):统一管理数据的工具
Mysql:简单,免费,市场占有高
Oracle:大型商业数据库
Spl Server
DB2
Posgre DB
数据的管理:
1.什么数据(data):姓名,年龄(属性,字段column,列) 张飞,18(值,Data,行)
2.表Table的概念:是将一批数据整理到一起的产物
3.库Database的概念:放置多个表
DBMS数据库管理系统:操作和管理数据的系统
Myspl—>navicat sqlyog
Oracle--->pl/sql developer
DBA:数据库管理员 Database Administrator
Sql:数据库语言
MySQL的数据类型:
整数:int
小数:float,numeric(m,n)表示总长,n表示小数位数
字符:
*char(32)开辟多少字符空间,
*varchar(32)可变字符空间,首先给32,可以超出
其他:data time(时间)
枚举类型:enum:规定选项内容 可选一个或者多个
例:enum(‘唱歌’,’跳舞’,’打球’,’跑步’)
集合类型:set:只能输入规定的内容 只能选一个
例:set(‘男’,’女’)
解决MySQL乱码问题:Sharset=utf8(防止中文乱码)
数据库知识大纲:
1.构建数据库系统:
1.创建数据库: create database 数据库名;
2.查看数据库: show databases;
3.查看包含zhuqiong的数据库: show databases like ‘%qiong%’;
4.删除数据库: drop database 数据库名;
2.数据表的操作:use zhuqiong 首先切换数据库
1.创建表:create table表名称(sname char(32),#sname 表示放学生姓名char(32)表示学生姓名值占32个字符长度
age int #表示还放age,表示为数字,而且为整数);
有三个重要的类型:
*字段名称 例:姓名 性别 年龄 身高 详细参考数据类型
*字段类型 例:字符 单选 数字 整数小数
*约束: @非空约束(不能为空) not null;
@唯一约束(不能重复) unique key ;
@主键约束(不能为空和重复)primary key;
@外键约束 foreign key;
@默认值约束: default ‘值’例:money default 0
@自增长约束: auto__increment
1.修改表的字段
@增加字段 alter table 表名称 可以增加一个,也可多个
add column sex set(‘nan’,‘nv’)
add column 。。。。。。。。。。。。
增加字段可以指定顺序
alter table 表名称
放在第一列 add column sex set(‘nan’,‘nv’) first
放在sname后面 add column sex set(‘nan’,‘nv’) after sname
@修改字段 alter table 表名称
Modify column 要修改的字段(要写全)
@删除字段alter table 表名称
drop 字段名称
3.删除表:drop table 表名称1,表名称2,表名称3
4.查看某个数据库中所有表:show tables
查看指定的表;show tables like ’%表名称%’;
查看表结构:desc 表名称
2.数据的操作:
1.数据的增:insert into 表名称(字段名称1,2,3,)values(需要增加的值1,2,3)
@可以增加一组完整的数据
@可以增加一行不完整数据
@值可以是null(为空) 可以是default(默认值)
2.数据的删:delete from表名称 where 字段名=值/ 字段名 is null)
(Truncate 表名称)
3.数据的改(更新):update 表名称 set 字段名=新值 where 字段名=值/ 字段名 is null
@更新一条数据 标准语法
@一次性更新多个数据
@可以设置更新数据为null 和 default
@可以进行表达式更新 age=age+1
例:update student set sex = ‘男’ where sno=1
2.数据的查询:
1.全表查询(不带查询条件)
Select * from 表名称
2.查询部分字段(重点)
Select 字段名1,字段名2....from 表名称
3.查询待表达式
加入查询哪些学生是单号 哪些是单号
Select sno,sno%2,sname from 表名称
例:如果sno%2的值是单数则显示单号,如果是单数则显示单号
Select sno,if(sno%2,‘单号’,‘双号’),sname from 表名称(使用了函数)
例:如果address的值是空的,则默认为中国
Select sname,address,ifnull(address,‘中国’) from student
4.查询设别名称
加入别名称:直接空格别名称 也可用as
Select sno 序号,sname as 姓名 from student
5.查询排序(重点)order by 字段名称 排序方式:desc(高--低)asc(低----高)
组成:select 查询字段名称 from 表名称 order by 被排序的字段 排序方式
例:select sname, sex ,score from student order by score desc
注意:不写排序方式默认为升序排序 在语句的最后面做排序
如果有相同的则其次执行第二种select 查询字段名称 from 表名称 order by 被排序的字段 排序方式,被排序的字段 排序方式(中间用逗号隔开)
例:Select * from student order by score asc,classid desc
5.查询部分数据(limit)
注意:写在最后面
组成:select 查询字段名称 from 表名称 limit 5,3从第六行开始,显示前三行
组成:select 查询字段名称 from 表名称 limit 5显示前五行
- 去重复关键字(重点):distinct
注意:写在select后面
组成:select distinct查询字段名称 from 表名称
单表查询
1等值查询:
Select * from 表名称 limit 1 (查询第一行)
Select * from 表名称 where sno=120 (查看学号是120的所有数据)
Select * from 表名称 where sex=’女’ (查看sex是‘女’的所有数据)
Select * from 表名称 where name=’朱琼’ (查看name是‘朱琼’的所有数据)
2不等值查询(> >= < <= != <>)
Select * from 表名称 where sno>120 (查看sno大于120的所有数据)
Select * from 表名称 where sex!=‘女’(查看sex不是女的所有数据)
Select * from 表名称 where sno<>75 (查询sno不等于75的所有数据)
3模糊查询(一定要有like关键字,一定要用%关键字,它表示任意内容)
例:查询name中第一个字为朱的所有数据
Select * from 表名称 where name like‘朱%’
例:查询name中含‘朱’的所有数据
Select * from 表名称 where name like ‘%朱%’
例:下划线表示要匹配一个字符且只能有一个字符 两个下划线表示匹配两个字符
Select * from 表名称 where name like‘朱_’
逻辑语句(not),and or 多条件查询(与Python一样)
例:两个条件都满足
Select * from 表名称 where name like ‘朱%’and sex=‘女’
例:满足其中一个
Select * from 表名称 where name like ‘朱%’or sex=‘女’
例:查询满足两个条件的
Select * from 表名称 where name like (‘%朱%’and sex=‘女’)or (sex= ‘女’and sno=123)
例:查询不符合条件
Select * from 表名称 where name not like‘朱%’
4空置处理(is null)(is not null)独有的
Select * from 表名称 where money is null 是空值
Select * from 表名称 where money is not null 不是空值
例:查询money不为空并且name不包含朱
Select * from 表名称 where money is not null and name not like=‘%朱%’
5区间查询(between and)
例:查询工资1000到1500的员工
在两个数之间,小的数在前面,大的数在后面,且包括输入的数
Select * from 表名称 where money between 1000 and 1500
例:查询工资在1000到两千之外的员工(not 不包括边界)
Select * from 表名称 where money not between 1000 and 1500
6集合查询(in)
例:查询员工工资在1200 1300 1500 2000这些数
Select * from 表名称 where money in(1200,1300,1500,2000)
例:查询员工工资不在1200 1300 1500 2000这些数
Select * from 表名称 where money not in(1200,1300,1500,2000)
例:查询两个字段 满足两组值的数据
Select * from 表名称 where(字段1,字段2)in((值1,值2),(值3,值4))
7分组查询
MySQL函数
Select score,floor(score), from表名称; 不管小数后面是多少,取整数
查询姓名为2个字符的所有数据 char_length(字段名)=数字
Select * from student where char_length(sname)=2;
得到当前日期 current_date
Select current_date;
根据出生日期得到年龄 datediff();
Select birth,datediff(current_date,birth)/30/12 from student
round();四舍五入
把姓名和密码进行加密 MD5(str)
Select * from student where sname=MD5(‘朱琼’)and password=MD5(‘123456’)
聚合函数只能在select中出现,不能在where中出现
统计个数的函数,不会统计空值null:count()
根据学生的学号统计学生共有多少人
例:select count(sno) from student
根据学生的姓名统计学生共有多少人
例:select count(sname) from student
求最大值函数 max()
select max(sal) from 表
求最小值函数 min()
select min(sal) from 表
求和函数(只能是数字),跳过空值null, sum()
select sum(sal) from 表
求平均值(只能是数字),跳过空值null avg()
select avg(sal) from 表
可以四舍五入
select round(avg(sal)) from 表
聚合函数(分组函数)
select avg(sal),sum(sal),max(sal),min(sal) from 表;
分组查询
分组的核心:group by;按什么分组:确定组名称
查询每个部门有多少人
查询各个部门各有多少人
分组后干嘛(针对谁)
例:select deptno, 让分组更直观
group_concate(ename), 把所有的姓名集合在一起(可要可不要)
count(ename) 统计有多少人
from emp 来自emp表
group by deptno 按deptno(部门)分组
一次筛选 where 写在分组前
二次筛选 having 写在分组后
Where针对普通字段筛选,在 group by 前面
Having 针对聚合函数进行筛选,在group by 后面
例:select job, 让分组更直观
count(ename) 统计有多少人
from emp 来自emp表
Where ename like ‘%a%’ 筛选名字中包含‘a’的名字
group by deptno 按deptno(部门)分组
Having min(sal)>1500 先分组,再筛选最低工资大于1500
多表查询:
等值连接:
1:Select st.*,sc*. 查询两个表的所有数据
From student st,score sc 来自两个表的查询 设别名称
Where st.cno=sc.cno 两个表之间的关联
2: Select st.*,sc*. 查询两个表的所有数据
From student st join score sc on st.cno=sc.cno 来自两个表的查询 设别名称 用join on 关 键字连接
From student st join score sc on st.cno=sc.cno join counse co on sc.sno=co.sno 来自三个表的查
Where degree>=80 where放筛选条件
非等值连接:(between and)
Select e.*,S.*
From emp e join sut s on e.sai between s.lisal and s.hisal 工资在某某等级之间
外连接(左连接和右连接);
单表查询
自身表连接:
Select a.ename,b.ename
From family a,family b 把一张表分开
Where a.pid=b.fid 相同的关联
外连接(左连接和右连接)right left 在join前面
单表:From family right join family on family.pid=family.fid
多表:form emp left join depn on emp.sal=depn.sal
子查询
子查询语句在from语句中
子查询在where语句中,但子查询的结果是单列单行
如果子查询得到的结果有多个值,用in关键字
例:查询最低工资的员工信息
Select * from emp sal=(select min(sal),from emp)
例:查询30部门最早入职的员工领导的基本信息
select * from emp where empno=(
select mgr from emp where hiredate=( select min(hiredate) from emp where deptno=30))
子查询在where语句中,但子查询的结果是单列多行或是多列多行
例:工资最高的那个员工,他的手下有几个
select max(sal) from emp 先得出最高工资
select empno from emp where sal in (select max(sal) from emp) 得出最高工资人的编号
select count(ename) from emp where mgr in (select empno from emp where sal in (select max(sal) from emp)) 得出手下的领导的编号属于这个领导
3.用户的管理(权限管理)
4.视图,索引,存储过程
5,Navicat的操作