SQL Database Keys and Relationship Core Quiz

SQL Database
0 Passed
0% acceptance

A 50-question quiz covering primary keys, foreign keys, one-to-many structures, many-to-many linking, and relationship reasoning.

50 Questions
~100 minutes
1

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?

A
Primary key
B
Foreign key
C
Composite relationship key
D
Index key only
2

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?

A
Foreign key
B
Primary key
C
Self key
D
Derived key
3

Question 3

A department can have many employees, but each employee works in exactly one department. How would you describe this relationship?

A
One-to-many
B
Many-to-many
C
One-to-one
D
Circular relationship
4

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?

A
A many-to-many relation using a link table
B
A one-to-one relation
C
Separate unrelated tables
D
A circular key match
5

Question 5

In a products table, the SKU value uniquely identifies each product and cannot be NULL. Which characteristic matches this kind of column?

A
Primary key behavior
B
Foreign key reference
C
Soft link key
D
Multi-value key
6

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?

A
Referential integrity
B
Arithmetic integrity
C
Computed integrity
D
Display integrity
7

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?

A
Enrollment link table with two foreign keys
B
Merged student-course table
C
A giant flattened table
D
A self-reference to the course table
8

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?

A
Orphan references to missing employee rows
B
Duplicate primary keys
C
Redundant table creation
D
Extra index creation
9

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?

A
ON DELETE CASCADE
B
ON UPDATE BLOCK
C
ON NULLIFY ALWAYS
D
NO CASCADE AVAILABLE
10

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?

A
A unique foreign key match
B
A many-to-many link
C
Two foreign keys both nullable
D
Unrelated tables
11

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?

A
Composite primary key
B
Foreign key
C
Surrogate key only
D
Soft identity key
12

Question 12

A movie may have multiple actors, and actors may star in multiple movies. Which pattern describes this association?

A
Many-to-many
B
One-to-one
C
One-to-many only
D
Self relation only
13

Question 13

A table requires each row to reference another table's primary key. What constraint enforces this?

A
FOREIGN KEY constraint
B
UNIQUE constraint
C
CHECK constraint
D
DEFAULT constraint
14

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?

A
Self-referencing foreign key
B
Circular join table
C
Many-to-many link
D
Standalone key
15

Question 15

A payments table links each payment to one order. However, an order may have multiple payments. How should this relationship be classified?

A
One-to-many (order → payments)
B
Many-to-many
C
One-to-one
D
Self relation
16

Question 16

A design team chooses surrogate keys (auto-incremented IDs) instead of natural keys. Why might this be useful?

A
They simplify updates to natural data fields
B
They automatically enforce cascades
C
They eliminate foreign keys
D
They merge all keys into one
17

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?

A
Foreign key identifying storage location
B
Primary key of items
C
Composite identity token
D
Derived metadata
18

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?

A
Referential integrity enforced by foreign keys
B
No-link loose model
C
Merged multi-dimension fields
D
Unordered data joins
19

Question 19

A contract must belong to exactly one customer, but a customer may have multiple contracts. What relationship best describes this rule?

A
One customer to many contracts
B
One contract to many customers
C
Many-to-many
D
One-to-one
20

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?

A
Foreign key constraint
B
TEXT CHECK constraint
C
AUTO INCREASE constraint
D
UNION constraint
21

Question 21

A blog post can have many comments, but each comment refers to only one post. Which structure represents this rule?

A
One-to-many
B
Self-join
C
One-to-one
D
Many-to-many
22

Question 22

If a foreign key field is allowed to be NULL, what does this imply about the relationship?

A
The relationship is optional
B
The primary key becomes NULL too
C
The relationship becomes many-to-many
D
The referenced table is redundant
23

Question 23

A many-to-many mapping requires storing pairs of references. How is uniqueness typically enforced in such a link table?

A
A composite primary key of both foreign keys
B
A single serial ID only
C
A CHECK on repeated pairs
D
A floating primary key
24

Question 24

A database prohibits deleting a customer who still has active orders. Which foreign key option enforces this behavior?

A
ON DELETE RESTRICT
B
ON DELETE CASCADE
C
ON DELETE SET NULL
D
ON DELETE FOLLOW
25

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?

A
ON DELETE SET NULL
B
ON DELETE CASCADE
C
ON DELETE RESTRICT
D
ON DELETE FORCE
26

Question 26

This table definition creates a unique identifier for each row. What does the highlighted column represent?

sql
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name TEXT
);
A
Primary key that uniquely identifies users
B
Foreign key referencing name
C
Composite identity
D
Temporary key only
27

Question 27

This table stores orders referencing customers. What does the customer_id column enforce?

sql
CREATE TABLE orders (
  id SERIAL PRIMARY KEY,
  customer_id INT REFERENCES customers(id)
);
A
Ensures the customer exists before an order references them
B
Blocks all inserts
C
Creates a composite key automatically
D
Ensures orders become deleted immediately
28

Question 28

This relationship links employees to departments. What kind of structure does it represent?

sql
CREATE TABLE employees (
  id SERIAL PRIMARY KEY,
  dept_id INT REFERENCES departments(id)
);
A
One department to many employees
B
Many departments to one employee
C
Many employees to many departments
D
Self-join
29

Question 29

In this linking table, what purpose do the two REFERENCES lines serve?

sql
CREATE TABLE enrollment (
  student_id INT REFERENCES students(id),
  course_id INT REFERENCES courses(id)
);
A
They create a many-to-many relationship
B
They create one-to-one mapping
C
They prevent student creation
D
They merge tables
30

Question 30

What does this structure enforce regarding user settings?

sql
CREATE TABLE settings (
  user_id INT UNIQUE REFERENCES users(id),
  theme TEXT
);
A
Each user can have at most one settings row
B
Multiple settings rows allowed per user
C
User references become optional always
D
A many-to-many mapping
31

Question 31

This table prevents deleting categories that still have products. Which rule makes this happen?

sql
CREATE TABLE products (
  id SERIAL PRIMARY KEY,
  category_id INT REFERENCES categories(id) ON DELETE RESTRICT
);
A
RESTRICT blocks deletion when dependents exist
B
CASCADE automatically deletes categories
C
SET NULL deletes all products
D
RESTRICT makes foreign keys nullable
32

Question 32

This column refers to another row in the same table. What type of reference is created?

sql
CREATE TABLE employees (
  id SERIAL PRIMARY KEY,
  manager_id INT REFERENCES employees(id)
);
A
Self-referencing foreign key
B
Full cascade key
C
Many-to-many link
D
One-to-one enforced key
33

Question 33

This entry pairs a student with a class. What does the pair represent?

sql
INSERT INTO enrollment (student_id, class_id) VALUES (10, 4);
A
A new many-to-many link record
B
A deletion rule
C
A one-to-one mapping
D
A self-lookup
34

Question 34

This query finds all orders belonging to customer 3. Which mechanism makes this relationship possible?

sql
SELECT * FROM orders WHERE customer_id = 3;
A
Foreign key matching customer_id
B
Primary key duplication
C
View merging
D
Synthetic mapping
35

Question 35

What does this delete action cause?

sql
DELETE FROM customers WHERE id = 10;
A
Dependent rows may block deletion depending on FK rules
B
All orders delete no matter what
C
Primary key becomes NULL
D
All tables update automatically
36

Question 36

This structure creates a link row storing book–author relationships. What does it represent?

sql
INSERT INTO book_authors (book_id, author_id) VALUES (5, 2);
A
A many-to-many association
B
A one-to-one link
C
Arbitrary foreign key pair
D
A broken reference
37

Question 37

This definition ensures that each comment relates to a blog post. Which relationship is created?

sql
CREATE TABLE comments (
  id SERIAL PRIMARY KEY,
  post_id INT REFERENCES posts(id)
);
A
One post to many comments
B
Many posts to many comments
C
One-to-one only
D
Self-mapping
38

Question 38

This foreign key links each order item to its order. What structure emerges?

sql
CREATE TABLE order_items (
  id SERIAL PRIMARY KEY,
  order_id INT REFERENCES orders(id)
);
A
One order to many order items
B
Many orders to one order item
C
Many-to-many
D
Self relation
39

Question 39

This update changes the customer linked to an order. What does it rely on?

sql
UPDATE orders SET customer_id = 7 WHERE id = 3;
A
Valid foreign key reference to customer 7
B
Automatic cascade to delete order 3
C
Turning the primary key into composite
D
Creating a new customer row automatically
40

Question 40

This insert attempts to add a product belonging to category 99. What requirement must be met?

sql
INSERT INTO products (name, category_id) VALUES ('Lamp', 99);
A
Category 99 must exist in categories
B
Category becomes created automatically
C
Category_id must be NULL
D
Products table becomes reassigned
41

Question 41

This join table stores the association of tags to posts. What role does each row play?

sql
INSERT INTO post_tags (post_id, tag_id) VALUES (5, 3);
A
A single mapping between a post and a tag
B
A removal rule
C
A one-to-one constraint
D
A surrogate key pair
42

Question 42

This design sets a foreign key to NULL when its parent row is deleted. What behavior is this?

sql
CREATE TABLE tasks (
  id SERIAL PRIMARY KEY,
  project_id INT REFERENCES projects(id) ON DELETE SET NULL
);
A
SET NULL on delete
B
CASCADE delete
C
RESTRICT all deletes
D
Merge delete
43

Question 43

This table holds product images. Each image references a product. What structure is formed?

sql
CREATE TABLE product_images (
  id SERIAL PRIMARY KEY,
  product_id INT REFERENCES products(id)
);
A
One product to many images
B
Many-to-many
C
One-to-one only
D
Self relation
44

Question 44

This cascading delete rule ensures consistency. What effect does it produce?

sql
CREATE TABLE addresses (
  id SERIAL PRIMARY KEY,
  user_id INT REFERENCES users(id) ON DELETE CASCADE
);
A
Deleting a user deletes related addresses
B
Addresses block user deletion
C
Addresses become NULL
D
Users become duplicated
45

Question 45

This change updates the relationship between an employee and department. What constraint ensures the new department exists?

sql
UPDATE employees SET dept_id = 4 WHERE id = 12;
A
Foreign key referencing departments
B
Primary key constraint on employees
C
UNIQUE constraint on dept_id
D
Self-join rule
46

Question 46

This structure defines order–product links. What pattern does it create?

sql
CREATE TABLE order_products (
  order_id INT REFERENCES orders(id),
  product_id INT REFERENCES products(id)
);
A
A many-to-many join table
B
One-to-one mapping
C
Self reference
D
Primary-only structure
47

Question 47

This insert attempts to assign an employee to a team. What requirement must be satisfied first?

sql
INSERT INTO team_members (team_id, employee_id) VALUES (2, 15);
A
Both team and employee rows must exist
B
employee_id must be NULL
C
team_id must be NULL
D
A cascade must occur
48

Question 48

This definition pairs categories and tags. What type of relationship table is it?

sql
CREATE TABLE category_tags (
  category_id INT REFERENCES categories(id),
  tag_id INT REFERENCES tags(id)
);
A
Many-to-many link table
B
One-to-one
C
Self-reference
D
Standalone data
49

Question 49

This operation attempts to remove a project. What determines whether it succeeds?

sql
DELETE FROM projects WHERE id = 6;
A
Foreign key rules referencing project 6
B
The width of the table
C
Whether indexes exist
D
The existence of triggers only
50

Question 50

This table links roles and permissions. Each row binds one role to one permission. What relationship is defined?

sql
CREATE TABLE role_permissions (
  role_id INT REFERENCES roles(id),
  perm_id INT REFERENCES permissions(id)
);
A
Many-to-many association
B
One-to-one only
C
Primary-unique mapping
D
Null-driven relationship

QUIZZES IN SQL Database