Unit 9
Unit 9
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.
Inside a database, data is stored in tables, which consists of many records. Each record consists of several fields.
An easy way to remember this is: each record is a row in the table and each field is a column in the table.
A data type classifies how the data is stored, displayed and the operations that can be performed on the stored
value. For example, a field with an integer data type is stored and displayed as a whole number and the value stored
can be used in calculations.
There are six basic data types that you need to be able to use in a database:
» text/alphanumeric
» character
» Boolean
» integer
» real
» date/time.
Primary key
A Primary key is used to make sure that the given column data should be uniquely entered. Each record in the table
consists of different columns and the columns which helps us to identify unique information such as Aadhar or PAN
should be given as primary keys.
(A field that is a primary key must contain data values that are never repeated in the table.)
SQL
Structured Query Language (SQL) is the standard query language for writing scripts to obtain useful information from
a database.
You will need to be able to understand and identify the output from the following SQL statements.
SQL Commands:
Create:- Used to create a database/table
Insert: Used to insert values into the existing tables
Select: Used to extract/display the data as per the user requirement
Examples:
1) CREATE DATABASE IGCSE
2) CREATE TABLE CUB( CubName VARCHAR(30), DateOfBirth DATE, Address VARCHAR(40),School
VARCHAR(30), DateJoined DATE);
SELECT * – this specifies that all fields (columns) are to be shown.
FROM TableName – this specifies the table to use. A WHERE statement takes the form:
WHERE Condition – this specifies the condition to apply
Conditions also require operators to compare values from fields.
An ORDER BY statement takes the form: ORDER BY Field1, Field2, etc. – this specifies a sort in ascending or
alphabetical order starting with the first field.
ORDER BY Field1, Field2 DESC – this specifies a sort in descending or reverse alphabetical order starting with the first
field.
A SUM statement takes the form: SELECT SUM (Field) – this specifies the field (column) for the calculation. The field
should be integer or real.
A COUNT statement takes the form: SELECT COUNT (Field) – this specifies the field (column) to count if the given
criterium is met.
Example: SELECT HospitalNumber, FirstName, FamilyName FROM PATIENT WHERE Consultant = 'Mr Smith';