本文用3個題目,從創建數據庫到建立存儲過程,詳細講解數據庫的功能。程序員
字段名稱sql |
數據類型數據庫 |
說明ide |
stuIDspa |
char(10).net |
學生編號,主鍵code |
stuNameblog |
Varchar(10)排序 |
學生名稱ip |
major |
Varchar(50) |
專業 |
字段名稱 |
數據類型 |
說明 |
stuID |
char(10) |
學生編號,主鍵 |
stuName |
Varchar(10) |
學生名稱 |
major |
Varchar(50) |
專業 |
字段名稱 |
數據類型 |
說明 |
borrowID |
char(10) |
借書編號,主鍵 |
stuID |
char(10) |
學生編號,外鍵 |
BID |
char(10) |
圖書編號,外鍵 |
T_time |
datetime |
借書日期 |
B_time |
datetime |
還書日期 |
USE master GO /* $建庫 */ --檢驗數據庫是否存在,若是爲真,刪除此數據庫-- IF exists(SELECT * FROM sysdatabases WHERE name='BOOK') DROP DATABASE BOOK GO CREATE DATABASE BOOK GO --建數據表-- USE BOOK GO CREATE TABLE student --學生信息表 ( stuID CHAR(10) primary key, --學生編號 stuName CHAR(10) NOT NULL , --學生名稱 major CHAR(50) NOT NULL --專業 ) GO CREATE TABLE book --圖書表 ( BID CHAR(10) primary key, --圖書編號 title CHAR(50) NOT NULL, --書名 author CHAR(20) NOT NULL, --做者 ) GO CREATE TABLE borrow --借書表 ( borrowID CHAR(10) primary key, --借書編號 stuID CHAR(10) foreign key(stuID) references student(stuID), --學生編號 BID CHAR(10) foreign key(BID) references book(BID),--圖書編號 T_time datetime NOT NULL, --借出日期 B_time datetime --歸還日期 ) GO --學生信息表中插入數據-- INSERT INTO student(stuID,stuName,major)VALUES('1001','林林','計算機') INSERT INTO student(stuID,stuName,major)VALUES('1002','白楊','計算機') INSERT INTO student(stuID,stuName,major)VALUES('1003','虎子','英語') INSERT INTO student(stuID,stuName,major)VALUES('1004','北漂的雪','工商管理') INSERT INTO student(stuID,stuName,major)VALUES('1005','五月','數學') --圖書信息表中插入數據-- INSERT INTO book(BID,title,author)VALUES('B001','人生若只如初見','安意如') INSERT INTO book(BID,title,author)VALUES('B002','入學那天碰見你','晴空') INSERT INTO book(BID,title,author)VALUES('B003','感謝折磨你的人','如娜') INSERT INTO book(BID,title,author)VALUES('B004','我不是教你詐','劉庸') INSERT INTO book(BID,title,author)VALUES('B005','英語四級','白雪') --借書信息表中插入數據-- INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T001','1001','B001','2007-12-26',null) INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T002','1004','B003','2008-1-5',null) INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T003','1005','B001','2007-10-8','2007-12-25') INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T004','1005','B002','2007-12-16','2008-1-7') INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T005','1002','B004','2007-12-22',null) INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T006','1005','B005','2008-1-6',null) INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T007','1002','B001','2007-9-11',null) INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T008','1005','B004','2007-12-10',null) INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T009','1004','B005','2007-10-16','2007-12-18') INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T010','1002','B002','2007-9-15','2008-1-5') INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T011','1004','B003','2007-12-28',null) INSERT INTO borrow(borrowID,stuID,BID,T_time,B_time)VALUES('T012','1002','B003','2007-12-30',null)
select 學生編號=stuID,學生名稱=(select stuName from student where stuID=borrow.stuID),圖書編號=BID,圖書名稱=(select title from book where BID=borrow.BID),借出日期=T_time from borrow where stuID in (select stuID from student where major='計算機') and T_time>'2007-12-15' and T_time<'2008-1-8'
select 學生編號=stuID,學生名稱=stuName,專業=major from student where stuID in (select stuID from borrow)
select 學生名稱=(select stuName from student where stuID=borrow.stuID),圖書名稱=(select title from book where BID=borrow.BID),借出日期=T_time,歸還日期=B_time from borrow where BID in (select BID from book where author='安意如')
select 學生名稱=(select stuName from student where stuID=borrow.stuID),借書數量=count(*) from borrow where B_time is null group by stuID
字段名稱 |
數據類型 |
說明 |
ID |
int |
自動編號,主鍵 |
PName |
Char(10) |
程序員姓名 |
Wage |
int |
工資 |
USE master GO /* $建庫 */ --檢驗數據庫是否存在,若是爲真,刪除此數據庫-- IF exists(SELECT * FROM sysdatabases WHERE name='Wage') DROP DATABASE Wage GO CREATE DATABASE Wage GO --建數據表-- USE Wage GO CREATE TABLE ProWage --程序員工資表 ( ID int identity(1,1) primary key, --工資編號 PName CHAR(10) NOT NULL , --程序員姓名 Wage int NOT NULL --工資 ) GO --插入數據-- INSERT INTO ProWage(PName,Wage)VALUES('青鳥',1900) INSERT INTO ProWage(PName,Wage)VALUES('張三',1200) INSERT INTO ProWage(PName,Wage)VALUES('李四',1800) INSERT INTO ProWage(PName,Wage)VALUES('二月',3500) INSERT INTO ProWage(PName,Wage)VALUES('藍天',2780)
字段名稱 |
數據類型 |
說明 |
MID |
Char(10) |
學生號,主鍵 |
MName |
Char(50) |
姓名 |
字段名稱 |
數據類型 |
說明 |
FID |
Char(10) |
課程,主鍵 |
FName |
Char(50) |
課程名 |
字段名稱 |
數據類型 |
說明 |
SID |
int |
自動編號,主鍵,成績記錄號 |
FID |
Char(10) |
課程號,外鍵 |
MID |
Char(10) |
學生號,外鍵 |
Score |
int |
成績 |
姓名 |
語文 |
數學 |
英語 |
歷史 |
張薩 |
78 |
67 |
89 |
76 |
王強 |
89 |
67 |
84 |
96 |
李三 |
70 |
87 |
92 |
56 |
李四 |
80 |
78 |
97 |
66 |
USE master GO /* $建庫 */ --檢驗數據庫是否存在,若是爲真,刪除此數據庫-- IF exists(SELECT * FROM sysdatabases WHERE name='Student') DROP DATABASE Student GO CREATE DATABASE Student GO --建數據表-- USE Student GO CREATE TABLE Member --學生表 ( MID char(10) primary key, --學生號 MName CHAR(50) NOT NULL --姓名 ) GO CREATE TABLE F --課程表 ( FID char(10) primary key, --課程號 FName CHAR(50) NOT NULL --課程名 ) GO CREATE TABLE score --學生成績表 ( SID int identity(1,1) primary key, --成績記錄號 FID char(10) foreign key(FID) references F(FID) , --課程號 MID char(10) foreign key(MID) references Member(MID) , --學生號 Score int NOT NULL --成績 ) GO --課程表中插入數據-- INSERT INTO F(FID,FName)VALUES('F001','語文') INSERT INTO F(FID,FName)VALUES('F002','數學') INSERT INTO F(FID,FName)VALUES('F003','英語') INSERT INTO F(FID,FName)VALUES('F004','歷史') --學生表中插入數據-- INSERT INTO Member(MID,MName)VALUES('M001','張薩') INSERT INTO Member(MID,MName)VALUES('M002','王強') INSERT INTO Member(MID,MName)VALUES('M003','李三') INSERT INTO Member(MID,MName)VALUES('M004','李四') INSERT INTO Member(MID,MName)VALUES('M005','陽陽') INSERT INTO Member(MID,MName)VALUES('M006','虎子') INSERT INTO Member(MID,MName)VALUES('M007','夏雪') INSERT INTO Member(MID,MName)VALUES('M008','璐璐') INSERT INTO Member(MID,MName)VALUES('M009','珊珊') INSERT INTO Member(MID,MName)VALUES('M010','香奈兒') --成績表中插入數據-- INSERT INTO Score(FID,MID,Score)VALUES('F001','M001',78) INSERT INTO Score(FID,MID,Score)VALUES('F002','M001',67) INSERT INTO Score(FID,MID,Score)VALUES('F003','M001',89) INSERT INTO Score(FID,MID,Score)VALUES('F004','M001',76) INSERT INTO Score(FID,MID,Score)VALUES('F001','M002',89) INSERT INTO Score(FID,MID,Score)VALUES('F002','M002',67) INSERT INTO Score(FID,MID,Score)VALUES('F003','M002',84) INSERT INTO Score(FID,MID,Score)VALUES('F004','M002',96) INSERT INTO Score(FID,MID,Score)VALUES('F001','M003',70) INSERT INTO Score(FID,MID,Score)VALUES('F002','M003',87) INSERT INTO Score(FID,MID,Score)VALUES('F003','M003',92) INSERT INTO Score(FID,MID,Score)VALUES('F004','M003',56) INSERT INTO Score(FID,MID,Score)VALUES('F001','M004',80) INSERT INTO Score(FID,MID,Score)VALUES('F002','M004',78) INSERT INTO Score(FID,MID,Score)VALUES('F003','M004',97) INSERT INTO Score(FID,MID,Score)VALUES('F004','M004',66) INSERT INTO Score(FID,MID,Score)VALUES('F001','M006',88) INSERT INTO Score(FID,MID,Score)VALUES('F002','M006',55) INSERT INTO Score(FID,MID,Score)VALUES('F003','M006',86) INSERT INTO Score(FID,MID,Score)VALUES('F004','M006',79) INSERT INTO Score(FID,MID,Score)VALUES('F002','M007',77) INSERT INTO Score(FID,MID,Score)VALUES('F003','M008',65) INSERT INTO Score(FID,MID,Score)VALUES('F004','M007',48) INSERT INTO Score(FID,MID,Score)VALUES('F004','M009',75) INSERT INTO Score(FID,MID,Score)VALUES('F002','M009',88)
-- 1)查詢各個學生語文、數學、英語、歷史課程成績--
SELECT Member.MName AS 姓名, 英語 = SUM(CASE F.FName WHEN '語文' THEN Score.Score END), 數學 = SUM(CASE F.FName WHEN '數學' THEN Score.Score END), 語文 = SUM(CASE F.FName WHEN '英語' THEN Score.Score END), 歷史 = SUM(CASE F.FName WHEN '歷史' THEN Score.Score END) FROM Score, Member,F WHERE F.FID = Score.FID AND Member.MID =Score.MID GROUP BY Member.MName
-- 2)查詢四門課中成績低於70分的學生及相對應課程名和成績--
select 姓名=(select MName from Member where MID=Score.MID),課程名=(select FName from F where FID=Score.FID),成績=Score from Score where Score<70
-- 3)統計各個學生四課程的平均分,且按平均分數由高到底排序--
select 姓名=(select MName from Member where MID=Score.MID),平均分=Avg(Score) from Score group by MID order by 平均分 desc
-- 4)建立存儲過程--
if exists (select * from sysobjects where name='P_stu') drop procedure P_stu GO create procedure P_stu @num int As print'參加'+convert(varchar(5),@num)+'門課考試的學生姓名及學號:' select 姓名=(select MName from Member where MID=Score.MID),學號=MID from Score group by MID having count(*)=@num --調用存儲過程-- exec P_stu @num=2
轉:http://blog.csdn.net/crazygolf/article/details/38989461