Databases and SQL Basics | Knowledge Hub Databases and SQL Basics | Knowledge Hub
Computer Studies

Databases and SQL Basics

Databases and SQL Basics

What is a database?

A database is an organised collection of related data that can be stored, searched and updated easily. Examples in Kenya include the KNEC candidate registry, M-Pesa transaction records and school admissions lists. A Database Management System (DBMS) such as MySQL, PostgreSQL or Microsoft Access manages the data.

Structure

  • Table: data arranged in rows and columns about one subject (e.g. students).
  • Field (column): one attribute, e.g. name.
  • Record (row): all data about one item.
  • Data types: text, number, date, Boolean.
id name class marks
1 Wanjiku 11A 78
2 Otieno 11B 65
3 Fatuma 11A 91

Keys and relationships

  • Primary key: uniquely identifies each record (id).
  • Foreign key: a field linking to another table's primary key.
  • Relationships: one-to-one, one-to-many (one class has many students), many-to-many.

Benefits of using keys: avoids duplication, keeps data consistent (integrity) and speeds searches.

SQL (Structured Query Language)

SQL is the standard language for relational databases. Commands end with a semicolon.

SELECT

SELECT name, marks FROM students; shows two columns. SELECT * FROM students; shows all columns.

WHERE and ORDER BY

SELECT name FROM students WHERE marks >= 70; SELECT * FROM students ORDER BY marks DESC;

INSERT

INSERT INTO students (id, name, class, marks) VALUES (4, 'Kamau', '11B', 55);

UPDATE

UPDATE students SET marks = 60 WHERE id = 4;

DELETE

DELETE FROM students WHERE id = 4;

Always include WHERE in UPDATE and DELETE. Without it every record changes.

Worked example 1

Show the names of Class 11A students sorted by marks, highest first. SELECT name FROM students WHERE class = '11A' ORDER BY marks DESC; Result: Fatuma, Wanjiku.

Worked example 2: aggregates

Find the average mark: SELECT AVG(marks) FROM students; For the table above: (78 + 65 + 91) / 3 = 78. Other functions: COUNT(), SUM(), MAX(), MIN().

Data security and ethics

  • Use passwords and user roles.
  • Back up regularly.
  • Follow the Kenya Data Protection Act, 2019: collect only necessary personal data and protect it.

Try it

  • Design a table for a school library with at least five fields and choose a primary key.
  • Write SQL to show students with marks between 50 and 80.
  • Write SQL to count students in class 11A.
  • Explain why a foreign key is needed between students and classes tables.
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 called?
  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. Which statement lists students in Form with class = '11A'?
  1. SELECT * FROM students WHERE class = '11A';
  2. GET students WHERE class = '11A';
  3. DELETE students WHERE class = '11A';
  4. SHOW class FROM students = '11A';
Answer: Option 1
4. A foreign key is used to?
  1. Encrypt data
  2. Link a table to the primary key of another table
  3. Delete duplicates
  4. Back up a database
Answer: Option 2

Flashcards

Front
Define a database and a DBMS.
Back
A database is an organised collection of related data. A DBMS (e.g. MySQL, MS Access) is software used to create and manage it.
Front
Differentiate between a record and a field.
Back
A field is a single column (e.g. Name). A record is a complete row of related fields about one item.
Front
Write SQL to remove a student with id 5.
Back
DELETE FROM students WHERE id = 5;

Practice Problems

Write SQL queries
intermediate

A table students(id, name, class, marks) holds data for a school. Write SQL to (a) display names of students with marks above 70, (b) change the class of student id 3 to '11B', (c) add a new student.

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: 3
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?