SQL Database Keys and Relationship Core Quiz
A 50-question quiz covering primary keys, foreign keys, one-to-many structures, many-to-many linking, and relationship reasoning.
Question 1
A customer table assigns each customer a unique ID so the system can distinguish records even when names repeat. Which type of key ensures this guaranteed uniqueness?
Question 2
A system links orders to customers so every order belongs to exactly one customer. The column storing the customer’s ID inside the orders table represents which type of relationship mechanism?
Question 3
A department can have many employees, but each employee works in exactly one department. How would you describe this relationship?
Question 4
A library system tracks which books are borrowed by which users, and each book may be borrowed many times while users may borrow multiple books. Which structure handles this?
Question 5
In a products table, the SKU value uniquely identifies each product and cannot be NULL. Which characteristic matches this kind of column?
Question 6
A foreign key rule ensures that an order cannot reference a customer who does not exist. This protective behavior is known as which kind of integrity?
Question 7
In a student–course system, each student may enroll in many courses, and each course may have many students. What table structure typically represents this relationship?
Question 8
A manager table references employees, but only those employees who are designated as managers. A foreign key ensures the referenced employee exists. What does this prevent?
Question 9
If a product category is deleted, a cascading rule may remove all products in that category. Which type of action handles this dependent removal?
Question 10
A one-to-one relationship appears when an item has exactly one detail row in another table, such as a profile having exactly one settings row. How is this often enforced?
Question 11
A contact table uses a composite key of (country_code, phone_number) because both together uniquely identify a contact. What key property does this illustrate?
Question 12
A movie may have multiple actors, and actors may star in multiple movies. Which pattern describes this association?
Question 13
A table requires each row to reference another table's primary key. What constraint enforces this?
Question 14
A user table stores an optional referral ID pointing back to another user. This type of reference within the same table demonstrates which pattern?
Question 15
A payments table links each payment to one order. However, an order may have multiple payments. How should this relationship be classified?
Question 16
A design team chooses surrogate keys (auto-incremented IDs) instead of natural keys. Why might this be useful?
Question 17
A warehouse table lists locations, while an items table lists products stored in those locations. The items table includes a location_id column referencing the warehouse table. What role does location_id play?
Question 18
A star-schema style fact table references several dimension tables. Each reference prevents missing dimension rows from being linked. Which principle governs this structure?
Question 19
A contract must belong to exactly one customer, but a customer may have multiple contracts. What relationship best describes this rule?
Question 20
A relationship between two tables indicates that rows on one side cannot exist without matching rows on the other. Which mechanism enforces this dependency?
Question 21
A blog post can have many comments, but each comment refers to only one post. Which structure represents this rule?
Question 22
If a foreign key field is allowed to be NULL, what does this imply about the relationship?
Question 23
A many-to-many mapping requires storing pairs of references. How is uniqueness typically enforced in such a link table?
Question 24
A database prohibits deleting a customer who still has active orders. Which foreign key option enforces this behavior?
Question 25
A user profile table references a user account table. If deleting a user sets the profile’s foreign key to NULL instead of deleting the profile, which behavior is this?
Question 26
This table definition creates a unique identifier for each row. What does the highlighted column represent?
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT
);Question 27
This table stores orders referencing customers. What does the customer_id column enforce?
CREATE TABLE orders (
id SERIAL PRIMARY KEY,
customer_id INT REFERENCES customers(id)
);Question 28
This relationship links employees to departments. What kind of structure does it represent?
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
dept_id INT REFERENCES departments(id)
);Question 29
In this linking table, what purpose do the two REFERENCES lines serve?
CREATE TABLE enrollment (
student_id INT REFERENCES students(id),
course_id INT REFERENCES courses(id)
);Question 30
What does this structure enforce regarding user settings?
CREATE TABLE settings (
user_id INT UNIQUE REFERENCES users(id),
theme TEXT
);Question 31
This table prevents deleting categories that still have products. Which rule makes this happen?
CREATE TABLE products (
id SERIAL PRIMARY KEY,
category_id INT REFERENCES categories(id) ON DELETE RESTRICT
);Question 32
This column refers to another row in the same table. What type of reference is created?
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
manager_id INT REFERENCES employees(id)
);Question 33
This entry pairs a student with a class. What does the pair represent?
INSERT INTO enrollment (student_id, class_id) VALUES (10, 4);Question 34
This query finds all orders belonging to customer 3. Which mechanism makes this relationship possible?
SELECT * FROM orders WHERE customer_id = 3;Question 35
What does this delete action cause?
DELETE FROM customers WHERE id = 10;Question 36
This structure creates a link row storing book–author relationships. What does it represent?
INSERT INTO book_authors (book_id, author_id) VALUES (5, 2);Question 37
This definition ensures that each comment relates to a blog post. Which relationship is created?
CREATE TABLE comments (
id SERIAL PRIMARY KEY,
post_id INT REFERENCES posts(id)
);Question 38
This foreign key links each order item to its order. What structure emerges?
CREATE TABLE order_items (
id SERIAL PRIMARY KEY,
order_id INT REFERENCES orders(id)
);Question 39
This update changes the customer linked to an order. What does it rely on?
UPDATE orders SET customer_id = 7 WHERE id = 3;Question 40
This insert attempts to add a product belonging to category 99. What requirement must be met?
INSERT INTO products (name, category_id) VALUES ('Lamp', 99);Question 41
This join table stores the association of tags to posts. What role does each row play?
INSERT INTO post_tags (post_id, tag_id) VALUES (5, 3);Question 42
This design sets a foreign key to NULL when its parent row is deleted. What behavior is this?
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
project_id INT REFERENCES projects(id) ON DELETE SET NULL
);Question 43
This table holds product images. Each image references a product. What structure is formed?
CREATE TABLE product_images (
id SERIAL PRIMARY KEY,
product_id INT REFERENCES products(id)
);Question 44
This cascading delete rule ensures consistency. What effect does it produce?
CREATE TABLE addresses (
id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(id) ON DELETE CASCADE
);Question 45
This change updates the relationship between an employee and department. What constraint ensures the new department exists?
UPDATE employees SET dept_id = 4 WHERE id = 12;Question 46
This structure defines order–product links. What pattern does it create?
CREATE TABLE order_products (
order_id INT REFERENCES orders(id),
product_id INT REFERENCES products(id)
);Question 47
This insert attempts to assign an employee to a team. What requirement must be satisfied first?
INSERT INTO team_members (team_id, employee_id) VALUES (2, 15);Question 48
This definition pairs categories and tags. What type of relationship table is it?
CREATE TABLE category_tags (
category_id INT REFERENCES categories(id),
tag_id INT REFERENCES tags(id)
);Question 49
This operation attempts to remove a project. What determines whether it succeeds?
DELETE FROM projects WHERE id = 6;Question 50
This table links roles and permissions. Each row binds one role to one permission. What relationship is defined?
CREATE TABLE role_permissions (
role_id INT REFERENCES roles(id),
perm_id INT REFERENCES permissions(id)
);