CompTIA Tech+ (FC0-U71) · chapter 15 of 18
Tables, keys and a messy spreadsheet
In this chapter Objectives 5.3
Objective 5.3
Shapes of data and tables
Structured, semi-structured, non-structured
Structured data fits neat rows and columns where every row has the same fields, like an orders table. Semi-structured data has labels but no fixed layout, like a JSON file where a toy has a color and kibble has a bag size. Non-structured data, also called unstructured, has no data model at all: photos, videos, recorded calls, free-written emails.
{"id": "P7", "name": "Rope toy",
"color": "red"}
{"id": "P8", "name": "Salmon Crunch",
"bag_lb": 12}Relational databases, tables and schema
A relational database keeps data in tables that link to each other. Each row, or record, is one thing, like one customer. Each field, or column, is one detail about it, like the customer's email. The schema is the plan of the whole database: which tables exist, their fields, each field's data type, and how the tables link. Adding a column changes the schema; adding a row just adds data.
You: So the schema is the floor plan, and the rows are the furniture?
Theo: I'm stealing that.
Check yourself 1Theo needs to add a new 'Delivery notes' column to the Orders table. What is he changing?
- Only the data stored in one record of the table
- The schema, because the table's design changes
- A foreign key that links Orders to another table
- Non-structured data
Show the answer
Answer: B. The schema, because the table's design changes
The schema describes the tables and their fields, so adding a column changes the schema. Adding a row would only add data.
Objective 5.3
Keys and constraints
Primary and foreign keys
A primary key is the field that identifies each row, like CustID. No two rows can share it, and it can't be blank. A foreign key is a field in one table that holds another table's primary key, which links the two: ORDERS.CustID points to a row in CUSTOMERS. Store each customer once and point to them, instead of retyping their name on every order.
CUSTOMERS CustID Name City C01 Ana Ruiz Lansing C02 Lee Park Detroit ORDERS OrderID CustID Item Bags 5001 C01 Salmon Crunch 2 5002 C02 Lamb Bites 1 5003 C01 Turkey Mix 3
Constraints
Constraints are rules the database enforces on fields so bad data can't get in. A primary key must be unique and not blank. A foreign key must point to a row that really exists. A price must be filled in and can't be below zero. When a new row breaks a rule, the database refuses it, and that's exactly what you want.
Rosa: So the database will just say no to my duplicate 5003?
Theo: Politely, but firmly. Like Ranger at bath time.
Exam tip Ana typed three ways? Store her once in CUSTOMERS and let ORDERS point to C01.
Check yourself 2Look at the two tables. What is CustID in the ORDERS table?
- The primary key that identifies each order in ORDERS
- A constraint on the Bags field
- A record
- A foreign key that links each order to a customer
Show the answer
Answer: D. A foreign key that links each order to a customer
ORDERS.CustID holds values from CUSTOMERS' primary key, so it's a foreign key linking each order to the customer who placed it. OrderID is the primary key of ORDERS.
Objective 5.3
Non-relational databases
Key/value and document databases
Non-relational databases, often called NoSQL, don't use fixed tables. A key/value database stores each value under a unique key and fetches it fast by that key, like a shopping cart saved under cart-1042. A document database stores each record as a document, often JSON, whose fields can differ from one record to the next, and it can search inside those fields, like every product whose color is red.
Theo: The rope toy has a color. The kibble has a bag size. The document database doesn't mind.
Exam tip Fixed tables, links and strict rules? Relational. Fast lookup by one key? Key/value. Records with different fields? Document.
Check yourself 3The web store must save each shopper's cart and get it back by its cart ID as fast as possible. It never needs to search inside carts or link them to other tables. Which fits best?
- A key/value database
- A relational database with constraints
- A flat file emailed each night
- A document database searched by color
Show the answer
Answer: A. A key/value database
A key/value database stores each value under one unique key and fetches it by that key very fast, which is all a cart lookup needs.
Go deeper
More on the objectives in this chapter, in the same exam-ready detail.
Objective 5.3 · Structured vs. semistructured vs. non-structured; Relational databases
Tables, keys and how data is shaped
Structured, semi-structured, non-structured
Data comes in three shapes:
- Structured: a fixed layout where every row has the same columns, like an orders table or a comma-separated list.
- Semi-structured: labeled fields that can change from item to item, like a JSON (JavaScript Object Notation) export where toys have a color field and kibble has a bag size field.
- Non-structured: no set fields at all, like photos, videos and recorded calls.
Relational databases: schema, tables, rows, fields
A relational database stores structured data in tables that link to each other, and it's managed with SQL (Structured Query Language). Each row is a record and each column is a field. The schema is the plan: which tables exist, their fields and data types, and how they link. Adding a 'Delivery notes' column changes the schema; adding an order adds a record.
ORDERS OrderID | CustID | Bag size 5001 | C101 | 30 lb 5002 | C102 | 15 lb 5003 | C101 | 30 lb
Exam tip The header line names the fields. It isn't a record, so this table holds 3 records.
Primary keys and foreign keys
A primary key is a field whose value is unique and never empty, so it identifies exactly one row: OrderID in ORDERS, CustID in CUSTOMERS. Names and cities repeat, so they make poor keys. A foreign key holds values from another table's primary key to link the tables. CustID in ORDERS is a foreign key: C101 appears twice because one customer has two orders.
- Adding a second order 5002: rejected, because primary keys must be unique.
- Adding an order for customer C999, who doesn't exist: rejected by the foreign key.
Constraints
Constraints are rules on a table that the database enforces on every save. 'Price must not be empty' and 'price must be zero or more' stop bad data before it gets in. A report could only spot bad prices after they're saved. Primary and foreign keys are constraints too.
Objective 5.3 · Non-relational databases
Non-relational databases: key/value and document
What non-relational means
Non-relational databases store data without fixed, linked tables. They're flexible and fast for certain jobs, but they don't enforce links between tables the way relational keys do. When the finance team needs every payment to match a real order, with the database enforcing it, they need a relational database.
Key/value databases
A key/value database stores each value under a unique key and finds it very fast by that key, like a coat check ticket. The web store saves each shopper's cart under its cart ID, like cart-1042, and fetches it by that ID. It never needs to search inside carts, so key/value fits. Most key/value stores treat the value as one block and can't search inside it.
KEY VALUE
cart-1042 -> {2 bags Salmon, 1 toy}
cart-1043 -> {1 bag Lamb}Document databases
A document database stores each record as a document, usually JSON (JavaScript Object Notation), and each document can have its own fields. Toys have colors, kibble has bag sizes, and beds have dimensions, all in one catalog. Unlike most key/value stores, it can search the fields inside documents, such as every product whose color is red.
{ "name": "Squeaky Bone", "color": "red" }
{ "name": "Salmon Crunch", "bagLb": 30 }Exam tip Fetch by ID only = key/value. Search inside varied records = document. Strict links between tables = relational.
Practice questions
Answer in your head, then open the answer. Ranger’s First Byte has 455 questions like these for Tech+, with mock exams.
Question 1Theo's product export is a JSON (JavaScript Object Notation) file. Each product has labeled fields, but toys have a color field and kibble has a bag size field instead. What kind of data is this?
- Structured data
- Semi-structured data
- Non-structured data
- Relational data
Show the answer
Answer: B. Semi-structured data
Semi-structured data has some structure, like labeled fields in JSON, but lets the fields vary from item to item. Structured data needs every entry to have the same fixed fields, like a table.
Question 2Which of these is structured data?
- A table of orders with the same columns in every row
- A recorded customer service phone call
- A photo of a damaged pallet on the dock
- A JSON file where each record has its own different fields
Show the answer
Answer: A. A table of orders with the same columns in every row
Structured data follows a fixed schema, so every row has the same columns, like an orders table. JSON with varying fields is semi-structured, and recordings and photos are non-structured.
Question 3In a relational database, what is the schema?
- The data stored in the rows
- A backup copy of the whole database, kept for recovery
- A printed summary of the data
- The plan of tables, fields, data types and links
Show the answer
Answer: D. The plan of tables, fields, data types and links
The schema is the database's design: which tables exist, their fields and data types, and how tables link with keys. The data is what fills that plan in, row by row.
Question 4The online store must save each shopper's cart and fetch it by its cart ID, like "cart-1042", as fast as possible. It never searches inside carts. Which database type is built for this?
- Relational database
- Document database
- Key/value database
- Flat file
Show the answer
Answer: C. Key/value database
A key/value database stores each value under a unique key and is built for fast lookups by that key, exactly what cart IDs need. A document database adds searching inside JSON records, which the store never uses.
Question 5Theo's Customers table needs one field that identifies each customer, where no two customers can ever share a value. Which field should be the primary key?
- CustomerID
- LastName
- City
- PetName
Show the answer
Answer: A. CustomerID
A primary key must be unique and never empty so it identifies exactly one row. A CustomerID number assigned to each customer does that. Last names, cities and pet names repeat across customers.
CompTIA, Tech+, A+, Network+, Security+ and Linux+ are trademarks of CompTIA, Inc. Ranger Gamez is not affiliated with or endorsed by CompTIA. The practice questions are written to the published exam objectives; they are not real exam questions.