0% found this document useful (0 votes)
19 views5 pages

Isc 305 Assignment Explained

Creating a database

Uploaded by

annmell921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Isc 305 Assignment Explained

Creating a database

Uploaded by

annmell921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Step-by-Step Guide to Understanding and Creating Databases(ISC 305 CAT)

1. Create the Database


Example SQL:
CREATE DATABASE Training;

2. Select the Database


Example SQL:
USE Training;

This sets the active database.

3. Create Tables
1. Design the Table Structure:
Example: For the Students table:

Columns: RegNo (Primary Key), StudName, DateOfBirth, DegreeCourse.

2. Write the SQL Code:


Example:

CREATE TABLE Students (


RegNo VARCHAR(15) PRIMARY KEY,
StudName VARCHAR(100) NOT NULL,
DateOfBirth DATE NOT NULL,
DegreeCourse VARCHAR(100) NOT NULL
);

3. Create Other Tables:


Repeat this for other entities like Courses and Registration.

-- Courses table
CREATE TABLE Courses (
CourseCode VARCHAR(10) PRIMARY KEY,
Title VARCHAR(100) NOT NULL,
Dept VARCHAR(50) NOT NULL
);

-- Registration table
CREATE TABLE Registration (
CourseCode VARCHAR(10),
RegNo VARCHAR(15),
TotalMark INT,
PRIMARY KEY (CourseCode, RegNo),
FOREIGN KEY (CourseCode) REFERENCES Courses(CourseCode),
FOREIGN KEY (RegNo) REFERENCES Students(RegNo)
);
---

Step 5: Populate the Tables

Insert data into each table.

Example for the Students table:

INSERT INTO Students (RegNo, StudName, DateOfBirth, DegreeCourse) VALUES


('J12/1234/2001', 'Mary Atieno', '1990-01-01', 'BSc Comp Science'),
('J25/9999/2001', 'Peter Kamau', '1989-04-07', 'BSc Maths');
(CONTINUE TO THE END)

-- Insert data into Courses table


INSERT INTO Courses (CourseCode, Title, Dept) VALUES
('SCH301', 'Physical Chemistry', 'Chemistry'),
('SCH302', 'Inorganic Chemistry', 'Chemistry'),
('SCH303', 'Thermodynamics', 'Chemistry'),
('SCO206', 'Database Systems', 'CIT'),
('SCO207', 'Computer Programming', 'CIT'),
('SMA100', 'Basic Maths', 'Mathematics'),
('SMA200', 'Linear Algebra I', 'Mathematics');
(CONTINUE TO THE END)

-- Insert data into Registration table


INSERT INTO Registration (CourseCode, RegNo, TotalMark) VALUES
('SCH301', 'P21/4564/2001', 62),
('SCH301', 'P21/4565/2001', 71),
('SCH302', 'P21/4564/2001', 41),
('SCH302', 'P21/4565/2001', 72),
(CONTINUE TO THE END)

---

Step 6: Write SQL Queries

1. Understand What You Need:


Example: Retrieve students registered for a specific course.

2. Query Examples:

Retrieve names of students in "Database Systems":


SELECT DISTINCT Students.StudName
FROM Students
JOIN Registration ON Students.RegNo = Registration.RegNo
JOIN Courses ON Registration.CourseCode = Courses.CourseCode
WHERE Courses.Title = 'Database Systems';

Explanation
SELECT DISTINCT Students.StudName: This part of the query selects the StudName
column from the Students table. The DISTINCT keyword ensures that the result only
includes unique student names (i.e., no duplicates).

FROM Students: Indicates that the main table we are selecting data from is the Students
table.

JOIN Registration ON Students.RegNo = Registration.RegNo: This JOIN clause connects


the Students table to the Registration table based on the RegNo column, which acts as a
primary key in Students and a foreign key in Registration.

JOIN Courses ON Registration.CourseCode = Courses.CourseCode: This JOIN connects


the Registration table to the Courses table based on the CourseCode. It allows access to the
course details associated with each registration.

WHERE Courses.Title = 'Database Systems': The WHERE clause filters the results to only
include students who have registered for the course titled 'Database Systems'.

What It Does:
This query retrieves the names of all students who have enrolled in the course called
'Database Systems'. The use of DISTINCT ensures that each student’s name appears only
once, even if they are registered in multiple courses or have multiple records in the
Registration table.

Count registrations per course:

SELECT Courses.Title, COUNT(Registration.RegNo)


AS NumberOfStudents
FROM Courses
JOIN Registration ON Courses.CourseCode = Registration.CourseCode
GROUP BY Courses.Title
ORDER BY NumberOfStudents DESC;

Explanation
SELECT Courses.Title, COUNT(Registration.RegNo) AS NumberOfStudents: This selects
the Title column from the Courses table and counts the number of registration records
(Registration.RegNo) for each course. The result is given an alias NumberOfStudents for
better readability.
FROM Courses: The main table for the query is Courses.

JOIN Registration ON Courses.CourseCode = Registration.CourseCode: This JOIN links the


Courses table to the Registration table based on the CourseCode column, allowing us to
count the registrations for each course.

GROUP BY Courses.Title: This clause groups the data by the Title of each course, so the
count is calculated per course.

ORDER BY NumberOfStudents DESC: This orders the result in descending order based on
the number of students enrolled in each course, showing courses with the highest number of
registrations first.

What It Does:
This query counts and displays the number of students enrolled in each course and orders
the results from the highest to the lowest number of students.

Calculate average marks for each student:


SELECT Students.StudName,
AVG(Registration.TotalMark) AS AverageMark
FROM Students
JOIN Registration ON Students.RegNo = Registration.RegNo
GROUP BY Students.StudName;

Explanation
SELECT Students.StudName, AVG(Registration.TotalMark) AS AverageMark: This part of
the query selects the StudName from the Students table and calculates the average of the
TotalMark column from the Registration table. The result is labeled as AverageMark.

FROM Students: The main table being queried is Students.

JOIN Registration ON Students.RegNo = Registration.RegNo: This JOIN clause links the


Students table to the Registration table, so we can access the marks that each student has
received.

GROUP BY Students.StudName: This groups the results by StudName, ensuring that the
average mark is calculated for each individual student.

What It Does:
This query calculates and displays the average marks for each student based on their
registrations. The result includes the student’s name and their average score across all
registered courses.

You might also like