-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeachingDBQuery.sql
More file actions
33 lines (31 loc) · 1.13 KB
/
TeachingDBQuery.sql
File metadata and controls
33 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- Create Students table
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Students]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Students](
[StudentID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[FirstName] NVARCHAR(50) NOT NULL,
[LastName] NVARCHAR(50) NOT NULL,
[GPA] [float] NULL
);
END;
GO
-- Create Courses table
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Courses]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Courses](
[CourseID] NVARCHAR(50) NOT NULL PRIMARY KEY,
[CourseName] NVARCHAR(100) NOT NULL,
[Credits] [int] NOT NULL
);
END;
GO
-- Create Enrollments table
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Enrollments]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Enrollments](
[EnrollmentID] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
[StudentID] [int] NOT NULL FOREIGN KEY REFERENCES Students(StudentID) ON DELETE CASCADE,
[CourseID] NVARCHAR(50) NOT NULL FOREIGN KEY REFERENCES Courses(CourseID) ON DELETE CASCADE
);
END;
GO