Information Technology Topic

Creating a Database Table

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. Design the table by hand in steps: decide its purpose, list the fields, choose a data type for each, choose a primary key, build and save the structure, then type in the records
  2. Use at least three different data types from alphanumeric, numeric, date and logical, and match each to what the field holds: names and codes are alphanumeric, amounts to calculate with are numeric
  3. The primary key must be different in every record and never empty, so use an ID field, and check the finished table for at least 25 records that all fit their fields

Section 1What you are building

A database table stores one kind of thing, such as players in a netball club. Each column is a field (one detail) and each row is a record (everything about one player). The list of fields and their settings is the table structure. The records are the data. For the practical task you build a table with at least three different data types and at least 25 records. You must design the table yourself. A wizard, which is a guided tool that offers ready-made fields and makes the choices for you, is not allowed. Here's the trick: design first, type later. Every choice in this topic is made before the first record goes in, because changing a table that already holds data can lose data. The steps are: plan, list the fields, choose the data types, choose the primary key, set the field properties, build and save the structure, enter the records, check the table.

Section 2Plan the table and list the fields

Start with the purpose. Ask: what is this table about, and what do I need to be able to find out from it? A netball club wants to see who plays which position and who has paid. Then list the fields. Give each field one fact only. Put the surname and first name in separate fields, so that you can sort or search by either one. Keep each detail in its own field. Store a date of birth, not an age. An age changes every year, but a date of birth stays the same and the age can be worked out from it. For the Portmore junior netball club the fields are PlayerID, Surname, FirstName, DateOfBirth, Position, MonthlyFee and FeePaid. That is 7 fields.

Section 3Choose a data type for each field

A data type says what kind of value a field holds, and the database checks every value against it. The four types you need to know are: Alphanumeric: letters, digits and symbols treated as text, such as a surname, a position code or a telephone number. Numeric: numbers used in calculations or comparisons, such as a fee or an ID number. Date: real calendar dates, such as a date of birth. Logical: only two possible values, true or false (shown as Yes or No). Ask yourself: will I add it up, compare it by size or work with it as a number? Then it is numeric. Is it only a label? Then it is alphanumeric. A telephone number is a label that can contain dashes, and nobody adds telephone numbers together, so alphanumeric suits it. The type also controls how the field sorts. A fee stored in a numeric field sorts by size, but a fee stored as text would sort as text. The netball table uses all four types: PlayerID and MonthlyFee are numeric, Surname, FirstName and Position are alphanumeric, DateOfBirth is a date and FeePaid is logical.

Section 4Choose the primary key

The primary key is the field that identifies every record. Two rules: its value is different in every record, and it is never empty. Surname looks tempting but fails the first rule. Two players in the netball table are called Brown. FirstName fails too, and DateOfBirth could repeat the day two players share a birthday. The safe answer is an ID field, so PlayerID is the key. Each player gets the next number (1, 2, 3 and so on), and the number never repeats. Sometimes two fields together make the key. This is a composite key, and the combination must be different in every record. If you save a table with no key, some database programs will not let you work with it until one exists, and they offer to add an automatic ID field.

Section 5Set the field properties and build the structure

Each field has properties as well as a type. The usual ones are: Name: short and clear, such as FeePaid. Size: the maximum number of characters in an alphanumeric field. Surname 20 and Position 2 (GS, GA, WA and so on) are enough. Required: whether the field may be left empty. Make the key and the important details required. Default value: a value filled in if nothing is typed. Now build it by hand. Open the table in design view. For each field, type the name, choose the data type and set the properties. Mark PlayerID as the primary key. Save the table with a name such as Player. Save the structure before you enter any records, because each field must have its type defined before data is entered.

Section 6Enter the records

Open the table in data view and type one record per row, a value in every field. Records can also be imported from another source, but for this task you type them. Here are the first five of the 25 netball records (PlayerID, Surname, FirstName, DateOfBirth, Position, MonthlyFee, FeePaid): 1 Brown Keisha 2012-03-14 GS 1500 Yes; 2 Campbell Shanice 2011-11-02 GA 1500 Yes; 3 Reid Tanya 2012-07-21 WA 2000 No; 4 Clarke Jody 2013-01-09 C 1500 Yes; 5 Gordon Marlene 2012-05-30 WD 2000 Yes. Tips: type numbers without units, so 1500 and not J$1500. Type every date in the same way. Use the same code for the same thing every time. If the position is GS in one record, never type G.S. or goal shooter in another, or the table will treat them as different positions. Number the IDs in order and keep a running count. When the last PlayerID is 25, you have 25 records, as long as no number was skipped.

Section 7Check the finished table

Before you hand in or save the table, check these points. The structure has at least three different data types. The netball table has four: alphanumeric, numeric, date and logical. There are at least 25 records. Count them. Every value fits its field. A word in a numeric field, an impossible date such as 2012-02-30, or an empty required field means a record to fix. The key is different in every record. Sort by PlayerID and look for repeated or missing numbers. The codes are consistent. Sort by Position and look for odd spellings. A quick summary of the finished netball table: 7 fields, 25 records, key PlayerID, 3 alphanumeric fields, 2 numeric fields, 1 date field and 1 logical field.

Examples

A bike hire shop in Negril wants a database table of its bikes: the bike number, the model, the hire rate per day, the date the bike was bought, and whether it is available to hire today. 11 bikes have been entered so far. (a) List the fields and choose a data type for each. (b) Choose the primary key and explain why the model is a poor key. (c) State how many different data types are used, and how many more records are needed to reach 25.

(a) Five fields: BikeID (numeric), Model (alphanumeric), HireRate (numeric, because it is an amount to calculate with), DateBought (date) and Available (logical, because it is only Yes or No). (b) The primary key is BikeID. It is different for every bike and is never left empty. Model is a poor key because the shop may own several bikes of the same model, so the model would repeat and could not identify one bike. (c) The types used are numeric, alphanumeric, date and logical: 4 different types, which is more than the 3 required. 25 − 11 = 14 more records are needed.

A junior netball club in Portmore wants to keep its 25 players in one table, designed by hand with no wizard. The fields are PlayerID (numeric), Surname (alphanumeric), FirstName (alphanumeric), DateOfBirth (date), Position (alphanumeric), MonthlyFee (numeric) and FeePaid (logical). The 25 records are typed in (PlayerID, Surname, FirstName, DateOfBirth, Position, MonthlyFee, FeePaid): 1 Brown Keisha 2012-03-14 GS 1500 Yes; 2 Campbell Shanice 2011-11-02 GA 1500 Yes; 3 Reid Tanya 2012-07-21 WA 2000 No; 4 Clarke Jody 2013-01-09 C 1500 Yes; 5 Gordon Marlene 2012-05-30 WD 2000 Yes; 6 Davis Ashanti 2011-12-18 GD 1500 No; 7 Thomas Kadia 2013-02-27 GK 1500 Yes; 8 Francis Renae 2012-09-05 GS 2000 Yes; 9 Allen Shelly-Ann 2012-04-11 GA 1500 No; 10 Morgan Dahlia 2011-12-03 WA 1500 Yes; 11 Lewis Tamika 2013-06-16 C 2000 Yes; 12 Bailey Nadine 2012-10-24 WD 1500 Yes; 13 Mills Chevelle 2013-03-08 GD 1500 No; 14 Grant Zadie 2012-01-19 GK 2000 Yes; 15 Walker Simone 2011-12-29 GS 1500 Yes; 16 Henry Alicia 2012-08-13 GA 1500 Yes; 17 Powell Latoya 2013-04-02 WA 2000 No; 18 Edwards Racquel 2012-02-06 C 1500 Yes; 19 Brown Britney 2013-05-22 WD 1500 Yes; 20 Williams Kayla 2011-12-11 GD 2000 No; 21 Scott Dionne 2012-11-15 GK 1500 Yes; 22 Wright Natalee 2013-08-01 GS 1500 No; 23 Palmer Odessa 2012-06-28 GA 2000 Yes; 24 Stewart Imani 2013-09-19 WA 1500 Yes; 25 Hall Candice 2012-12-07 C 1500 Yes. (a) Choose the primary key and explain why Surname is not a good choice. (b) Count the fields, the records and the different data types, and say how many fields have each type. (c) State whether the table meets the task of at least three data types and 25 records. (d) Use the records to find how many players have not paid their fee.

(a) The primary key is PlayerID, which is numbered 1 to 25, so no two players share it and none is empty. Surname is a poor key because it can repeat: players 1 and 19 are both called Brown. (b) There are 7 fields and 25 records. The data types are alphanumeric, numeric, date and logical: 4 different types. Alphanumeric: Surname, FirstName and Position (3 fields). Numeric: PlayerID and MonthlyFee (2 fields). Date: DateOfBirth (1 field). Logical: FeePaid (1 field). 3 + 2 + 1 + 1 = 7. (c) Yes. There are 4 different data types, which is at least 3, and 25 records, which is at least 25. Every value fits its field, and the key is different in every record. (d) Count the records with FeePaid = No: players 3, 6, 9, 13, 17, 20 and 22, which is 7 players. The other 18 have paid.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from