设为首页 加入收藏

TOP

Mysql重新整理笔记--woods备忘(三)
2015-11-21 02:02:03 来源: 作者: 【 】 浏览:2
Tags:Mysql 重新 整理 笔记 --woods 备忘
um(chinese)/count(*) from exam ;
? 注意:sum仅对数值起作用,否则会报错。
AVG(列名):
求一个班级数学平均分?先查出所有的数学分,然后用avg包上。
?select avg(ifnull(math,0)) from exam;
求一个班级总分平均分
?select avg((ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0))) from exam ;
Max、Min
求班级最高分和最低分(数值范围在统计中特别有用)
select Max((ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0))) from exam;
select Min((ifnull(math,0)+ifnull(chinese,0)+ifnull(english,0))) from exam;
?
? (6)group by字句,其后可以接多个列名,也可以跟having子句对group by 的结果进行筛选。
练习:对订单表中商品归类后,显示每一类商品的总价
?select product,sum(price) from orders group by product;
练习:查询购买了几类商品,并且每类总价大于100的商品
? select product,sum(price) from orders group by product having sum(price)>100;
?!~having 和 where 的差别: where语句用在分组之前的筛选,having用在分组之后的筛选,having中可以用合计函数,where中就不行。使用where的地方可以用having替代。
?练习:查询商品列表中除了橘子以外的商品,每一类商品的总价格大于500元的商品的名字
? select product,sum(price) from orders where product<>'桔子'group by product having sum(price)>500;
?
? (7)重点:Select from where group by having order by
Mysql在执行sql语句时的执行顺序:from where select group by having order by
?*分析:
? select math+english+chinese as 总成绩 from exam where 总成绩 >250; ---- 不成功
? select math+english+chinese as 总成绩 from exam having 总成绩 >250; --- 成功
? select math+english+chinese as 总成绩 from exam group by 总成绩 having 总成绩 >250; ----成功
? select ?math+english+chinese as 总成绩 from exam order by 总成绩;----成功
? select * from exam as 成绩 where 成绩.math>85; ---- 成功
?
?
--------------------------------------------------------------------------------------------------
?
四、约束
?
?1.创建表时指定约束:
create table tb(
 id int primary key auto_increment,
 name varchar(20) unique not null,
 ref_id int,
  foreign key(ref_id) references tb2(id)
);

create table tb2(
 id int primary key auto_increment
);

?

?
?2.外键约束:
(1)增加外键:
?可以明确指定外键的名称,如果不指定外键的名称,mysql会自动为你创建一个外键名称。
?RESTRICT : 只要本表格里面有指向主表的数据, 在主表里面就无法删除相关记录。
?CASCADE : 如果在foreign key 所指向的那个表里面删除一条记录,那么在此表里面的跟那个key一样的所有记录都会一同删掉。
?alter table book add [constraint FK_BOOK] foreign key(pubid) references pub_com(id) [on delete restrict] [on update restrict];
?
(2)删除外键
?alter table 表名 drop foreign key 外键(区分大小写,外键名可以desc 表名查看);
?
?
?3.主键约束:
(1)增加主键(自动增长,只有主键可以自动增长)
?Alter table tb add primary key(id) [auto_increment];
(2)删除主键
?alter table 表名 drop primary key
(3)增加自动增长
?Alter table employee modify id int auto_increment;
(4)删除自动增长
?Alter table tb modify id int;
?
?
?
--------------------------------------------------------------------------------------------------
?
五、多表设计
?一对一(311教室和20130405班级,两方都是一):在任意一方保存另一方的主键
?一对多、多对一(班级和学生,其中班级为1,学生为多):在多的一方保存一的一方的主键
?多对多(教师和学生,两方都是多):使用中间表,保存对应关系
?
?
--------------------------------------------------------------------------------------------------
?
六、多表查询
  create table tb (id int primary key,name varchar(20) );
  create table ta (
 id int primary key,
 name varchar(20),
 tb_id int
 );
  insert into tb values(1,'财务部');
  insert into tb values(2,'人事部');
  insert into tb values(3,'科技部');

  insert into ta values (1,'刘备',1);
  insert into ta values (2,'关羽',2);
  insert into ta values (3,'张飞',3);


  mysql> select * from ta;
+----+------+-------+
| id | name | tb_id |
+----+------+---
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇辛星浅析MySQL中的last_insert_id.. 下一篇MySQL转换Oracle的注意事项

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: