数据库—SQL语言

  1 /*创建数据库*/
  2 create database db211416145
  3 /*创建student表*/
  4 create table student
  5 (
  6 Sno  char(10) primary key,
  7 Sname char(20),
  8 Ssex char (10),
  9 Sage int,
 10 Sdept char(20),
 11 );
 12 /*创建course表*/
 13 create table course
 14 (
 15 Cno char(10) primary key,
 16 Cname char(20),
 17 Chours int,
 18 );
 19 /*创建SC表*/
 20 create table SC
 21 (
 22 Sno char(10),
 23 Cno char(10),
 24 Grade int,
 25 foreign key (Sno)references student(Sno),
 26 foreign key (Cno)references course(Cno),
 27 primary key(Sno,Cno),
 28 );
 29 /*插入Student表数据*/
 30 insert into student(Sno,Sname,Ssex,Sage,Sdept)
 31 values('9512101','李勇','',19,'计算机系');
 32 insert into student(Sno,Sname,Ssex,Sage,Sdept)
 33 values('9512102','刘晨','',20,'计算机系');
 34 insert into student(Sno,Sname,Ssex,Sage,Sdept)
 35 values('9512103','王敏','',20,'计算机系');
 36 insert into student(Sno,Sname,Ssex,Sage,Sdept)
 37 values('9521101','张立','',22,'信息系');
 38 insert into student(Sno,Sname,Ssex,Sage,Sdept)
 39 values('9521102','吴宾','',21,'信息系');
 40 insert into student(Sno,Sname,Ssex,Sage,Sdept)
 41 values('9521103','张海','',20,'信息系');
 42 insert into student(Sno,Sname,Ssex,Sage,Sdept)
 43 values('9531101','钱小力','',18,'数学系');
 44 insert into student(Sno,Sname,Ssex,Sage,Sdept)
 45 values('9531102','王大力','',19,'数学系');
 46 /*插入course表数据*/
 47 insert into course(Cno,Cname,Chours)
 48 values('C01','计算机文化学',70)
 49 insert into course(Cno,Cname,Chours)
 50 values('C02','VB',90)
 51 insert into course(Cno,Cname,Chours)
 52 values('C03','计算机网络',80)
 53 insert into course(Cno,Cname,Chours)
 54 values('C04','数据库基础',108)
 55 insert into course(Cno,Cname,Chours)
 56 values('C05','高等数学',180)
 57 insert into course(Cno,Cname,Chours)
 58 values('C06','数据结构',72)
 59 /*插入SC表数据*/
 60 insert into SC(Sno,Cno,Grade)
 61 values('9512101','C01',90)
 62 insert into SC(Sno,Cno,Grade)
 63 values('9512101','C02',86)
 64 insert into SC(Sno,Cno,Grade)
 65 values('9512101','C06',null)
 66 insert into SC(Sno,Cno,Grade)
 67 values('9512102','C02',78)
 68 insert into SC(Sno,Cno,Grade)
 69 values('9512102','C04',66)
 70 insert into SC(Sno,Cno,Grade)
 71 values('9512102','C01',82)
 72 insert into SC(Sno,Cno,Grade)
 73 values('9512102','C02',75)
 74 insert into SC(Sno,Cno,Grade)
 75 values('9512102','C04',92)
 76 insert into SC(Sno,Cno,Grade)
 77 values('9512102','C05',50)
 78 insert into SC(Sno,Cno,Grade)
 79 values('9512103','C02',68)
 80 insert into SC(Sno,Cno,Grade)  /*这里注意空值一定要标明NULL*/
 81 values('9512103','C06',null)
 82 insert into SC(Sno,Cno,Grade)
 83 values('9531101','C01',80)
 84 insert into SC(Sno,Cno,Grade)
 85 values('9531101','C05',95)
 86 insert into SC(Sno,Cno,Grade)
 87 values('9531102','C05',85)
 88 
 89 --修改基本表(新增加一竖列):向student表加入入学时间,类型为日期型            //(新加表为空值)
 90 alter table student add Sentrance date
 91 
 92 --修改基本表(更改一竖列名字):将student表Sentrance改为1111
 93 
 94 
 95 --修改基本表(删除一竖列):删除student表的sentrance一行                        //不要忘记column
 96 alter table student drop column Sentrance
 97 
 98 --修改基本表(修改列的数据类型):将student表的Sage转为char
 99 alter table student alter column Sage char
100 --改回
101 alter table student alter column Sage int ;                    --        //出错无法改回
102 
103 --修改基本表(增加属性必须取唯一值约束条件)
104 alter table student add unique(Sname);
105 
106 --修改基本表(取消属性约束条件)
107 
108 
109 
110 
111 --1.分别查询学生表和学生修课表中的全部数据。
112 select * from student;
113 select * from SC;
114 
115 
116 --删除基本表(有限制)有关联到表的东西就删不掉
   
  drop table student restrict 117 --删除基本表(无限制) 将表和上面有关的表,视图全删了
 drop table student cascade  

118 119 120 121 -- ********************************索引******************************** 122 123 --建立索引 124 create unique index Stusno ON student(Sno); 125 --删除索引 126 drop index Stusno on student --//按照书上的不行 drop index <索引名> 127 128 --建立索引(按照多个属性)按照Sno升序,Cno降序 129 create unique index SCno on SC(Sno ASC,Cno DESC); 130 --删除索引 131 drop index SCno on SC; 132 133 134 135 136 137 -- ****************************查询************************* 138 --单表查询: 139 140 --查询学生学号,姓名 141 select Sno,Sname 142 from student; 143 144 --查询全体学生详细记录 145 select * 146 from student; 147 148 --(查询经过计算的值):查询出生年份 149 select Sname,2016-Sage 150 from student; 151 152 --(可以指定别名) --//lower可将相应的英文字符串转换成小写,记住select后跟的永远是显示的,别名写在后面是标题 153 select Sname NAME,'Year Of Birth'Birth,2016-Sage BIRTHDAY,LOWER(Sdept)DEPARTMENT 154 from student; 155 156 --(取消重复) --//在查询的属性前加distinct可取消重复属性 157 select distinct Sno 158 from SC; 159 160 161 162 163 164 --查询满足条件元组: 165 166 --查询计算机学生名单 167 select * 168 from student 169 where Sdept = '计算机系'; 170 171 --查询20岁以下学生信息 (< > = >= <= != !> !< 不大小于 !>) 172 select * 173 from student 174 where Sage < 20; 175 176 177 --查询年龄在20~23岁之间的学生 (between....and....) 178 select * 179 from student 180 where Sage between 20 and 23; 181 182 --查询年龄不在20~23岁之间的学生(not between... and....) 183 select * 184 from student 185 where Sage not between 20 and 23; 186 187 --查询计算机系和数学系的学生 (在确定集合之中,in的用法) 188 select * 189 from student 190 where Sdept in ('计算机系','数学系'); 191 192 --查询学号为9512101学生详细情况 193 select * 194 from student 195 where Sno like '9512101'; 196 197 --查询张姓学生情况 (通配符%, 代表任意长度(可以=0)的字符串,a%b表示以a开头b结尾任意长度字符串acb,addgb 198 select * 199 from student 200 where Sname like '张%'; 201 202 --查询姓”钱小“且全名为3个汉字的学生情况(下划线代表任意单个字符,字符集GBK一个_代表一个汉字,字符集ASCII两个__代表一个汉字) //也可以代表空字符吗? 203 select * 204 from student 205 where Sname like '钱小_'; 206 207 --查询第二个字为“小”的学生 (如果查询的对象中有% _等通配符使用逃逸字符,where Sname like '张\_'escape ‘\';这里的_不再当成通配符 208 select * 209 from student 210 where Sname like '__小%'; 211 212 --查询不姓钱学生 213 select * 214 from student 215 where Sname not like '钱%'; 216 217 --查询没有成绩学生学号 (涉及空值查询) 218 select Sno 219 from SC 220 where Grade is NULL; 221 222 --查询有成绩学生学号 223 select Sno 224 from SC 225 where Grade is not null; 226 227 --查询计算机系且男的学生 (多重条件查询,用and 或 or来连接条件 228 select * 229 from student 230 where Sdept = '计算机系' and Ssex = ''; 231 232 --查询选修1号课程的学号和成绩,按分数降序 (order by 子句,使用order by子句对查询结果一个多个属性升序ASC降序DESC排列缺省为升 233 select Sno,Grade 234 from SC 235 where Cno = 'C01' 236 order by Grade desc; 237 238 --查询选修一号课程学号和成绩,按成绩降序,按学号升序 239 select Sno,Grade 240 from SC 241 where Cno = 'C01' 242 order by Grade desc,Sno; 243 244 --查询学生总人数(聚集函数 count sum avg max min) 245 select COUNT(*) 246 from student; 247 248 --查询选修了课程的学生人数(给聚集函数指定distinct短语,表示取消指定列重复值) 249 select COUNT(distinct Sno) 250 from SC; 251 252 --计算1号课程学生平均成绩 253 select AVG(Grade) AverageScore 254 from SC 255 where Cno = 'C01'; 256 257 --查询每个课程号对应选课人数(group by 语句 加上group by语句将会对分组中的项进行统计,而不是对所有表中对象 258 select Cno,COUNT(Sno) 259 from SC 260 group by Cno; 261 262 --查询选修了3门课程以上学生学号 (having 短句与where区别在于作用对象不同,where作用于基本表或试图,having作用于groupby中的组,从组中选择满足条件的组 263 select Sno 264 from SC 265 group by Sno 266 having COUNT(*)>3; 267 268 269 --*********连接查询********** 270 --查询学生的信息和他的选课情况 271 select student.*,SC.* 272 from student,SC 273 where student.Sno = SC.Sno; 274 275 --对上例用自然连接 (去掉了连接表中的相同属性) 276 select student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade 277 from student,SC 278 where student.Sno = SC.Sno; 279 280 --使用自身连接(表可以连接自己,使用first,second区分) 281 --select first.cno,second.cpno 282 --from course first,course second 283 --where first.cpno = second.Cno; 284 285 --列出全部学生基本情况和选课情况,NULL也要列出(此处即为外连接,此处以student主体,若选课没选该生信息也列出) 286 select student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade 287 from student left OUTer JOIN SC on (student.Sno = SC.Sno); 288 289 290 --查询选修二号课程且成绩在90分以上学生 (复合条件连接) 291 select student.Sno,Sname 292 from student,SC 293 where student.Sno = SC.Sno and 294 SC.Cno = 'C02'and 295 SC.Grade > 90; 296 297 --查询与李勇在同一个系的学生(嵌套查询) 298 select * 299 from student 300 where student.Sdept in 301 (select Sdept 302 from student 303 where student.Sname = '李勇'); 304 305 --查询选修了数据结构的学生信息 (三重嵌套) 306 select * 307 from student 308 where student.Sno in 309 (select Sno 310 from SC 311 where SC.Cno in 312 (select Cno 313 from course 314 where course.Cname = '数据结构') 315 ); 316 317 --查询每个学生超过他选修课程平均成绩的课程号 (相关子查询,因为要查课程号和学生,所以select sno,cno 从SC表中,呢么什么情况下的课程呢 318 --分数大于他的平局分,他的平均分是什么?再次求他的平均分,select avg(grade) from sc;是谁的平均分是外层循中的所以用X,Y来代表) 319 select Sno,Cno 320 from SC x 321 where Grade > ( 322 select AVG(grade) 323 from SC y 324 where x.Sno = y.Sno); 325 326 --带有any all谓词的查询,any表示某一个,all表示所有 327 --查询其他系中比计算机系中某一学生年龄小的学生姓名年龄 328 select Sname,Sage 329 from student 330 where Sage < any (select Sage 331 from student 332 where Sdept = '计算机系'); 333 334 --带有exits的子查询 335 --查询所有选修了1号课程的学生姓名 (因为子查询返回BOOL值,所以子查询通常用*, 336 select Sname 337 from student 338 where exists (select * 339 from SC 340 where Sno = student.Sno and Cno= 'C01'); 341 342 343 --查询选修了全部课程的学生姓名 (不存在这样的课程该学生没有选修) ****************难点,运用到离散数学知识(找时间复习,) 344 select Sname 345 from student 346 where not exists( 347 select * 348 from course 349 where not exists ( 350 select * from SC 351 where Sno = student.Sno 352 and Cno = course.Cno) 353 ); 354 --查询至少选修了学生选修的全部课程的学生号码(不存在这样的课程Y,学生200215122选修了Y,而学生X没有选 *********难点 355 select distinct Sno 356 from SC SCX 357 where not exists 358 (select * 359 from SC SCY 360 where SCY.Sno = '200215122' and 361 not exists 362 (select * 363 from SC SCZ 364 where SCZ.Sno = SCX.Sno AND 365 SCZ.Cno = SCY.Cno) 366 ); 367 368 --集合查询 369 --查询计算机系的学生及年龄不大于19的学生(两个查询可用 并unioin 交intersect 差except 370 select * 371 from student 372 where Sdept = '计算机系' 373 union 374 select * 375 from student 376 where Sage <= 19; 377 378 --数据更新 379 --插入数据一行元组 (指明了数据可以按照顺序填入数据,如果只有表名,要按照属性顺序输入,且空值要手动输入NULL) 380 insert 381 into student(Sno,Sname,Ssex,Sdept,Sage) 382 values ('1111','啊去','','计算机系',15); 383 384 --插入子查询结果 385 --insert into deptage(sdept,avg-age) 386 --select sdept,AVG(sage) 387 --from student 388 --group by Sdept; 389 390 391 --修改数据 392 --修改一行元组值 393 --将学生951201的年龄改为30岁 394 update student 395 set Sage = 30 396 where Sno = '951201'; 397 398 --修改多行元组 399 --将所有学生年龄+1 400 update student 401 set Sage = Sage + 1; 402 403 404 --带子查询的修改语句 405 --将所有学生成绩置0; 406 update SC 407 set Grade = 0 408 where '计算机系'= 409 (select Sdept 410 from student 411 where student.Sno = SC.Sno); 412 413 --删除一行(一个元组) 414 delete 415 from student 416 where Sno = '951201'; 417 418 --删除多行 419 --删除所有学生选课记录 420 delete 421 from SC 422 423 --带子查询删除 424 --删除计算机系学生选课记录 425 delete 426 from SC 427 where 'cs'= 428 (select Sdept 429 from student 430 where student.Sno = SC.Sno); 431 432 433 434 435 436 437 --*********************************视图***************************************** 438 439 --建立信息系学生视图 440 create view fffgwe 441 as 442 select Sno,Sname,Sage 443 from student 444 where Sdept = '计算机系'; 445 446 --建立计算机系学生视图,要求进行修改和插入操作是仍需保证概述图只有信息系学生 447 create view us_student 448 as 449 select * 450 from student 451 where Sdept = '计算机系' 452 with check option; 453 454 --视图可以建立在多个表上 455 --建立信息系选修了一号课程的学生视图 456 create view g_1(Sno,Sname,Grade) 457 as 458 select student.Sno,Sname,Grade 459 from student,SC 460 where Sdept = '计算机系' AND 461 student.Sno = SC.Sno and 462 SC.Cno = '1'; 463 464 --视图也可建立在其他视图上 465 --建立信息系选修了1号课程成绩在90分以上学生的视图 466 create view gg 467 as 468 select Sno,Sname,Grade 469 from g_1 470 where grade >= 90 471 472 473 --定义反应学生出生年份视图 (带有表中不存在属性的属性的视图,那列成为虚拟列,视图称为代表达式视图 474 create view bt_s(Sno,Sname,Sbirth) 475 as 476 select Sno,Sname,2004 - Sage 477 from student; 478 479 --删除视图 480 drop view xxxx; 481 --如果视图上有其他视图(根据该表创建的其他视图都删除了) 482 drop view xxxx cascade 483 484 --查询视图 485 --查询选修了1号课程的信息系学生 (DBMS会将语句转换为对基本表的查询语句) 486 select g_1.Sno,Sname 487 from g_1,SC 488 where g_1.Sno = SC.Sno And SC.Cno = '1'; 489 490 --注意不是所有情况都可对视图和表进行连接查询(只有行列子集视图才可,(视图只是少了基本表中的几列属性而没有多出其他的属性列),如果 491 --多出了其他列的视图会错误 492 --在S-G视图中查询平均分90分以上的学生 493 --select * 494 --from s_g 495 --where gavg >= 90; 496 --定义的S-G视图的子查询为 497 --select sno,SVG(GRADE) 498 --FROM SC GROUP BY Sno; 499 --结合的话会出现select sno,avg(grade) 500 --from sc 501 --where avg(grade)>= 90 502 --group by sno; 503 --而where子句不能用聚集函数要用having所以错了,此种情况一般直接对基本表查询 504 505 --更新视图与表一样,但有些视图是不能更新的,如那个学生平均成绩表,你不能修改平均成绩,因为无法反应到基本表上,修改基本表成绩使平均成绩与你修改的相等 506 --一般行列子集视图是可更新的

 

posted @ 2016-12-01 21:45  Ponytai1  阅读(385)  评论(0编辑  收藏  举报