MySQL:基础知识

  • 数据库: 数据库是一些关联表的集合。
  • 数据表: 表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。
  • 列: 一列(数据元素) 包含了相同类型的数据, 例如邮政编码的数据。
  • 行:一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。
  • 冗余:存储两倍数据,冗余降低了性能,但提高了数据的安全性。
  • 主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。
  • 外键:外键用于关联两个表。
  • 复合键:复合键(组合键)将多个列作为一个索引键,一般用于复合索引。
  • 索引:使用索引可快速访问数据库表中的特定信息。索引是对数据库表中一列或多列的值进行排序的一种结构。类似于书籍的目录。
  • 参照完整性: 参照的完整性要求关系中不允许引用不存在的实体。与实体完整性是关系模型必须满足的完整性约束条件,目的是保证数据的一致性。

数据库:存储数据 管理数据

非关系型数据库:key-value

关系型数据库:存储固定的格式
            
        Mysql        小 灵活    ---开源    插入式存储引擎(5.0之前Mylsam 5.0之后INNODB 支持行锁 事务)
                                支持事务
                                表锁    行级锁

           sql server
              oracle

基本操作:



#创建用户
create user myuser identified by '123456';

#修改用户
alter user myuser identified by '1234';

#删除用户
drop user myuser;

#授权
#grant 操作 on 表 to 用户
grant select on 0902test.new_table to myuser;
#取消授权
#revoke 操作 on 表 from 用户
revoke select on 0902test.new_table from myuser;

use 0902test;
#外键约束 : 保证数据的一致性
create table mytable(
	number int primary key,  #主键约束 唯一的信息,不允许重复和为空。在一个表里面只有一个。
	name varchar(45) unique, #唯一约束 唯一的信息,不允许重复但允许为空,在一个表中可以有多列。
    sex enum('男','女'),     #枚举类型
    birthday datetime default now() #默认约束mytableinfomytableinfo
);
create table mytableinfo(
	num int,
    course varchar(45),
    constraint fk_num foreign key(num) references mytable(number)
);

#添加列
alter table mytable add column tt int, add column ttt varchar(45);
#修改列
alter table mytable modify column tt datetime;
#删除列
alter table mytable drop column tt, drop column ttt;

select now();

#增加
#insert into 表名 values(值, 值);
#insert into 表名(列名, 列名) values(值, 值);

insert into mytable values(5,'小小','男','2022-2-2');
insert into mytable(number,sex) values(10,'女');
#查询
#select 列名 from 表名
select number from mytable;
select number,name mytable;
select * from mytable;
#修改
#update 表名 set 列名 = ;
update mytable set birthday = now();
#删除
drop table mytableinfo;

delete from mytable;

#加条件 where
select * from mytable where number = 1;
select * from mytable where number != 1;
select * from mytable where number = 2 or number = 3;
select * from mytable where number >= 2 and number <= 4;
select * from mytable where number between 2 and 4;
select * from mytable where number in(1,3,4);
select * from mytable where number not in(1,3,4);

#模糊查询 % 代表任意0个或者多个字符  _代表任意一个字符
select * from mytable where birthday like '2022-09%';
select * from mytable where birthday like '%09%';
select * from mytable where birthday like '____-09%';

#limit 限制显示个数以及位置
select * from mytable limit 3;
select * from mytable limit 2,3;

#排序函数 order by + lie + asc 升序/desc 降序
select * from mytable order by age desc,number desc;


#键表
#第一范式 属性不可以拆分
#number ----> name tel btel
#1		xiaobo 1234xx/345xxx
#2		xaioze 134xxx/%75xx

#第二范式 不允许组合主键的部分依赖
#(学生id,课程id)--->(name sex course score)
#	1	1			  xiao  nan   c		59
#	1	2			  xiao  nan	  c++	60
#	1	3			  xiao  nan	  os	59
#(学生id)-->(name sex) student	n
#(课程id)-->(course,	tid)   course	n
#(学生id,课程id)-->(score)
#表之间的关系	1-1(看具体情况)	1-n(1放在n里面作为一个成员)	n-n 
#teacher (tid)-->(tname,tage,tsex,	课程id)

#第三范式 不允许传递依赖
#(num)-->(name,schoolid)	1
#	1		aa	哈理工	学府路xx
#	2		bb	黑大	学府路xx
#(schoolid)-->(schoolname,schooladdress)

select * from student;
#聚合函数  将多个结果聚合成一个值
#最高分 max()
#最低分	min()
#总分	sum()
#平均分	avg()
#总行数 count()

#求student 男, 女的个数
#步骤:先走查表 再走条件 去算过滤后的信息有多少条
#联合函数 将多个表显示为一个表,前提多个表保证列的个数相同,属性尽量相同
select count(*) from student where Ssex = '男'
union all
select count(*) from student where Ssex = '女';

#group by 分组  + having 加条件
select Ssex, count(*) from student group by Ssex;
#总数等于4的显示
select Ssex, count(*) from student group by Ssex having count(*) = 4;

#44、检索至少选修两门课程的学生学号   sc
select Snum,count(*) 总数 from sc group by Snum having count(*) >= 2;
#count(*)只要这一行上不管哪一列存在数据就代表他有
#count(Snum)这一列上有数据才算

#26、查询每门课程被选修的学生数 	  sc
select Cnum,count(*) from sc group by Cnum;

#45、查询选修了全部课程的学生信息 
select count(*) from course;
select Snum,count(*) from sc group by Snum having count(*) = (select count(*) from course);

select * from student where Snum in(select Snum from sc group by Snum having count(*) = (select count(*) from course));

#11、查询没有学全所有课程的同学的信息
select Snum,count(*) from sc group by Snum having count(*) != (select count(*) from course);

#7、查询学过"张三"老师授课的同学的信息 
select Tnum from teacher where Tname = '张三'; 
select Cnum from course where Tnum = (select Tnum from teacher where Tname = '张三');
select Snum from sc where Cnum = (select Cnum from course where Tnum = (select Tnum from teacher where Tname = '张三'));
select * from student where Snum in(select Snum from sc where Cnum = (select Cnum from course where Tnum = (select Tnum from teacher where Tname = '张三')));


#select 列 表 条件

select *,
(select score from sc where Cnum = '01' and sc.Snum = student.Snum) sc1,
(select score from sc where Cnum = '02' and sc.Snum = student.Snum) sc2
 from student;
 
 select score from sc where Cnum = '01';
 
 # sc1 > sc2
 select *,
(select score from sc where Cnum = '01' and sc.Snum = student.Snum) sc1,
(select score from sc where Cnum = '02' and sc.Snum = student.Snum) sc2
 from student;
 
 select * from (select *,
(select score from sc where Cnum = '01' and sc.Snum = student.Snum) sc1,
(select score from sc where Cnum = '02' and sc.Snum = student.Snum) sc2
 from student) a where sc1 > ifnull(sc2,-1);
 
 
 
 #多表查询
 #内联(取两个表的交集)
 select * from student inner join sc on student.Snum = sc.Snum;
 #先查student 再内联sc 最后以条件把它们关联起来
 #左联(以左表为主)
 select * from student left join sc on student.Snum = sc.Snum;
 #右联
 select * from student right join sc on student.Snum = sc.Snum;
 #笛卡尔积
 select * from student,sc where student.Snum = sc.Snum;
 #先关联再把不要的地方剔除掉
 
 
 select student.*,sc.score c1,sc2.score c2 from student
 left join sc on student.Snum = sc.Snum and Cnum = '01'
 left join sc sc2 on student.Snum = sc2.Snum and sc2.Cnum = '02'
 
 where sc.score > ifnull(sc2.score,-1);
 
 
 #27查询出只有两门课程的全部学生的学号和姓名 
 select student.Snum,Sname,count(*) from student inner join sc on student.Snum = sc.Snum
 group by student.Snum having count(*) = 2;


#3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select * from mytable order by age desc,number desc;
select * from student;
select student.Snum,Sname,avg(score) from student inner join sc on student.Snum = sc.Snum group by student.Snum having avg(score) >= 60 order by avg(score) desc;


#视图 由一张表或者多张表导出的虚拟表
#简化代码 仅此而已
#一般用于查询 (如果想用增加修改删除,视图一定是一张原始表)
select student.*,cnum,score from student inner join sc on student.Snum = sc.Snum;

create view myview as
(select student.*,cnum,score from student inner join sc on student.Snum = sc.Snum);

select * from myview;

drop view myview;




#创建函数
delimiter :
create function myfun(a int, b int)
returns int
begin
	#局部变量
	declare c int default 0;
    #赋值
	set c = a + b;
    return c;
end :
delimiter ;

#调用函数
#会话函数 必须以@开头,生命周期知道会话结束
select @a = myfun(3,4);
select @a;

#系统变量(全局变量)程序关了也有效 一直有效 @@


#删除函数
drop function myfun;

#分支语句
drop function if exists myfun;
delimiter :
create function myfun(a int)
returns varchar(45)
begin
	declare c varchar(45) default '';
    if(a > 0)
    then set c = '你真好看';
    elseif(a = 0)
    then set c = '你真磕碜';
    else
	set c = '你不可言语';
    end if;
    return c;
end :
delimiter ;

select myfun(-5);


drop function if exists myfun;
delimiter :
create function myfun(a int)
returns varchar(45)
begin
	declare c varchar(45) default '';
    case when a > 0 then set c = '你真好看';
		 when a = 0 then set c = '你真磕碜';
    else
	set c = '你不可言语';
    end case;
    return c;
end :
delimiter ;

drop function if exists myfun;
delimiter :
create function myfun(a int)
returns varchar(45)
begin
	declare c varchar(45) default '';
    case a when 0 then set c = '你真好看';
		   when 1 then set c = '你真磕碜';
    else
	set c = '你不可言语';
    end case;
    return c;
end :
delimiter ;


#循环语句
drop function if exists myfun;
delimiter :
create function myfun(a int)
returns int
begin
	declare snum int default 0;
    declare i int default 0;
    while(i <= a)
    do
		set snum = snum + i;
        set i = i + 1;
    end while;
    return snum;
end :
delimiter ;
select myfun(100);


drop function if exists myfun;
delimiter /
create function myfun(a int)
returns int
begin
	declare snum int default 0;
    declare i int default 0;
    out_label : BEGIN
    while(1)
    do
		set snum = snum + i;
        set i = i + 1;
        if(i > a)
        then leave out_label;
        end if;
    end while;
    END out_label;
    return snum;
end /
delimiter ;
select myfun(100);


#有一对兔子,从第三个月开始每个月生一对兔子,问:第十个月,总共有多少对兔子

drop function if exists myrabbit;
delimiter /
create function myrabbit(month int)
returns int
begin
	declare m1 int default 1;
	declare m2 int default 1;
	declare m3 int default 1;
	declare i  int default 2;
	if(month < 3)
	then
		return 1;
	end if;
	while(i < month)
	do 
	set m3 = m1 + m2;
	set m1 = m2;
	set m2 = m3;
	set i  = i + 1;
	end while;
	return m3;
end /
delimiter ;

select myrabbit(10);



drop function if exists myrabbit;
delimiter /
create function myrabbit(month int)
returns int
begin
	select * from student;
    return 0;
end /
delimiter ;

drop function myrabbit;




#存储过程:是一种用来封装复杂结构(包括sql语句)的子程序
delimiter /
create procedure myproc(a int)
begin
	select * from studentmyproc limit a;
end /
delimiter ;
call myproc(2);

drop procedure myproc;



drop procedure if exists myproc;
delimiter /
create procedure myproc(a int,out b int)
begin
	#limit 显示前n条数据
	select * from studentmyproc limit a;
    set b = 10;
end /
delimiter ;
call myproc(2,@c);
select @c;

drop procedure myproc;
#存储过程可以通过参数返回n个值
#函数里面只能放算法 不可放sql语句  存储过程什么都可以
#select调用				call调用

#触发器:一种特殊的存储过程,当指定事件发生的时候,系统自动调用
#new(新的数据)  old(原始数据)
select * from student;
select * from sc;

delimiter /
create trigger myinsert
before insert	#操作
on student		#操作表
for each row	
begin
	
    insert into sc values(new.Snum,'01',null);
end /
delimiter ;

drop trigger myinsert;
insert into student values(12,'ttt',now(),'女');


delimiter /
create trigger mydelete
before delete	#操作
on student		#操作表
for each row	
begin
	
    delete from sc where snum = old.Snum;
end /
delimiter ;

drop trigger mydelete;
delete from student where Snum = 12;




delimiter /
create trigger myupdate
before update	#操作
on student		#操作表
for each row	
begin
	
    update sc set Snum = new.Snum where Snum = old.Snum;
end /
delimiter ;

drop trigger myupdate;
update student set Snum = 11 where Sname like '赵%';



#游标(遍历表)
select * from student;
#索引student



create table bank(
	name varchar(45),
    money double
);
select * from bank;
insert into bank values('aa',100);
insert into bank values('bb',0);

start transaction;
update bank set money = money - 100 where name = 'aa';
update bank set money = money + 100 where name = 'bb';

rollback; #回滚
commit;	  #提交

用到的数据库:

问题及描述:
--1.学生表
Student(Ssum,Sname,Sage,Ssex) --Snum 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表 
Course(Cnum,Cname,Tnum) --Cnum --课程编号,Cname 课程名称,Tnum 教师编号
--3.教师表 
Teacher(Tnum,Tname) --Tnum 教师编号,Tname 教师姓名
--4.成绩表 
SC(Snum,Cnum,score) --Snum 学生编号,Cnum 课程编号,score 分数
*/
--创建测试数据

create table Student(Snum varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10));
insert into Student values('01' , N'赵雷' , '1998-01-01' , N'男');
insert into Student values('02' , N'钱电' , '1999-12-21' , N'男');
insert into Student values('03' , N'孙风' , '1999-05-20' , N'男');
insert into Student values('04' , N'李云' , '1999-08-06' , N'男');
insert into Student values('05' , N'周梅' ,  '2000-12-01' , N'女');
insert into Student values('06' , N'吴兰' , '2003-03-01' , N'女');
insert into Student values('07' , N'郑竹' , '2010-07-01' , N'女');
insert into Student values('08' , N'王菊' , '2021-01-20' , N'女');
create table Course(Cnum varchar(10),Cname nvarchar(10),Tnum varchar(10));
insert into Course values('01' , N'语文' , '02');
insert into Course values('02' , N'数学' , '01');
insert into Course values('03' , N'英语' , '03');
create table Teacher(Tnum varchar(10),Tname nvarchar(10));
insert into Teacher values('01' , N'张三');
insert into Teacher values('02' , N'李四');
insert into Teacher values('03' , N'王五');
create table SC(Snum varchar(10),Cnum varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
insert into SC values('09' , '03' , 98);


#6、查询"李"姓老师的数量 teacher
#29、查询名字中含有"风"字的学生信息

#28、查询男生、女生人数 

#44、检索至少选修两门课程的学生学号 
#26、查询每门课程被选修的学生数 

#45、查询选修了全部课程的学生信息 
#11、查询没有学全所有课程的同学的信息 
#7、查询学过"张三"老师授课的同学的信息 
#8、查询没学过"张三"老师授课的同学的信息 


#27查询出只有两门课程的全部学生的学号和姓名 


#1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
#1.1、查询同时存在"01"课程和"02"课程的情况

#1.2、存在"01"课程但可能不存在"02"课程的情况(不存在时显示为null)(以下存在相同内容时不再解释)


#2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
#2.1、查询同时存在"01"课程和"02"课程的情况

#2.2、查询同时存在"01"课程和"02"课程的情况和不存在"01"课程但存在"02"课程的情况


#3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩


#4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
#4.1、查询在sc表存在成绩的学生信息的SQL语句。

#4.2、查询在sc表中不存在成绩的学生信息的SQL语句。


#5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
#5.1、查询所有有成绩的SQL。

#5.2、查询所有(包括有成绩和无成绩)的SQL。


#9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息


#10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息




#12、查询至少有一门课与 学号为"01"的同学所学相同的同学的信息 



#13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 XX


#14、查询没学过"张三"老师讲授的任一门课程的学生姓名 



#15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 



#16、检索"01"课程分数小于60,按分数降序排列  的学生信息


#17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩


#18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
#及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90


#19、按各科成绩进行排序,并显示排名


#20、查询学生的总成绩并进行排名
#20.1 查询学生的总成绩




#20.2 查询学生的总成绩并进行排名,分总分重复时保留名次空缺和不保留名次空缺两种。


#21、查询不同老师所教不同课程平均分从高到低显示 


#22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩



#23、查询学生平均成绩及其名次 


#24查询学生的平均成绩并进行排名.

  
#25、查询各科成绩前三名的记录
#25.1 分数重复时保留名次空缺

#25.2 分数重复时不保留名次空缺,合并名次




 



#30、查询同名同性学生名单,并统计同名人数 

 
#31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime) 


#32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 


#33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 


#34、查询课程名称为"数学",且分数低于60的学生姓名和分数 


#35、查询所有学生的课程及分数情况; 


#36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 


#37、查询不及格的课程


#38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 




#40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩



#41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 


#42、查询每门功成绩最好的前两名 

#43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列  





#46、查询各学生的年龄
#46.1 只按照年份来算

#46.2 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

#47、查询本周过生日的学生

#48、查询下周过生日的学生

#49、查询本月过生日的学生

#50、查询下月过生日的学生


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值