PostgreSQL Database Quiz Questions

Course Name:PostgreSQL
Chapter Name:Chapter 8 - RDBMS Concepts
Lesson Content Link:Foreign Key
Current Quiz Count:30
Progress
0%
Q1
True / False

In PostgreSQL, a foreign key is used to link two tables together.

Q2
True / False

A foreign key in PostgreSQL can reference columns in the same table.

Q3
True / False

In PostgreSQL, foreign keys do not enforce referential integrity.

Q4
True / False

In PostgreSQL, you can define a foreign key constraint using the ALTER TABLE statement.

Q5
True / False

A foreign key in PostgreSQL can reference a unique constraint instead of a primary key.

Q6
True / False

PostgreSQL automatically creates an index on the foreign key column.

Q7
True / False

In PostgreSQL, ON DELETE CASCADE ensures that when a referenced row is deleted, all rows with matching foreign key values are also deleted.

Q8
True / False

PostgreSQL allows foreign key constraints to be deferred.

Q9
True / False

In PostgreSQL, you cannot define a foreign key constraint that references multiple columns (composite key).

Q10
True / False

In PostgreSQL, foreign key constraints can reference tables in other databases.

Q11
Single Choice

In PostgreSQL, what is the primary purpose of a foreign key?

Q12
Single Choice

Which keyword is used in a PostgreSQL table creation statement to define a foreign key constraint?

Q13
Single Choice

In PostgreSQL, which of the following statements correctly creates a foreign key?

SQL Code
CREATE TABLE orders (
 order_id SERIAL PRIMARY KEY,
 customer_id INT,
 CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
Q14
Single Choice

What happens in PostgreSQL when a record in a parent table with a foreign key reference is deleted?

Q15
Single Choice

Which of the following SQL statements adds a foreign key constraint to an existing table in PostgreSQL?

SQL Code
ALTER TABLE orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id);
Q16
Single Choice

In PostgreSQL, which of the following statements creates a table with a composite foreign key?

SQL Code
CREATE TABLE order_items (
 order_id INT,
 product_id INT,
 CONSTRAINT fk_order_product FOREIGN KEY (order_id, product_id) REFERENCES orders(order_id, product_id)
);
Q17
Single Choice

Which of the following ON DELETE actions ensures that when a referenced row in PostgreSQL is deleted, all associated rows in the referencing table are also deleted?

Q18
Single Choice

What does the following PostgreSQL SQL statement do?

SQL Code
CREATE TABLE payments (
 payment_id SERIAL PRIMARY KEY,
 order_id INT,
 FOREIGN KEY (order_id) REFERENCES orders(order_id) ON DELETE SET NULL
);
Q19
Single Choice

How would you modify an existing foreign key constraint in PostgreSQL to include the ON UPDATE CASCADE option?

SQL Code
ALTER TABLE orders DROP CONSTRAINT fk_customer, ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON UPDATE CASCADE;
Q20
Single Choice

Consider the following tables in PostgreSQL:

SQL Code
CREATE TABLE customers (
 customer_id SERIAL PRIMARY KEY,
 customer_name VARCHAR(100)
);

CREATE TABLE orders (
 order_id SERIAL PRIMARY KEY,
 customer_id INT,
 CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON DELETE RESTRICT
);
What happens if you attempt to delete a customer who has orders?
Q21
Multiple Choice

What is the primary role of a foreign key in PostgreSQL?

SQL Code
CREATE TABLE departments (
 department_id SERIAL PRIMARY KEY,
 department_name VARCHAR(100)
);

CREATE TABLE employees (
 employee_id SERIAL PRIMARY KEY,
 first_name VARCHAR(50),
 department_id INT REFERENCES departments(department_id)
);
Q22
Multiple Choice

Which of the following is true about foreign keys in PostgreSQL?

SQL Code
CREATE TABLE customers (
 customer_id SERIAL PRIMARY KEY,
 customer_name VARCHAR(100)
);

CREATE TABLE orders (
 order_id SERIAL PRIMARY KEY,
 order_date DATE,
 customer_id INT REFERENCES customers(customer_id)
);
Q23
Multiple Choice

How can you define a foreign key constraint in PostgreSQL?

SQL Code
CREATE TABLE courses (
 course_id SERIAL PRIMARY KEY,
 course_name VARCHAR(100)
);

CREATE TABLE students (
 student_id SERIAL PRIMARY KEY,
 student_name VARCHAR(100),
 course_id INT REFERENCES courses(course_id)
);
Q24
Multiple Choice

What happens if you try to insert a value in a foreign key column that does not exist in the referenced primary key column?

SQL Code
CREATE TABLE library_branches (
 branch_id SERIAL PRIMARY KEY,
 branch_name VARCHAR(100)
);

CREATE TABLE books (
 book_id SERIAL PRIMARY KEY,
 title VARCHAR(100),
 branch_id INT REFERENCES library_branches(branch_id)
);

INSERT INTO books (title, branch_id) VALUES ('PostgreSQL Essentials', 999);
Q25
Multiple Choice

Can a foreign key reference a unique constraint instead of a primary key in PostgreSQL?

SQL Code
Create a table 'employees' with a unique constraint on 'email', and another table 'departments' with a foreign key 'manager_email' referencing 'employees' on the 'email' column.

CREATE TABLE employees (
 employee_id SERIAL PRIMARY KEY,
 email VARCHAR(100) UNIQUE,
 first_name VARCHAR(50)
);

CREATE TABLE departments (
 department_id SERIAL PRIMARY KEY,
 department_name VARCHAR(100),
 manager_email VARCHAR(100) REFERENCES employees(email)
);
Q26
Multiple Choice

How do cascading updates and deletes work with foreign keys in PostgreSQL?Create a table 'categories' and 'products' where deleting a category cascades to delete all associated products.

SQL Code
CREATE TABLE categories (
 category_id SERIAL PRIMARY KEY,
 category_name VARCHAR(100)
);

CREATE TABLE products (
 product_id SERIAL PRIMARY KEY,
 product_name VARCHAR(100),
 category_id INT REFERENCES categories(category_id) ON DELETE CASCADE
);

INSERT INTO categories (category_name) VALUES ('Electronics');
INSERT INTO products (product_name, category_id) VALUES ('Smartphone', 1);
DELETE FROM categories WHERE category_id = 1;
Q27
Multiple Choice

What is the purpose of the ON DELETE SET NULL option in a foreign key constraint?Create a table 'projects' with a foreign key 'leader_id' referencing 'employees'. Use the ON DELETE SET NULL option.

SQL Code
CREATE TABLE employees (
 employee_id SERIAL PRIMARY KEY,
 employee_name VARCHAR(100)
);

CREATE TABLE projects (
 project_id SERIAL PRIMARY KEY,
 project_name VARCHAR(100),
 leader_id INT REFERENCES employees(employee_id) ON DELETE SET NULL
);

INSERT INTO employees (employee_name) VALUES ('Alice');
INSERT INTO projects (project_name, leader_id) VALUES ('New Project', 1);
DELETE FROM employees WHERE employee_id = 1;
Q28
Multiple Choice

How can you create a composite foreign key in PostgreSQL?Create a table 'registrations' with a composite foreign key referencing 'students' and 'courses'.

SQL Code
CREATE TABLE students (
 student_id SERIAL PRIMARY KEY,
 student_name VARCHAR(100)
);

CREATE TABLE courses (
 course_id SERIAL PRIMARY KEY,
 course_name VARCHAR(100)
);

CREATE TABLE registrations (
 student_id INT,
 course_id INT,
 FOREIGN KEY (student_id, course_id) REFERENCES students(student_id), courses(course_id)
);
Q29
Multiple Choice

Can a foreign key reference a primary key in the same table in PostgreSQL?Create a self-referencing foreign key in a table 'employees' where 'manager_id' references 'employee_id' in the same table.

SQL Code
CREATE TABLE employees (
 employee_id SERIAL PRIMARY KEY,
 employee_name VARCHAR(100),
 manager_id INT REFERENCES employees(employee_id)
);

INSERT INTO employees (employee_name, manager_id) VALUES ('John', NULL);
INSERT INTO employees (employee_name, manager_id) VALUES ('Doe', 1);
Q30
Multiple Choice

What does the ON UPDATE CASCADE clause do in a foreign key definition in PostgreSQL?Create a table 'authors' and 'books' where updating an 'author_id' in 'authors' cascades the update to 'books'.

SQL Code
CREATE TABLE authors (
 author_id SERIAL PRIMARY KEY,
 author_name VARCHAR(100)
);

CREATE TABLE books (
 book_id SERIAL PRIMARY KEY,
 book_title VARCHAR(100),
 author_id INT REFERENCES authors(author_id) ON UPDATE CASCADE
);

INSERT INTO authors (author_name) VALUES ('George Orwell');
INSERT INTO books (book_title, author_id) VALUES ('1984', 1);
UPDATE authors SET author_id = 2 WHERE author_id = 1;