Chapter 3
Chapter 3
3.1
Start by identifying the main entities (data objects) you want to store information about. In
this case, the entities are:
Student: Represents an individual student in your class.
Attendance: Represents a record of a student's presence or absence in a specific class.
Grade: Represents a student's score for an assignment, quiz, or exam.
For each entity, list the attributes (data points) you want to capture.
Student: Student ID, First Name, Last Name, Email Address, Registration Number.
Attendance: Attendance ID (unique identifier), Student ID (foreign key referencing Student
table), Date of Class, Attendance Status (Present, Absent, Excused).
Grade: Grade ID (unique identifier), Student ID (foreign key referencing Student table),
Assignment/Exam Name, Grade Value (numerical score or letter grade).
Identify how the entities relate to each other. This is crucial for efficient data retrieval and
manipulation. In this case:
One-to-Many Relationship:
One student has many attendance records (One Student - Many Attendance)
One student has many grades (One Student - Many Grades)
Foreign Keys: Implement foreign keys to enforce relationships. A foreign key in a table
references the primary key of another table. For example:
The Attendance table will have a "Student ID" field that references the primary key ("Student
ID") of the Student table.
Normalize the database to reduce data redundancy and improve data integrity. This involves
eliminating duplicate data and organizing tables to minimize dependencies. There are
different normalization levels (1NF, 2NF, 3NF) with increasing complexity. For your project,
aiming for 2nd Normal Form (2NF) is a good starting point.
Choose appropriate data types for each attribute based on the kind of data being stored.
Examples:
Student ID: Integer
First Name, Last Name: Text
Email Address: Text
Date of Class: Date
Attendance Status: Text (with limited options like "Present", "Absent", "Excused")
Grade Value: Integer (for numerical scores) or Text (for letter grades)
Document your database design clearly. This includes:
Entity-Relationship Diagram (ERD) depicting the relationships between tables.
Table structure details, listing attributes and their data types.
Foreign key constraints and any additional constraints applied to the data.
FLOWCHART.
A[Start] --> B{Identify Entities (Student, Attendance, Grade)}
B --> C{Define Attributes for each Entity}
C --> D{Identify Relationships between Entities}
D --> E{One-to-Many (Student - Attendance, Student - Grade)}
E --> F{Design Document Schemas (Student, Attendance, Grade)}
F --> G{Consider Embedded Documents
G --> H{Choose: Separate Collections or Embedded (based on Scalability needs)}
H --> I{Define Indexes (Student ID, Attendance Date etc.)}
I --> J{Implement Data Validation Rules}
J --> K{Test with Sample Data & Optimize Queries}
K {End} .
CODE SNIPPET.
STORING STUDENT PROFILES.
Click the "New Database" button and name it "student_database"
Within "student_database", click on "Collections" and then the "+" button to create a new
collection.
Click on the "Schema" tab and define the document structure
{
"_id": ObjectId,
"studentId": string,
"firstName": string,
"lastName": string,
"email": string,
"dateOfBirth": ISODate,
"graduationYear": int,
"coursesEnrolled": [string],
"contactInformation": {
"phone": string,
"address": {
"street": string,
"city": string,
"state": string,
"postalCode": string
}
}
}
Inserting Data using PHP:
<?php
require 'vendor/autoload.php'; // Assuming you have composer and the MongoDB PHP
driver installed
$studentData = [
"studentId" => "2024-001",
"firstName" => "Paul",
"lastName" => "Phineas",
"email" => "[email protected]",
"dateOfBirth" => new DateTime("2001-03-12"), // Convert to ISODate if needed
"graduationYear" => 2026,
"coursesEnrolled" => [“Bachelor of Science in Telecommunications and Information
Engineering"],
"contactInformation" => [
"phone" => "+254114921041",
"address" => [
"city" => "Nyeri",
"country" => "Kenya",
"postalCode" => "10100"
]
]
];
$insertResult = $collection->insertOne($studentData);
?>
Courses Collection
{
"_id": ObjectId("..."),
"unit_id": "ETI5101",
"unit_name": "Antennas",
"lecturer": "Dr. Manene"
}
attendance_records Collection:
{
"_id": ObjectId("..."),
"student_id": "E020-01-0914/2020",
"unit_id": "ETI5101",
"date": ISODate("2024-05-01T00:00:00Z"),
"status": "Present"
}
Connect to MongoDB:
<?php
require 'vendor/autoload.php'; // include Composer's autoloader
// Collections
$studentsCollection = $database->students;
$unitsCollection = $database->units;
$attendanceCollection = $database->attendance_records;
?>
Insert a Student:
$newStudent = [
'student_id' => 'E020-01-0914/2020',
'first_name' => 'Paul',
'last_name' => 'Phineas',
'email' => '[email protected]',
'units' => ['ETI5101', 'ETI4208']
];
$studentsCollection->insertOne($newStudent);
Insert a Unit:
$newunit = [
'unit_id' => 'ETI4208',
'course_name' => 'Digital Communication',
'lecturer' => 'Dr. Waweru'
];
$coursesCollection->insertOne($newCourse);
Insert an Attendance Record:
$newAttendance = [
'student_id' => 'E020-01-0914/2020',
'unit_id' => 'ETI4208',
'date' => new MongoDB\BSON\UTCDateTime(new DateTime('2024-05-01')),
'status' => 'Absent'
];
$attendanceCollection->insertOne($newAttendance);
Find a Student by ID:
$student = $studentsCollection->findOne(['student_id' => 'E020-01-0914/2020']);
print_r($student);
Find Attendance Records for a Student:
$attendanceRecords = $attendanceCollection->find(['student_id' => 'E020-01-0914/2020']);
foreach ($attendanceRecords as $record) {
print_r($record);
}
Update a Student's Email:
$studentsCollection->updateOne(
['student_id' => 'E020-01-0914/2020'],
['$set' => ['email' => '[email protected]']]
);
Delete an Attendance Record:
$attendanceCollection->deleteOne(['student_id' => 'E020-01-0914/2020', ‘unit_id' =>
'ETI4208']);