-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProyecto-Udemy-DDL.sql
More file actions
172 lines (159 loc) · 5.37 KB
/
Proyecto-Udemy-DDL.sql
File metadata and controls
172 lines (159 loc) · 5.37 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
CREATE DATABASE UdemyClone;
GO
USE UdemyClone;
GO
-- ========================================
-- TABLE: Categories
-- ========================================
CREATE TABLE Categories (
CategoryId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
Name NVARCHAR(100) NOT NULL,
ParentCategoryId UNIQUEIDENTIFIER NULL,
CONSTRAINT FK_Categories_Parent
FOREIGN KEY (ParentCategoryId) REFERENCES Categories(CategoryId)
);
GO
-- ========================================
-- TABLE: Instructors
-- ========================================
CREATE TABLE Instructors (
InstructorId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
Name NVARCHAR(100) NOT NULL,
Email NVARCHAR(100) UNIQUE NOT NULL,
CreatedAt DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME()
);
GO
-- ========================================
-- TABLE: Students
-- ========================================
CREATE TABLE Students (
StudentId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
Name NVARCHAR(100) NOT NULL,
Email NVARCHAR(100) UNIQUE NOT NULL,
CreatedAt DATETIME2 NOT NULL DEFAULT SYSUTCDATETIME()
);
GO
-- ========================================
-- TABLE: LessonTypes
-- ========================================
CREATE TABLE LessonTypes (
LessonTypeId INT PRIMARY KEY IDENTITY(1,1),
Name NVARCHAR(100) NOT NULL
);
GO
-- ========================================
-- TABLE: Courses
-- ========================================
CREATE TABLE Courses (
CourseId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
Title NVARCHAR(250) NOT NULL,
Description NVARCHAR(250) NOT NULL,
Price DECIMAL(10,2) NOT NULL,
Language NVARCHAR(50),
Level NVARCHAR(50),
CategoryId UNIQUEIDENTIFIER NOT NULL,
CONSTRAINT FK_Courses_Category
FOREIGN KEY (CategoryId) REFERENCES Categories(CategoryId)
);
GO
-- ========================================
-- TABLE N:M: CourseInstructors
-- ========================================
CREATE TABLE CourseInstructors (
CourseId UNIQUEIDENTIFIER NOT NULL,
InstructorId UNIQUEIDENTIFIER NOT NULL,
PRIMARY KEY (CourseId, InstructorId),
CONSTRAINT FK_CI_Course
FOREIGN KEY (CourseId) REFERENCES Courses(CourseId),
CONSTRAINT FK_CI_Instructor
FOREIGN KEY (InstructorId) REFERENCES Instructors(InstructorId)
);
GO
-- ========================================
-- TABLE: Sections
-- ========================================
CREATE TABLE Sections (
SectionId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
CourseId UNIQUEIDENTIFIER NOT NULL,
Title NVARCHAR(255) NOT NULL,
SortOrder INT NOT NULL,
CONSTRAINT FK_Sections_Course
FOREIGN KEY (CourseId) REFERENCES Courses(CourseId)
);
GO
-- ========================================
-- TABLE: Lessons
-- ========================================
CREATE TABLE Lessons (
LessonId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
SectionId UNIQUEIDENTIFIER NOT NULL,
LessonTypeId INT NOT NULL,
Title NVARCHAR(255) NOT NULL,
DurationSeconds DECIMAL(10,2) NOT NULL,
SortOrder INT NOT NULL,
CONSTRAINT FK_Lessons_Section
FOREIGN KEY (SectionId) REFERENCES Sections(SectionId),
CONSTRAINT FK_Lessons_Type
FOREIGN KEY (LessonTypeId) REFERENCES LessonTypes(LessonTypeId)
);
GO
-- ========================================
-- TABLE: Enrollments
-- ========================================
CREATE TABLE Enrollments (
EnrollmentId UNIQUEIDENTIFIER PRIMARY KEY DEFAULT NEWID(),
StudentId UNIQUEIDENTIFIER NOT NULL,
CourseId UNIQUEIDENTIFIER NOT NULL,
EnrolledAt DATETIME2 DEFAULT SYSUTCDATETIME(),
PricePaid DECIMAL(10,2) NOT NULL,
CouponCode NVARCHAR(20) NULL,
CONSTRAINT UQ_Student_Course UNIQUE (StudentId, CourseId),
CONSTRAINT FK_Enrollments_Student
FOREIGN KEY (StudentId) REFERENCES Students(StudentId),
CONSTRAINT FK_Enrollments_Course
FOREIGN KEY (CourseId) REFERENCES Courses(CourseId)
);
GO
-- ========================================
-- TABLE: Reviews
-- ========================================
CREATE TABLE Reviews (
StudentId UNIQUEIDENTIFIER NOT NULL,
CourseId UNIQUEIDENTIFIER NOT NULL,
Rating INT CHECK (Rating BETWEEN 1 AND 5),
ReviewText NVARCHAR(250) NOT NULL,
CreatedAt DATETIME2 DEFAULT SYSUTCDATETIME(),
PRIMARY KEY (StudentId, CourseId),
CONSTRAINT FK_Reviews_Student
FOREIGN KEY (StudentId) REFERENCES Students(StudentId),
CONSTRAINT FK_Reviews_Course
FOREIGN KEY (CourseId) REFERENCES Courses(CourseId)
);
GO
-- ========================================
-- TABLE: Wishlist
-- ========================================
CREATE TABLE Wishlist (
CourseId UNIQUEIDENTIFIER NOT NULL,
StudentId UNIQUEIDENTIFIER NOT NULL,
PRIMARY KEY (CourseId, StudentId),
CONSTRAINT FK_Wishlist_Course
FOREIGN KEY (CourseId) REFERENCES Courses(CourseId),
CONSTRAINT FK_Wishlist_Student
FOREIGN KEY (StudentId) REFERENCES Students(StudentId)
);
GO
-- ========================================
-- TABLE: Progress
-- ========================================
CREATE TABLE Progress (
StudentId UNIQUEIDENTIFIER NOT NULL,
LessonId UNIQUEIDENTIFIER NOT NULL,
CompletedAt DATETIME2 DEFAULT SYSUTCDATETIME(),
PRIMARY KEY (StudentId, LessonId),
CONSTRAINT FK_Progress_Student
FOREIGN KEY (StudentId) REFERENCES Students(StudentId),
CONSTRAINT FK_Progress_Lesson
FOREIGN KEY (LessonId) REFERENCES Lessons(LessonId)
);
GO