Database Design and SQL | Knowledge Hub Database Design and SQL | Knowledge Hub
Computer Studies

Database Design and SQL

Database Design and SQL

What is a database?

A database is an organised collection of related data stored so it can be accessed, managed and updated easily. A DBMS (database management system) such as MySQL or MS Access controls it. Examples in Kenya: KCSE results, M-Pesa transaction logs, school fee records, county land registries.

Terms

  • Table: data arranged in rows and columns.
  • Record (row): one entry, e.g. one student.
  • Field (column): one attribute, e.g. name.
  • Primary key: uniquely identifies each record (admission number).
  • Foreign key: a field linking to a primary key in another table.

Relationships

  • One-to-one: one learner has one NEMIS UPI number.
  • One-to-many: one class has many students.
  • Many-to-many: students take many subjects and each subject has many students (solved with a link table).

Designing a database

  1. Identify the purpose.
  2. List entities (students, subjects, teachers).
  3. Define fields and data types (text, integer, date).
  4. Choose primary keys.
  5. Set relationships.
  6. Apply normalisation.
  7. Test with sample data.

Normalisation

  • 1NF: each field holds one value; no repeating groups.
  • 2NF: 1NF and every non-key field depends on the whole primary key.
  • 3NF: 2NF and no non-key field depends on another non-key field.

Benefits: less redundancy, easier updates, better integrity.

SQL basics

SQL (Structured Query Language) has:

  • DDL: CREATE, ALTER, DROP
  • DML: INSERT, UPDATE, DELETE, SELECT

Create a table:

CREATE TABLE students (
  admno INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(5),
  marks INT
);

Insert:

INSERT INTO students VALUES (101, 'Achieng', '12A', 78);

Select with condition:

SELECT name, marks FROM students WHERE marks >= 50 ORDER BY marks DESC;

Update and delete:

UPDATE students SET marks = 80 WHERE admno = 101;
DELETE FROM students WHERE admno = 105;

Aggregate functions

COUNT, SUM, AVG, MIN, MAX with GROUP BY:

SELECT class, COUNT(*) FROM students GROUP BY class;

Joins

If marks are in a separate table:

SELECT students.name, results.score
FROM students JOIN results ON students.admno = results.admno;

Data integrity and security

Use validation rules, backups, user permissions and passwords. The Data Protection Act, 2019 requires careful handling of personal data, including learners' details.

Worked example

Count students scoring 60 or more in each class: SELECT class, COUNT(*) FROM students WHERE marks >= 60 GROUP BY class;

Try it

  • Design two tables for a school library and link them with a foreign key.
  • Write a query listing all students in class 12B.
  • Normalise a table that stores student name, subject1, subject2 and subject3 in one row.
Was this helpful?

Get new Computer Studies notes by email

Join thousands of Kenyan students. We'll send fresh Computer Studies notes, past papers and revision tips - free, no spam.

By subscribing you agree to our Privacy Policy.

Quick Quiz

1. A field that uniquely identifies each record in a table is the?
  1. Foreign key
  2. Primary key
  3. Index card
  4. Query
Answer: Option 2
2. Which SQL command retrieves data from a table?
  1. INSERT
  2. UPDATE
  3. SELECT
  4. DROP
Answer: Option 3
3. A foreign key is used to?
  1. Encrypt data
  2. Link two tables
  3. Delete duplicates
  4. Sort records
Answer: Option 2
4. Normalisation mainly aims to?
  1. Reduce data redundancy
  2. Increase file size
  3. Slow queries
  4. Remove all keys
Answer: Option 1

Flashcards

Front
What is a DBMS? Give two examples.
Back
Database management system, software to create and manage databases. Examples: MySQL, MS Access, PostgreSQL.
Front
Write the SQL to show all students whose class is 12A.
Back
SELECT * FROM students WHERE class = '12A';

Practice Problems

Write SQL queries
intermediate

Table students(admno, name, class, marks). Write SQL to (a) list names of students with marks above 70, sorted highest first, and (b) find the average marks per class.

Comments

Log in to leave a comment.

No comments yet. Be the first to comment!

Join free to unlock more

Bookmark articles, download past papers, track revision and get AI study help - free for Kenyan students.

Save & bookmark notes Download past papers Track revision progress AI study help
Create free account

Already have one? Log in

Related Articles
Learning Tools
MCQs: 4
Flashcards: 2
Practice Problems: 1
Browse formula sheets
Study Assistant

Instant help with course questions

Hi there! I'm your YnetStudyHub assistant. How can I help with your studies today?