CIE IGCSE Computer Science Revision Notes IGCSEComputer ScienceCIERevision Notes9. Databases9.1 Database TheoryDatabases Databases Download PDF Test Yourself Single Table Databases • • • A database is a structured collection of data so it can be searched, sorted, filtered and analysed quickly o Data in a database can be any type of data including text, images, videos, sound Databases use tables to store data Tables have records of data represented by one row o In the example below, each row represents the data stored about a single customer (the customer’s record) o In the customer table there are 3 records o Each record is divided into fields (CustomerID, FirstName, LastName, DOB and Phone Number) A Database Table Containing Customer Details CustomerID FirstName LastName • DOB PhoneNumber 1 Andrea Bycroft 05031976 0746762883 2 Melissa Langler 22012001 0756372892 3 Amy George 22111988 0746372821 Fields are represented by the columns in a table o There are 5 fields in the customer table o The first row in a table contains the field names which is the heading for the data stored in that field o Each field in a table has a data type which defines what data can be entered into that field Worked example A Database Table Containing Pet Details DogID Name Breed Age Gender DG12 Smokey Poodle 12 M DG34 Harvey Springer Spaniel 10 M DG48 Maisie Labradoodle 3 F DG49 Maggie Labradoodle 3 F DG88 Winston Bulldog 7 M DG95 Shona Golden Retriever 6 F How many fields are there in the Dogs table shown above? [1] 5 [1] How many records are there in the Dogs table? [1] 6 [1] Exam Tip • It is very likely you will be presented with an example database table and identify either how many fields there are or how many records there are so make sure you remember a record is row and a field is a column Validation and Verification • • Verification checks check whether the data that has been entered is the correct data and is accurate o This is often completed by getting data entered by one person is then checked by another person When a table is created, validation rules can be assigned to the different fields o A validation rule controls what data can be entered into that field o There are different types of validation checks used to limit what data can be entered into each field The different types of validation check Type Description Length Check This type of validation checks the number of characters that have been entered into a field. For example, you might make phone numbers entered have to be eleven characters long Format This type of validation checks data entered meets an exact format. For example, a Check product code might have to be two letters followed by five numbers Range Check A range check will check the number entered is within a specific range. For example, the age of a dog would be between 0 - 40. Any other number would be disallowed Presence Check A presence check can be added to fields which cannot be left blank Type Check A type check will allow data with a specific data type to be entered into a field. For example, if text was entered into a field which is supposed to contain the date of birth of a person it would not be allowed Check digit validation is a process used to verify the accuracy of numbers such as credit card numbers. A check digit is a single digit added to the end of the number, Check which is calculated based on a specific algorithm applied to the other digits in the Digits number. When the data is re-entered the same algorithm can be applied and if it produces a different result the code is incorrect Worked example A Database Table Containing Student Grades StudentID FirstName LastName MarkSubmitted 1483792 1498378 1500121 1382972 1598264 1548282 Shanay Poppy Diya Joe Anton Felicity Giles Petit Dinesh Swaile Smith Hall Y N Y Y Y N Percentage 55 20 74 68 34 47 Describe two validation checks that could be used to check data being inputted into the table above [4] StudentID could have a length check to ensure 7 characters are entered [2] FirstName and LastName could have a presence check to make a record cannot be entered without entering the name of the student [2] A type check of boolean could be applied to the Mark Submitted field so that only Y or N are entered [2] A range check could be assigned to the Mark column to ensure only numbers between 0 and 100 are entered [2] Data Types • • Table fields are represented by columns in a table There are 5 fields in the customer table below o These include CustomerID, FirstName, LastName, DOB and PhoneNumber A Database Table Containing Customer Details CustomerID FirstName LastName 1 2 3 • Andrea Melissa Amy Bycroft Langler George DOB PhoneNumber 05031976 22012001 22111988 0746762883 0756372892 0746372821 Each field in a table, has a data type o Common data types include text/alphanumeric, character, boolean, integer, real and date/time o Phone numbers have to be assigned the text/alphanumeric data type because they begin with a 0 ▪ If you assigned the data type Integer to a phone number it would remove the initial 0 Database Data Types Data Type Text/Alphanumeric Character Boolean Integer Real Explanation Example This data type allows letters, special characters like spaces and NG321AE punctuation and numbers to be entered into a field This allows single characters to be entered into a field. Characters can be any alphanumeric value and can be lowercase A or uppercase This data type can be used in fields where there are only two possible options. Data is stored as a 1 or 0 in the database but True/False can be used to represent True/False or Yes/No Only whole numbers can be entered 15 Numbers including decimal numbers can be stored 30.99 Date/Time Only dates or times can be entered into a field with this type. A format for the date/time can also be assigned to the field 180855 Worked example A Database Table Containing Dog Details DogID Name Breed Age Gender DG12 Smokey Poodle 12 M DG34 Harvey Springer Spaniel 10 M DG48 Maisie Labradoodle 3 F DG49 Maggie Labradoodle 3 F Bulldog 7 M Golden Retriever 6 F DG88 Winston DG95 Shona What data type should Age be? [1] Integer [1] What data type should DogID be? [1] Text/Alphanumeric [1] CIE IGCSE Computer Science Revision Notes IGCSEComputer ScienceCIERevision Notes9. Databases9.1 Database TheoryPrimary Keys Primary Keys Primary Keys • Each table has a primary key field which acts as a unique identifier o Each item of data in this field is unique Duplicate data items would be blocked if they were entered into the primary key field Because the items of data are unique within the primary key field they can be used to identify individual records o • A Database Table Containing Customer Details CustomerID FirstName LastName • • DOB PhoneNumber 1 Andrea Bycroft 05031976 0746762883 2 Melissa Langler 22012001 0756372892 3 Amy George 22111988 0746372821 In the example customer table, the primary key would be the CustomerID because each customer’s ID is unique If there was a customer with the same name they could be identified correctly using the CustomerID Worked example A database table containing the details of dogs DogID DG12 DG34 DG48 DG49 DG88 DG95 Name Smokey Harvey Maisie Maggie Winston Shona Breed Poodle Springer Spaniel Labradoodle Labradoodle Bulldog Golden Retriever Age 12 10 3 3 7 6 Gender M M F F M F Which field would be suitable for the primary key? Explain why [2] DogID [1] It is a unique identifier [1] It does not contain any duplicate data items [1] CIE IGCSE Computer Science Revision Notes IGCSEComputer ScienceCIERevision Notes9. Databases9.2 SQLSQL SQL Download PDF Test Yourself SQL • • • Records in a database can be searched and data can be manipulated using Structured Query Language (SQL) SQL statements can be written to query the data in the database and extract useful information SQL statements follow this structure: o SELECT the fields you want to display o FROM the table/tables containing the data you wish to search o WHERE the search criteria A Database Table Containing Movie Details MovieID Name Genre M23 Moana Family M8 Shaun of the Dead Comedy M56 Die Hard Action M34 Big Family Example Certificate Rating U 18 18 PG 8.1 8.7 8.4 8.5 SELECT Name, Rating FROM Movie WHERE Rating>8.4; The results of this query would be: Name Rating Shaun of the Dead Big 8.7 8.5 • • The two fields - Name and Rating have been extracted from the Movie table and then the records have been filtered by Rating This example uses the > operator to search for records where the rating is greater than 8.4 • There are several other comparison operators which can be used to create the filter criteria in the WHERE line of a SQL query SQL Comparison Operators Operator Description > >= < <= = <> Example Greater than Greater than or equal to Less than Less than or equal to Equal to Not equal to SELECT Name,Rating FROM Movie WHERE Genre=”Family” AND Certificate=”U”; The results of this query would be: • • • Name Rating Moana 8.1 The two fields Name and Rating have been extracted from the Movie table and the records have been filtered by both Genre and Certificate This query uses the AND logical operator to include multiple criteria in the WHERE line of the SQL query Another logical operator which can be used in the WHERE statement is OR o For example, WHERE Genre=”Comedy” OR Genre=”Family” Worked example A database table, Dogs2023, is used to keep a record of all dogs registered at a veterinary practice DogID Name Breed Age Gender DG12 Smokey Poodle 12 M DG34 Harvey Springer Spaniel 10 M DG48 Maisie Labradoodle 3 F DG49 Maggie Labradoodle 3 F DG88 Winston Bulldog 7 M DG95 Shona Golden Retriever 6 F Complete the structured query language (SQL) to return the name and breed of all Female dogs. SELECT Name,Breed ________ Dogs2023 WHERE _______; [2] FROM [1] Gender=”F” [1] Write an SQL query to find out the name and breed of all dogs aged 10 years old or older [4] SELECT Name, Breed [1] FROM Dogs2023 [1] WHERE Age>=10; [2] ORDER BY • You can enter a fourth line to the statement using the ORDER BY command, followed by ASC or DESC o If you enter ASC the results of the query will be sorted in ascending order o If you enter DESC the results of the query are sorted in descending order Example SELECT Name,Genre, Certificate, Rating FROM Movie ORDER BY Name ASC • The results of this query would be: Name Genre Certificate Rating Big Die Hard Moana Shaun of the Dead Family Action Family Comedy PG 18 U 18 8.5 8.4 8.1 8.7 • The query has returned four fields and all records because there were no WHERE criteria. The records are sorted by Name alphabetically o If numbers are sorted in ascending order they go from the lowest number at the top of the table to the highest number at the bottom o Descending order is the highest number to the lowest SUM and COUNT commands • • The SUM command can be used to add numerical data The COUNT command can be used to count items of data ProductID ProductName Price QuantityInStock 1 Sausages 1.99 3 2 Chips 2.99 2 3 Beans 2.50 5 4 Bananas 2.10 12 5 Avocado 1.00 3 Example SELECT SUM(QuantityInStock) FROM ProductTable; • This query will add up all of the numbers in the QuantityInStock field o The result of this query would be 25 Example SELECT COUNT(*) FROM ProductTable WHERE Price>2; • This query will count all the records with a price greater than 2 o The result of this query would be 3 o This is because there are three products with a price greater than £2 (Chips, Beans, Bananas)