1.数据库
- 文件系统
2.sql
- 结构化查询语言
3.对数据库的CRUD
1)创建数据库
- create database 数据库 [character set 字符集]
2)查看数据库
- show databases;
- show create database 数据库名;
3)删除数据库
- drop database 数据库;
4)修改数据库
- alter database 数据库 character set 字符集
5)其它操作
- use 数据库;
- select database();
4.对表的CRUD
1)创建表
create table 表名(
字段 类型 约束,
字段 类型 约束,
字段 类型 约束
);
2)查看表
- show tables;
- desc 表;
3)修改表
- alter table 表名 add 字段 类型 约束;
- alter table 表名 modify 字段 类型 约束;
- alter table 表名 change 字段 字段 类型 约束;
- alter table 表名 drop 字段;
- rename tbale 表名 to 新表名;
4)删除表
- drop table 表名;
5.对数据的CRUD
1)插入数据
insert into 表values(值,值,值...)
insert into 表(字段,字段)values(值,值)
2)更新数据
- update 表 set 字段 = 值 [where 条件]
3)删除数据
- delete from 表[where 条件]
- truncate table 表;
4)查询数据
select [*][字段,字段...][distinct 字段,...][聚合函数] from 表 [where条件]
- 别名查询; 字段 as 别名;表 as 别名
- 分组查询; group by 字段;
- 分组后筛选;having
- 排序查询; order by 字段; desc;asc
顺序: select —from—where—group by—having—order by
6.多表关系
1)一对多
- 在多的一方创建一个字段作为外键指向一个一方的主键
2)多对多
- 创建一张第三方表,至少包含两个字段作为外键分别指向各自的主键
3)一对一
- 先当做一对多,在外键字段添加唯一约束
7.多表查询
合并结果集 :作用就是把两个select语句的查询结果合并到了一起.
union :合并的结果去重 select * from t1 union select * from t2;
union all :合并的结果不去重
8,多表连接查询(n张表联查就有n-1个条件)
1.交叉查询(笛卡尔积)—基本不用
- select [*][字段,字段] from 表,表
2.内连接查询
select [*][字段,字段] from 表,表 where 条件(用主外键关系作为判断条件,两个表中同时出现stuid(数据类型一致)才显示)
- select * from student,score where student.stuid=score.stuid;
select [*][字段,字段] from 表 inner join 表 on 条件(on只能用于主外键关联)
3.外链接
1)左外连接
- select * from 表 left outer join 表 on 条件
2)右外连接
- select *from 表 right outer join 表 on 条件
4.子查询(嵌套查询)
- 一个查询的结果当做另一个查询的条件
eg:查询分类名称是手机数码的商品
select *from product p where p.cno in(select cid from category where cname = '手机数码');
5.分页查询
- limit a,b;
- a(开始的索引,从0开始计数的)
b(代表每一页显示的数目)
a = (当前页数 - 1)*b;
应用场景:
- 百度页面分页查询
- 安卓和ios里面上拉加载更多和下拉刷新
两种:
- 每次加载都查询数据库(适合一条数据量很大)
一次加载完,全部返回(适合一条数据量很小)
pageBean{ List<Item> list;//item的数据集合 int curPage;//当前的页数 int curCount;//每一个显示的数量 int count;//总个数 int sumPage;//总页数 }
eg:count = 12; curCount = 4;
if(count % curCount == 0){
sumPage = count/curCount;
}else{
sumPage = count/curCount+1;
}
6.数据库的备份和恢复
扩展
一,完整性
保证数据的完整性=创建表时给表添加约束
实体完整性:主键约束(primary key),唯一约束(unique),自动增长(aotu_increment)
域完整性:数据类型,非空约束(not null),默认值(default) check()
引用完整性(参照完整性):外键约束(foreign key)