2. Spotle.ai Study Material
Spotle.ai/Learn
A relational database stores data in
the form of tables. (Like excel grids
or tables in Word).
Employee_Name Employee_Id Designation
Seema Rathod 123344 Manager
G Balakrishnan 144555 Software Engineer
Asif Iqbal 134455 Senior Architect
Tina Das 157772 Business Analyst
2
Table: Employee
3. Spotle.ai Study Material
Spotle.ai/Learn
See the employee table. Each
column stores a particular attribute.
Each row stores data of one
employee.
Employee_Name Employee_Id Designation
Seema Rathod 123344 Manager
G Balakrishnan 144555 Software Engineer
Asif Iqbal 134455 Senior Architect
Tina Das 157772 Business Analyst
3
Table: Employee
4. Spotle.ai Study Material
Spotle.ai/Learn
4
Using tables it is
easy to model or
store relationships
between entities. For
example, Spotle can
have a user table, a
course table and a
user x course table
that maps users to
courses they are
enrolled in.
User_id
User_name
Course_id
Course_name
User_id
Course_id
User Course
User_x_course
11. Spotle.ai Study Material
Spotle.ai/Learn
11
Step 2:
So you have got 3
entities: student, marks
of students, courses
subscribed by students.
These become your
tables. You may need
some more (or less)
tables which we
discover in next steps.
14. Spotle.ai Study Material
Spotle.ai/Learn
14
Course Attributes:
Course Name
Course Description
Course Instructor Name
Course Subject
Course Difficulty Level
Image
Challenge: Think and share what other
attributes you will want to store about a
course?
16. Spotle.ai Study Material
Spotle.ai/Learn
A student has a unique roll_no. We
can designate roll_no as the
primary key.
16
Table: Student
Student_Name Roll_No Email_Id
Seema Rathod 123344 Manager
G Balakrishnan 144555 Software Engineer
Asif Iqbal 134455 Senior Architect
Tina Das 157772 Business Analyst
17. Spotle.ai Study Material
Spotle.ai/Learn
In the list of course attributes, we do not have
a unique identifier. (Course names can be
same for example 2 courses on Biology with
different difficulty levels). So we give course a
unique id and add it to the table.
17
Table: Course
Course_id Course_Name Course_desc Course_level
1 Certificate In Biology Basic Biology Beginner
2 Certificate In Biology Advanced biology Advanced
3 English Level 1 Basic English Beginner
4 Mathematics
Refresher
Advanced Maths Advanced
18. Spotle.ai Study Material
Spotle.ai/Learn
18
Step 5: Model
relationships
between tables
You may need to derive
additional tables to store
the relationships. In our
example, we derived a
student_x_course table to
store which student has
taken what course.
Student Course
Roll_no
Course_id
Student_x_course
20. Spotle.ai Study Material
Spotle.ai/Learn
Suppose a course has multiple instructors. How will
you store the information? You can add another row for
the course but then the primary key is repeated
causing design to break. Also all course details are
repeated unnecessarily.
20
Table: Course
Course_id Course_Name Course_desc Course_level Instructor Name
1 Certificate In
Biology
Basic Biology Beginner Md Rafi
1 Certificate In
Biology
Basic Biology Beginner Kishor Ganguly
2 Certificate In
Biology
Advanced
biology
Advanced Asif Iqbal
3 English Level 1 Basic English Beginner Smriti K
4 Mathematics
Refresher
Advanced
Maths
Advanced Sam Joseph
21. Spotle.ai Study Material
Spotle.ai/Learn
21
Table: Course
Course_id Course_Name Course_desc Course_level
1 Certificate In
Biology
Basic Biology Beginner
2 Certificate In
Biology
Advanced
biology
Advanced
3 English Level 1 Basic English Beginner
4 Mathematics
Refresher
Advanced
Maths
Advanced
Instructor_Id Instructor
_Name
1 Md Rafi
2 K Ganguly
3 Asif Iqbal
4 Smriti K
5 Sam Joseph
Course_x_instructor_
id
Course_id Instructor_id
1 1 1
2 1 2
3 2 3
4 3 4
5 4 5
Normalised design where
the larger course table is
split into a course table
and instructor table. Their
relationship is stored in a
third table: the
course_x_instructor table.
Table: Course_x_Instructor
Table: Instructor
23. Spotle.ai Study Material
Spotle.ai/Learn 23
Constraints are used to ensure
integrity of the data in the
database. They are imposed on
table columns to enforce certain
rules on the data ensuring
integrity, accuracy and reliability.
Constraints
24. Spotle.ai Study Material
Spotle.ai/Learn 24
1. Primary Key
2. Unique
3. Not Null
4. Foreign Key
5. Default Constraint
6. Check Constraint
7. Index
Types Of Constraints
25. Spotle.ai Study Material
Spotle.ai/Learn
25
Primary Key:
A primary key is a table column (or
combination of columns) used to
uniquely identify every record in the
table. A primary key value has to be
unique and not-null for every row in
the database. For example, the
roll_no in the student table.
26. Spotle.ai Study Material
Spotle.ai/Learn
26
Foreign Key:
A foreign key is a column or group
of columns in a table links records
between data in two tables. It
creates a cross-reference between
tables by referencing the
primary key of another table.
27. Spotle.ai Study Material
Spotle.ai/Learn
27
Table: Course
Course_id Course_Name Course_desc Course_level
1 Certificate In
Biology
Basic Biology Beginner
2 Certificate In
Biology
Advanced
biology
Advanced
3 English Level 1 Basic English Beginner
4 Mathematics
Refresher
Advanced
Maths
Advanced
Instructor_Id Instructor
_Name
1 Md Rafi
2 K Ganguly
3 Asif Iqbal
4 Smriti K
5 Sam Joseph
Course_x_instructor_
id
Course_id Instructor_id
1 1 1
2 1 2
3 2 3
4 3 4
5 4 5
The course_id in the
course_x_instructor table is a
foreign key referencing the
course_id in Course table. The
instructor_id in the
course_x_instructor table is a
foreign key references the
instructor_id in the Instructor
table.
Table: Instructor
Table: Course_x_Instructor
28. Spotle.ai Study Material
Spotle.ai/Learn 28
1. Unique – enforces that the column value is
unique for every row.
2. Not Null – enforces that the column value is
not null for any row.
3. Default Constraint – prescribes a default
value of the column which is used to
populate any new row. The default value
can be over-written.
4. Check Constraint – designates a pre-
condition that the column data must adhere
to.
More Constraints Explained
29. Spotle.ai Study Material
Spotle.ai/Learn 29
Indexes are used to retrieve data from the database fast. An index is a pointer to
data in a table that the database search engine uses to speed up select queries.
Indexes are created on columns which are more likely to be used in search
queries or after the where condition in the select statement. For example if you are
likely to look up the Course table based on course_name you will create an index
on name.
Databases automatically create indexes on columns corresponding to primary
keys and unique constraints. Any custom index you need has to be create with the
Create Index command as shown:
Create Index Course_Name on Course;
Note: Indexes speed up select queries but slow down insert and update
statements as any insert or update will need an update of the corresponding index
too.
Indexes