又是一天萌萌哒地过去了,今天是处理数据库的部分。SQL Server 2008,这东西是上学期搞MFC的时候接触到的,那时候话说安装就是一个大问题,然后在学SQL语句的时候感觉还好,一切都还过得去。现在将这东西感觉挺轻松的,还不时的帮助大家解决了不少的问题,感觉挺棒。但是毕竟是速成的,缺陷也是很明显的。比如创建表单可以直接用代码实现,但是这一部分是完全的不会的,虽然说效率是相对来说低了一点,但是移植性相当的好的,直接的运行一下一切就都OK了。好了,话说搞MFC的时候连接数据库自己还没有写博客,忘记了不少东西,现在也应该好好的记录一下了。
首先就先是上传自己上课时用到的PPT和写作业用到的习题吧。
对于数据库的基本操作主要是增、删、改、查这四个操作,外加上一些其他的东西,就让SQL语句也变得非常的有趣同时也和生活中的一些现象适应起来了。确实挺不错的。
这里先将老师共享的一个SQL语句贴出来吧。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
USE master GO IF EXISTS (SELECT * FROM sysDATABASEs WHERE name='BigCollege') BEGIN RAISERROR('Dropping existing BigCollege DATABASE ....',0,1) DROP DATABASE BigCollege END GO --BigCollege网上选课系统 建库脚本 CREATE DATABASE BigCollege; GO USE BigCollege; GO CREATE TABLE Department --系信息 ( depNo VARCHAR(8) PRIMARY KEY, --系编号 depName VARCHAR(16) NOT NULL, --系名 depDeanName VARCHAR(16), --系主任名称 depTel VARCHAR(20), --联系电话 depAddress VARCHAR(30) --地址 ); CREATE TABLE Major --专业信息 ( majorNo VARCHAR(8) PRIMARY KEY, --专业编号 majorName VARCHAR(16) NOT NULL, --专业名 majorLeader VARCHAR(16), --专业带头人 majorDepNo VARCHAR(8), --隶属系编号 majorTotalCredit INT, --专业总学分 FOREIGN KEY(majorDepNo) REFERENCES Department(depNo) ); CREATE TABLE Student --学生信息 ( stuNo VARCHAR(8) PRIMARY KEY, --学号 stuName VARCHAR(16) NOT NULL, --姓名 stuSex VARCHAR(2), --性别 stuAge INT, --年龄 stuMajorNo VARCHAR(8), --专业编号 stuTotalCredit INT, --已取得的总学分 FOREIGN KEY(stuMajorNo) REFERENCES Major(majorNo) ); CREATE TABLE ClassRoom --教室信息 ( crNo VARCHAR(8) PRIMARY KEY, --教室编号 crBuildingName VARCHAR(16), --所在的教学楼名称 crRoomNo VARCHAR(16), --房间编号 crNumberOfSeat INT --座位数量 ); CREATE TABLE Classes --班级信息 ( classNo VARCHAR(8) PRIMARY KEY, --班级编号 classCourseName VARCHAR(20) NOT NULL, --课程名称 classStartTime DATETIME, --开课时间 classDepNo VARCHAR(8), --系编号 classLimitNum INT, --最大的学生人数 classExistNum INT, --目前已有的学生人数 classCredit INT, --选修该课程能够得到的学分 classRoomNo VARCHAR(8), --该课程对应的授课教室 classTeacherName VARCHAR(16), --任课教师姓名 FOREIGN KEY(classDepNo) REFERENCES Department(depNo), FOREIGN KEY(classRoomNo) REFERENCES ClassRoom(crNo) ); CREATE TABLE Register --选课信息 ( stuNo VARCHAR(8), --学号 classNo VARCHAR(8), --班级编号 score numeric(4,1), --考试成绩 PRIMARY KEY(stuNo,classNo), FOREIGN KEY(stuNo) REFERENCES Student(stuNo), FOREIGN KEY(classNo) REFERENCES Classes(classNo) ); CREATE TABLE Teacher --教师信息 ( vTchNo VARCHAR(4) PRIMARY KEY, --教师编号 vTchName VARCHAR(20), --教师姓名 vTchGrouperNo VARCHAR(4), --组长编号 ); --插入系信息 INSERT INTO Department VALUES('D001','外语系','李强生','87323123','BigCollege大道123号'); INSERT INTO Department VALUES('D002','工商系','赵德胜','87323234','BigCollege大道234号'); INSERT INTO Department VALUES('D003','计算机系','王海洋','87323688','BigCollege大道688号'); INSERT INTO Department VALUES('D004','建筑系','刘玉明','87323666','BigCollege大道666号'); INSERT INTO Department(depNo,depName,depDeanName) VALUES('D005','电信工程系','章小节'); INSERT INTO Department(depNo,depName,depDeanName,depAddress) VALUES('D006','工业外贸系','王乐乐','BigCollege大道669号'); --插入专业信息 INSERT INTO Major VALUES('M001','日语','王金宝','D001',156); INSERT INTO Major VALUES('M002','英语','陈龙','D001',178); INSERT INTO Major VALUES('M003','阿拉伯语','何炅新','D001',168); INSERT INTO Major VALUES('M004','工商管理','任德华','D002',218); INSERT INTO Major VALUES('M005','金融','张学斌','D002',228); INSERT INTO Major VALUES('M006','国际贸易','李东生','D002',196); INSERT INTO Major VALUES('M007','经济学','钱海波','D002',234); INSERT INTO Major VALUES('M008','软件工程','朱晓明','D003',232); INSERT INTO Major VALUES('M009','计算机网络','刘少云','D003',248); INSERT INTO Major VALUES('M010','建筑学','白意愿','D004',248); INSERT INTO Major VALUES('M011','心理学','许静',null,220); INSERT INTO Major VALUES('M012','财会','赵灵',null,260); --插入教室信息 INSERT INTO ClassRoom VALUES('CR001','科教楼','101',60); INSERT INTO ClassRoom VALUES('CR002','科教楼','102',45); INSERT INTO ClassRoom VALUES('CR003','科教楼','103',45); INSERT INTO ClassRoom VALUES('CR004','科教楼','201',120); INSERT INTO ClassRoom VALUES('CR005','科教楼','301',160); INSERT INTO ClassRoom VALUES('CR006','科教楼','302',40); INSERT INTO ClassRoom VALUES('CR007','科教楼','303',80); INSERT INTO ClassRoom VALUES('CR008','电教楼','101',60); INSERT INTO ClassRoom VALUES('CR009','电教楼','102',100); INSERT INTO ClassRoom VALUES('CR010','电教楼','103',80); INSERT INTO ClassRoom VALUES('CR011','电教楼','104',600); INSERT INTO ClassRoom VALUES('CR012','电教楼','201',30); INSERT INTO ClassRoom VALUES('CR013','电教楼','202',30); INSERT INTO ClassRoom VALUES('CR014','电教楼','203',50); INSERT INTO ClassRoom VALUES('CR015','电教楼','204',50); INSERT INTO ClassRoom VALUES('CR016','电教楼','301',60); --插入班级信息 INSERT INTO Classes VALUES('C001','微观经济学','2009-3-1','D002',60,3,2,'CR001','常强'); INSERT INTO Classes VALUES('C002','大学英语','2009-3-1','D001',30,16,4,'CR002','董金琪'); INSERT INTO Classes VALUES('C003','建筑艺术','2009-3-16','D004',30,12,3,'CR003','潘裕'); INSERT INTO Classes VALUES('C004','计算机网络','2009-3-25','D003',45,30,2,'CR001','朱紫英'); INSERT INTO Classes VALUES('C005','操作系统','2009-4-6','D003',30,20,2,'CR002','张兵'); INSERT INTO Classes VALUES('C006','数据库原理','2009-4-8','D003',30,0,4,'CR006','李海秋'); INSERT INTO Classes VALUES('C007','软件工程','2009-4-8','D003',60,0,3,'CR008','孟京珂'); INSERT INTO Classes VALUES('C008','项目管理','2009-4-12','D003',100,0,6,'CR009','王泽民'); INSERT INTO Classes VALUES('C009','美术欣赏','2009-4-15','D004',30,6,2,'CR012','王若'); INSERT INTO Classes VALUES('C010','日语','2009-5-16','D001',75,60,2,'CR014','杨历'); INSERT INTO Classes VALUES('C011','市场营销','2009-5-30','D002',500,240,3,'CR007','赵冰'); INSERT INTO Classes VALUES('C012','会计学','2009-6-1','D002',60,30,2,'CR009','程凯明'); --插入学生信息 INSERT INTO Student VALUES('S001','白玉芬','女',19,'M002',123); INSERT INTO Student VALUES('S002','唐嘉芝','女',18,'M007',67); INSERT INTO Student VALUES('S003','刘健鹏','男',21,'M006',120); INSERT INTO Student VALUES('S004','田洋','男',19,'M005',97); INSERT INTO Student VALUES('S005','申一雅','男',18,'M004',62); INSERT INTO Student VALUES('S006','范力','男',19,'M008',134); INSERT INTO Student VALUES('S007','高红','女',20,'M006',154); INSERT INTO Student VALUES('S008','张埔涛','男',18,'M004',21); INSERT INTO Student VALUES('S009','蒲惠妃','女',19,'M009',122); INSERT INTO Student VALUES('S010','邓平瑶','女',20,'M010',217); INSERT INTO Student VALUES('S011','刘晨','男',19,'M003',128); INSERT INTO Student VALUES('S012','尹日媛','女',19,'M001',109); INSERT INTO Student VALUES('S013','岳元干','女',18,'M003',87); INSERT INTO Student VALUES('S014','朱传宁','男',19,'M006',149); INSERT INTO Student VALUES('S015','尹志','男',20,'M007',198); INSERT INTO Student VALUES('S016','刘晓为','男',18,'M008',83); INSERT INTO Student VALUES('S017','王金婷','女',19,'M005',114); INSERT INTO Student VALUES('S018','张青','男',18,'M005',141); INSERT INTO Student VALUES('S019','杨凡祺','女',19,'M004',105); INSERT INTO Student VALUES('S020','沈冉','女',20,'M003',136); INSERT INTO Student VALUES('S021','崔墨','男',18,'M002',82); INSERT INTO Student VALUES('S022','李新童','男',19,'M009',67); INSERT INTO Student VALUES('S023','徐心飞','女',21,'M006',153); INSERT INTO Student VALUES('S024','王一兰','女',20,'M005',213); INSERT INTO Student VALUES('S025','代政','男',19,'M004',107); INSERT INTO Student VALUES('S026','刘珊','女',18,'M001',62); INSERT INTO Student VALUES('S027','徐一玮','男',17,'M001',20); INSERT INTO Student VALUES('S028','李峰','男',18,'M008',49); INSERT INTO Student VALUES('S029','李沐','男',19,'M008',83); INSERT INTO Student VALUES('S030','吴嵩','男',18,'M009',29); INSERT INTO Student VALUES('S031','王涵如','男',19,'M007',105); INSERT INTO Student VALUES('S032','王梦冰','女',20,'M006',156); INSERT INTO Student VALUES('S033','谢妍','女',17,'M005',41); INSERT INTO Student VALUES('S034','周亿','男',18,'M003',66); INSERT INTO Student VALUES('S035','郑梅','女',19,'M002',81); INSERT INTO Student VALUES('S036','艾家羽','男',20,'M001',105); INSERT INTO Student VALUES('S037','何宇','男',18,'M001',78); INSERT INTO Student VALUES('S038','方庆轩','男',19,'M007',116); --插入学生选课信息 INSERT INTO Register VALUES('S001','C001',90); INSERT INTO Register VALUES('S001','C002',54); INSERT INTO Register VALUES('S002','C003',67); INSERT INTO Register VALUES('S002','C001',67); INSERT INTO Register VALUES('S003','C002',66); INSERT INTO Register VALUES('S004','C004',98); INSERT INTO Register VALUES('S005','C006',87); INSERT INTO Register VALUES('S005','C003',56); INSERT INTO Register VALUES('S006','C005',90); INSERT INTO Register VALUES('S007','C002',98); INSERT INTO Register VALUES('S008','C002',90); INSERT INTO Register VALUES('S009','C003',64); INSERT INTO Register VALUES('S010','C009',77); INSERT INTO Register VALUES('S011','C011',56); INSERT INTO Register VALUES('S012','C012',80); INSERT INTO Register VALUES('S016','C012',82); INSERT INTO Register VALUES('S017','C012',58); INSERT INTO Register VALUES('S018','C011',95); INSERT INTO Register VALUES('S018','C012',76); INSERT INTO Register VALUES('S019','C012',78); INSERT INTO Register VALUES('S020','C012',89); INSERT INTO Register VALUES('S021','C011',53); INSERT INTO Register VALUES('S022','C011',26); INSERT INTO Register VALUES('S022','C001',78); INSERT INTO Register VALUES('S022','C012',84); INSERT INTO Register VALUES('S023','C011',57); INSERT INTO Register VALUES('S024','C011',68); INSERT INTO Register VALUES('S025','C004',69); INSERT INTO Register VALUES('S026','C006',70); INSERT INTO Register VALUES('S027','C008',73); INSERT INTO Register VALUES('S028','C008',74); INSERT INTO Register VALUES('S029','C008',72); INSERT INTO Register VALUES('S029','C009',78); INSERT INTO Register VALUES('S030','C006',90); INSERT INTO Register VALUES('S031','C002',94); INSERT INTO Register VALUES('S031','C003',92); INSERT INTO Register VALUES('S033','C004',60); INSERT INTO Register VALUES('S034','C005',50); --插入教师信息 INSERT INTO Teacher VALUES('T001','张音',Null); INSERT INTO Teacher VALUES('T002','赵阳',Null); INSERT INTO Teacher VALUES('T003','邓刚',Null); INSERT INTO Teacher VALUES('T004','李玫','T001'); INSERT INTO Teacher VALUES('T005','王亚弟','T002'); INSERT INTO Teacher VALUES('T006','李静','T002'); INSERT INTO Teacher VALUES('T007','刘桦','T003'); INSERT INTO Teacher VALUES('T008','陈美','T003'); INSERT INTO Teacher VALUES('T009','谭一','T003');
感觉确实不错,然后是自己写的一些语句,这里也粘贴出来吧。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
insert Student values('S001','白玉芬','女',19,'英语') insert Student values('S002','唐嘉芝','女',18,'英语') insert Student values('S003','刘健鹏','男',21,'英语') insert Student values('S004','田洋','男',19,'英语') insert Student values('S005','申一雅','男',18,'软件工程') insert Student values('S006','范力','男',19,'软件工程') insert Student values('S007','高红','女',20,'软件工程') insert Student values('S008','刘晨','男',19,null) insert Student values('S009','王金婷','女',19,'工商管理') insert Student values('S0010','张青','男',18,null) --这是一条清空语句 --delete from Student where stuNo like '%'--,'白玉芬','女',19,'英语') --先将表全部都打印出来 select * from Student --第四题 select stuNo,stuNam from Student where stuMajor='英语' --第五题 select * from Student where stuSex='女' and stuAge >= 19 --第六题 select * from Student where (stuSex='男' and stuMajor = '软件工程')or(stuSex = '女' and stuMajor = '英语') --第七题 select stuNo,stuNam from Student where stuMajor is null --这个地方运行有问题,求解,为什么用is可以实现查询但是用=进行判断的时候无法实现 --第八题 select stuNam,stuMajor from Student where stuNo='S002' or stuNo='S004' --第九题,将学生张青的专业改为工商管理 --参考案例 --UPDATE ClassRoom SET crNumberOfSeat = 90 WHERE crBuildingName='科教楼' AND crRoomNo='101' update Student set stuNam='工商管理' where stuNam='张青' --第十题 update Student set stuNam='英语' where stuNam='英语教育' --第十一题 delete from Student where stuNo='S006' --第十二题 delete from Student where stuSex='男' and stuAge >= 20
对应试题:
Java 实训上机实验三
SQL Server 及SQL:
1. 创建一个名为HappyCollegeDB 的数据库。
2. 创建学生表Student,包括如下字段:学号(stuNo)、姓名(stuName)、性别(stuSex)、
年龄(stuAge)、专业(stuMajor)。(数据类型和长度自选)
3. 使用SQL 语句在学生表中增加如下数据:
stuNo(学号) stuName(姓名) stuSex(性别) stuAge(年龄) stuMajor(专业)
S001 白玉芬女 19 英语
S002 唐嘉芝女 18 英语
S003 刘健鹏男 21 英语
S004 田洋男 19 英语
S005 申一雅男 18 软件工程
S006 范力男 19 软件工程
S007 高红女 20 软件工程
S008 刘晨男 19
S009 王金婷女 19 工商管理
S010 张青男 18
4. 查询“英语”专业所有学生的学号和姓名。
5. 查询年龄大于等于19 岁的所有女生信息。
6. 查询“软件工程”专业男生或“英语”专业女生的全部信息。
7. 查询专业名为空的学生学号和姓名。
8. 查询学号为“S002”和“S004”的学生的姓名和专业名。
9. 将学生“张青”的专业改为“工商管理”。
10. 将所有“英语”专业学生的专业名改为“英语教育”。
11. 删除学号为“S006”的学生信息。
12. 删除年龄大于等于20 岁的男生信息。
找到了以前的周伟经理的SQL语句讲解部分,这里也一并贴出来:
--SQL语言是一种脚本语言, --在这里,我们接触只是其中最常用的部分 --添加 --Insert into 表名 (列1,列2,列3...) values (值1,值2,值3...) --Insert into T_UserInfo (F_NAME, F_UID, F_PWD, F_TEL, F_Type) --values ('我很好','124','456','119',1) Insert into T_UserInfo Values('125','你也好','110', NULL, 0, '111'); Insert into T_UserInfo Values ('126','你也好','110', NULL, 0, '111'), ('127','你也好','110', NULL, 0, '111'), ('128','你也好','110', NULL, 0, '111'), ('129','你也好','110', NULL, 0, '111') Insert into T_PCInfo (F_Type) values (0),(1),(0),(1) --删除语句的语法是:delete 表名 where 删除条件 --开发过程中不允许出现物理删除 delete T_UserInfo where F_UID='125' --修改语句的语法:Update 表名 set 列名1=值1,列2=值2... --where 定位条件 update T_UserInfo set F_PWD='123' where F_UID='129' update T_UserInfo set F_PWD='234', F_TEL='120',F_Type=5 where F_UID='129' update T_UserInfo set F_TEL='0731' where F_TEL='110' and F_Type=0 and F_PWD='111' -- select 列1,列2,列3... from 表名 where 条件 select * from T_UserInfo select F_UID, F_Name, F_Pwd from t_userinfo select f_uid from T_UserInfo where F_NAME='你好' select f_uid from T_UserInfo where F_TEL='119' and F_Type=1 -- count函数用来统计符合条件的总行数 select COUNT(*) from T_UserInfo select F_UID,f_Pwd from T_UserInfo select COUNT(*) from T_UserInfo where F_UID = '124'and F_PwD='456'; select * from T_UserInfo; select * from T_LaundryList; select * from T_Recharge; select * from T_UserInfo, T_Recharge where T_UserInfo.F_UID = T_Recharge.F_UID select * from T_UserInfo, T_Recharge where T_UserInfo.F_UID = T_Recharge.F_UID and T_Recharge.F_Balance =100 select T_UserInfo.F_NAME,T_Recharge.F_Time from T_UserInfo, T_Recharge where T_UserInfo.F_UID = T_Recharge.F_UID and T_Recharge.F_Balance =100 -- 正确的写法 select a.F_NAME,b.F_Time from T_UserInfo a, T_Recharge b where a.F_UID = b.F_UID and b.F_Balance =100 --以下简化仅只证明语法上可行,不允许使用 select F_NAME,F_Time from T_UserInfo a, T_Recharge b where a.F_UID = b.F_UID and F_Balance =100 select a.F_NAME,b.F_Time, c.* from T_UserInfo a, T_Recharge b,T_LaundryList c where a.F_UID = b.F_UID and c.F_UID= a.F_UID select * from T_Recharge --sum函数是对字段值进行求和 select SUM(T_Recharge.F_Balance) from T_Recharge where T_Recharge.F_UID='128' select SUM(T_Recharge.F_Balance) from T_Recharge where T_Recharge.F_Time>'2014-07-01 00:00:00.000' and T_Recharge.F_Time<'2014-07-04 12:00:00.000'
-- count函数用来统计符合条件的总行数
select COUNT(*) from T_UserInfo
select F_UID,f_Pwd from T_UserInfo
select COUNT(*) from T_UserInfo
where F_UID = '124'and F_PwD='456';
注意的是现在为了保证数据库中密码的安全性,在登录的时候就应该采用这个方法实现而不是将密码返回客户端