个人笔记
- inner join和where,多表inner join on
联表查询inner join…on比where效率高,为什么我这inner join查询时间更长?
select t_orders.id
ordersid,t_orders.employeeid employee,
t_orders.customerid
customer,t_orders.ordertime,
t_employee.id eid,t_employee.name ename,
t_customer.id cid,t_customer.name cname,
t_orderdetail.*,t_goods.id
gid,t_goods.name gname,t_goods.price
from
t_orders
INNER JOIN t_customer ON t_orders.customerid=t_customer.id
INNER JOIN t_employee ON t_orders.employeeid=t_employee.id
INNER JOIN t_orderdetail ON t_orderdetail.orderid=t_orders.id
INNER JOIN t_goods ON t_orderdetail.goodsid=t_goods.id
order by ordersid asc
查询时间0.117s
select t_orders.id
ordersid,t_orders.employeeid employee,
t_orders.customerid
customer,t_orders.ordertime,
t_employee.id eid,t_employee.name ename,
t_customer.id cid,t_customer.name cname,
t_orderdetail.*,t_goods.id
gid,t_goods.name gname,t_goods.price
from
t_orders, t_customer,
t_employee, t_orderdetail, t_goods
where
t_orders.customerid=t_customer.id
and t_orders.employeeid=t_employee.id
and t_orderdetail.orderid=t_orders.id
and t_orderdetail.goodsid=t_goods.id
order by ordersid asc
查询时间0.106s
- 创建表时的约束越多,数据执行速度越慢,实际运用应该放在程序中验证
- 多数据库集群不用外键
- 与数据表对应的简单java类:
几个别名POJO(原始古老的java对象)、VO(value object值对象)、
TO(实现了序列化接口的类对象)、PO(基于JPA标准的持久化对象)JPA(Java Persistence API,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中)
①类名与表名对应
②属性用private封装,必须setter、getter,类中不允许使用基本数据类型(默认0), 必须是包装类(默认空)
③类中可以多个构造方法,但必须保留无参构造,(反射用。不写默认有一个隐式无参构造,一旦写了其他构造方法将被覆盖,必须显式写一个)
④可选的:equals(),toString(),hashCode()等方法
⑤必须序列化,实现Serializable接口 - 软件分层设计,控制层——>业务层(services用接口方式)——>数据层(持久层dao用接口方式)——>数据库
不同层之间操作用接口
不用框架自己写管理数据库连接用ThreadLocal - 数据类型
varchar:varchar(3)表示可以存3个汉字或者3个字母不分大小写或者3位数字或者3个符号。(这里和字符编码的汉字和字母不一样,gbk,gb2132一个汉字是占两个字节,utf-8是3个字节,字母是一个字节)
8bit(位)=1Byte(字节)
1024Byte(字节)=1KB
int:默认是11,写int(4)也差不多
int的几种大小详解
没有外键约束一样的可以连接查询 - 修改MySQL自动递增值:alter table tablename auto_increment=1