【MySQL】数据库课程实验
数据定义
#mysql --version 查版本号
#mysql -uroot -p #登录
show databases; #查询当前服务存在的数据库
#create database test ; #创建SQL数据库
use test;
drop table if EXISTS JS,Course,SK;
#建表
create table JS(
Tno CHAR(7),
Tname CHAR(10),
Tsex CHAR(2),
Birthday DATE,
Dept CHAR(20),
Sid CHAR(18)
);
create table Course(
Cno CHAR(10),
Cname CHAR(20),
Credit TINYINT,-- 短整形
Property CHAR(10),
Hours INT
);
create table SK(
Tno CHAR(7),
Cno CHAR(10),
Hours INT
);
#插入数据
insert into JS (Tno,Tname,Tsex,Birthday,Dept,Sid) VALUES ('T001', '刘薇', '女', '1971-3-20', '电信', '551021197103203121'),
('T002', '张骐劲', '男', '1963-7-13', '数理', '32010119630713318X'),
('T003', '李子文', '女', '1973-9-15', '外语', '461031197309153829'),
('T004', '江海防', '女', '1960-2-18', '社科', '560102196002185623'),
('T005', '李铁', '男', '1977-10-11', '数理', '230103197710118632'),
('T006', '吴天一', '男', '1962-4-23', '电信', '320104196204237516'),
('T007', '赵志华', '男', '1968-8-27', '社科', '321102196808277214'),
('T008', '钱进', '男', '1980-7-10', '电信', '570102198007103452'),
('T009', '孙星南', '女', '1981-3-2', '外语', '110102198103024125');
insert into Course (Cno,Cname,Credit,property) VALUES ('01010101', '大学英语1', 4, '考试'),
('01010102', '普通物理1', 4, '考试'),
('01010103', '高等数学1', 6, '考试'),
('01010104', '形势政策', 2, '考查'),
('01010105', '计算机基础', 4, '考查');
insert into SK (Tno,Cno,Hours) VALUES ('T001', '01010105', 64),
('T002', '01010102', 64),
('T009', '01010101', 64),
('T004', '01010104', 32),
('T005', '01010103', 96),
('T006', '01010105', 64),
('T003', '01010101', 64);
alter table SK add Type char(4);
alter table SK change Hours Hours Smallint;
alter table Course drop Hours;
-- 4.1
drop table if EXISTS students;
#建表
create table students(
SNO CHAR(10) NOT NULL,
SNAME CHAR(8) NOT NULL,
AGE NUMERIC(3,0),
SEX CHAR(2),
BPLACE CHAR(20),
Polity CHAR(20),
primary key(SNO)
);
insert into students (SNO,SNAME,SEX,AGE,BPLACE,Polity) VALUES ('S001', '刘薇', '女','22', '1997-3-20', '团员'),
('S002', '张骐劲', '男','20', '1999-7-13', '团员'),
('S003', '李子文', '女','22', '1997-9-15', '党员'),
('S004', '江海防', '女','19','2000-2-18', '团员');
-- 4.2
alter TABLE JS ADD ADDR char(50);
-- 4.3
-- 根据现有表建立
drop table if EXISTS GRIL,score,js1;
create table GRIL SELECT SNO,SNAME,AGE FROM students;
-- 4.4
create table score(
SNO CHAR(10),
CNO CHAR(10),
score NUMERIC(6,0)
);
insert into score (SNO,CNO,score) VALUES ('S001', '01010105', 64),
('S002', '01010102', 64),
('S003', '01010101', 64),
('S004', '01010104', 32);
-- 5.1
create table js1 SELECT * FROM JS;
create UNIQUE INDEX I_js_sid ON js1(Sid);
-- 5.2
create UNIQUE INDEX I_cource_xf ON Course(Cno,Credit);
数据操纵
SHOW DATABASE;
USE test;
SELECT SNAME,BPLACE FROM students WHERE SEX='男';
SELECT Tsex,count(Tno) FROM JS group by Tsex;
SELECT SNO,SNAME,AGE FROM students WHERE AGE>=20 and AGE<=23 ORDER BY AGE;
-- 1-4
SELECT SNAME FROM students WHERE AGE>=(SELECT AVG(AGE) FROM students);
-- 1-5
select SNAME,students.SNO,Course.Cname,score.score
from students,Course,score
where score.score<60 and students.SNO=score.SNO and score.Cno=Course.Cno;
-- 1-6
SELECT JS.Tno,Tname,Tsex,Birthday,Dept,Sid
from JS,SK where JS.Tno=SK.Tno and SK.Cno='01010105';
-- 1-7
SELECT Tname,Birthday,Cname,Hours
from JS,SK,course
where JS.Birthday<'1972-1-1' and JS.Tno=SK.Tno and SK.Cno=course.Cno;
-- 1-8
select * from JS where JS.Tno not in (select JS.Tno from JS,SK where JS.Tno = SK.Tno);
-- 2-1 在ex1中已做
insert into students (SNO,SNAME)values('S666','小坤');
insert into JS (Tno,Tname) values ('1476','李映雪');
-- 2-3 已存在
-- 3-1
update students set AGE=AGE+1;
update score set score=score+5 where Cno='01010105';
update score set score = 0 where Sno='S004';
-- 4-1
drop from JS where DATEDIFF('2019-11-20',Birthday)>60*365;
-- 为score表添加外键
-- alter table score add foreign key(SNO) REFERENCES students(SNO);
-- 采用级联删除
alter table score drop foreign key score_ibfk_1;
DELETE from students where SNO='S001';
DELETE from score where SNO='S001';
-- 创建视图
-- CREATE VIEW op_age(Tno,Tname) AS
-- SELECT Tno,Tname FROM JS;
-- DROP VIEW op_age;
-- 1-3
-- SELECT Tno,Tname,DATEDIFF('2019-11-20',Birthday)/365 Age FROM JS
-- WHERE DATEDIFF('2019-11-20',Birthday)>=20*365 and DATEDIFF('2019-11-20',Birthday)<=50*365
-- ORDER BY Birthday DESC;#年龄升序=生日降序
-- 1-4
-- SET @avg=0;
-- SELECT @avg:=AVG(DATEDIFF('2019-11-20',Birthday)/365) Age FROM JS;
-- SELECT Tno,Tname,DATEDIFF('2019-11-20',Birthday)/365 Age FROM JS
-- WHERE DATEDIFF('2019-11-20',Birthday)>=@avg * 365;
数据约束
show databases;
-- 主键
alter table js add PRIMARY KEY(Tno);
-- 非空
alter table js modify Tname varchar(10) NOT NULL;
-- 限制取值
alter table js add CONSTRAINT c_Tsex CHECK (Tsex in ('男','女'));
-- 唯一
alter table js add UNIQUE(Sid);
-- Course
alter table course add PRIMARY KEY(Cno);
alter table course modify Cname varchar(20) NOT NULL;
alter table course add CONSTRAINT c_Credit CHECK (Credir>0);
-- 默认值
alter table course modify property varchar(10) DEFAULT '必修';
-- SK
alter table SK add primary key(Tno,Cno);
-- 外码
alter table SK add foreign key(Tno) references js(Tno);
alter table SK add foreign key(Cno) references course(Cno);
alter table sk add CONSTRAINT c_hours CHECK (hours>0);#限制
数据库编程
-- 1-1 最高分
SELECT students.SNO,students.SNAME,Course.Cname,score
FROM score,students,Course
where students.SNO=score.SNO and Course.Cno=score.Cno
ORDER BY score DESC limit 1;#降序排取第一个
-- 最低分
SELECT students.SNO,students.SNAME,Course.Cname,score
FROM score,students,Course
where students.SNO=score.SNO and Course.Cno=score.Cno
ORDER BY score ASC limit 1;
-- 建立班级表
drop table if EXISTS class,class_student;
create table class(
Bno varchar(4),
Bname varchar(20)
);
create table class_student(
Sno char(10),
Bno varchar(4)
);
insert into students (SNO,SNAME,AGE,SEX,BPLACE,Polity) VALUES ('8103','张三',20,'男','1999-07-07','团员'),('8104','李芬',21,'女','1998-01-06','党员');
insert into class values('100','一班');
insert into class_student (Sno,Bno) VALUES ('8103','100'),('8104','100');
-- 1-2
select class.Bname, students.SNO,students.SNAME,students.SEX,students.BPLACE,students.Polity
from class,class_student,students
where class.Bno = 100 and class_student.Sno = students.SNO;
-- 1-3
drop table students;
create table students(
SNO CHAR(10) PRIMARY KEY NOT NULL,
SNAME CHAR(8) NOT NULL,
AGE NUMERIC(3,0),
SEX CHAR(2),
BPLACE CHAR(20),
Polity CHAR(20) DEFAULT '群众'
);
insert into students (SNO,SNAME,AGE,SEX,BPLACE,Polity) VALUES ('8103','张三',20,'男','1999-07-07','团员'),('8104','李芬',21,'女','1998-01-06','党员');
insert into students (SNO,SNAME) VALUES ('8101','张三丰'),('8102','李四');
select * from students;
-- 1-4
insert into students(SNO,SNAME,AGE,SEX,BPLACE) values('8105','小鬼',20,'男','1999-09-08');
insert into score (SNO,CNO,score) values ('8103','01010101',70),('8104','01010101',50);
-- 查询
SELECT avg(score) AVG,Course.Cname,class.Bname
FROM score,students,Course,class,class_student
where Course.Cno=score.CNO and class.Bno=class_student.Bno
and class.Bno=100 and class_student.Sno = students.SNO
and class_student.Sno=score.SNO;
-- 1-5
/*
CASE 属性
WHEN 条件1 THEN 结果1
WHEN 条件2 THEN 结果2
ELSE 其他结果
END
*/
SELECT score,(CASE score.CNO WHEN 1 THEN Course.Cname ELSE Course.Cname END) Cname,class.Bname
FROM score,students,Course,class,class_student
where Course.Cno=score.CNO and class.Bno=class_student.Bno
and class.Bno=100 and class_student.Sno = students.SNO and class_student.Sno=score.SNO;
-- 1-6
SELECT students.SNAME,
(case
when score < 60 then '不及格'
when score BETWEEN 60 AND 90 then '良好'
else '优秀'
end) Level,Course.Cname,class.Bname
FROM score,students,Course,class,class_student
where Course.Cno=score.CNO and class.Bno=class_student.Bno
and class.Bno=100 and class_student.Sno = students.SNO and class_student.Sno=score.SNO;
-- 存储过程的创建、调用
alter table students add class INT DEFAULT 1;#为学生表添加班级信息,默认1班
-- 创建
drop procedure if EXISTS stuscoreinfo;
delimiter $$ #将语句的结束符号从分号;临时改为两个$$(可以是自定义)
create procedure stuscoreinfo()
BEGIN
select class,SNAME,SEX,Cname,score
from students,course,score
where students.SNO = score.SNO and course.CNO = score.CNO;
END$$
delimiter ; #将语句的结束符号恢复为分号
# drop procedure p_name;#删除
-- 调用
call stuscoreinfo();
drop procedure if EXISTS stu_info;
delimiter $$
create procedure stu_info(IN s_sno varchar(10))
BEGIN
select * from students
where s_sno = students.SNO;
END$$
delimiter ;
call stu_info('8101');
/*
默认情况下,存储过程和默认数据库相关联,如果想指定存储过程创建在某个特定的数据库下,那么在过程名前面加数据库名做前缀;
在定义过程时,使用DELIMITER $$ 命令将语句的结束符号从分号 ;
临时改为两个$$,使得过程体中使用的分号被直接传递到服务器,而不会被客户端(如mysql)解释。
*/
-- 1-3
drop procedure if EXISTS stu_age;
delimiter $$
set @age = 0;
create procedure stu_age(IN s_sno varchar(10),OUT age INT)
BEGIN
select DATEDIFF('2019-11-20',BPLACE)/365 Age from students
where s_sno = students.SNO;
END$$
delimiter ;
call stu_age('8103',@age);
-- 1-4.5.6
call stuscoreinfo();
call stu_info('8101');
call stu_age('8103',@age);
-- 1-7
show procedure status;
-- 1-8
drop procedure stuscoreinfo;
-- 补充班级
insert into class values('101','二班');
insert into class values('102','三班');
insert into class_student (Sno,Bno) VALUES ('S002','101'),('S003','101'),('S004','102');
-- 自定义函数
delimiter $$
create procedure fun_sumscores()
BEGIN
select class.Bname,Course.Cname,avg(score) AVG
from class,score,class_student,Course
where class.Bno=class_student.Bno and class_student.Sno=score.Sno and Course.Cno=score.Cno
group by Course.Cname,class.Bno;
END$$
delimiter ;
DELIMITER $$
create function fun_sumscores() returns int deterministic
begin
declare c int;
SELECT avg(score) from score into c;
return c;
END $$
DELIMITER ;
select fun_sumscores();
-- 1-2
delimiter $$
create function fun_sumscores1(in_class VARCHAR(10),in_cno VARCHAR(10)) returns int deterministic
begin
declare c int;
SELECT sum(score) from score,class_student
where cno = in_cno and in_class = class_student.Bno
and class_student.Sno = score.SNO into c;
return c;
END $$
DELIMITER ;·
select fun_sumscores1('100','01010101');
-- 1-3
DELIMITER $$
create function fun_sumscores2(in_sname VARCHAR(10)) returns int deterministic
begin
declare c int;
SELECT count(*) from students where sname=in_sname into c;
return c;
END $$
DELIMITER ;
select fun_sumscores2('李浩');
/*
mysql查看存储过程函数
查询数据库中的存储过程和函数
select `name` from mysql.proc where db = 'xx' and `type` = 'PROCEDURE' //存储过程
select `name` from mysql.proc where db = 'xx' and `type` = 'FUNCTION' //函数
show procedure status; //存储过程
show function status; //函数
查看存储过程或函数的创建代码
show create procedure proc_name;
show create function func_name;
查看视图
SELECT * from information_schema.VIEWS //视图
SELECT * from information_schema.TABLES //表
查看触发器
SHOW TRIGGERS [FROM db_name] [LIKE expr]
SELECT * FROM triggers T WHERE trigger_name=”mytrigger” \G
*/
纯粹于当下,执着于理想。