create table goods_table(
goods_id int(11) not null primary key auto_increment,
goods_name varchar(100) not null,
goods_price double(8,2) not null,
goods_address varchar(255) not null,
goods_unit char(6),
goods_pic varchar(255),
goods_begintime timestamp not null default current_timestamp,
goods_endtime timestamp
) charset=utf8,engine=innodb;
create table goods_table2(
goods_id int(11) not null primary key auto_increment,
goods_name varchar(100) not null,
goods_price double(8,2) not null,
goods_pic varchar(225),
goods_endtime timestamp not null default current_timestamp
);
drop table if EXISTS order_table;
create table order_table(
order_id int(11) not null auto_increment primary key,
user_id int(11) not null, -- 外键 用户id
goods_id int(11) not null, -- 外键 商品id
order_no varchar(50),
order_price double(10,2),
order_address varchar(255),
goods_num int(3),
order_type char(1),-- 0下单 1发货 2 签收 3评价
order_begintime timestamp not null default current_timestamp,
order_endtime timestamp
);
insert into order_table values
(null,1,5,'no:2017091125684',3999.99,'重庆开发新区',10,0,'2017-08-20 20:15:10',null),
(null,1,6,'no:4253252454623',1999.99,'重庆开发新区',50,0,NOW(),NOW()),
(null,1,3,'no:4542616532463',3199.99,'重庆开发新区',10,0,NOW(),null),
(null,1,4,'no:4235423642333',1699.99,'重庆开发新区',50,0,NOW(),NULL);
insert into order_table(user_id,goods_id,order_no,order_price,order_address,goods_num,order_type,order_begintime,order_endtime)
values(2,5,'no:2017354543644',2799.99,'重庆开发新区',10,0,'2017-08-20 20:15:10',null);
select * from order_table;
insert into goods_table values
(null,'海尔冰箱',3999.99,'深圳工业园区','台','files/img/hebx.jpg',NOW(),null),
(null,'格力空调',9999.99,'北京工业园区','台','files/img/geli.jpg',NOW(),null),
(null,'华为路由器',999.99,'深圳工业园区','台','files/img/huawei.png',NOW(),null);
insert into goods_table2(goods_name,goods_price,goods_pic,goods_endtime)
values
('海尔冰箱',3999.99,'files/img/hebx.jpg',NOW()),
('格力空调1',9999.99,'files/img/geli.jpg',NOW()),
('华为路由器2',999.99,'files/img/huawei.png',NOW());
交叉连接 笛卡尔连接
select * from userinfo,order_table,goods_table;
左连接
select * from userinfo us
left join order_table o1 on us.id=o1.user_id
left join goods_table g1 on o1.goods_id=g1.goods_id;
右连接
select us.id,us.name,o1.order_price,g1.goods_name from userinfo us
right join order_table o1 on us.id=o1.user_id
right join goods_table g1 on o1.goods_id=g1.goods_id;
内连接
select us.id,us.name,o1.order_price,g1.goods_name from userinfo us
inner join order_table o1 on us.id=o1.user_id
inner join goods_table g1 on o1.goods_id=g1.goods_id;
自连接 union 不允许重复 nuion all 将两张不同的表的数据,查询到同一个结果集中 允许重复数据
select t1.goods_name,t1.goods_price,t1.goods_begintime from goods_table t1
union
select t2.goods_name,t2.goods_price,t2.goods_endtime from goods_table2 t2
--子查询 查询语句嵌套在另外一个语句中
select * from (select * from 表名) 别名;
select * from (select * from userinfo where useAge=6) t1 WHERE t1.useName = '托马斯' and or ;
select * from 表名 where 字段=(select 字段 from 表名 where 条件表达式);
delete from 表名 where id in(select id from 表名 );
--mysql管理
数据备份 cmd>找到mysql安装路径下路径 mysqldump
mysqldump -u 用户名 -p密码 -h 主机号 数据库 > 备份文件存放路径;
--mysqldump -u root -proot -h 127.0.0.1 yygdb>d:/yygdbback.sql;
数据还原:
mysql -u 用户名 -p 密码 数据库名称 < 备份文件地址.sql;
mysql -u root -proot -h 127.0.0.1 yygdb2<d:/yygdbback.sql;
mysql 中的外键: mysql 中只有 innodb 引擎支持外键约束
restrict : no action: 如果字表中有匹配的记录,不允许对父表对应的候选键进行 update/delete 操作
cascade: 在父表上 update/delete 记录时,同步到字表 update/delete 记录
set null : 父表有字表的时候,字表将把外键类 设置为 一个默认值(null), innodb 不支持
索引类型 normal: 表示普通索引
unique : 表示不唯一的,不允许重复的索引
full text: 表示全文搜素索引,用于搜素很长一篇文章时,速度快,效果好
索引方法 btree 所有的值都是排过序的
hash 支持'=' 和'<=>'精确查找某一条数据,不能使用范围查询
rtree 只支持 geometry 数据类型
建索引的原则: 方便查询(字典)
1 选择唯一性 数据唯一,主键
2 需要排序,分组,联合查询的字段
3 经常做为条件来检索数据的字段
4 限制索引数目(越少越好)
5 数据量少的作为索引
6 使用字段的前缀作为索引
7 维护索引,经常进行更新操作 不需要的索引及时清理