Sie sind auf Seite 1von 6

SET ANSI_NULLS ON

GO
SET QUOTED_IDENTIFIER ON
GO
USE [master];
GO
IF EXISTS (SELECT * FROM sys.databases WHERE name = 'School')
DROP DATABASE School;
GO
-- Create the School database.
CREATE DATABASE School;
GO
ALTER DATABASE School
SET RECOVERY SIMPLE;
GO
USE School;
GO
-- Create the Department table.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Department]')
AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Department](
[DepartmentID] [int] NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Budget] [money] NOT NULL,
[StartDate] [datetime] NOT NULL,
[Administrator] [int] NULL,
CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED
(
[DepartmentID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
-- Create the Student table.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Person]')
AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Student](
[StudentID] [int] IDENTITY(1,1) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[HireDate] [datetime] NULL,
[EnrollmentDate] [datetime] NULL,
CONSTRAINT [PK_School.Student] PRIMARY KEY CLUSTERED
(
[StudentID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END

GO
-- Create the Lecturer table.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Lecturer]')
AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Lecturer](
[CourseID] [int] NOT NULL,
[lecturerID] [int] NOT NULL,
CONSTRAINT [PK_lecturer] PRIMARY KEY CLUSTERED
(
[CourseID] ASC,
[lecturerID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END

GO
-- Create the chairman table.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[chairman]')
AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[chairman](
[departmentID] [int] NOT NULL,
[chairmanID] [int] NOT NULL,
CONSTRAINT [PK_chairman] PRIMARY KEY CLUSTERED
(
[departmentID] ASC,
[lecturerID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
-- Create the Course table.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Course]')
AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Course](
[CourseID] [int] NOT NULL,
[Title] [nvarchar](100) NOT NULL,
[Credits] [int] NOT NULL,
[DepartmentID] [int] NOT NULL,
CONSTRAINT [PK_School.Course] PRIMARY KEY CLUSTERED
(
[CourseID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
-- Define the relationship between StudentGrade and Course.
IF NOT EXISTS (SELECT * FROM sys.foreign_keys
WHERE object_id = OBJECT_ID(N'[dbo].[FK_StudentGrade_Course]')
AND parent_object_id = OBJECT_ID(N'[dbo].[StudentGrade]'))
ALTER TABLE [dbo].[StudentGrade] WITH CHECK ADD
CONSTRAINT [FK_StudentGrade_Course] FOREIGN KEY([CourseID])
REFERENCES [dbo].[Course] ([CourseID])
GO
ALTER TABLE [dbo].[StudentGrade] CHECK
CONSTRAINT [FK_StudentGrade_Course]
GO
-- Define the relationship between Lecturer and Course.
IF NOT EXISTS (SELECT * FROM sys.foreign_keys
WHERE object_id = OBJECT_ID(N'[dbo].[FK_Lecturer_Course]')
AND parent_object_id = OBJECT_ID(N'[dbo].[Lecturer]'))
ALTER TABLE [dbo].[Lecturer] WITH CHECK ADD
CONSTRAINT [FK_Lecturer_Course] FOREIGN KEY([CourseID])
REFERENCES [dbo].[Course] ([CourseID])
GO
ALTER TABLE [dbo].[Lecturer] CHECK
CONSTRAINT [FK_Lecturer_Course]
GO
-- Define the relationship between Lecturer and Student.
IF NOT EXISTS (SELECT * FROM sys.foreign_keys
WHERE object_id = OBJECT_ID(N'[dbo].[FK_lecturer_student]')
AND parent_object_id = OBJECT_ID(N'[dbo].[lecturer]'))
ALTER TABLE [dbo].[lecturer] WITH CHECK ADD
CONSTRAINT [FK_lecturer_student] FOREIGN KEY(studentID])
REFERENCES [dbo].[student] ([studentID])
GO
ALTER TABLE [dbo].[lecturer] CHECK
CONSTRAINT [FK_lecturer_student]
GO
-- Define the relationship between Course and Department.
IF NOT EXISTS (SELECT * FROM sys.foreign_keys
WHERE object_id = OBJECT_ID(N'[dbo].[FK_Course_Department]')
AND parent_object_id = OBJECT_ID(N'[dbo].[Course]'))
ALTER TABLE [dbo].[Course] WITH CHECK ADD
CONSTRAINT [FK_Course_Department] FOREIGN KEY([DepartmentID])
REFERENCES [dbo].[Department] ([DepartmentID])
GO
ALTER TABLE [dbo].[Course] CHECK CONSTRAINT [FK_Course_Department]
GO
-- Create the Deletestudent stored procedure.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Deletestudent]')
AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[Deletestudent]
@studentID int
AS
DELETE FROM Person WHERE studentID = @studentID;
'
END
GO
-- Create the Updatestudent stored procedure.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Updatestudent]')
AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[Updatestudent]
@studentID int,
@LastName nvarchar(50),
@FirstName nvarchar(50),
@HireDate datetime,
@EnrollmentDate datetime
AS
UPDATE Person SET LastName=@LastName,
FirstName=@FirstName,
HireDate=@HireDate,
EnrollmentDate=@EnrollmentDate
WHERE studentID=@studentID;
'
END
GO
-- Create the Insertstudent stored procedure.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Insertstudent]')
AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[Insertstudent]
@LastName nvarchar(50),
@FirstName nvarchar(50),
@HireDate datetime,
@EnrollmentDate datetime
AS
INSERT INTO dbo.student (LastName,
FirstName,
HireDate,
EnrollmentDate)
VALUES (@LastName,
@FirstName,
@HireDate,
@EnrollmentDate);
SELECT SCOPE_IDENTITY() as NewstudentID;
'
END
GO
-- Create GetDepartmentName stored procedure.
IF NOT EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[GetDepartmentName]')
AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROCEDURE [dbo].[GetDepartmentName]
@ID int,
@Name nvarchar(50) OUTPUT
AS
SELECT @Name = Name FROM Department
WHERE DepartmentID = @ID
'
END
GO
-- Insert data into the Person table.
USE School
GO
SET IDENTITY_INSERT dbo.Person ON
GO
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (1, 'Abercrombie', 'Kim', '1995-03-11', null);
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (2, 'Barzdukas', 'Gytis', null, '2005-09-01');
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (3, 'Justice', 'Peggy', null, '2001-09-01');
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (4, 'Fakhouri', 'Fadi', '2002-08-06', null);
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (5, 'Harui', 'Roger', '1998-07-01', null);
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUS(6, 'Harui', 'Roger', '1998-07-01', null);
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (7, 'Norman', 'Laura', null, '2003-09-01');
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (8, 'Olivotto', 'Nino', null, '2005-09-01');
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (9, 'Tang', 'Wayne', null, '2005-09-01');
INSERT INTO dbo.student (studentID, LastName, FirstName, HireDate, EnrollmentDate)
VALUES (10, 'Alonso', 'Meredith', null, '2002-09-01');

GO
-- Insert data into the Department table.
INSERT INTO dbo.Department (DepartmentID, [Name], Budget, StartDate, Administrator)
VALUES (1, 'Industrial Technology', 350000.00, '2007-09-01', 2);
INSERT INTO dbo.Department (DepartmentID, [Name], Budget, StartDate, Administrator)
VALUES (2, 'Applied Science', 120000.00, '2007-09-01', 6);
INSERT INTO dbo.Department (DepartmentID, [Name], Budget, StartDate, Administrator)
VALUES (3, 'Built Environment', 200000.00, '2007-09-01', 4);
INSERT INTO dbo.Department (DepartmentID, [Name], Budget, StartDate, Administrator)
VALUES (4, 'Commerce', 250000.00, '2007-09-01', 3);
INSERT INTO dbo.Department (DepartmentID, [Name], Budget, StartDate, Administrator)
VALUES (5, 'Medicine', 350000.00, '2007-09-01', 2);

GO
-- Insert data into the Course table.
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (tie2109, 'Computer Applications', 1);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (tie2108, 'Thermodynamics', 1);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (sma2116, 'Engineering Mathematics', 7);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (tie2104, 'Materials technology', 2);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (tie2105, 'Design', 2);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (tie2106, 'Dynamics', 2);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (tie2103, 'Solid mechanics', 4);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (tie1102, 'Workshop Technology', 4);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (tee2114,'Electrical technology', 4);
INSERT INTO dbo.Course (CourseID, Title, DepartmentID)
VALUES (sms1205, 'Visual basics', 7);

-- Insert data into the lecturer table.


INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (sma2116, 1);
INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (tie2109, 31);
INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (tie2104, 5);
INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (tee2104, 4);
INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (tie2107, 27);
INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (tie1205, 25);
INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (sms1103, 18);
INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (tie2103, 32);
INSERT INTO dbo.lecturer(CourseID, lecturerID)
VALUES (tee2114, 34);

-- Insert data into the chairman table.


INSERT INTO dbo.lecturer(departmentID, chairmanID)
VALUES (industrial technology, 1);
INSERT INTO dbo.lecturer(departmentID, chairmanID)
VALUES (medicine, 31);
INSERT INTO dbo.lecturer(departmentID, chairmanID)
VALUES (commerce, 5);
INSERT INTO dbo.lecturer(departmentID, chairmanID)
VALUES (applied sciences, 4);

Das könnte Ihnen auch gefallen