Mysql工作整理总结笔记
  fTDyHqWMynbY 2023年12月06日 14 0

一、终端数据库的登陆

1.如何登陆终端数据库

Mysql -u root -p+密码

2. 如何查询数据库服务器中所有的数据库

Show databases

3. 如何选中某一个数据库进行操作

首先需要选use一个数据库

查询数据库的记录:Select * from admin

4. exit退出数据库

5. 如何在数据库服务器中创建数据库

Create database 名字

6. 如何查看某个数据库中所有的数据表

Show tables

7. 如何创建一个数据表

Create table 表名 ()

8. 查询数据表格具体的结构

Describe 表名

9. 如何往数据表中添加数据记录

Insert into 表名

Values(‘*’,‘#’,‘%’);


10. mysql常用数据类型

数值

TINYINT  SMALLINT  INT  BIGINT  FLIOAT  DOUBLE

日期/时间

DATE  TIME  YEAR  DATETIME

字符串

CHAR  VARCHAR  TEXT

11. 如何删除数据

Delete from 表名 where name=’*’;

12. 如何修改数据

Update 表名 set  name = ‘¥¥’where  *=‘周星驰’;


二、mysql建表约束.sql

1. 主键约束:能够唯一确定一张表中的一条记录,也就是我们通过某个字段添加约束,可以使得该字段不重复且不为空

Primary key 的使用,在数据类型后添加

l 如果我们创建表格的时候忘记添加主键约束时,应该怎么操作?

Alter table 表名 add primary key(列名) ;

l 那如何删除这个添加的主键数据?

Alter table 表名 drop primary key ;

l 使用modify修改字段,添加约束

Alter table 表名 modify id int primary key ;

2. 联合主键:只要联合的主键值加起来不重复即可,并不是两个都需要不同。


3. 自增约束:自动生成一个递增的序号

Auto_increment


4. 唯一约束:约束修饰该字段的值不能重复

通过unique来实现,可以直接在类型后添加,也可以之后通过Alter添加


Alter table 表名 add unique (列名);


如何删除唯一约束?

Alter table 表名 drop index 列名 ;


如何添加?

Alter table 表名 modify 列名 类型 unique;

关于约束的总结:

Ø 建表的时候就添加约束

Ø 可以使用alter...add...

Ø Alter...modify...

Ø 删除alter...drop...

5. 非空约束

Not null  约束了之后就一定要添加值

6. 默认约束:当插入字段值时,如果没有穿的话,就会使用默认值

例如 :name varchar(20)not null Default ‘张三’,

7. 外键约束

Forign key 列名 references 主表(列名)


补充:主表(父表)中没有的数据值,在副表(子表)中,是不可以使用的

主表的记录被副表引用,是不可以被删除的


8. 数据库的三大设计范式

1)第一范式:数据表中的所有字段都是不可分割的原子值

字段值还可以继续拆分的,就不满足第一范式


范式设计的越详细,对实际的操作可能更好,但也有不好找的缺点


2)第二范式:必须是满足第一范式的前提下,第二范式要求除主键外每一列都必须完全依赖于主键,如果要出现不完全依赖,那么只可能发生在联合主键的情况下。


3)第三范式:必须先满足第二范式,除开主键列的其他列之间不能有传递依赖的关系。

三、MySQL查询练习

先建立四个表格

学生表(student):学号(sno) 主键

姓名(sname)

性别(ssex)

出生年月日(sbirsday)

所在班级(class)


教师表(teacher):教师编号(tno)

教师姓名(tname)

教师性别(tsex)

出生年月日(tbirsday)

职称(prof)

所在部门(depart)


课程表(course): 课程号(cno)

                 课程名称(cname)

                 教师标号(tno)

外键tno teacher tno


成绩表(score):  学号 (sno)主键

                课程号(cno)

                 成绩(degree)

外键sno、sno


查询练习:

1. 查询stydent表的所有记录

Select * from student;


2. 查询student表中的所有记录的Sname、ssex、和class列

Select sname,ssex,class from student;


3. 查询教师所有的单位不重复的depart列

Selcect distinctdepart from teacher;


4. 查询score表中成绩在60到80之间的所有记录

Select * from score where degree between60 and80;

Select * from score where degree > 60 and degree < 80;(运算比较)


5. 查询score表成绩为85,86,88的记录

Select * from score  where degree in(85,86,88);

6. 查询student中‘95031’班或者性别为‘女’的同学记录

Select * from student where class =’95031’orssex =’女’;


7. 以class降序查询student表的所有记录

Select * from student  order byclass desc;

8. 以cno升序、degree降序查询score、表的所有记录

Select * from score  order by cno asc ,degree desc;


9. 查询‘95031’班的学生人数

Selectcount(*)from student where class=‘95031’ ;


10.查询score表中的最高分的学生学号和课程号(子查询)

Select sno,cno from score where degree =(select max(degree)from score);(分两步。第一步先找到最高分,再找最高分中的学号和课程号)

Select sno,cno,degree from score order by degree desc limit 0,2;(limit第一个数字表示从多少开始,第二个数字表示查多少条)


11.查询每一门课的平均成绩(分组查询)

Select  cno, AVG(degree)  from  score group bycno;(先分组再将平均成绩算出来)


12.查询score表中至少有2名学生选修的并以3开头的课程的平均分数

Select cno,SVG(degree,count(*) from score group bycno havingcount(cno)>=2 and cno like ‘3%(模糊查询)’;  


13.查询分数大于70,小于90的sno列(范围查询)

Select degree,sno from score where degree between 70 and 90;


14.查询所有学生的sname、cno、degree列(多表查询)

Select sname ,cno,degree from student,score  where student.sno = score.sno;(先一个一个表查询,再合在一起,这样思路会清晰很多)


15.查询所有学生的sno、cname和degree列(查询存在的共同字段)

Select sno,cname,degree from course,score where course.cno = score.cno;


16.查询所有学生的sname、cname和degree列

Sname→student

Cname→course

Degree→score

Select sname,cname,degree from student,course,score where student.sno = score.sno and course.cno = score.cno;


17.查询‘95031’班学生每门课的平均分(子查询加分组求平均分)

1)Select sno from student where class = ‘95031’;先查95031班级编号

2)Select * from score where sno in (Select sno from student where class = ‘95031’) ;再查出这个班学生的成绩和课程编号

3)Select cno,AVG(degree) from score where sno in (Select sno from student where class = ‘95031’) group by cno;再查出平均分,因为课程编号有重复所以可以分组


18. 查询选修“3-105”课程的成绩高于“109”号同学“3-105”成绩的所有的记录(子查询)

1)Selcet degree from score where sno=‘109’and cno = ‘3-105’;

第一步首先根据课程编号,学生编号锁定在score表格中,查出109的成绩和3-105的成绩

2)Select * from score where cno = ‘3-105’and  degree > (Selcet degree from score where sno=‘109’and cno = ‘3-105’);

再查出所有成绩中大于这个成绩的所有记录


19. 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录(子查询)

Select * from score where degree  > (select degree from score where sno = ‘109’ and cno = ‘3-109’);


20. 查询和学号为108、101的同学同年出生的所有学生的sno、sname和sbirsday列。

1)Select * from student where sno in (108,101);

2)Select year(sbirsday)from student where sno in (‘108’,’101’);

3)Select sno,sname,sbirday  from student where

Year(sbirsday) in (Select year(sbirsday)from student where sno in (108,101)) ;


21.查询“张旭”教师任课的学生成绩(多层嵌套子查询)

1)Select tno from teacher where tname = ‘张旭’;

先查询出该教师的教师编号

2)Selec cno from course where  tno = (Select tno from teacher where tname = ‘张旭’);

在课程表中查询该教师编号对应的课程号

3)Select degree from score where cno = (Selec cno from course where  tno = (Select tno from teacher where tname = ‘张旭’));

再查询出对应课程标号的学生的成绩,不得不说真是妙呀!


22.查询选修某课程的同学人数多于5人的教师姓名(多表查询)

1)select cno from score group by cno having count(*)>5;

先通过分组查询到选择课程人数大于5人的课程编号

2) select * from teacher ;select * from course;

查出所有的老师表中信息和课程表中的信息比较找到中间连接的序号就是tno

3) select tno from course where cno =(select cno from score group by cno having count(*)>5);

查询出选择课程人数大于5人的课程编号所对应的老师编号

4) select tname from teacher where tno =(select tno from course where cno =(select cno from score group by cno having count(*)>5));

通过之前查询的老师编号找到对应的老师名称



23.查询95033班和95031班全体学生的记录

Select * from student where class in (‘95033’,’95031’);


24. 查询存在有85分以上成绩的课程Cno

Select cno ,degree from score where degree > 85;


25. 查询出“计算机系”教师所教课程的成绩表

解析:先查出计算机系的老师,再查计算机系老师所教的课程,最后再查对应课程的成绩表

Select tno from teacher where depart = ‘计算机系’;

Select cno from course where tno in (Select tno from teacher where depart = ‘计算机系’);

Select * from score where  cno in (Select cno from course where tno in (Select tno from teacher where depart = ‘计算机系’));


26. 查询“计算机系”与“电子工程系”不同职称的教师的tname和prof(union求并集 和not in )

1)Select prof from teacher where depart = ‘电子工程系’;

2)Select prof from teacher where depart = ‘计算机系’ and prof not in (Select prof from teacher where depart = ‘电子工程系’)  union  Select prof from teacher where depart = ‘电子工程系’ and prof not in (Select prof from teacher where depart = ‘计算机系’);


27.查询选修编号为“3-105”课程且成绩至少高于选修标号为“3-245”的同学的Cno、sno和degree,并按照degree从高到低次序排序

1)select degree from score where cno = ‘3-245’;

2)Select * from course where cno=’3-105’and degree > any(select degree from score where cno = ‘3-245’) order by degree desc;


28.查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和degree

Select degree from score where cno = ‘3-245’;

Select * from score where sno =“3-105” and degree > all(Select degree from score where cno = ‘3-245’);


29. 查询所有教师和同学的name、sex和birsday(union求并集/别名as)

Select  tname,tsex,tbirsday from teacher ;

union

Select  sname,ssex,sbirsday from student ;

这里查询出来后会发现查询出来的字段名变为老师所的所以不严谨,这里就需要用到别名as 所以要将老师的字段名改为

Select  tname as name ,tsex as sex,tbirsday as birsday from teacher ;

union

Select  sname,ssex,sbirsday from student ;


30. 查询所有女教师女同学的name、sex和birsday (较上一题增加了一个条件而已,我一开始竟然想用group by ?)

Select tname ,tsex ,tbirsday from teacher  where tsex = ‘女’;

Union

Select  sname,ssex,sbirsday from student where ssex = ‘女’;


31. 查询成绩比该课程平均成绩低的学生的的成绩表

1) select cno , AVG(degree) from score group by cno;

2) select * from score ;

这里将查询出来的表复制一份,将第一张表命名为a,第二张表命名为b,将a表中的cno对应的分数与b表中的平均值做比较得出最后的结论。

Select * from score a where degree >(Select  AVG(degree) from score b wherea.cno = b.cno)


32.查询所有任课教师的Tname和depart

Select tno from course;

Select Tname ,depart from teacher where tno in (Select tno from course) ;


33.查询至少有两个人的班号(条件加分组查询)

Select * from student;

Select class from student where ssex = ‘男’group by class having count(*)>1;


34.查询student表中不姓“王”的同学记录(not like/like)

Select * from student where sname not like ‘王%’;


35.查询student表中每个学生的姓名和年龄(year函数和now函数)

Select * from student;

Select sname,year(now())-year(sbirsday)as‘年龄’ from student;


36.查询student表中最大和最小的sbirsday日期值(max/min)

Select sbirsday from student order by sbirsday;

Selectmax(sbirsday) as ‘最大’,min(sbirsday) as ‘最小’from student ;


37.以班号和年龄从大到小的顺序查询student表中的全部记录(多字段排序)

Select * from student order by  class desc,sbirsday ;(这里要注意优先级)


38.查询“男”教师及其所上的课程

两个表中都含有tno,所以查的时候可以利用这个教师编号连接查询

Select tno from teacher where tsex = “男”;

Select * from teacher where tno in (Select tno from teacher where tsex = “男”);


39.查询最高分同学的sno,cno,degree列

Select * from score where degree = (select max(degree) from score );


40.查询和“李军”同性别的所有同学的Sname

Select ssex from student where sname =‘李军’;

Select Sname from student where ssex =(Select ssex from student where sname =‘李军’) ;


41.查询和“李军”同性别同班的所有同学的Sname

Select  Sname from student where ssex =(Select ssex from student where sname =‘李军’) and class =(select class from student where sname = ‘李军’);


42.查询所有选修“计算机导论”的课程的“男”同学的成绩表

Select cno from course where cname = ‘计算机导论’;

Select sno from student where ssex = ‘男’;

Select * from score where cno  =(Select cno from course where cname = ‘计算机导论’)and sno in (Select sno from student where ssex = ‘男’) ;


43.假设使用如下命令建立了一个grade表



Select sno ,cno,grade from score,grade where degree between low and upp;


四、Sql的四种连接查询

首先建立两个表格

Create table Person(

Id int,

Name varchar(20),

cardID int

);

Create table card(

Id int

Name varchar);


1.内连接

Inner join或者 join

内联查询就是两张表中的数据通过某个字段相等查询出相关记录查询数据。


2.外连接

1)左外连接

Left join 或者left outer join

左外连接会把左边表的数据全部取出来,而右边的表中的数据,如果有相等的,就显示出来,如果没有,就会补上NULL。


2)右外连接

Right join 或者right outer join

与左连接刚好相反


3)完全外连接

Full join 或者 full outer join(mysql不支持)

可以用union将左外连接和右外连接组合起来得到全连接

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月06日 0

暂无评论

推荐阅读
  JiJ96DoSHEh4   2023年11月13日   19   0   0 分隔符字段sed
  KtCMyDnFN1Sj   2023年11月02日   29   0   0 hibernate主键数据库
  M6W6MDdHSmGU   2023年11月02日   17   0   0 表名
fTDyHqWMynbY
最新推荐 更多