【数据库】相关代码集

时间:2022-02-28 01:43:45

    【数据库】相关代码集【数据库】相关代码集

 1 Create Table student
 2 (
 3     Sno char (9) Primary key , 
 4     Sname char (20) Unique ,
 5     Ssex char (2) ,
 6     Sage smallint , 
 7     Sdept char(20)
 8 
 9 );
10 
11 Create Table Course
12 (
13     Cno char (4) Primary key ,
14     Cname char (40) not Null , 
15     Cpno char (4) ,
16     Ccredit smallint 
17     foreign key (Cpno) References Course(Cno)
18 );
19 
20 Create Table SC
21 (
22     Sno char (9) ,
23     Cno char (4) , 
24     Grade smallint 
25     
26     Primary key ( Sno , Cno ) ,
27     Foreign key ( Sno ) References Student (Sno),
28     Foreign key ( Cno ) References Course (Cno)     
29     
30 );
31 
32 Insert Into student(Sno,Sname,Ssex,Sage,Sdept)
33 values(201215121,李勇,,20,CS);
34 Insert Into student(Sno,Sname,Ssex,Sage,Sdept)
35 values(201215122,刘晨,,19,CS);
36 Insert Into student(Sno,Sname,Ssex,Sage,Sdept)
37 values(201215123,王敏,,18,MA);
38 Insert Into student(Sno,Sname,Ssex,Sage,Sdept)
39 values(201215125,张立,,19,IS);
40 
41 
42 Insert Into Course(Cno,Cname,Cpno,Ccredit)
43 values(2,数学,NULL,2)
44 Insert Into Course(Cno,Cname,Cpno,Ccredit)
45 values(6,数据处理,NULL,2)
46 Insert Into Course(Cno,Cname,Cpno,Ccredit)
47 values(4,操作系统,6,3)
48 Insert Into Course(Cno,Cname,Cpno,Ccredit)
49 values(7,PASCAL语言,6,4)
50 Insert Into Course(Cno,Cname,Cpno,Ccredit)
51 values(5,数据结构,7,4)
52 Insert Into Course(Cno,Cname,Cpno,Ccredit)
53 values(1,数据库,5,4)
54 Insert Into Course(Cno,Cname,Cpno,Ccredit)
55 values(3,信息系统,1,4)
56 
57 /*
58 drop table SC;
59 drop table Course;
60 drop table student;
61 */
62 
63 
64 Insert Into SC(Sno,Cno,Grade)
65 values ( 201215121 , 1 , 92 )
66 Insert Into SC(Sno,Cno,Grade)
67 values ( 201215121 , 2 , 85 )
68 Insert Into SC(Sno,Cno,Grade)
69 values ( 201215121 , 3 , 88 )
70 Insert Into SC(Sno,Cno,Grade)
71 values ( 201215122 , 2 , 90 )
72 Insert Into SC(Sno,Cno,Grade)
73 values ( 201215122 , 3 , 80 )
实验1   【数据库】相关代码集【数据库】相关代码集
  1 Create Table student
  2 (
  3     Sno char (9) Primary key , 
  4     Sname char (20) Unique ,
  5     Ssex char (2) ,
  6     Sage smallint , 
  7     Sdept char(20)
  8 
  9 );
 10 
 11 Create Table Course
 12 (
 13     Cno char (4) Primary key ,
 14     Cname char (40) not Null , 
 15     Cpno char (4) ,
 16     Ccredit smallint 
 17     foreign key (Cpno) References Course(Cno)
 18 );
 19 
 20 Create Table SC
 21 (
 22     Sno char (9) ,
 23     Cno char (4) , 
 24     Grade smallint 
 25     
 26     Primary key ( Sno , Cno ) ,
 27     Foreign key ( Sno ) References Student (Sno),
 28     Foreign key ( Cno ) References Course (Cno)     
 29     
 30 );
 31 
 32 Insert Into student(Sno,Sname,Ssex,Sage,Sdept)
 33 values(201215121,李勇,,20,CS);
 34 Insert Into student(Sno,Sname,Ssex,Sage,Sdept)
 35 values(201215122,刘晨,,19,CS);
 36 Insert Into student(Sno,Sname,Ssex,Sage,Sdept)
 37 values(201215123,王敏,,18,MA);
 38 Insert Into student(Sno,Sname,Ssex,Sage,Sdept)
 39 values(201215125,张立,,19,IS);
 40 
 41 
 42 Insert Into Course(Cno,Cname,Cpno,Ccredit)
 43 values(2,数学,NULL,2)
 44 Insert Into Course(Cno,Cname,Cpno,Ccredit)
 45 values(6,数据处理,NULL,2)
 46 Insert Into Course(Cno,Cname,Cpno,Ccredit)
 47 values(4,操作系统,6,3)
 48 Insert Into Course(Cno,Cname,Cpno,Ccredit)
 49 values(7,PASCAL语言,6,4)
 50 Insert Into Course(Cno,Cname,Cpno,Ccredit)
 51 values(5,数据结构,7,4)
 52 Insert Into Course(Cno,Cname,Cpno,Ccredit)
 53 values(1,数据库,5,4)
 54 Insert Into Course(Cno,Cname,Cpno,Ccredit)
 55 values(3,信息系统,1,4)
 56 
 57 /*
 58 drop table SC;
 59 drop table Course;
 60 drop table student;
 61 */
 62 
 63 
 64 Insert Into SC(Sno,Cno,Grade)
 65 values ( 201215121 , 1 , 92 )
 66 Insert Into SC(Sno,Cno,Grade)
 67 values ( 201215121 , 2 , 85 )
 68 Insert Into SC(Sno,Cno,Grade)
 69 values ( 201215121 , 3 , 88 )
 70 Insert Into SC(Sno,Cno,Grade)
 71 values ( 201215122 , 2 , 90 )
 72 Insert Into SC(Sno,Cno,Grade)
 73 values ( 201215122 , 3 , 80 )
 74 
 75 
 76 /*1、查询女生中年龄大于19岁的学生姓名*/
 77 Select Sname
 78 From student
 79 where Sage>=19 and Ssex = ;
 80 
 81 /*2、查询计算机系的学生学号、姓名、出生年份,
 82 按出生年份降序显示(给出生年份起个别名);*/
 83 
 84 Select Sno 学生学号 , Sname 姓名 , 2019 - Sage 出生年份
 85 From student 
 86 Order by 2019 - Sage Desc;
 87 
 88 /*3、查询没有先行课的课程号、课程名;*/
 89 
 90 Select Cno 课程号 ,Cname 课程名 
 91 From Course
 92 Where Cpno IS NULL;
 93 
 94 /*4、查询选修了课程的学生学号;*/
 95 
 96 Select distinct Sno  学生学号
 97 From SC;
 98 
 99 /*5、查询年龄在18到20岁之间的学生学号、姓名、性别;*/
100 Select Sno 学生学号, Sname 姓名 , Ssex 性别 
101 From student
102 Where Sage between 18 and 23;
103 
104 /*6、查询是计算机系或数学系的学生姓名,年龄;*/
105 Select Sname 学生姓名 , Sage 年龄
106 From student
107 Where Sdept in (CS,MA);
108 
109 /*7、查询课程名含有‘系统’的全部信息;*/
110 Select Cno 课程号 , Cname 课程名称 , Cpno 先行课号 , Ccredit 学分
111 From Course
112 Where Cname Like %系统%;
113 
114 /*8、查询学号倒数第二位是‘2’的学生姓名、院系;*/
115 Select Sno 学号 , Sname 学生姓名 , Sdept 院系
116 From student 
117 Where Sno Like %2_ ;
118 
119 /*9、查询被选修了的课程号;*/
120 Select distinct Cno 已选修的课程号
121 From SC ;
122 
123 /*10、查询学分大于3的课程号和课程名;*/
124 Select Cno  课程号 , Cname 课程名 , Ccredit 学分
125 From Course 
126 Where Ccredit > 3 ;
实验2

 

【数据库】相关代码集【数据库】相关代码集
 1 //按照数据库 (1号课)从高到低 排序
 2 
 3 select grade *
 4 from SC
 5 where Cno = 1
 6 order by grade Desc ;
 7 
 8 //把所有课的成绩从高到低排序
 9 select grade *
10 from SC
11 where grade is not NULL
12 order by grade Desc ;
13 
14 //统计每门课的可好,及其最高成绩
15 select Cno , max( grade )
16 from SC
17 group by Cno ;
18 
19 //按照每个人的总成绩降序
20 select Sno , sum(grade)
21 from SC
22 where grade is not NULL
23 group by Sno
24 order by sum(grade) Desc ;
课堂讲解事例