Computing Science: Databases Computing Science Level - National 4 / 5 Databases Revised May 2012 St Andrew’s High School Computing Science Slide 1 Computing Science: Databases What is a Database • A database is a structured collection of similar information on one topic. • Examples: – Phone book, library catalogue, criminal records, dictionary • A database can be ordered either in ascending (A to Z) or descending (Z to A) order and on one or more fields. • Example: – A phone book can be sorted by last name and first name in ascending order (A to Z) Revised May 2012 St Andrew’s High School Computing Science Slide 2 Computing Science: Databases Important Elements • A database contains 3 important elements: – Fields – Records – Files Revised May 2012 St Andrew’s High School Computing Science Slide 3 Computing Science: Databases Field • A field holds one piece of information Example: Forename Date of Birth Town Revised May 2012 Helen 12/12/95 Coatbridge St Andrew’s High School Computing Science Slide 4 Computing Science: Databases Record • A record is a collection of fields on one person or thing. Example: Your record in school would contain: your name; date of birth; your address. Revised May 2012 St Andrew’s High School Computing Science Slide 5 Computing Science: Databases File • A file is a collection of records on the same topic. Examples: - The Police National Computer - Customer records in a bank - Pupil files held on school computers Revised May 2012 St Andrew’s High School Computing Science Slide 6 Computing Science: Databases Create and Add Records • Firstly the basic record structure is created by deciding on the fields names and field types. • Secondly you must add new records – You can add records through a form or just entering data straight to the table Revised May 2012 St Andrew’s High School Computing Science Slide 7 Computing Science: Databases Alter Records • Once you have created your database, you must ensure the data is correct. • You can alter the records through a form, or through the table. • You can also alter the record format. Revised May 2012 St Andrew’s High School Computing Science Slide 8 Computing Science: Databases Field • A field is one single piece of information Example: - “name”, is one field this would be a text field. - “date of birth”, is another field and this would be a date field. Revised May 2012 St Andrew’s High School Computing Science Slide 9 Computing Science: Databases Types of Fields Text holds letters, numbers and symbols Numeric hold numbers for calculations Date holds a date Time holds a time Graphic holds a picture Calculated field performs a calculation on the contents of one or more fields Revised May 2012 St Andrew’s High School Computing Science Slide 10 Computing Science: Databases Types of Fields (contd.) Link Stores a reference to an external media file or a connection to a related database table Boolean Only allows one of two values: yes/no true/false male/female Revised May 2012 St Andrew’s High School Computing Science Slide 11 Computing Science: Databases Field Validation Validation ensures data entered is allowable and sensible Range check:- Ensures the data entered in the field is between a lower and upper limit: e.g. Cost > £0 AND < £100 Time > 1 min AND < 5 mins Length check:- Ensures that the data entered in the field has a restricted number of characters: e.g. PIN = 4 chars Password >= 6 chars Comments < 200 chars Revised May 2012 St Andrew’s High School Computing Science Slide 12 Computing Science: Databases Field Validation (cont’d) Restricted choice Gives the user a list of options to choose from. Prevents the user from typing in errors. e.g. Available dress sizes 8, 10, 12, 14, 16. Presence check Ensures that the field is not left blank. Unique check Ensures that the data entered in the field is different from any other record. Revised May 2012 St Andrew’s High School Computing Science Slide 13 Computing Science: Databases Creating a new Field • Fields can be added at any time. • When on the table view, select the design view option • This view will allow you to enter a new field. Revised May 2012 St Andrew’s High School Computing Science Slide 14 Computing Science: Databases Searching The search facility allows you to look for information in the database. A search may be: Simple Look for records with a match on one field ( They have one thing in common.) Eg Hair = “Brown” Complex Look for records with a match on more than one item in one or more fields. Eg Hair = “Brown” AND Eyes = “Blue” Revised May 2012 St Andrew’s High School Computing Science Slide 15 Computing Science: Databases Comparison operators < < = = > = > < > Less than Less than or equal to Equal to Equal to or greater than Greater than Not equal to Contains Eg. To find all records for 1st to 3rd year in a school database you could search for: Year <= 3 Revised May 2012 St Andrew’s High School Computing Science Slide 16 Computing Science: Databases Sorting Sorting allows you to arrange the records in a database in alphabetic or numeric order. This can be ascending (A to Z or 1 to 9) or descending (Z to A or 9 to 1) Sorting on More than one field When two items are the same in one field they can be separated using a second field for sorting. For example, it is common to sort lists of names first by surname and then by first name Revised May 2012 St Andrew’s High School Computing Science Slide 17 Computing Science: Databases Question Time Complete the questions below from the Standard Grade Computing J Walsh book chapter 4, pages 63 and 64. NAT 4: Foundation KU 1-3 and PS 1-5 General KU 1-3 and PS 1 OR NAT 4: Complete the booklet Exercise 1 - 4 NAT 5: Complete the booklet Exercise 5 Finish the questions above for next day. Revised May 2012 St Andrew’s High School Computing Science Slide 18 Computing Science: Databases Calculated Field/ Computed Field A calculated field allows you to carry out a calculation on another field or fields and return the answer in the calculated field (similar to formulae in a spreadsheet). Example: Field 1: Date of birth Field 2: Today's date Field 3: Age Field 3 is a calculated field and contains the formula: Today’s date - Date of birth Other examples of calculated fields often used in reports include totals and sub-totals. Revised May 2012 St Andrew’s High School Computing Science Slide 19 Computing Science: Databases Report Any information on your database that you print out is a report. You would normally do a search and / or a sort, and then select which fields you want to print. Revised May 2012 St Andrew’s High School Computing Science Slide 20 Computing Science: Databases Size of a field This is the total number of characters, including spaces, needed to hold the information in a field. Eg. A Field containing the data ‘Computing Department’ Would have a field size of 20. Revised May 2012 Examples of databases include:Telephone directory Police National Computer A personal Christmas card list. St Andrew’s High School Computing Science Slide 21 Computing Science: Databases Calculating the storage requirements of a database file Field Size of field 1 2 3 4 5 6 7 8 30 4 25 24 8 4 4 8 Revised May 2012 St Andrew’s High School Computing Science Slide 22 Computing Science: Databases Field Size 1 2 3 4 5 6 7 8 Bytes 30 4 25 24 8 4 4 8 required 30 4 25 24 8 4 4 8 Total for one record= 107 bytes If a database has 50 records the storage space required= 107 X 50 = 5350 bytes 5350 / 1024 = 5.22 Kilobytes Revised May 2012 St Andrew’s High School Computing Science Slide 23 Computing Science: Databases Keywords This is the text used to search a file for a particular entry. Key Field This is a field which contains unique information for each record. That is, each record has a different number or text in the key field. Doing a search for an item on a unique field will only give one record. Example: SQA has a database of all pupils attempting Standard Grade Exam. Each pupil has a unique candidate number because there will be more than one pupil with the same name and date of birth. Revised May 2012 St Andrew’s High School Computing Science Slide 24 Computing Science: Databases Types of Database Flat File database Used to store information about one topic Relational or Linked Database Used to store information about several related topics. Each topic stored as a separate file or table. Database tables linked to create one large database. The tables are linked through a key field, referred to as a primary key in one table and a foreign key in the other table. e.g. Pupils database Revised May 2012 St Andrew’s High School Computing Science Slide 25 Computing Science: Databases Linked/Relational Database Example Pupils Database Pupils table Registration Classes table Forename Surname Date of Birth Registration Class Room Registration teacher Room phone number Registration Class The two tables are linked by the same field being in both tables. This must be a key field in one of the tables. Registration Class is the key field in the Registration Classes table because it uniquely identifies a single class. This is also referred to as the primary key. The Registration Class field in the Pupils table is regarded as the St Andrew’s High School foreign key. Slide 26 Revised May 2012 Computing Science Computing Science: Databases Data Protection Act Definitions: Data User is a person who holds and uses personal data about others or controls the use of it. Data Subject is a person about whom personal data is stored by a data user. Revised May 2012 St Andrew’s High School Computing Science Slide 27 Computing Science: Databases The Data subjects have the following rights: • • • • to know if data is held about them on a computer to see a copy of this personal data to make corrections if necessary to ask for compensation if data is inaccurate or access given to an unauthorised person. Revised May 2012 St Andrew’s High School Computing Science Slide 28 Computing Science: Databases Under the Data Protection Act (1984) data users must: • get and process the information fairly and lawfully • register what reason they hold it for • hold only relevant information • hold only accurate and up to date information • not keep information any longer than needed • give individuals access to information about themselves and, where necessary, correct or remove wrong information • take appropriate security measures. Revised May 2012 St Andrew’s High School Computing Science Slide 29 Computing Science: Databases Exceptions to the Act There are exceptions to people’s right to see data held about them. The public are denied access to data held by the Police or security forces. Revised May 2012 St Andrew’s High School Computing Science Slide 30 Computing Science: Databases Misuse of Computers The Computer Misuse Act is intended to protect all types of information (not just personal) stored on computer systems. Hacking This is the act of trying to gain unauthorised entry to files. This is done by using a wide area network and passwords. Revised May 2012 St Andrew’s High School Computing Science Slide 31 Computing Science: Databases Viruses Some people enjoy writing and distributing computer viruses which destroy data and cause computers to crash or take up processor time in meaningless calculations. Viruses are usually spread by copying files (from unofficial sources). To prevent viruses spreading: • Don’t share disks. • Don’t copy software. • Use an anti-virus program to check disks regularly. Revised May 2012 St Andrew’s High School Computing Science Slide 32 Computing Science: Databases Mail Merge A database is the second general purpose package (along with a word processor) required to produce a mail merged document. Having studied both these packages, we are now in a better position to understand how a mail merge works. Mail merging is the process of combining details from a database with a standard letter in a word processing package, to produce personalised letters - as many letters as there are records in the database. Revised May 2012 St Andrew’s High School Computing Science Slide 33 Computing Science: Databases Database Name Flossie Year S1 Name Josie Year S5 Name Phyllis Year S4 Word Processed Standard Letter Dear Parent, I am pleased to inform you that your child ___________ has won a prize for the best Computing student in __________ Head Teacher. Having created your database and your standard letter, you are ready to combine the two, filling the gaps in the standard letter with information from the database. Revised May 2012 St Andrew’s High School Computing Science Slide 34 Computing Science: Databases Word Processed Standard Letter with database fields inserted ready for mail merge. Dear Parent, The database field names are used to mark where in the standard letter information from the database will be inserted. These are shown in brackets like so << >> to mark them. I am pleased to inform you that your child <<Name>> has won a prize for the best Computing When the mail merge is performed the field names in student in <<Year>> brackets are replaced with the Head Teacher. appropriate fields from the database. This is done for every record in the database. Revised May 2012 St Andrew’s High School Computing Science Slide 35 Computing Science: Databases Question Time Complete the questions below from the Standard Grade Computing J Walsh book chapter 4, pages 63 and 64. NAT 4 and 5: Credit KU 1 and PS 1-2 and Exercise 6 and 7 from the Database Booklet NAT 5: Exercise 8 from the Database Booklet Complete questions for next day. Copy key points into your jotter. Revised May 2012 St Andrew’s High School Computing Science Slide 36