1.数据类型整数:int,有符号范围(-2147483648~2147483647),无符号范围(0~4294967295),长度没有意义小数:decimal,如decimal(5,2)表示共存5位数,小数占2位,整数占3位,如果插入的小数点后面超出长度,会自动的四舍五入字符串:varchar,范围(0~65533),如varchar(3)表示最多存3个字符,一个中文或一个字母都占一个字符日期时间: datetime,范围(1000-01-0100:00:00~9999-12-3123:59:59),如'2020-01-01 12:29:59'查看任何一个数据类型的使用1、打开mysql命令行客户端2、help 数据类型名称,如 help tinyint2.约束主键(primarykey):代表一条记录的唯一标识,此自动的值必须唯一不能重复,不能为空;自动递增:从1开始,一直累加,值不会取以前的值表清空:删除所有数据,不删表结构,自动递增的值继续累加表截断:删除所有数据,不删表结构,自动递增的值从1开始应用时:主键+自动递增+无符号非空(notnull):此字段不允许填写空值惟一(unique):此字段的值不允许重复,通过sql语法演示默认值(default):当不填写此值时会使用默认值,如果填写时以填写为准外键(foreignkey):维护两个表之间的关联关系,后面多表时讲
SQL语言
1.表操作创建表-- 创建学生表,字段要求如下:注释 ctrl + / -- 姓名(长度为10) createtable students(name varchar(10))-- 创建学生表,字段要求如下:取消注释 ctrl + shift + /-- 姓名(长度为10), 年龄createtable sutdents1(name varchar(10),age tinyintunsigned)-- 创建学生表,字段要求如下: -- 姓名(长度为10), 年龄,身高(保留小数点2位)createtable students2(id intunsignedprimarykeyauto_increment,name varchar(10),age tinyintunsigned,height decimal(5,2))删除表-- 删除学生表droptable studentsdroptableifexists sutdents删除并创建新表droptableifexists students;createtable students(name varchar(10),age int)2.增删改查查询-- 查询性别为男的数据select*from stu where sex='男'插入-- 插入一个学生,设置所有字段的信息,值的顺序与表中字段的顺序对应insertinto students values('亚瑟',20)-- 插入一个学生,只设置姓名,值的顺序与给出的字段顺序对应insertinto students(name)values('鲁班')insertinto students(age)values(30)insertinto students(age,name)values(30,'亚瑟2')-- 插入查询出来的数据insertinto goods_cate(cate_name)selectdistinct cate from goods-- 在创建表时插入数据createtable goods_brand(brand_id intunsignedprimarykeyauto_increment,brand_name varchar(20))selectdistinct brand_name from goods-- 备份表数据createtable goods_back select*from goods-- 当表中有auto_increment的字段时,添加数据时使用0或者 default 或者 null 来占位insertinto students values(0,'老夫子3',20);插入多条数据insertinto students values(0,'老夫子3',20);insertinto students values(0,'老夫子4',20);insertinto students values(0,'老夫3',20),(0,'老夫4',20),(0,'老夫5',20)insertinto students(id,name)values(0,'老夫3'),(0,'老夫4'),(0,'老夫5')修改数据-- 更新数据 设置某一个学生的年龄加3岁update students set age=age+3where name='亚瑟3'删除数据-- 删除数据deletefrom students where name='亚瑟3'逻辑删除1、-- 添加字段,标识数据是否被删除 is_delete默认设置为0,代表数据没有被删除2、-- update students set is_delete=03、-- 删除一条数据,只是修改了这条数据的is_delete 改为1update students set is_delete=1where name='老夫子6'4、-- 查询所有学生时,不显示删除的学生select*from students where is_delete=06.sql查询给字段起别名-- select name as 姓名,age as 年龄,hometown as 家乡 from students where name='王昭君'-- select name 姓名,age 年龄,hometown 家乡 from students where name='王昭君'给表起别名select s.name,s.age from students as s消除重复数据selectdistinct age,class from studentsselectdistinct*from students比较运算-- 例1:查询小乔的年龄select age from students where name='小乔'-- 例2:查询20岁以下的学生select*from students where age<20-- 例3:查询家乡不在北京的学生select*from students where hometown<>'北京逻辑运算-- 例2:查询女学生或'1班'的学生select * from students where sex='女' or class='1班'-- 例3:查询非天津的学生select * from students where not hometown='天津'模糊查询-- 例1:查询姓孙的学生select * from students where name like '孙%'-- 例2:查询姓孙且名字是一个字的学生select * from students where name like '孙_'-- 例3:查询叫乔的学生select * from students where name like '%乔'-- 例4:查询姓名含白的学生select * from students where name like '%白%'范围查询-- 例1:查询家乡是北京或上海或广东的学生select * from students where hometown in ('北京','上海','广东')select * from students where hometown not in ('北京','上海','广东')-- 例2:查询年龄为18至20的学生 between 20 and 18 小值在前select * from students where age between 18 and 20判空-- 例1:查询没有填写身份证的学生select * from students where card is null-- 例2:非空select * from students where card is not null-- 例3:判断身份证为空字符select * from students where card=''排序-- 例1:查询所有学生信息,按年龄从小到大排序select * from students order by age asc降序select * from students order by age desc-- 例2:查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序select * from students order by age desc,studentNo聚合函数-- 例1:查询学生总数 select count(*) as 学生总数 from studentsselect count(name) from students-- count(card) 不统计为null数据select count(card) from students-- 例2:查询女生的最小年龄select min(age) from students where sex='女'-- 例3:查询1班的最大年龄select max(age) from students where class='1班'-- 例4:查询北京学生的年龄总和select sum(age) from students-- 例5:查询女生的平均年龄select avg(age) from students where sex='女'分组-- 例1:查询各种性别的人数select sex,count(*) from students group by sex-- 例2:查询各种年龄的人数select age,count(*) from students group by age-- 例1:查询男生总人数select sex,count(*) from students group by sex having sex='男'select count(*) from students where sex='男'分页select*from 表名 limitstart,count -- 从start开始,获取count条数据,start索引从0开始-- 例1:查询前3行学生信息select*from students limit0,3-- 已知:每页显示m条数据,求:显示第n页的数据select*from students limit(n-1)*m,m
笛卡尔积(两个表数据个数相乘)
1、等值连接(普通:select*from 表1,表2where 表1.列=表2.列)(推荐内连接:先匹配,不产生大量笛卡尔积无效值select*from 表1innerjoin 表2on 表1.列=表2.列)-- 如果没有的数据,则不显示,取交集-- 查询学生信息及学生的成绩select stu.name,sc.score from students as stu ,scores as sc where stu.studentNo=sc.studentNo-- 查询成绩信息及课程信息 内连接select sc.score,cs.name from scores sc innerjoin courses cs on sc.courseNo=cs.courseNo-- 查询学生信息及学生的课程对应的成绩select stu.name 姓名,sc.score 成绩,cs.name 课程名 from students stu,scores sc,courses cs where stu.studentNo=sc.studentNo and sc.courseNo=cs.courseNo-- 查询学生信息及学生的课程对应的成绩 内连接select stu.name,sc.score,cs.name from students stu innerjoin scores sc on stu.studentNo=sc.studentNo innerjoin courses cs on sc.courseNo=cs.courseNo2、左连接(查询的结果为两个表匹配到的数据加上左表特有的数据,对于右表中不存在的数据使用null填充,取并集)select*from (左)表1leftjoin 表2on 表1.列=表2.列-- 例1:查询所有学生的成绩,包括没有成绩的学生select stu.name,sc.score from students stu leftjoin scores sc on stu.studentNo=sc.studentNo-- 例2:查询所有学生的成绩,包括没有成绩的学生,需要显示课程名select stu.name,sc.score,cs.name from students stu leftjoin scores sc on stu.studentNo=sc.studentNo leftjoin courses cs on sc.courseNo=cs.courseNo3、右连接(查询的结果为两个表匹配到的数据加上右表特有的数据,对于左表中不存在的数据使用null填充,取并集)select*from (左)表1rightjoin 表2on 表1.列=表2.列-- 课程表插入一些数据insertinto courses values(0,'语文'),(0,'数学');-- 例1:查询所有课程的成绩,包括没有成绩的课程select sc.score,cs.name from scores sc rightjoin courses cs on sc.courseNo=cs.courseNo-- 例2:查询所有课程的成绩,包括没有成绩的课程,包含学生select sc.score,cs.name,stu.name from scores sc rightjoin courses cs on sc.courseNo=cs.courseNoleft join students stu on stu.studentNo=sc.studentNo-- 左右连接:join前是左表,后是右表
自关联 子查询
1、自关联(省市县这种用一个表格创建,存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,故此自己和自己关联,达到查询表内下级信息的作用 Where A.编号=B.上级)-- 例1:查询一共有多少个省selectcount(*)from areas where pid isnull-- 例2:查询河南省的所有城市select*from areas p,areas c where p.aid=c.pid and p.atitle='河南省'-- 例3:查询郑州市的所有区县select*from areas p,areas c where p.aid=c.pid and p.atitle='郑州市'select*from areas p innerjoin areas c on p.aid=c.pid where p.atitle='郑州市'
2、子查询(子查询是可以独立存在的语句,是一条完整的 select 语句)标量子查询: 子查询返回的结果是一个数据(一行一列)-- 例1:查询班级大于平均年龄的学生select*from students where age>(selectavg(age)from students)-- 例2:查询王昭君的成绩,要求显示成绩select*from scores where studentNo=(select studentNo from students where name='王昭君')列子查询:返回的结果是一列(一列多行)-- 例3:查询18岁的学生的成绩,要求显示成绩('002','006')select*from scores where studentNo in(select studentNo from students where age=18)行级子查询: 返回的结果是一行(一行多列)-- 例4:查询男生中年龄最大的学生信息标量:select*from students where sex='男'and age=(selectmax(age)from students where sex='男')标量:select*from students where(sex,age)=('男',30)行级:select*from students where(sex,age)=(select sex,age from students where sex='男'orderby age desclimit1)表级子查询:返回的结果是多行多列-- 例5:查询数据库和系统测试的课程成绩-- select * from 数据源select*from scores sc innerjoin(select*from courses where name in('数据库','系统测试')) c on sc.courseNo=c.courseNo
第二节总结
1.子查询中特定关键字使用--01 in 范围(select * from students where age in (select age from students where age between 18 and 20))格式: 主查询 where 条件 in(列子查询)--02 any | some 任意一个 格式: 主查询 where 列 =any(列子查询)在条件查询的结果中匹配任意一个即可,等价于 in--03 all格式: 主查询 where 列 =all(列子查询) : 等于里面所有格式: 主查询 where 列 <>all(列子查询) : 不等一其中所有条件查询:where 字段 <,>,=,!=,and,or,not,in,between,and,like(% _)排序:orderbyasc,desc(降序)聚合函数:sum max min avg count(*) 分组:groupbyhaving分页:limit0,3 游标 索引 角标连接查询:等值连接(from 表1,表2innerjoin) 左连接 leftjoin 右连接 rightjoin子查询:标量子查询 列子查询 行子查询 表级子查询
字符串函数1.-- 拼接字符串concat(str1,str2...)select concat(12,34,'ab');select name,sex,hometown,concat(name,'是',hometown,'的',sex,'生')as des from students2.-- 包含字符个数length(str)select length('abc');3.-- 截取字符串--left(str,len)返回字符串str的左端len个字符--right(str,len)返回字符串str的右端len个字符--substring(str,pos,len)返回字符串str的位置pos起len个字符select substring('abc123',2,3);pos指的位置时从1开始(bc1)4.-- 去除空格--ltrim(str)返回删除了左空格的字符串str--rtrim(str)返回删除了右空格的字符串strselect ltrim(' bar '),rtrim(' bar '),rtrim(ltrim(' bar '));5.-- 大小写转换lower(str)/upper(str)select lower('aBcD'),upper('aBcD');
数学函数 1.-- 求四舍五入值round(n,d),n表示原数,d表示小数位置,默认为0selectround(1.6,1);2.-- 求x的y次幂pow(x,y)select pow(2,3);3.-- 获取圆周率PI()selectround(PI(),10)4.-- 随机数rand(),值为0-1.0的浮点数select rand();select rand(),s.*from students s orderby rand()limit1(select*from students orderby rand()limit1)日期时间函数1.-- 当前日期current_date()2.-- 当前时间current_time()3.-- 当前日期时间now()selectcurrent_date(),current_time(),now();4.-- 日期格式化date_format(date,format)select date_format(now(),'%Y-%m-%d/%h/%i/%s');5.-- 流程控制 case语法selectcase1when1then'one'when2then'two'else'zero'endas result;6.-- 姓 美女 帅哥selectleft(name,1),sex,case sex when'男'then concat(left(name,1),'帅哥')when'女'then concat(left(name,1),'美女')else'保密'endas resfrom students自定义函数
(命令行使用delimiter $$ 用于设置分割符,默认为分号)1、在navicat查询中,执行下面sql语句,可以在navicat中的数据库下面的函数中找到创建的自定义函数createfunction my_trim(aaa varchar(100))returnsvarchar(100)beginreturn rtrim(ltrim(aaa));end2、使用自定义函数select my_trim(' name ')
1、创建视图(视图本质就是对查询的封装)-- 视图可以隐藏真正的表结构,对于比较重要的数据(如银行),只让别人访问视图,没有权限使用真正的表createview v_stu_score_course asselect stu.name,stu.class,sc.score,cs.name as a from students stuINNERJOIN scores sc on stu.studentNo=sc.studentNoINNERJOIN courses cs on cs.courseNo=sc.courseNo2、使用视图select*from v_stu_score_course3.删除视图dropview v_stu_score_course;