昨天内容:
数据库的创建 : create database 数据库的名 character set 字符集 collate 校对规则
数据库的删除: drop database 数据库名
修改: alter database 数据库 character set 字符集(utf8)
查询: show databases;
show create database 数据库的名字
select database();
切换数据库 :
use 数据库的名字
表结构的操作:
创建: create table 表名(
列名 列的类型 列的约束,
列名 列的类型 列的约
)
列的类型: char / varchar
列的约束:
primary key 主键约束
unique : 唯一约束
not null 非空约束
自动增长 : auto_increment
删除 : drop table 表名
修改: alter table 表名 (add, modify, change , drop)
rename table 旧表名 to 新表名
alter table 表名 character set 字符集
查询表结构:
show tables; 查询出所有的表
show create table 表名: 表的创建语句, 表的定义
desc 表名: 表的结构
表中数据的操作
插入: insert into 表名(列名,列名) values(值1,值2);
删除: delete from 表名 [where 条件]
修改: update 表名 set 列名=‘值’ ,列名=‘值’ [where 条件];
查询: select [distinct] * [列名1,列名2] from 表名 [where 条件]
as关键字: 别名
where条件后面:
关系运算符: > >= < <= != <>
--判断某一列是否为空: is null is not null
in 在某范围内
between…and
逻辑运算符: and or not
模糊查询: like
_ : 代表单个字符
%: 代表的是多个字符
分组: group by
分组之后条件过滤: having
聚合函数: sum() ,avg() , count() ,max(), min()
排序: order by (asc 升序, desc 降序)
分类表和商品表之间是不是有关系? 如果有关系,在数据库中如何表示这种关系
create table category( cid int primary key auto_increment, cname varchar(10), cdesc varchar(31) ); insert into category values(null,'手机数码','电子产品,黑马生产'); insert into category values(null,'鞋靴箱包','江南皮鞋厂倾情打造'); insert into category values(null,'香烟酒水','黄鹤楼,茅台,二锅头'); insert into category values(null,'酸奶饼干','娃哈哈,蒙牛酸酸乳'); insert into category values(null,'馋嘴零食','瓜子花生,八宝粥,辣条'); select * from category; select cname,cdesc from category; --所有商品 1.商品ID 2.商品名称 3.商品的价格 4.生产日期 5.商品分类ID 商品和商品分类 : 所属关系 create table product( pid int primary key auto_increment, pname varchar(10), price double, pdate timestamp, cno int ); insert into product values(null,'小米mix4',998,null,1); insert into product values(null,'锤子',2888,null,1); insert into product values(null,'阿迪王',99,null,2); insert into product values(null,'老村长',88,null,3); insert into product values(null,'劲酒',35,null,3); insert into product values(null,'小熊饼干',1,null,4); insert into product values(null,'卫龙辣条',1,null,5); insert into product values(null,'旺旺大饼',1,null,5); //插入数据会失败 insert into product values(null,'康师傅泡面',1,null,12);多表之间的关系如何来维护
外键约束: foreign key
给product中的这个cno 添加一个外键约束
alter table product add foreign key(cno) references category(cid);
自己挖坑
从分类表中,删除分类为5信息,
delete from category where cid =5; //删除失败首先得去product表, 删除所有分类ID5 商品建数据库原则:
通常情况下,一个项目/应用建一个数据库多表之间的建表原则
一对多 : 商品和分类
建表原则: 在多的一方添加一个外键,指向一的一方的主键多对多: 老师和学生, 学生和课程
建表原则: 建立一张中间表,将多对多的关系,拆分成一对多的关系,中间表至少要有两个外键,分别指向原来的那两张表
一对一: 班级和班长, 公民和身份证, 国家和国旗
建表原则:
将一对一的情况,当作是一对多情况处理,在任意一张表添加一个外键,并且这个外键要唯一,指向另外一张表直接将两张表合并成一张表将两张表的主键建立起连接,让两张表里面主键相等实际用途: 用的不是很多. (拆表操作 )
相亲网站: 个人信息 : 姓名,性别,年龄,身高,体重,三围,兴趣爱好,(年收入, 特长,学历, 职业, 择偶目标,要求)拆表操作 : 将个人的常用信息和不常用信息,减少表的臃肿,网上商城表实例的分析: 用户购物流程
用户表 (用户的ID,用户名,密码,手机)
create table user( uid int primary key auto_increment, username varchar(31), password varchar(31), phone varchar(11) ); insert into user values(1,'zhangsan','123','13811118888');订单表 (订单编号,总价,订单时间 ,地址,外键用户的ID)
create table orders( oid int primary key auto_increment, sum int not null, otime timestamp, address varchar(100), uno int, foreign key(uno) references user(uid) ); insert into orders values(1,200,null,'黑马前台旁边小黑屋',1); insert into orders values(2,250,null,'黑马后台旁边1702',1);商品表 (商品ID, 商品名称,商品价格,外键cno)
create table product( pid int primary key auto_increment, pname varchar(10), price double, cno int, foreign key(cno) references category(cid) ); insert into product values(null,'小米mix4',998,1); insert into product values(null,'锤子',2888,1); insert into product values(null,'阿迪王',99,2); insert into product values(null,'老村长',88,3); insert into product values(null,'劲酒',35,3); insert into product values(null,'小熊饼干',1,4); insert into product values(null,'卫龙辣条',1,5); insert into product values(null,'旺旺大饼',1,5);订单项: 中间表(订单ID,商品ID,商品数量,订单项总价)
create table orderitem( ono int, pno int, foreign key(ono) references orders(oid), foreign key(pno) references product(pid), ocount int, subsum double ); --给1号订单添加商品 200块钱的商品 insert into orderitem values(1,7,100,100); insert into orderitem values(1,8,101,100); --给2号订单添加商品 250块钱的商品 () insert into orderitem values(2,5,1,35); insert into orderitem values(2,3,3,99);商品分类表(分类ID,分类名称,分类描述)
create table category( cid int primary key auto_increment, cname varchar(15), cdesc varchar(100) ); insert into category values(null,'手机数码','电子产品,黑马生产'); insert into category values(null,'鞋靴箱包','江南皮鞋厂倾情打造'); insert into category values(null,'香烟酒水','黄鹤楼,茅台,二锅头'); insert into category values(null,'酸奶饼干','娃哈哈,蒙牛酸酸乳'); insert into category values(null,'馋嘴零食','瓜子花生,八宝粥,辣条');多表之间的关系如何维护: 外键约束 : foreign key
添加一个外键: alter table product add foreign key(cno) references category(cid);
foreign key(cno) references category(cid)删除的时候, 先删除外键关联的所有数据,再才能删除分类的数据建表原则:
一对多: 建表原则: 在多的一方增加一个外键,指向一的一方 多对多: 建表原则: 将多对多转成一对多的关系,创建一张中间表 一对一: 不常用, 拆表操作 建表原则: 将两张表合并成一张表 将两张表的主键建立起关系将一对一的关系当作一对多的关系去处理主键约束: 默认就是不能为空, 唯一
外键都是指向另外一张表的主键主键一张表只能有一个唯一约束: 列面的内容, 必须是唯一, 不能出现重复情况, 为空
唯一约束不可以作为其它表的外键可以有多个唯一约束一对多 : 建表原则: 在多的一方添加一个外键,指向一的一方
多对多: 建表原则:
拆成一对多
创建一张中间表, 至少要有两个外键, 指向原来的表
一对一: 建表原则: 合并一张表, 将主键建立关系 , 将它当作一对多的情况来处理
数据库客户端软件在我们的商城案例中,我的订单中包含很多信息.打开我的订单需要去查询表
交叉连接查询 笛卡尔积 select * from product; select * from category; 笛卡尔积查出来是两张表的乘积,查出来的结果没有意义 select * from product,category;
过滤出有意义的数据 select * from product as p,category as c where p.cno=c.cid;
内连接查询
隐式内连接 select * from product p,category c where p.cno=c.cid;
显示内连接 select * from product p inner join category c on p.cno=c.cid;
区别 隐式内连接:在查询出结果的基础上去做的where条件过滤 显示内连接:带着条件去查询结果(执行效率高)
左外连接 select * from product p left outer join category c on p.cno=c.cid; 左外连接会把左表中的所有数据都查询出来,如果右表中没有对应的数据,用NULL代替
右外连接 select * from product p right outer join category c on p.cno=c.cid; 右外连接会把右表中的所有数据都查询出来,如果左表中没有对应的数据,用NULL代替
inner 和 outer可以省略
每页数据数据3
起始索引从0
第1页: 0
第2页: 3
起始索引: index 代表显示第几页 页数从1开始
每页显示3条数据
startIndex = (index-1)*3
第一个参数是索引
第二个参数显示的个数
select * from product limit 0,3; 从0开始,每页3条
select * from product limit 3,3; 从3开始,每页3条
查询分类名称为手机数码的所有商品
select cid from category where cname ='手机数码'; select * from product where cno = 1; select * from product where cno = (select cid from category where cname ='手机数码');查询出高于本部门平均工资的员工信息
列出达拉斯加工作的人中,比纽约平均工资高的人
查询7369员工编号,姓名,经理编号和经理姓名
查询出各个部门薪水最高的员工所有信息