--创建数据库MySchool
CREATE DATABASE MySchool
ON
(
NAME='bankDB_data',
FILENAME='E:\MySchool_data.mdf',
SIZE=5mb,
FILEGROWTH=15%
)
LOG ON
(
NAME= 'MySchool_log',
FILENAME='E:\MySchool_log.ldf',
SIZE=5mb,
FILEGROWTH=15%
)
GO
USE [MySchool]
GO
/****** Object: Table [dbo].[Grade] Script Date: 08/06/2014 15:03:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Grade](
[GradeId] [int] IDENTITY(1,1) NOT NULL,
[GradeName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Grade_1] PRIMARY KEY CLUSTERED
(
[GradeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_Grade_GradeName] UNIQUE NONCLUSTERED
(
[GradeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Grade] ON
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (1, N'S1')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (2, N'S2')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (3, N'Y2')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (4, N'精英班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (11, N'zhanan')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (12, N'S1288无敌班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (13, N'高一二班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (14, N'高一三班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (15, N'高一一班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (16, N'高一1班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (17, N'高一2班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (18, N'高一3班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (19, N'aa')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (20, N'mine')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (21, N'mine')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (22, N'mine')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (23, N'mine')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (26, N'无敌S2214')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (27, N'无敌S2214')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (28, N'好人2214')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (30, N'aa')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (31, N'bb')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (32, N'cc')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (33, N'dd')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (34, N'S2216')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (48, N'S2216第二个班')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (49, N'S2216终结版')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (51, N'JDBC测试啊啊啊啊')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (52, N'JDBC测试啊啊啊啊2')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (53, N'JDBC测试啊啊啊啊2')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (54, N'DBC啊速度速度')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (55, N'DBC阿萨德完全')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (56, N'DBC阿萨德完全')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (57, N'DBC阿萨德完全')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (58, N'aa')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (59, N'bb')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (60, N'aaa''')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (61, N'ccc')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (62, N'1')
INSERT [dbo].[Grade] ([GradeId], [GradeName]) VALUES (63, N's1')
SET IDENTITY_INSERT [dbo].[Grade] OFF
/****** Object: Table [dbo].[Subject] Script Date: 08/06/2014 15:03:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Subject](
[SubjectId] [int] IDENTITY(1,1) NOT NULL,
[SubjectName] [nchar](10) NOT NULL,
[ClassHour] [int] NULL,
[GradeId] [int] NOT NULL,
CONSTRAINT [PK_Subject] PRIMARY KEY CLUSTERED
(
[SubjectId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Subject] ON
INSERT [dbo].[Subject] ([SubjectId], [SubjectName], [ClassHour], [GradeId]) VALUES (1, N'oop ', 78, 1)
INSERT [dbo].[Subject] ([SubjectId], [SubjectName], [ClassHour], [GradeId]) VALUES (2, N'java ', 67, 2)
INSERT [dbo].[Subject] ([SubjectId], [SubjectName], [ClassHour], [GradeId]) VALUES (3, N'C# ', 78, 3)
INSERT [dbo].[Subject] ([SubjectId], [SubjectName], [ClassHour], [GradeId]) VALUES (4, N'分层开发 ', 24, 2)
SET IDENTITY_INSERT [dbo].[Subject] OFF
/****** Object: Table [dbo].[Student] Script Date: 08/06/2014 15:03:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Student](
[StudentNo] [int] IDENTITY(1000,1) NOT NULL,
[LoginPwd] [nvarchar](50) NOT NULL,
[StudentName] [nvarchar](50) NOT NULL,
[Gender] [char](1) NULL,
[GradeId] [int] NOT NULL,
[Phone] [nvarchar](255) NULL,
[Address] [nvarchar](255) NULL,
[Birthday] [datetime] NULL,
[Email] [nvarchar](50) NULL,
[MyTT] [int] NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[StudentNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Student] ON
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23, N'000000', N'灰太狼2222', N'1', 2, N'23', N'中国平安啊啊', CAST(0x0000759800000000 AS DateTime), N'好@3', 1)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23213, N'000000', N'李金香好人', N'0', 3, N'13835231533/13503542995', N'北京海淀区北宫门', CAST(0x0000759800000000 AS DateTime), N'1@3', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23219, N'000000', N'美洋洋222', N'0', 2, N'6666666', N'北京市海淀区', CAST(0x000081D500000000 AS DateTime), N'1@3', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23225, N'000000', N'张靓颖', N'0', 2, N'13503001234', N'北京市海淀区', CAST(0x0000A1E5009B6025 AS DateTime), N'1@3', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23268, N'000001', N'张玲', N'0', 2, N'11', N'北京', CAST(0x0000A0BA011CBBCC AS DateTime), N'1@3', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23270, N'000000', N'李小龙', N'1', 2, NULL, N'武汉', CAST(0x0000A227011C8BC0 AS DateTime), N'1@3', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23311, N'111111', N'微冷的雨', N'1', 1, N'11', N'111', CAST(0x0000A2D000C38D54 AS DateTime), N'[email protected]', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23316, N'123456', N'习近平', N'1', 4, N'010-1234567', N'北京人民大会堂', CAST(0x00008D8900FBB008 AS DateTime), N'[email protected]', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23317, N'111111', N'111', N'0', 1, N'11', N'11', CAST(0x0000A2F000FCE310 AS DateTime), N'@', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23318, N'111111', N'习近平222', N'1', 4, N'11111', N'北京任命', CAST(0x0000A2F000FD4DB4 AS DateTime), N'[email protected]', NULL)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23322, N'111111', N'张靓颖', N'0', 1, N'010-123', N'中国', CAST(0x0000A2FE00A0C0BC AS DateTime), N'zly@', 11)
INSERT [dbo].[Student] ([StudentNo], [LoginPwd], [StudentName], [Gender], [GradeId], [Phone], [Address], [Birthday], [Email], [MyTT]) VALUES (23323, N'111111', N'张靓颖', N'0', 1, N'010-123', N'中国', CAST(0x0000A2FE00A0C0BC AS DateTime), N'zly@', 11)
SET IDENTITY_INSERT [dbo].[Student] OFF
/****** Object: Table [dbo].[Result] Script Date: 08/06/2014 15:03:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Result](
[StudentNo] [int] NOT NULL,
[SubjectId] [int] NOT NULL,
[StudentResult] [int] NOT NULL,
[ExamDate] [datetime] NOT NULL,
[Id] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_Result] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Result] ON
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23219, 2, 92, CAST(0x0000A215010D13FC AS DateTime), 1)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23213, 1, 58, CAST(0x00009C8200000000 AS DateTime), 2)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23213, 1, 92, CAST(0x0000A215010D13FC AS DateTime), 3)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23219, 2, 82, CAST(0x00009C9D00000000 AS DateTime), 4)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23231, 1, 93, CAST(0x0000A1ED009052E0 AS DateTime), 5)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23231, 2, 97, CAST(0x0000A1ED00905C40 AS DateTime), 6)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23213, 1, 96, CAST(0x00009C7F00000000 AS DateTime), 9)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23213, 2, 93, CAST(0x00009C7F00000000 AS DateTime), 10)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23225, 1, 62, CAST(0x0000A215010D13FC AS DateTime), 11)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23225, 2, 48, CAST(0x0000A215010D27E8 AS DateTime), 12)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23225, 3, 98, CAST(0x0000A2DA00C441CC AS DateTime), 16)
INSERT [dbo].[Result] ([StudentNo], [SubjectId], [StudentResult], [ExamDate], [Id]) VALUES (23213, 3, 98, CAST(0x0000A2DB00CAB4F8 AS DateTime), 17)
SET IDENTITY_INSERT [dbo].[Result] OFF
/****** Object: Default [DF_Student_GradeId] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Student] ADD CONSTRAINT [DF_Student_GradeId] DEFAULT ((1)) FOR [GradeId]
GO
/****** Object: Check [CK_Student_Birthday] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [CK_Student_Birthday] CHECK (([BirthDay]>'1970-01-01'))
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [CK_Student_Birthday]
GO
/****** Object: Check [CK_Student_Email] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [CK_Student_Email] CHECK (([Email] like '%@%'))
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [CK_Student_Email]
GO
/****** Object: Check [CK_Student_Gender] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [CK_Student_Gender] CHECK (([Gender]=(0) OR [Gender]='1'))
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [CK_Student_Gender]
GO
/****** Object: Check [CK_Student_LoginPwd] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [CK_Student_LoginPwd] CHECK ((len([LoginPwd])>=(6)))
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [CK_Student_LoginPwd]
GO
/****** Object: Check [CK_studentname] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [CK_studentname] CHECK (([studentname] IS NOT NULL))
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [CK_studentname]
GO
/****** Object: Check [CK_Result_StudentResult] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Result] WITH CHECK ADD CONSTRAINT [CK_Result_StudentResult] CHECK (([StudentResult]>=(0) AND [Studentresult]<=(100)))
GO
ALTER TABLE [dbo].[Result] CHECK CONSTRAINT [CK_Result_StudentResult]
GO
/****** Object: ForeignKey [FK_Student_GradeId] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Student] WITH CHECK ADD CONSTRAINT [FK_Student_GradeId] FOREIGN KEY([GradeId])
REFERENCES [dbo].[Grade] ([GradeId])
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_GradeId]
GO
/****** Object: ForeignKey [FK_Subject_Grade] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Subject] WITH CHECK ADD CONSTRAINT [FK_Subject_Grade] FOREIGN KEY([GradeId])
REFERENCES [dbo].[Grade] ([GradeId])
GO
ALTER TABLE [dbo].[Subject] CHECK CONSTRAINT [FK_Subject_Grade]
GO
/****** Object: ForeignKey [FK_Result_Subject] Script Date: 08/06/2014 15:03:17 ******/
ALTER TABLE [dbo].[Result] WITH CHECK ADD CONSTRAINT [FK_Result_Subject] FOREIGN KEY([SubjectId])
REFERENCES [dbo].[Subject] ([SubjectId])
GO
ALTER TABLE [dbo].[Result] CHECK CONSTRAINT [FK_Result_Subject]
GO
--第二章课后解答
--创建数据库
use master
if exists(SELECT * FROM sysdatabases WHERE name='Library')
DROP DATABASE Library
CREATE DATABASE Library
on PRIMARY
(
NAME='Library_data',
FILENAME='D:\project_data.mdf',
SIZE=5mb,
FILEGROWTH=15%
)
LOG on
(
NAME='S2226_log',
FILENAME='D:\project_log.ldf',
SIZE=5mb,
FILEGROWTH=15mb
)
--创建图书信息表Book
use Library
CREATE TABLE Book
(
BID nvarchar(20) not null,
BName nvarchar(20) not null,
Author nvarchar(20) not null,
PubComp nvarchar not null,
PubDate datetime not null,
BCount int not null,
Price money not null
)
--创建读者信息表Reader
use Library
create table Reader
(
RID nvarchar(20) not null,
RName nvarchar(20) not null,
LendNum int not null,
RAddress nvarchar(100)not null
)
--创建图书借阅表Borrow
use Library
create table Borrow
(
RID nvarchar(20) not null,
BID nvarchar(20) not null,
LendDate datetime not null,
WillDate datetime not null,
ReturnDate datetime not null
)
--创建罚款记录表Penalty
use Library
create table Penalty
(
RID nvarchar(20) not null,
BID nvarchar(20) not null,
PDate datetime not null,
PType int not null,
Amount money not null
)
--添加约束
--图书信息表Book
-- 添加主键约束将BID 作为主键
alter table Book
add constraint pk_BID primary key (BID)
--必须以"ISBN"开头
alter table Book
add constraint ck_BID CHECK(BID='ISBN%')
--出版日期,必须小于当前日期
alter table Book
add constraint ck_PubDate CHECK(PubDate=1)
--单价必须大于0
alter table Book
add constraint ck_Price CHECK(Price>0)
--读者信息表Reader
--添加主键约束将RID 作为主键
alter table Reader
add constraint pk_RID primary key (RID)
--以借数量,必须大于0
alter table Reader
add constraint ck_LendNum CHECK(LendNum>0)
--图书借阅表Borrow
--读者信息表的外键
alter table Borrow
add constraint fk_RID foreign key (RID) references Reader(RID)
--图书信息表的外键
alter table Borrow
add constraint fk_BID foreign key (BID) references Reader(BID)
-- 添加借阅日期 复合主键将LendDate作为主键
alter table Borrow
add constraint pk_LendDate primary key (LendDate)
--应归还日期,必须大于等于借阅日期,默认值为借阅日期+1个月
alter table Borrow
add constraint df_WillDate
default(DateAdd(mm,1,getdate())) for WillDate
--罚款记录表Penalty
-- 复合主键,读者信息表的外键
alter table Penalty
add constraint fk_RIDd foreign key (RID) references Reader(RID)
-- 复合主键 ,图书信息表的外键
alter table Penalty
add constraint fk_BIDd foreign key (BID) references Book(BID)
-- 添加复合主键,将PType作为主键
alter table Penalty
add constraint pk_PDate primary key (PDate)
-- 罚款日期, 默认值为当前日期
alter table Penalty
add constraint df_PDate default(GetDate()) for PDate
-- 罚款类型 ,1- 延期 ,2-损坏, 3- 丢失
alter table Penalty
add constraint CK_PType check (1='延期'or 2='损坏' or 3='丢失')
-- 罚款金额 ,必须大于0
alter table Penalty
add constraint CK_Amount check (Amount>0)