搭建一个简单的数据库实例

硬件:MAC

软件:MySQL workbench

创建数据库

CREATE database OJBK_2;

使用这个数据库

USE OJBK_2;



创建学生表格
CREATE TABLE STUDENT(
Sno CHAR(8),
Sname CHAR(10),
Class CHAR(8)
);

创建成绩表格
CREATE table GRADE(
Sno char(8),
Details varchar(8)
);

-- 

创建选课表格

CREATE table SC(
Sno char(8),
Cno char(8)
-- Details varchar(8)
);

-- drop  table SC;

向学生表格插入学生的数据

INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('103','dld3','1');
INSERT INTO STUDENT(Sno,Sname)
VALUES('104','dld4','1');
INSERT INTO STUDENT(Sno,Sname)
VALUES('105','dld5','2');
INSERT INTO STUDENT(Sno,Sname)
VALUES('106','dld6','2');



向选课的表格插入选课数据
insert into SC(Sno,Cno)
VALUES('104','1');
insert into SC(Sno,Cno)
VALUES('104','2');
insert into SC(Sno,Cno)
VALUES('103','1');
insert into SC(Sno,Cno)
VALUES('105','1');
insert into SC(Sno,Cno)
VALUES('106','1');
insert into SC(Sno,Cno)
VALUES('103','2');
insert into SC(Sno,Cno)
VALUES('105','2');










INSERT INTO AWARD(Sno,Details)
VALUES('103','HEHE04');


select A.Sno,A.SNAME,B.details
from STUDENT A LEFT JOIN AWARD B ON A.Sno=B.Sno
WHERE A.Sno='103';


-- drop database OJBK_1;
-- select STUDENT.Sno,STUDENT.SNAME
-- from STUDENT;
-- 
-- drop  table SC;-- 
drop table STUDENT;


select A.Sno,A.Cno

from SC A;

==================v1.0



drop database OJBK_2;
CREATE database OJBK_2;
USE OJBK_2;
-- 
-- drop table STUDENT;
-- 
-- drop table SC;
-- drop table  Courses;








CREATE TABLE STUDENT(
Sno CHAR(8) not null,
Sname CHAR(10) not null,
Class CHAR(8)not null,
primary key(Sno,Sname)


);


create table Courses(
Cno char(8) not null,
Tno char(8) not null,
primary key(Cno)
);


insert into Courses(Cno,Tno)
VALUES('1','T1');
insert into Courses(Cno,Tno)
VALUES('2','T1');


-- 
CREATE table SC(
Sno char(8) not null,
Cno char(8) not null,
Grade char(8),
primary key(Sno,Cno),
foreign key (Sno) references STUDENT(Sno),
foreign key (Cno) references Courses(Cno)
-- Details varchar(8)
);
-- drop  table SC;
INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('103','dld3','1');
INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('104','dld4','1');
INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('105','dld5','2');
INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('106','dld6','2');




insert into SC(Sno,Cno,Grade)
VALUES('104','1','90');
insert into SC(Sno,Cno,Grade)
VALUES('104','2','60');
insert into SC(Sno,Cno,Grade)
VALUES('103','1','70');
insert into SC(Sno,Cno,Grade)
VALUES('105','1','100');
insert into SC(Sno,Cno,Grade)
VALUES('106','1','25');
insert into SC(Sno,Cno,Grade)
VALUES('103','2','30');
insert into SC(Sno,Cno,Grade)
VALUES('105','2','56');














-- select A.Sno,A.SNAME,B.details
-- from STUDENT A LEFT JOIN AWARD B ON A.Sno=B.Sno
-- WHERE A.Sno='103';


-- drop database OJBK_1;
-- select STUDENT.Sno,STUDENT.SNAME
-- from STUDENT;
-- 
-- drop  table SC;-- 
-- drop table STUDENT;
-- 
select A.Sno,A.Cno

from SC A;

===============



drop database OJBK_2;
CREATE database OJBK_2;
USE OJBK_2;
-- 
-- drop table STUDENT;
-- 
-- drop table SC;
-- drop table  Courses;








CREATE TABLE STUDENT(
Sno CHAR(8) not null,
Sname CHAR(10) not null,
Class CHAR(8)not null,
primary key(Sno,Sname)


);


create table Courses(
Cno char(8) not null,
Tno char(8) not null,
primary key(Cno)
);


insert into Courses(Cno,Tno)
VALUES('1','T1');
insert into Courses(Cno,Tno)
VALUES('2','T1');


-- 
CREATE table SC(
Sno char(8) not null,
Cno char(8) not null,
Grade char(8),
primary key(Sno,Cno),
-- foreign key (Sno) references STUDENT(Sno),
foreign key (Cno) references Courses(Cno)
-- Details varchar(8)
);
-- drop  table SC;
INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('103','dld3','1');
INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('104','dld4','1');
INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('105','dld5','2');
INSERT INTO STUDENT(Sno,Sname,Class)
VALUES('106','dld6','2');




insert into SC(Sno,Cno,Grade)
VALUES('104','1','90');
insert into SC(Sno,Cno,Grade)
VALUES('104','2','60');
insert into SC(Sno,Cno,Grade)
VALUES('103','1','70');
insert into SC(Sno,Cno,Grade)
VALUES('105','1','100');
insert into SC(Sno,Cno,Grade)
VALUES('106','1','25');
insert into SC(Sno,Cno,Grade)
VALUES('103','2','30');
insert into SC(Sno,Cno,Grade)
VALUES('105','2','56');








-- select  STUDENT.Sno,STUDENT.Sname,STUDENT.Class,SC.Cno,SC.Grade
-- from STUDENT,SC
-- where SC.Cno='1' and STUDENT.Sno=SC.Sno
-- order by Grade desc;


-- select A.Sno,A.SNAME,B.details
-- from STUDENT A LEFT JOIN AWARD B ON A.Sno=B.Sno
-- WHERE A.Sno='103';


-- drop database OJBK_1;
-- select STUDENT.Sno,STUDENT.SNAME
-- from STUDENT;
-- 
-- drop  table SC;-- 
-- drop table STUDENT;
-- 
SELECT  
  sum(CASE when Grade<60 then 1 else 0 end)   AS '不及格60-',  
  sum(CASE when Grade>=60 and Grade<70 then 1 else 0 end)   AS '及格60-70',  
  sum(CASE when Grade>=70 and Grade<80 then 1 else 0 end)   AS '中等70-80',  
  sum(CASE when Grade>=80 and Grade<90 then 1 else 0 end)   AS '良好80-90',  
  sum(CASE when Grade>=90 and Grade<=100 then 1 else 0 end)   AS '优秀90+' ,
  SC.Sno AS'学生学号' 
  -- Class AS 'Class'
from SC,STUDENT 
 where STUDENT.Sno=SC.Sno
group by SC.Sno;


-- 
-- select SC.Grade,SC.Sno,SC.Cno,STUDENT .Class
-- from SC,STUDENT;

你可能感兴趣的:(实验报告)