9.
1 Database theory
1
Database
• A DATABASE is a structured collection of data that allows people to extract information in a way that
meets their needs.
• The data can include text, numbers, pictures: anything that can be stored in a computer.
• Database management system (DBMS) is a software which is used to manage (store, retrieve, query)
the database. Eg. MS Access, MySQL, Oracle
2
Single-table databases
• Databases store information in the form of a table.
• A table contains data about one type of item or person or event.
• e.g. a school database could have one table for students and another for parents.
• A single-table database contains only one table.
3
Single-table databases
• Tables consists of many records.
• Each record consists of several fields.
• Each record is a row in the table and each field is a column in the table.
4
Examples – fields and records
A table contains data about one type of item or person or event, and will be given a meaningful name,
5
Data Types
• A datatype is a format that can be given to data in a field.
• A data type classifies how the data is stored, displayed and the operations that can be performed on the
stored value.
6
Basic Data types
7
Question
Write down four database data types. Describe each data type and give an example of a field where it
would be a suitable choice. Design a table of your choice to use the data types.
8
Answer
9
Primary key
• A single column/field in a table that uniquely identifies each record in a database table.
• A table can have only one PRIMARY KEY, which may consist of single or multiple fields.
• Eg. telephone numbers, Employee Id, Student Id, Car number plate, National insurance number.
• A field that is a primary key must contain data values that are never repeated in the table.
10
Validation
Validation should be used on each field to ensure that data is entered correctly and in required format.
11
Example - validation check
12
Question
Design a single table database to store the PATIENT records using the fields given. FirstName, Family Name, Date Of Admission,
Consultant, Ward Number, Bed Number.
• Choose a suitable data type for each field.
• Suggest appropriate validation checks to show that the ward number has whole number values from 1 to 10, and that the bed number
has whole number values from 1 to 8.
• Enter the following sample data in the PATIENT table: Winnie Sing, 12/10/2022, Mr Smith, 6, 8
Steve Chow, 23/10/2022, Miss Abebe, 6, 3
Chin Wee, 30/10/2022, Mr Jones, 7, 1
Min Hoo, 1/11/2022, Mr Smith, 6, 1
Peter Patel, 12/11/2022, Mr Jones, 7, 8
Sue Sands, 19/11/2022, Miss Abebe, 6, 2
Farouk Khan, 22/11/2022, Mr Jones, 7, 4
Ahmad Teo 22/11/2022, Mr Jones, 7, 2
13
Answer
14
9.2 SQL
15
SQL
❖ Structured Query Language (SQL) is the standard query language for writing scripts to obtain useful
information from a database.
16
SQL SCRIPT
❖ An SQL script is a list of SQL commands that perform a given task
17
Real Life Application – Role Play
18
SYNTAX
19
SQL SCRIPT
20
Question
Research on arranging records in ascending or descending order and write SQL queries to do the
following.
1. Display all records in ascending order of Age_in_Years
2. Display the type and position of map of trees which are at risk, and more than 100 years old in
descending order of Age_in_Years.
21
Answer
22