1、首先先了解MySQL和Navicat的关系,MySQL和Navicat本身是一个进程或软件,不同的是MySQL是存储数据,Navicat是展示数据的。
2、表与表之间的关系:1、一对一关系 2、一对多关系 3、多对多关系
(1)、一对一:可以再任意一个表中设置一个关联另一个表的字段
(2)、一对多:只能在多表中设置外键
(3)、多对多:设置关联表
3、数据库完整性约束:为了使数据完整表达
(1)主键约束 不为空,是我们数据的唯一标识符
(2)唯一约束 手机号唯一,添加索引字段,注意字符长度不能为255
(3)非空约束(4)默认约束
(5)外键约束 就是添加一个字段
-----------------------------------------------------------------------------------------------------------------
操作来啦!
#sql 查询语句
select * from student
#select 查询关键字 ;*代表所有列 select后面跟表名,多个列用 ' , ’ 隔开
#from 后面跟的是表名
#我们查询出来的数据是一个二维的表
-------------------------------------------------------------------
#条件查询 关键字是 where 列名=值
select name,sex from student where class_name = '223344'
#列名后面还可以跟其他符号,不过一般这种比较的情况是int类型的
#多条件查询
#查询条件取交集:and
select * from student where class_name = ' ' and sex=' '
#查询条件取并集:or
select * from student where class_name = ' ' or sex=' '
#否定情况用not
select * from student where not class_name = ' '
-----------------------------------------------------------------------------------------
#去重查询关键字distinct
select distinct sex from student
#模糊查询,一般多用于搜索 关键字 like 还有通配符(%匹配任意多少字符,_:只能匹配一个字符)
#查询出所有姓王的同学
select * from student where name like ' 王%'
#查询出所有名字带王的同学
select * from student where name like ' %王%'
--------------------------------------------------------------------------------------------------
#查询排序 只能用数值型字段
#默认的排序规则;id从小到大排序 正序
select * from student order by age
#倒叙排序
select * from student order by age desc
--------------------------------------------------------------------------------------------------------
#分页查询 限制查询 limit pageSize offset pageSize*(pageIndex-1)
select * from student limit 5 offset 0
select * from student limit 0,5
------------------------------------------------------------------------------------------------------------
#聚合查询
#count()计算数据条数,()里面是根据那个字段来进行查询
select count(*)from student
#sum()计算某一列的和,但是该列只能是数值类型
select sum(age)from student
#avg()计算一列的平均值
select avg(age)from student
#max() 找到一列的最大值 不限制类型 找到排在最后或最前的字符;(这个要想理解最好去Navicat上自己测试一下)
#min() 找到一列的最小值 不限制类型 找到排在最后或最前的字符;
select max(name)from student
#分组聚合 group by
#查询出各班男生的平均年龄
select avg(age) from student group by class_num
#查询出各班人数
select count(*) from student group by class_num
#命别名
select name num from student group by class_num
#名字会以num为名的表展示
------------------------------------------------------------------------------------------------------------------------
#表与表之间的关联查询
select * from student1,student2
#给表重命名
select t.name,c.class_name from student1 t,student2 c
或者也可以直接select student1.name,student2.class_name from student1 ,student2
------------------------------------------------------------------------------------------------------------------------
#链接查询:一对一;一对多;多对一
#查询出各个班的学生所在的班级
select student.name,class.class_name from student inner join class on student.class_num=class.class_num
select 查询字段(为了防止出现歧义,我们要规定是在那个表中的) from 主表 inner join 从表 on 两个相等的字段
#inner join 是匹配两个表中都有的相同字段
#left join 是以左边的表为主表的,显示的内容也是以左边的表为准,如果左边的表中与右边的表信息没有匹配的,就显示右边为空
#right join 同理left join
------------------------------------------------------------------------------------------------------------------------
#嵌套查询 我们每次查询出来的都是一个二维的表,这样我们就可以进行下一次继续查询
select t1.name from
(select student.name,relationship.con from student left join relationship on student.sno=relationship.sno) t1
where t1.cno ='1001'
#查询出所有选课名称为数学的学生名(三表联系)
select t2.name from
(select t1.name,source.gradeName from
(select student.name,relationship.con from student left join relationship on student.sno=relationship.sno) t1 left join scoure on t1.cno=scoure.cno)t2
where t2.gradeName="数学"