java開發地三天——數據庫介紹

又是一天萌萌噠地過去了,今天是處理數據庫的部分。SQL Server 2008,這東西是上學期搞MFC的時候接觸到的,那時候話說安裝就是一個大問題,而後在學SQL語句的時候感受還好,一切都還過得去。如今將這東西感受挺輕鬆的,還不時的幫助你們解決了很多的問題,感受挺棒。可是畢竟是速成的,缺陷也是很明顯的。好比建立表單能夠直接用代碼實現,可是這一部分是徹底的不會的,雖說效率是相對來講低了一點,可是移植性至關的好的,直接的運行一下一切就都OK了。好了,話說搞MFC的時候鏈接數據庫本身尚未寫博客,忘記了很多東西,如今也應該好好的記錄一下了。數據庫

首先就先是上傳本身上課時用到的PPT和寫做業用到的習題吧。安全

對於數據庫的基本操做主要是增、刪、改、查這四個操做,外加上一些其餘的東西,就讓SQL語句也變得很是的有趣同時也和生活中的一些現象適應起來了。確實挺不錯的。網絡

這裏先將老師共享的一個SQL語句貼出來吧。app

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');
View Code


感受確實不錯,而後是本身寫的一些語句,這裏也粘貼出來吧。ide

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
View Code

 對應試題:函數

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 歲的男生信息。ui

找到了之前的周偉經理的SQL語句講解部分,這裏也一併貼出來:spa

--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';

注意的是如今爲了保證數據庫中密碼的安全性,在登陸的時候就應該採用這個方法實現而不是將密碼返回客戶端
操作系統

相關文章
相關標籤/搜索