Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Chapter-9Database

The document provides an overview of databases, focusing on single-table databases, data types, and data validation techniques. It explains the structure of a database, the importance of primary keys, and includes examples of SQL queries for data extraction. Additionally, it outlines validation rules and activities related to database management using MS Access.

Uploaded by

samresh36
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chapter-9Database

The document provides an overview of databases, focusing on single-table databases, data types, and data validation techniques. It explains the structure of a database, the importance of primary keys, and includes examples of SQL queries for data extraction. Additionally, it outlines validation rules and activities related to database management using MS Access.

Uploaded by

samresh36
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Chapter-9 Database

IGCSE - CS - 0478
Single-table database
Define Database:

A database is a structured collection of data that allows


people to extract information in a way that meets their
needs. Data can include text, numbers and pictures. For
example, a simple database of items for sale could include a
description, the price and a picture of the item.
Single-table database
A single-table database contains only one table.

A table consists of many records. The number of records in a table will


vary, as new records can be added and deleted from a table as required.

Each record consists of several fields. The number of fields in table


is fixed so each record contains the same number of fields.

An easy way to remember this is:

● each record is a row in the table

● each field is a column in the table.


Basic data types
A data type classifies how the data is stored, displayed and
the operations that can be performed on the stored value.
Each field requires a data type to be selected.

These are the database data types you need to be able to


use. They are available to use as Access data types, but the
names Access uses may be different from the terms in the
syllabus.
What is Data validation
Data validation
● Whenever data is copied manually, there is a chance that mistakes will be made.
This can be for a number of reasons:
● The person doing the copying may misread the original data
● Long codes which have no (human) meaning are especially prone to input error
● A common typing error is a transposition error – where two characters are
accidentally swapped, e.g. age 18 incorrectly entered as 81
● An omission error is when data is omitted or excluded. Data could be lost, damaged
or overlooked prior to entry into the computerised system
● Validation is an automatic computer check to ensure that the data entered is
sensible and reasonable. It does not check the accuracy of data.
Examples
Examples of validation checks:
● Presence check: Ensures that data has been entered into a data field, i.e. it is
not blank
● Length check: Ensures that data entered has the correct number of characters, or
isn't longer than a certain number of characters. For example, a full UK mobile
telephone number is typically 11 digits long
● Type check: Ensures that data entered is of the correct data type, e.g.
text/numbers
● Format check: Ensures that data entered is in a specific format, e.g. dates
written as dd/mm/yyyy
● Range check: Ensures that data entered is within a given range, e.g. a value
between 1 and 100
Validation (recap) of chapter 7
● Range check
● length checks
● type checks
● presence checks
● format checks
● check digits.
Some validation rules that can be set in ms access
Validation Text Validation Rule
<>0 Enter a nonzero value.

>=0 Value must be zero or greater. -or-

You must enter a positive number.


0 or >100 Value must be either 0 or greater than 100.

BETWEEN 0 AND 1 Enter a value with a percent sign. (For use with a
field that stores number values as percentages).
<#01/01/2007# Enter a date before 2007.

>=#01/01/2007# AND <#01/01/2008# Date must occur in 2007.

[EndDate]>=[StartDate] Enter an ending date on or after the start date.


Some validation rules that can be set in ms access
Validation Text Validation Rule
<Date() Birth date cannot be in the future.

StrComp(UCase([LastName]),[LastName],0) = 0 Data in a field named LastName must be


uppercase.
>=Int(Now()) Enter today's date.

M Or F Enter M for male or F for female.

LIKE "[A-Z]*@[A-Z].com" OR "[A-Z]*@[A-Z].net" Enter a valid .com, .net, or .org e-mail address.
OR "[A-Z]*@[A-Z].org"
[RequiredDate]<=[OrderDate]+30 Enter a required date that occurs no more than 30
days after the order date.
Primary keys
Each record within a table contains data about a single
item, person or event.

It is important to be able to uniquely identify each record.

The primary key is a field that uniquely identifies the


record. Each primary key contains a unique value; it must
contain data values that are never repeated in the table.

So, A single database record contains one primary key.


Some keywords in the table-student
Primary Key Field

ID FirstName LastName Age House Hobby

001 Jay Chou 11 Yellow Drawing


Record
002 Jane Watson 10 Red Dancing

003 Will Smith 13 Blue Music

004 MAX Verst 12 Green Drawing

005 Anna Maria 11 Yellow Music

006 Jay Chou 14 Red Music

entity/entry
Activity 9.3 ( in Lab)
Using Access (or any other suitable database management
software), add the HospitalNumber field to the single table
database PATIENT.
» Choose ‘text’ as the data type for this field.
» Include validation checks to ensure that 8 characters
must be entered starting with HN followed by 6 digits for
example HN123456.
» Choose suitable data to store in each primary key field
for the 6 patients in the table.
Activity 9.4
database record data type char
fields table validation Boolean
records primary key Text field

A single .......................... .......................... contains one ......................... . Each .......................... consists of


many .......................... . Every .......................... has the same number of .......................... . Every
.......................... is given a .......................... . Examples of data types are .........................., ..........................
and .......................... . Some .......................... will have .......................... rules.
Activity 9.4 Answers
A single database record contains one primary key. Each table
consists of many records. Every record has the same number of
fields. Every field is given a data type. Examples of data types
are char, Boolean, and text. Some fields will have validation
rules.
What if I want to extract some data from the table-student
ID FirstName LastName Age House Hobby

001 Jay Chou 11 Yellow Drawing

002 Jane Watson 10 Red Dancing

003 Will Smith 13 Blue Music

004 MAX Verst 12 Green Drawing

005 Anna Maria 11 Yellow Music

006 Jay Chou 14 Red Music

How will i do????


All students with Red house from the table-student
ID FirstName LastName Age House Hobby

001 Jay Chou 11 Yellow Drawing

002 Jane Watson 10 Red Dancing

003 Will Smith 13 Blue Music

004 MAX Verst 12 Green Drawing

005 Anna Maria 11 Yellow Music

006 Jay Chou 14 Red Music

Select all the fields from the table student where the field “House” is equal to data “Red”
All students with Hobby music
ID FirstName LastName Age House Hobby

001 Jay Chou 11 Yellow Drawing

002 Jane Watson 10 Red Dancing

003 Will Smith 13 Blue Music

004 MAX Verst 12 Green Drawing

005 Anna Maria 11 Yellow Music

006 Jay Chou 14 Red Music

Select all the fields from the table student where the field “hobby” is equal to data “music”
SQL- Structured Query Language
Structured Query Language (SQL) is the standard query language
for writing scripts to obtain useful information from a database.
SQL Query Statement Description
SELECT Fetches specified fields (columns) from a table; queries always
begin with SELECT.
WHERE Includes only records (rows) in a query that match a given
condition.
FROM Identifies the TABLE to use
ORDER BY Sorts the results from a query by a given column either
alphabetically or numerically.
SUM Returns the sum of all the values in a field (column). Used with
SELECT.
COUNT Counts the number of records (rows) where the field (column)
matches a specified condition. Used with SELECT.
All students with Hobby music FROM TABLE - STUDENT
SELECT FirstName, LastName,
ID FirstNa LastNam Age House Hobby Hobby
me e
FROM STUDENT
001 Jay Chou 11 Yellow Drawing WHERE Hobby = “Music”;
002 Jane Watson 10 Red Dancing
ID FirstName LastName Hobby
003 Will Smith 13 Blue Music

004 MAX Verst 12 Green Drawing 003 Will Smith Music

005 Anna Maria 11 Yellow Music 005 Anna Maria Music

006 Jay Chou 14 Red Music 006 Jay Chou Music


All students with HOUSE “RED” FROM TABLE - STUDENT
SELECT firstname,House
ID FirstName LastNa Age House Hobby
me FROM STUDENT
001 Jay Chou 11 Yellow Drawing
WHERE House = “Red”
002 Jane Watson 10 Red Dancing

003 Will Smith 13 Blue Music


ID FirstN House
004 MAX Verst 12 Green Drawing ame

005 Anna Maria 11 Yellow Music 002 Jane Red

006 Jay Chou 14 Red Music 006 Jay Red


All students in order if first name- STUDENT
SELECT *
ID FirstName LastNa Age House Hobby FROM STUDENT
me
ORDER BY Firstname
001 Jay Chou 11 Yellow Drawing

002 Jane Watson 10 Red Dancing ID First Last Age House Hobby
Name Name
003 Will Smith 13 Blue Music
005 Anna Maria 11 Yellow Music
004 MAX Verst 12 Green Drawing
004 MAX Verst 12 Green Drawing
005 Anna Maria 11 Yellow Music
002 Jane Watson 10 Red Dancing
006 Jay Chou 14 Red Music
001 Jay Chou 11 Yellow Drawing

006 Jay Chou 14 Red Music

003 Will Smith 13 Blue Music


All students in order if first name- STUDENT
SELECT *
ID FirstName LastNa Age House Hobby FROM STUDENT
me
ORDER BY Firstname DESC
001 Jay Chou 11 Yellow Drawing

002 Jane Watson 10 Red Dancing ID First Last Age House Hobby
Name Name
003 Will Smith 13 Blue Music
003 Will Smith 13 Blue Music
004 MAX Verst 12 Green Drawing
004 MAX Verst 12 Green Drawing
005 Anna Maria 11 Yellow Music
001 Jay Chou 11 Yellow Drawing
006 Jay Chou 14 Red Music 006 Jay Chou 14 Red Music

002 Jane Watson 10 Red Dancing

005 Anna Maria 11 Yellow Music


Salesman database
Salesman_ID Name City Commission

5001 James Hoog New York 0.15

5002 Nail Knite Paris 0.13

5003 Pit Alex London 0.11

5006 Mc Lyon Paris 0.14

5007 Paul Adam Rome 0.13

5003 Lauson San Jose 0.12

SELECT COUNT(Salesman_ID) Ans: 6


FROM SALESMAN
Salesman database
Salesman_ID Name City Commission

5001 James Hoog New York 0.15

5002 Nail Knite Paris 0.13

5003 Pit Alex London 0.11

5006 Mc Lyon Paris 0.14

5007 Paul Adam Rome 0.13

5003 Lauson San Jose 0.12

SELECT COUNT(Salesman_ID)
Output: 4
FROM SALESMAN
WHERE Commission >= 0.13
Salesman database
Salesman
_ID
Name City Commission Amount SELECT SUM(Amount)
FROM SALESMAN
5001 James Hoog New York 0.15 5000 WHERE City =”New York”

5002 Nail Knite Paris 0.13 1000

5003 Pit Alex New York 0.11 15000


Output: 20000
5006 Mc Lyon Paris 0.14 7000

5007 Paul Adam Rome 0.13 1500

5003 Lauson San Jose 0.12 1000


Patient Database
HospitalNumber FirstName FamilyName DateofAdmission Consultant WardNumber BedNumber

HN0001 Winnie Sing 12/10/2022 Mr. Smith 6 8

HN0002 Steve Chow 23/20/2022 Miss Abebe 6 3

HN0003 Chin Wee 30/12/2022 Mr Smith 7 8

HN0004 Peter Patel 12/11/2022 Mr. Jones 7 8

HN0005 Sue Sands 19/11/2022 Mr Jones 7 4

HN0006 Farouk Khan 22/11/2022 Mr Jones 7 2

HN0007 Ahmed Teo 22/11/2022 Mr. Jones 7 2


Activity 9.5
1 Using the single table database PATIENT you have created.

a Write an SQL query to list all Mr Jones’ patients.

b Write an SQL query to list all the patients not in ward 6.

c Write an SQL query to list all the patients who arrived on 12/11/2022.

d Write an SQL query to list all the patients who arrived between 12/10/2022 AND
30/10/2022.

2 Write down the output from this SQL query.

SELECT FirstName, FamilyName, BedNumber FROM PATIENT


WHERE WardNumber = 7;
Answers Activity 9.5
1.
a. SELECT * FROM Patient

WHERE Consultant=”Mr. Jones”

b. SELECT * FROM Patient

WHERE ward <>6

c. SELECT * FROM Patient

WHERE DatofAdmission =#12/11/2022#

d. SELECT * FROM Patient

WHERE DateofAdmission between #12/10/2022# AND #30/10/2022#


2.

HospitalNumber FirstName FamilyName DateofAdmission Consultant WardNumber BedNumber

HN0003 Chin Wee 30/12/2022 Mr Smith 7 8

HN0004 Peter Patel 12/11/2022 Mr. Jones 7 8

HN0005 Sue Sands 19/11/2022 Mr Jones 7 4

HN0006 Farouk Khan 22/11/2022 Mr Jones 7 2

HN0007 Ahmed Teo 22/11/2022 Mr. Jones 7 2


Activity 9.6
Activity 9.6
1 Using the single table database PATIENT you have created.
a Write an SQL query to count the number of patients in ward 7.
b Write an SQL query to count the number of patients not in ward 7.
2 Write down the output from this SQL query.
SELECT HospitalNumber, FirstName, FamilyName, Consultant
FROM PATIENT
ORDER BY Consultant, FamilyName;
Answers 9.6
1.

a. SELECT count(First Name) FROM Patient WHERE ward=7

b. SELECT count(First Name) FROM Patient WHERE ward<>7


Activity 9.6 Ans.2
HospitalNumber FirstName FamilyName DateofAdmission Consultant WardNumber BedNumber

HN0002 Steve Chow 23/20/2022 Miss. Abebe 6 3

HN0006 Farouk Khan 22/11/2022 Mr. Jones 7 2

HN0004 Peter Patel 12/11/2022 Mr. Jones 7 8

HN0005 Sue Sands 19/11/2022 Mr. Jones 7 4

HN0007 Ahmed Teo 22/11/2022 Mr. Jones 7 2

HN0001 Winnie Sing 12/10/2022 Mr. Smith 6 8

HN0003 Chin Wee 30/12/2022 Mr. Smith 7 8


Activity 9.7
Ans1. Create Table with Validation checks

CubNumber ShortText Not Null PrimaryKey

CubName ShortText Not Null

Age Int >=7 AND <=11

Six ShorText

Badges Int >=0


9.7 Q. 1.a
Cub_ID Cub_name Age Six Badges
1 John 7 Red 5
2 Jane 10 Blue 3
3 Tom 9 Green 4
4 Lucy 8 Yellow 2
5 Mike 11 Red 6
6 Emily 7 Blue 7
7 Adam 9 Green 1
8 Emma 8 Yellow 2
9 Jake 11 Red 4
10 Sophia 9 Blue 5
Activity 9.7 Q. 1.b
Write an SQL query to pick out any cubs in the red six.

SELECT * FROM CubScouts WHERE Six = 'Red';

Write an SQL query to pick out any cubs in the red six or the blue six.

SELECT * FROM CubScouts WHERE Six = 'Red' OR Six = 'Blue';

Write an SQL query to count the number of cubs in the red six.

SELECT COUNT(*) FROM CubScouts WHERE Six = 'Red';


Activity 9.7 Q.1.c
The cub scout leader wants to calculate the number of badges that all the cubs have been awarded.
Add a new integer field called Badges, enter the number of badges awarded to each cub.
Write an SQL query to count the number of badges awarded to the whole cub group.

SELECT SUM(Badges) AS TotalBadges FROM CubScouts;


Activity 9.8 ( Home Work)
1 A database of students is to be set up with the following fields:
» Family name
» Other names
» Student ID
» Date of Birth
» Date of Entry to School
» Current Class
» Current school year/grade
» Email address.
a Select a data type for each field.
» Family name Short Text
» Other names Short Text
» Student ID Text/Number
» Date of Birth Date/Time
» Date of Entry to School Date/Time
» Current Class Number
» Current school year/grade Int
» Email address. Shortname/
b. Which fields should be validated, and which fields should be verified?
Validation should be done for following fields:
» Date of Birth
» Date of Entry to School
» Current school year/grade
» Email address.
Verification should be done for following fields:
» Student ID
c. Decide the validation rules for those fields which should be validated.
» Date of Birth : Date must be between 5 and 20 : Format : YYYY-MM-DD
» Date of Entry to School : Must be more than DateofBirth Format: YYYY-MM-DD
» Current school year/grade : Must be a positive integer and between 1 and 12
» Email address : Must be in a proper format of email for eg. aaa@aaa.com
d. Which field would you choose for the primary key?
» Student ID ( Unique identifier)

e Choose a suitable format for the student ID.


» Student ID (A combination of letters and numbers, such as "STUD1234567”
f Build a database with at least 10 records; include all your validation checks.

Ensure there are at least 3 different classes and 2 different years/grades.

Student_ID Family Other_ Date_Of_ Date_Of Current_ Current_ Email_Address


_Name Names Birth _Entry Class Grade

STU0000001 Smith John 2010-05-12 2016-09-01 ClassA 5 john.smith@example.com


STU0000002 Doe Jane 2012-07-22 2018-09-01 ClassB 3 jane.doe@example.com
STU0000003 Brown Michael 2011-11-30 2017-09-01 ClassA 4 michael.brown@example.com
STU0000004 Johnson Emily 2009-03-15 2015-09-01 ClassC 6 emily.johnson@example.com
STU0000005 Davis Christopher 2013-01-10 2019-09-01 ClassB 2 chris.davis@example.com
STU0000006 Martinez Laura 2008-08-25 2014-09-01 ClassC 7 laura.martinez@example.com
STU0000007 Wilson David 2010-10-05 2016-09-01 ClassA 5 david.wilson@example.com
STU0000008 Anderson Emma 2011-04-20 2017-09-01 ClassB 4 emma.anderson@example.com
STU0000009 Taylor Sophia 2012-09-09 2018-09-01 ClassC 3 sophia.taylor@example.com
STU0000010 Thomas James 2009-12-12 2015-09-01 ClassA 6 james.thomas@example.com
g Set up and test SQL scripts to:
i Display Other names, Family and Email address in alphabetical order of family name.
SELECT OtherNames, FamilyName, EmailAddress
FROM Students
ORDER BY FamilyName;
ii Select all the students from each class in alphabetical order.
SELECT * FROM Students
ORDER BY CurrentClass, FamilyName, OtherNames;
iii Select all the students for each year/grade and print Other names, Family name and
Date of Birth, grouping the students by class.

SELECT CurrentSchoolYear, CurrentClass, OtherNames, FamilyName, DateOfBirth


FROM Students
ORDER BY CurrentSchoolYear, CurrentClass, FamilyName, OtherNames;

You might also like