Information Technology Topic

Modifying a Table Structure

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

Study focus - 3 things

  1. Modifying a table structure means adding a field, deleting a field or changing a field's definition (name, data type, size, default value, required), not editing the data in one record
  2. A new field starts empty (or with its default value) in every existing record, while a deleted field takes all its data with it, and the number of records never changes
  3. A data type change works only if every existing value can be converted, and making a field smaller or required can fail or lose data, so check first

Section 1Structure and data are different things

The structure of a table is its list of fields, and each field has a definition: a name, a data type, a size, and settings such as whether it can be empty and what its default value is. The data is the records stored under those fields. Modifying the structure means changing the fields: adding one, deleting one, or changing a field's definition. Correcting a misspelt surname in one record changes the data, not the structure. You make structure changes in the table's design view, where each field is a row with its name, data type and properties, not in the data view where the records are typed. Here's the trick: a structure change touches every record at once, because every record has every field.

Section 2Adding a field

Add a field when you need to keep a new kind of detail, such as a Category for each item in a tuck shop table. Steps: open the table in design view, go to the first empty row, type the field name, choose its data type, set the size and the other properties, then save. The table already has records, so each of them receives a value in the new field. If you gave the field a default value, every existing record gets that value. If you did not, the new cells are empty. Example: a table with 8 records and 5 fields gets a Category field with the default Snack. It now has 6 fields, still 8 records, and all 8 records show Snack in the new field. The new field never changes the old values. You still have to fill in the real values record by record.

Section 3Deleting a field

Delete a field when you no longer need what it stores. Select the field's row in design view, delete it and save. Watch out: the data in that field is lost along with the field. Saving after the delete cannot bring it back, so be sure the field is not needed. Deleting a field does not delete any records. A table with 8 records still has 8 records afterwards, each with one fewer value. A field that is part of a link to another table is protected in most programs. The relationship must be removed first, so that the other table is not left pointing at something that no longer exists. Also think before deleting the key field. The primary key identifies every record, so a table needs one.

Section 4Changing a field's definition

Five changes to a field are common. Rename: only the field's heading changes. All the values stay exactly as they were. Change the data type: for example text to numeric. Change the size: the maximum number of characters in a text field. Set or change the default value: the value a new record receives if nothing is typed. Make the field required or not required: a required field may not be left empty. Each of these can touch the data that is already stored, so it has to suit the existing records as well as the new ones.

Section 5When a change can fail or lose data

Data type: the type can be changed only if every existing value can be converted to the new type. Changing a text field holding 24, 18, 40 and 30 to a numeric field works, because each value is a number. If one value is the word none, it cannot be converted and the change is not allowed until you correct it. Any number can be written as text, so numeric to text works. Text to date works only if each value is a real date, and 2012-02-30 is not, because February never has 30 days. Size: making a text field larger is safe. Making it smaller is risky. Some programs cut off any value that is longer than the new size and the extra characters are lost, and others refuse the change, so check the longest value first. The examples in this topic use the cut-off behaviour. Required: a field cannot be made required while some records still have an empty value there. Fill the empty cells first. Easy to mix up: renaming and changing the default are harmless to the stored values. Deleting, shrinking and converting are the risky ones.

Section 6Counting what changes

Every field of every record has a cell, so a table has fields × records cells. Take the tuck shop table. Its fields are ItemCode, ItemName, Supplier, Price and InStock, and it has 8 records: 101 | Beef Patty | Island Bakery | 180 | 24; 102 | Coco Bread | Island Bakery | 90 | 18; 103 | Box Juice | Cool Drinks Ltd | 120 | 40; 104 | Bag Juice | (empty) | 40 | 30; 105 | Fruit Cup | Fresh Farms | 150 | 12; 106 | Bulla Cake | Island Bakery | 60 | 20; 107 | Peanuts | (empty) | 50 | 35; 108 | Coconut Water | Fresh Farms | 130 | 15. That is 5 × 8 = 40 cells. Two Supplier cells are empty. Add Category (no default): 6 fields, 8 records, 48 cells, and 8 new empty cells. Delete Supplier: 4 fields, 8 records, 32 cells. The 6 Supplier values that were there are lost, but the 2 empty ones had nothing to lose. Delete Supplier and InStock: 3 fields, 8 records, 24 cells. Rename Price: still 5 fields and 40 cells. Reduce ItemName to 9 characters (in a program that cuts long values off): four names are longer than 9, so Beef Patty becomes Beef Patt, Coco Bread becomes Coco Brea, Bulla Cake becomes Bulla Cak and Coconut Water becomes Coconut W.

Section 7Safe habits when changing a table

Plan the structure well at the start. Changing a table after it holds data can fail or lose data, so the best protection is a good design in the first place. Before a risky change, make a copy of the database so that you can go back. Then check what the change will do to the records you already have: look for empty cells before making a field required, look for long values before shrinking a field, and look at the values before changing a data type. After the change, check the table: count the fields, count the records, and look at a few records to make sure the values are still right.

Examples

A school library in Mandeville keeps a table called Reader with the fields ReaderID, Surname, Form and Phone. It has 10 records and every record has a phone number. (a) The librarian adds a field called Email without a default value. How many fields, records and cells does the table have, and how many cells are empty? (b) The Phone field is then deleted. How many fields, records and cells are there now, and how many values are lost? (c) State whether deleting the field deleted any readers.

(a) Before the change there are 4 fields and 10 records, so 4 × 10 = 40 cells. After adding Email there are 5 fields, still 10 records, and 5 × 10 = 50 cells. There is no default value, so the new Email cell is empty in all 10 records: 10 empty cells. (b) Deleting Phone leaves 4 fields (ReaderID, Surname, Form, Email), still 10 records, and 4 × 10 = 40 cells. All 10 phone numbers are lost. (c) No reader was deleted. Deleting a field removes the values in that field from every record, but the records stay, so there are still 10 readers.

A school tuck shop keeps a table Item with the fields ItemCode, ItemName (size 15), Supplier, Price and InStock: 101 | Beef Patty | Island Bakery | 180 | 24; 102 | Coco Bread | Island Bakery | 90 | 18; 103 | Box Juice | Cool Drinks Ltd | 120 | 40; 104 | Bag Juice | (empty) | 40 | 30; 105 | Fruit Cup | Fresh Farms | 150 | 12; 106 | Bulla Cake | Island Bakery | 60 | 20; 107 | Peanuts | (empty) | 50 | 35; 108 | Coconut Water | Fresh Farms | 130 | 15. The teacher makes these changes in order: (a) Add a field Category with the default value Snack. (b) Delete the field Supplier. (c) Rename the field Price to UnitPrice. (d) Reduce the size of ItemName from 15 to 9 characters (the program cuts off long values). Give the number of fields, records and cells after changes (a) and (b), list the data that is lost, and show the final field list.

Start: 5 fields, 8 records, 5 × 8 = 40 cells. (a) Category is added. There are 6 fields, 8 records and 48 cells, and all 8 records show Snack in the new field because of the default value. (b) Supplier is deleted. There are 5 fields, 8 records and 40 cells. The 6 supplier names that were stored are lost (Island Bakery three times, Fresh Farms twice, Cool Drinks Ltd once). The 2 records with an empty Supplier had no value to lose. (c) Renaming changes only the heading. There are still 5 fields and 40 cells, and every price is unchanged. (d) Assume the program cuts off names longer than 9 characters. Beef Patty, Coco Bread and Bulla Cake have 10 characters and Coconut Water has 13, so 4 records lose characters: Beef Patt, Coco Brea, Bulla Cak and Coconut W. The other four names fit. Final fields: ItemCode, ItemName, UnitPrice, InStock, Category, with 8 records and 40 cells. Steps (b) and (d) are the risky ones, because they destroy data.

Now let's water it

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

Where this comes from