黑马

--简单的增删改查

--插入一条数据

insert into 表名 (列1,列2,列3)values(值1,值2,值3)

1.自动编号列。默认会自动增长,所以不需要(默认情况下也不能向自动编号列加值)

2.当sql一行代码比较多的时候 可以折行。--按照关键字折行

3.如果省略列名,同时必须保证后面的值与表中的顺序必须与表中列顺序一致 --向自动编号列插入值

--启动某个列“自动编号值”手动插入值的动能

--在sql语句中的直接写的字符串中,如果有汉字 要在字符串前面加N

--ctrl +R 关闭打开查询结果 --更新语句

--update 表名 set列=新值,列2=新值2,...where 条件

--删除语句

--delete from 表名 where ...

--delete语句如果不加where条件,表示将表中所有数据都删除,如加where条件后,按照条件进行删除。 删除表中全部数据 --truncate table biao(建议使用这种)

--自动编号恢复到初始值

--使用turncate 删除表中所有数据要比delete快的多

--delete from 表

--distinct 去除重复数据 是去除查询结果的重复数据

--top 一般会和排序一起使用

--排序 order by 列名

--按照年龄,降序排序

select * from student order by age desc

-降序排序

select * from student order by age  asc

-升序排序

select * from student order by age  asc

-默认就是升序排序

--top 关键字 --查询成绩最高的前五名成绩

select top 5 * from student order by math desc

--top 关键字 --查询成绩最低的前五名成绩

select top 5 * from student order by math asc

--如果top后面跟的不是数字,而是一个表达式一定要使用()把表达式括起来。

select top (2*2) * from student order by math desc

 --如果按照百分比取数据  ,则向上取整。

select top 10 precent * from student order by math desc

--聚合函数 max(最大值)  min(最小值)  avg(平均值) sum(求和)  count(记录总条数)

--统计出所有人的年龄的总和

select sum(age) as  年龄总和 from student

--统计表中有多少条记录 select count (*) from student

--计算平均年龄

select  

      平均年龄 =(select sum (age) as 年龄总和 from  student)*1.0 /(select count (*) from student)

--计算年龄最大的

select max (age) from student

--计算年龄最小的

select min (age) from student

--计算平均年龄

select avg (age) from student

--------------聚合函数的一些问题------------

posted on 2017-07-26 17:30  王宏琳  阅读(124)  评论(0)    收藏  举报