【数据库】SQL练习

/*

有3张表,学生、课程、成绩

t_student:

    student_id number(6) 主键,

    student_name varchar2(20) 不能重复 不能为空,

    student_sex varchar2(1)  '1'-男 '2'-女,默认为'1',进行check约束

t_class:

    class_id number(6) 主键,

    class_name varchar(20) 不能重复 不能为空

t_score:

    score_id number(6) 主键,

    student_id number(6) 外键约束 不能为空,如果主表删除了级联删除,

    class_id number(6) 外键约束 不能为空,如果主表删除了级联删除,

    score_score(成绩) number(4,1) 不能为空,最大100分

    score_makeup(补考成绩) number(4,1),-1表示没有进行补考,最大100分

要求:

1、根据上述信息创建3张表

2、t_student表添加1列student_status varchar2(1)  '1'-有效 '0'-无效,默认为'1',进行check约束,要用alter table实现

3、输入一些测试数据,要用insert into语句

4、用1条sql语句查询所有不及格学生的信息,要求显示:学生姓名  不及格课程总数

5、用1条sql语句查询男生、女生分别有多少人,要求显示:男生人数  女生人数

6、用1条sql语句查询0-10分、11-20分、21-30分、...81-90分、91-100分分别有多少人,要求显示:分数段区间  人数

7、查询补考也不及格的学生信息,要求显示:学生姓名  课程名称   该课成绩  该课补考成绩   不及格课程总数  补考不及格课程总数

*/

--1.创表

/* Table: "t_student" */

/*drop table ttt_student cascade constraints;

create table ttt_student  (

   student_id         number(6)                       not null,

   student_name       varchar2(20)                    unique not null,

   student_sex        varchar2(1)                     default 1 check(student_sex='1' or student_sex='2'),

   constraint PK_TTT_STUDENT primary key (student_id)

);*/

DROP TABLE TTT_STUDENT CASCADE CONSTRAINTS;--删除关联

CREATE TABLE TTT_STUDENT (

STUDENT_ID               NUMBER(6)                    NOT NULL,

STUDENT_NAME             VARCHAR2(20)                 UNIQUE NOT NULL,

STUDENT_SEX              VARCHAR2(1)                  DEFAULT 1 CHECK(STUDENT_SEX='1' OR STUDENT_SEX='1'),

PRIMARY KEY(STUDENT_ID)

);

/* Table: "t_class" */

/*drop table ttt_class cascade constraints;

create table ttt_class  (

   class_id           number(6)                       not null,

   class_name         varchar2(20)                    unique not null,

   constraint PK_TTT_CLASS primary key (class_id)

);*/

DROP TABLE TTT_CLASS CASCADE CONSTRAINTS;

CREATE TABLE TTT_CLASS(

CLASS_ID     NUMBER(6)           NOT NULL,

CLASS_NAME   VARCHAR(20)       UNIQUE NOT NULL,

PRIMARY KEY(CLASS_ID)

);

/* Table: "t_score" */

/*alter table ttt_score

   drop constraint FK_TTT_SCORE_REFERENCE_TTT_STUDEN;

alter table ttt_score

   drop constraint FK_T_SCORE_REFERENCE_T_CLASS;

drop table ttt_score cascade constraints;

create table "t_score"  (

   "score_id"           number(6)                       not null,

   "student_id"         number(6)                       not null,

   "class_id"           number(6)                       not null,

   "score_score"        number(4,1)                     not null check("score_score" between 0 and 100),

   "score_makeup"       number(4,1)                     default -1 check(("score_makeup" between 0 and 100) or "score_makeup"=-1),

   constraint PK_T_SCORE primary key ("score_id"),

   foreign key ("student_id") references "t_student"("student_id") on delete cascade,

   foreign key ("class_id") references "t_class"("class_id") on delete cascade

);*/

ALTER TABLE TTT_SCORE

      DROP CONSTRAINT FK_TTT_SCORE_REFERENCE_TTT_STUDENT;

ALTER TABLE TTT_SCORE

      DROP CONSTRAINT FK_TTT_SCORE_REFERENCE_TTT_CLASS;

DROP TABLE TTT_SCORE CASCADE CONSTRAINTS;

CREATE TABLE TTT_SCORE(

SCORE_ID     NUMBER(6)       NOT NULL,

STUDENT_ID   NUMBER(6)       NOT NULL,

CLASS_ID     NUMBER(6)       NOT NULL,

SCORE_SCORE  NUMBER(4,1)     NOT NULL CHECK(SCORE_SCORE BETWEEN 0 AND 100),

SCORE_MAKEUP NUMBER(4,1)     DEFAULT -1 CHECK((SCORE_MAKEUP BETWEEN 0 AND 100) or SCORE_MAKEUP = -1),

PRIMARY KEY(SCORE_ID),

FOREIGN KEY(STUDENT_ID) REFERENCE TTT_STUDENT(STUDENT_ID) ON DELETE CASCADE,

FOREIGN KEY(CLASS_ID) REFERENCE TTT_CLASS(CLASS_ID) ON DELETE CASCADE

);

--------------------------------------------------------------------------------

--2.alter加列

--alter table "t_student" add "student_status" number(1) default 1 check("student_status"=1 or "student_status"=0);

/*alter table "t_student" add "student_status" varchar2(1) default 1 check("student_status"='1' or "student_status"='0');*/

ALTER TABLE TTT_STUDENT ADD STUDENT_STATUS VARCHAR2(1) DEFAULT 1 CHECK(STUDENT_STATUS = 1 OR STUDENT_STATUS = 0);

--------------------------------------------------------------------------------

--3.insert加数据,update改数据

insert into "t_student" ("student_id" , "student_name" ) values (1,'dwq');

insert into "t_student" ("student_id" , "student_name" ) values (2,'jtd');

insert into "t_student" ("student_id" , "student_name" ) values (3,'lch');

update "t_student" set "student_sex"='2' where "student_name" ='lch';

insert into "t_class" ("class_id" , "class_name" ) values (1,'math');

insert into "t_class" ("class_id" , "class_name" ) values (2,'english');

insert into "t_score" ("score_id" ,"student_id","class_id","score_score" ) values (1,1,1,100);

insert into "t_score" ("score_id" ,"student_id","class_id","score_score" ) values (2,2,1,55);

insert into "t_score" ("score_id" ,"student_id","class_id","score_score" ) values (3,3,1,59);

insert into "t_score" ("score_id" ,"student_id","class_id","score_score" ) values (4,2,2,55);

update "t_score" set "score_makeup"=59 where "score_id" =2;

update "t_score" set "score_makeup"=33 where "score_id" =3;

update "t_score" set "score_makeup"=21 where "score_id" =4;

update "t_score" set "score_makeup"=80 where "score_id" =4;

--------------------------------------------------------------------------------

--4.学生姓名  不及格课程总数

/*select "t_student"."student_name", count("t_score"."class_id") as "failed"

from "t_student" join "t_score"

on ("t_student"."student_id" = "t_score"."student_id" and "t_score"."score_score" <60)

group by "t_student"."student_name";*/

SELECT TTT_STUDENT.STUDENT

--------------------------------------------------------------------------------

--5.男生人数  女生人数

/*select (case when "student_sex"='1' then 'M' when "student_sex"='2' then 'F' end  ) "sex",Count(*) "num" from "t_student"

group by "student_sex";*/

select sum(decode("student_sex",'1',1)) boy_sum,sum(decode("student_sex",'2',1)) girl_sum from "t_student";

--------------------------------------------------------------------------------

--6.分数段区间  人数

select  

case when "score_score" <= 10 then '[0,10]'  

when "score_score" > 10 and "score_score" <= 20 then '(10,20]'  

when "score_score" > 20 and "score_score" <= 30 then '(20,30]'  

when "score_score" > 30 and "score_score" <= 40 then '(30,40]'  

when "score_score" > 40 and "score_score" <= 50 then '(40,50]'  

when "score_score" > 50 and "score_score" <= 60 then '(50,60]'  

when "score_score" > 60 and "score_score" <= 70 then '(60,70]'  

when "score_score" > 70 and "score_score" <= 80 then '(70,80]'

when "score_score" > 80 and "score_score" <= 90 then '(80,90]'  

when "score_score" > 90 and "score_score" <= 100 then '(90,100]'    

end "score",

count(*) "num"

from "t_score" 

group by  

case when "score_score" <= 10 then '[0,10]'  

when "score_score" > 10 and "score_score" <= 20 then '(10,20]'  

when "score_score" > 20 and "score_score" <= 30 then '(20,30]'  

when "score_score" > 30 and "score_score" <= 40 then '(30,40]'  

when "score_score" > 40 and "score_score" <= 50 then '(40,50]'  

when "score_score" > 50 and "score_score" <= 60 then '(50,60]'  

when "score_score" > 60 and "score_score" <= 70 then '(60,70]'  

when "score_score" > 70 and "score_score" <= 80 then '(70,80]'

when "score_score" > 80 and "score_score" <= 90 then '(80,90]'  

when "score_score" > 90 and "score_score" <= 100 then '(90,100]'    

end;

--------------------------------------------------------------------------------

--7.学生姓名  课程名称   该课成绩  该课补考成绩   不及格课程总数  补考不及格课程总数

select c."student_name",c."class_name",c."score_score",c."score_makeup",b."score_failed",a."makeup_failed"

from(select stu."student_name",COUNT(*) "makeup_failed"

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."score_makeup"<60  and sco."score_score"<60 and sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"

group by stu."student_name")a

left join

(select stu."student_name",COUNT(*) "score_failed"

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."score_score"<60   and sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"

group by stu."student_name")b 

on (a."student_name"=b."student_name")

left join

(select stu."student_name",cla."class_name",sco."score_score",sco."score_makeup"

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id")c 

on(a."student_name"=c."student_name" and b."student_name"=c."student_name");

--------------------------------------------------------------------------------

--

select stu."student_name",cla."class_name",sco."score_score",sco."score_makeup"

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"; 

-------------

 select deptno,ename,sal,

   2   sum(sal) over (partition by deptno order by ename) 部门连续求和,--各部门的薪水"连续"求和

   3   sum(sal) over (partition by deptno) 部门总和,   -- 部门统计的总和,同一部门总和不变

   4   100*round(sal/sum(sal) over (partition by deptno),4)"部门份额(%)",

   5   sum(sal) over (order by deptno,ename) 连续求和, --所有部门的薪水"连续"求和

   6   sum(sal) over () 总和,   -- 此处sum(sal) over () 等同于sum(sal),所有员工的薪水总和

   7   100*round(sal/sum(sal) over (),4) "总份额(%)"

   8   from emp

--------------------------------------------------------------------------------

--------------------------------------------------------------------------------

--草稿:(可忽略)

select * from "t_student" order by "student_id";

select * from "t_class" order by "class_id";

select * from "t_score" order by "score_id";

select "t_student".* , "t_student".rowid from "t_student";

select "t_class".* , "t_class".rowid from "t_class";

select "t_score".* , "t_score".rowid from "t_score";

insert into "t_student" ("student_id" , "student_name" ) values (1,'dwq');

insert into "t_student" ("student_id" , "student_name" ) values (2,'jtd');

insert into "t_student" ("student_id" , "student_name" ) values (3,'lch');

---insert into "t_student" ("student_id" , "student_name" ) values (4,'lll','2');

update "t_student" set "student_sex"='2' where "student_name" ='lch';

--insert into "t_student" ("student_id" , "student_name" ) values (2,'dwq');

insert into "t_class" ("class_id" , "class_name" ) values (1,'math');

insert into "t_class" ("class_id" , "class_name" ) values (2,'english');

insert into "t_score" ("score_id" ,"student_id","class_id","score_score" ) values (1,1,1,100);

insert into "t_score" ("score_id" ,"student_id","class_id","score_score" ) values (2,2,1,55);

insert into "t_score" ("score_id" ,"student_id","class_id","score_score" ) values (3,3,1,59);

insert into "t_score" ("score_id" ,"student_id","class_id","score_score" ) values (4,2,2,55);

insert into "t_score" ("score_id" ,"student_id","class_id","score_score","score_makeup" ) values (5,3,2,55,80);

update "t_score" set "score_makeup"=59 where "score_id" =2;

update "t_score" set "score_makeup"=33 where "score_id" =3;

update "t_score" set "score_makeup"=21 where "score_id" =4;

update "t_score" set "score_makeup"=80 where "score_id" =4;

------------------------------------------------------------------

---课程不及格

select "t_student"."student_name", count("t_score"."class_id") as "failed"

from "t_student" join "t_score"

on ("t_student"."student_id" = "t_score"."student_id" and "t_score"."score_score" <60)

group by "t_student"."student_name";

------------------------------------------------------------------

---课程不及格/补考不及格

select * from(select stu."student_name",count(*) makeup_failed

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."score_makeup"<60  and sco."score_score"<60 and sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"

group by stu."student_name")a left join 

(select stu."student_name",count(*) score_failed

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."score_score"<60   and sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"

group by stu."student_name")b 

on(a."student_name"=b."student_name")

------------------------------------------------------------

---课程不及格/补考不及格【改进】

select a."student_name",a.makeup_failed,b.score_failed 

from(select stu."student_name",count(*) makeup_failed

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."score_makeup"<60  and sco."score_score"<60 and sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"

group by stu."student_name")a left join 

(select stu."student_name",count(*) score_failed

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."score_score"<60 and sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"

group by stu."student_name")b 

on(a."student_name"=b."student_name")

----------------------------------------------------------

---【终极版】

select c."student_name",c."class_name",c."score_score",c."score_makeup",b.score_failed,a.makeup_failed

from(select stu."student_name",count(*) makeup_failed

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."score_makeup"<60  and sco."score_score"<60 and sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"

group by stu."student_name")a

left join

(select stu."student_name",count(*) score_failed

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."score_score"<60   and sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id"

group by stu."student_name")b 

on (a."student_name"=b."student_name")

left join

(select stu."student_name",cla."class_name",sco."score_score",sco."score_makeup"

from "t_student" stu,"t_class" cla,"t_score" sco

where sco."class_id"=cla."class_id" and stu."student_id" =sco."student_id")c 

on(a."student_name"=c."student_name" and b."student_name"=c."student_name")

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

question:数据库死掉了,或者连接不上,如何解决?

think:

1.连接人数暴增,并发性不够好。

2.连接数据库死锁,不是每次都发生,连接很多次才会出现一次死(连接多次,在缓存中有很多,出现死锁)

猜你喜欢

转载自blog.csdn.net/qq_23996157/article/details/82712610