Database

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Database

Chapter 9
L.O: Define a single-table database from given
data storage requirements
• Keywords
• Database
• table
• Records (Rows)
• Fields (column)
What is a Database?
✓ A database is a collection of data stored in a logical and ordered manner. Databases
can be both electronic or paper-based
✓ Data is stored in tables consisting of records and fields
✓ A database with a single table is known as a flat-file database. Databases with multiple
tables are known as relational databases
✓ A record is a collection of data about one single item in a database
✓ Each database field must be given a datatype
✓ Tables need a primary key so records are unique and can be identified quickly.
✓ A search for data is known as a query
✓ SQL is a programming language that is used to maintain databases to create queries.
What are Databases used for?
A Database is a structured collection of data that allow information to
be easily extracted based on the conditions
Databases are useful in preventing data problems because data is only
stored once there is no duplication and the stored data is more consistent

Example Database:
• School records – data about students – parents’ occupation, illness, year/grade, etc.
• Exam results – candidates’ exam results
Customer Table: Example for Single table Database (Datasheet View)
Customer_ID CustomerName Address City PostalCode Country
1 Alfred Obere Str. 57 Berlin 12209 Germany

2 Mariam Avda. de la Constitución 2222 México D.F. 05021 Mexico

3 Jana Mataderos 2312 México D.F. 05023 Mexico

4 Mohammed Yassir 120 Hanover Sq. London WA1 1DP UK

5 Yahya Berguvsvägen 8 Luleå S-958 22 Sweden

• Single table Database consists of one or more tables.


• Each table contains many records (Rows). Records are in the same structure, though
their contents are different.
• Each record consists of a number of separate fields (Columns) Records
Design View

Field Type Null Key Extra


CustomerID int(11) NO PRI Auto_increment

CustomerName varchar(255) YES

ContactName varchar(255) YES

Address varchar(255) YES

City varchar(255) YES

PostalCode varchar(255) YES

Country varchar(255) YES


0478/21/O/N/20
Data Validation:
Validation text lets you provide a message to help users who input data that is not valid.
When data is entered, the database checks to see whether the input breaks a validation rule – if so,
the input is not accepted, and Access displays a message

There are actually two boxes that relate to data validation:


➢ Validation Rule box: Used to specify the requirements for data
entered in the field.
➢ Validation Text box: Used to specify the message that will be
displayed to the user when data that violates the validation
rule is entered.
L.O: Understand the purpose of a primary key and identify a suitable primary key for a given
database table

A primary key is a column or a group of columns in a table that uniquely identifies the rows of data in
that table.
For example, in the table below, CustomerID, which displays the ID number assigned to different
customers, is the primary key

Customer_ID CustomerName Address City PostalCode Country


1 Alfred Obere Str. 57 Berlin 12209 Germany

2 Mariam Avda. de la Constitución 2222 México D.F. 05021 Mexico

3 Jana Mataderos 2312 México D.F. 05023 Mexico

4 Mohammed 120 Hanover Sq. London WA1 1DP UK


Yassir
5 Yahya Berguvsvägen 8 Luleå S-958 22 Sweden
How to identify a primary key of a table?
The primary key is the column whose values are different in every row. Because they are
different, they make each row unique.

What are the 3 characteristics of a primary key?


A primary key's main features are:
✓ It must contain a unique value for each row of data.
✓ It cannot contain null values.
✓ Every row must have a primary key value.

Which of the following fields is NOT suitable for a primary key in a database?
A. An automatic number field
B. A student’s registration number
C. A Bank account number
D. Date of Birth
2018 May/ June paper 2
Question 5
Types of Data in the Table:

1. text/alphanumeric: Any combination of letters, symbols, and/ or


numbers
E.g.: “Mohammed”, “9E190”, “2190”, “Hi!”
2. Character: A single letter or symbol, or number.
E.g.: “H”, “2”, “?”
3. Boolean: Boolean (true/false) data; one of two options usually. True/
False or Yes/ No.
4. Integer: A whole number
E.g.: 122, 90, -928
5. Real: A number with at least one decimal place.
E.g.: 90.34, 12.0
5. date/time: date and time or only date
Eg: 12/10/2022 8:30 12/10/2022 08:30
L.O: Read, understand and complete the structured query language (SQL) scripts to query data stored in a
single database table

– SELECT
– FROM
– WHERE
– ORDER BY
– SUM
– COUNT
• Identifying the output given by an SQL statement that will query the given contents of a database table
SELECT Statement
• The SELECT statement is used to select data from a database.
• The data returned is stored in a result table, called the result set.
SELECT Syntax

SELECT column1, column2, ...FROM table_name;


Here, column1, column2, ... are the field names of the table you want to select data from. If
you want to select all the fields available in the table, use the following syntax:
SELECT * FROM table_name;

For example;
SELECT CustomerName, City, Country FROM Customers;

CustomerName City Country


Alfred Berlin Germany
Mariam México D.F. Mexico
Jana México D.F. Mexico
SELECT… FROM… WHERE
The WHERE clause is used to filter records.

It is used to extract only those records that fulfill a specified condition.

WHERE Syntax:

The following operators can be used in the WHERE clause:

Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=
SELECT… FROM… WHERE- Examples with relational operators
Produ ProductName Supp Catego Price
SELECT * FROM Products WHERE Price = 18; ctID lierID ryID

SELECT * FROM Products WHERE Price > 30; 1 Chais 1 1 18


2 Chang 1 1 19
SELECT * FROM Products WHERE Price > =30;
3 Aniseed Syrup 1 2 10
4 Chef Anton's Cajun 2 2 22
SELECT * FROM Products WHERE Price < 30; Seasoning
5 Chef Anton's Gumbo Mix 2 2 21.35
SELECT * FROM Products WHERE Price < =30;
6 Grandma's Boysenberry 3 2 25
Spread
SELECT * FROM Products WHERE Price <> 18;
7 Uncle Bob's Organic Dried 3 7 30
Pears
8 Northwoods Cranberry Sauce 3 2 40

9 Mishi Kobe Niku 4 6 97


10 Ikura 4 8 31
2018 May/ June paper 2
Question 5
2018 May/ June paper 2
Question 5
• Incorrect field name for Reference Number
• Incorrect criteria for Price in $ should be
• Type not checked
(b) (i) Give the name of the field that should be used for the primary key.
.......................................................................................................................................[1]
(ii) State the reason for choosing this field for the primary key
20

CatNo

It is a unique identifier

18m02 Golfwatch Yes No Yes


18m03 Chair27 Yes Yes No
2022/0478_s22_qp_23
2022/0478_s22_qp_23
2022/0478_s22_qp_22
2022/0478_s22_qp_22
2022/0478_s22_qp_21
2022/0478_s22_qp_21
• field, FlowerID, not required / should not be
displayed
• Type field not included and displayed
• Fragrance field should not be displayed
• Fragrance criteria should not be Y / should be N
(b) State the number of records in the table GENRE.
More than one condition in the “Where” clause
The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one condition:

The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

Syntax
SELECT column1, column2, ... FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT * FROM Customers WHERE Country = 'Germany' AND City = 'Berlin';

SELECT * FROM Customers WHERE City = 'Berlin' OR City = 'Stuttgart';

SELECT * FROM Customers WHERE NOT Country = 'Germany';

You might also like