Creating & managing tables — quiz
8 questions covering this module. Data types, CREATE TABLE, INSERT/UPDATE/DELETE and views.
Which type should store a currency amount?
- FLOAT
- DECIMAL(10,2)
- REAL
- DOUBLE
Answer: DECIMAL(10,2) — DECIMAL is exact; floating point accumulates rounding error.
CHAR(10) storing "cat" occupies:
- 3 characters
- 10 characters, space-padded
- Variable
- 1 character
Answer: 10 characters, space-padded — The padding is why CHAR comparisons sometimes fail unexpectedly.
The difference between PRIMARY KEY and UNIQUE:
- None
- UNIQUE allows nulls, PRIMARY KEY does not
- PRIMARY KEY allows duplicates
- UNIQUE cannot be indexed
Answer: UNIQUE allows nulls, PRIMARY KEY does not — A table also has only one primary key.
A FOREIGN KEY constraint prevents:
- Duplicate rows
- Orphaned rows referencing a non-existent parent
- Null values
- Slow queries
Answer: Orphaned rows referencing a non-existent parent — It guarantees referential integrity.
UPDATE customers SET credit_limit = 0; with no WHERE:
- Errors
- Updates every row
- Updates nothing
- Asks for confirmation
Answer: Updates every row — There is no confirmation step outside a transaction.
DELETE differs from TRUNCATE because DELETE:
- Is faster
- Can be filtered with WHERE and rolled back
- Removes the table
- Resets identity columns
Answer: Can be filtered with WHERE and rolled back — TRUNCATE empties everything and usually cannot be rolled back.
A standard view stores:
- A copy of the data
- The query definition
- An index
- A snapshot
Answer: The query definition — The query runs each time the view is selected from.
A materialised view differs because it:
- Runs faster queries by storing the result
- Cannot be indexed
- Updates in real time
- Only works on one table
Answer: Runs faster queries by storing the result — The cost is staleness until it is refreshed.