-- 查找
SELECT name,age from 表名;
-- *查找所有字段
SELECT * from 表名;
-- 条件查询 where子语句在删除、修改也能使用
-- 运算符
-- =等于 >大于 =大于等于
-- !=和<>为不等于
SELECT * from 表名 where age != 20
-- between .. and 介于谁到谁之间 [] 左开右开
SELECT * from 表名 where age BETWEEN 10 and 25
-- and 于 or 或 not 非 主要用在is in
SELECT * from s表名where age>=1 and age
SELECT * from 表名 where age=20 or age=15 or age=1
SELECT * from 表名 where age20
-- in 零零散散的挑表示包含
SELECT * from 表名 where age in(21,1,34)
SELECT * from 表名 where id not in(2,1,4)
-- is null 判空
SELECT * from 表名 where age is null
SELECT * from 表名 where age is not null
-- 模糊查找 like 占位符: _代表一个字符 %代表任意个字符
SELECT * from 表名 where name like "张_"
SELECT * from 表名 where name like "%张%"
SELECT * from 表名 where name like "张%"
SELECT * from 表名where name like "%张"
-- 分页查询 limit子语句
-- limit a,b a表示起始的索引值 b表示查询的个数
-- limit b offset a
-- 第一页
SELECT * from 表名limit 0,5
SELECT * from 表名limit 5 offset 0
-- 第二页
SELECT * from 表名limit 5,5
SELECT * from 表名limit 5 offset 5
-- 第三页
SELECT * from 表名limit 10,5
SELECT * from 表名limit 5 offset 10
-- 一页大小pageSize 当前页码 page
SELECT * from 表名limit (page-1)*pageSize,pageSize
SELECT * from 表名where name like "%张%" limit 0,2
-- 排序子语句
-- order by 列名 desc(降序) asc(升序)可以不用写默认为升序
SELECT * from 表名ORDER BY age
SELECT * from 表名ORDER BY age desc
-- 先where 中排序 后限制
SELECT * from 表名where sex="女" order by age desc
SELECT * from 表名where sex="女" order by age desc limit 0,3
-- 聚合函数和分组函数
-- 聚合函数 min()最小 max()最大 sum()求和 avg()求平均 count()求数量不统计值为null的记录
SELECT min(age) from 表名
SELECT avg(age) from 表名
SELECT max(age) from 表名
SELECT count(age) from 表名
SELECT count(*) from 表名
-- 分组函数
-- group by 分组函数 当分完组之后查找用having having用法和where一摸一样
SELECT avg(age),class from 表名group by class having class=1
SELECT max(age),class from 表名group by class having class=3 or class=1
-- 添加 insert into 表名(字段1,字段2,字段3) values(字段1内容,字段2内容,字段3内容)
insert into 表名(name,age,sex) values("暮夏",22,"男")
-- 修改 不加where会全修改
update 表名set name="aaa",age=2,sex="女" where id = 12
-- 删除
delete from 表名 where id=12
-- 删除多个指定内容
delete from 表名 where id in (2,4,6,7)
表于表的关系:一对一 一对多(多对一) 多对多
一对一: 丈夫表和妻子表
一对多(多对一):学生表的班级表 员工表和部门表
在多方加入外键
多表查询
-- 一对多
SELECT * from 表名,class where student.c_id = class.id
-- 重命名 as 新名字 as 可省
SELECT s1.*,c1.c_name 班级名称 from 表名as s1,class c1 where s1.c_id = c1.id
-- 连表查询
-- 内连接 join ... on 和多表查询的结果一样
-- 外连接
-- 左外连接 left join ... on 左边的表结果多
-- 右外连接 right join ... on 右边的表结果多
SELECT * from 表名 join class on student.c_id = class.id
SELECT * from 表名 left join class on student.c_id = class.id
SELECT * from 表名 right join class on student.c_id = class.id
-- 多对多
SELECT * from 学生,课程表,选课表 where 学生.id=选课表.s_id and 课程表.id=选课表.c_id
SELECT 学生.*,name,score from 学生,课程表,选课表 where 学生.id=选课表.s_id and 课程表.id=选课表.c_id
SELECT 学生.*,name,score from 学生 join 课程表 join 选课表 on 学生.id=选课表.s_id and 课程表.id=选课表.c_id
在数据库系统三级模式结构中
1.索引的组织方式是B+树索引,还是Hash索引与数据哭的内模式有关
2.外模式是面向数据库用户或应用程序的局部数据试图
3.模式不会涉及数据的物理储存细节
4.模式/内模式映像保证了数据库具有较高的物理独立性
在SQL中删除触发器用drop语句进行
SELECT @@ERROR 是返回最后一个T_SQL错误的错误符号
SELECT @@IDENTITY是返回最后一个插入的标识值
SELECT @@ROWCOUNT返回受上一个SQL语句影响的行数
SELECT @@MAX_CONNECTIONS 显示可以创建的同时链接的最大数目
inner join ... on t1.birth = t2.birth and t1.name = t2.name ; inner join意思是左右表中的birth、name都不为NULL时才会匹配上,结果中不含有一个字段为NULL或两个字段都为NULL的记录
仅记录自最近一次完整数据库备份以后发生改变的数据的是:差异数据库备份
完整数据库备份:备份整个数据库,包括事务日志部分
事务日志备份:并不备份数据库本身,只记录事务日志内容
文件和文件组备份:如果在创建数据库时,为数据库创建了多个数据库文件或文件组,可以使用该备份方式,对数据库中的部分文件或文件组备份。
索引:索引滥用会降低更新表的速度,更新表时,MySQL不仅要更新数据,保存数据,还要更新索引,保存索引,索引会占用磁盘空间。
如果WHERE子句中使用了索引,那么ORDER BY子句中不会使用索引
数据库索引采用B+树是因为B+树在提高了磁盘IO性能的同时解决了元素遍历效率低下的问题
可通过索引快速查找数据,减少查询执行时间