Mysql基础知识点汇总

2019-01-04 20:53:14王旭

--特点:
--1.一次性删除所有数据,没有条件,那么日志文件只以最小化的数据写入
--2.它可以使用标识列从种子值重新计算
--3.它不能触发delete触发器
truncate table teacher
数据更新(数据修改):一定需要考虑是否有条件

语法:

update 表名 set 字段=值,字段=值 。。where 条件
update Teacher set Gender='true'
--修改时添加条件
update Teacher set Gender=0 where Id=20
--多字段修改
update Teacher set ClassId=4,Age+=5,Salary=5000 where Id=22
--修改班级id=4,同时年龄》20岁的人员工资+500
update Teacher set Salary=Salary+500 where ClassId=4 and Age>20
数据检索--查询

语法: *代表所有字段

select */字段名称列表 from 表列表
select StudentNo,StudentName,Sex,[Address] from Student
--可以为标题设置  别名,别名可以是中文别名
select StudentNo as 学号,StudentName 姓名,性别=Sex,[Address] from Student
--添加常量列
select StudentNo as 学号,StudentName 姓名,性别=Sex,[Address] ,国籍='中华人民共和国' from Student
--select的作用
--1.查询
--2.输出
select 1+1
--+是运算符,系统会自动为你做类型转换
select 1+'1'
select '1'+1
--如果+两边都是字符串,那么它就是一字符串连接符
select '1'+'1'
select 'a'+1
--可以输出多列值
select 1,2,34,3,545,67,567,6,7
--Top、Distinct
select * from Student
--top可以获取指定的记录数,值可以大于总记录数.但是不能是负值
select top 100 * from Student
--百分比是取ceiling()
select top 10 percent * from Student

--重复记录与原始的数据表数据无关,只与你查询的结果集有关系 distinct可以去除结果集中的重复记录--结果集中每一列的值都一样
select distinct LoginPwd,Sex,Email from Student
select distinct Sex from Student

select的作用
--聚合函数:
--1.对null过滤
--2.都需要有一个参数
--3.都是返回一个数值
--sum():求和:只能对数值而言,对字符串和日期无效
--avg():求平均值
--count():计数:得到满足条件的记录数
--max():求最大值:可以对任意类型的数据进行聚合,如果是字符串就比较拼音字母进行排序
--min():求最小值
--获取学员总人数
select COUNT(*) from Student
--查询最大年龄值
select  MIN(BornDate) from Student
select  max(BornDate) from Student

--查询总分
select SUM(StudentResult) from Result where StudentNo=2
--平均分
select avg(StudentResult) from Result where SubjectId=1
--注意细节:
select  SUM(StudentName) from Student
select  SUM(BornDate) from Student

select  min(StudentName) from Student