PostgreSQL Database Quiz Questions

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

In PostgreSQL, a business key is a key that uniquely identifies a record using business logic.

Q2
True / False

In PostgreSQL, a natural key is always the best choice for a primary key.

Q3
True / False

In PostgreSQL, a candidate key is any column or set of columns that can uniquely identify a row in a table.

Q4
True / False

In PostgreSQL, a table can have multiple candidate keys.

Q5
True / False

In PostgreSQL, a natural key is always a candidate key.

Q6
True / False

In PostgreSQL, a surrogate key is the same as a business key.

Q7
True / False

In PostgreSQL, using a natural key as a primary key can lead to issues if the business logic changes.

Q8
True / False

In PostgreSQL, a business key can never be a surrogate key.

Q9
True / False

In PostgreSQL, a composite key can be a candidate key.

Q10
True / False

In PostgreSQL, a table must have at least one candidate key that can act as a primary key.

Q11
Single Choice

In PostgreSQL, which of the following is true about a Business Key?

Q12
Single Choice

Which of the following statements is correct regarding a Natural Key in PostgreSQL?

Q13
Single Choice

What distinguishes a Candidate Key from other keys in PostgreSQL?

Q14
Single Choice

In PostgreSQL, how would you define a Candidate Key using SQL?

SQL Code
CREATE TABLE employees (
 emp_id SERIAL PRIMARY KEY,
 emp_ssn VARCHAR(11) UNIQUE,
 emp_email VARCHAR(255),
 UNIQUE(emp_email)
);
Q15
Single Choice

Given the following PostgreSQL table definition, which key is a Natural Key?

SQL Code
CREATE TABLE products (
 product_code VARCHAR(10) PRIMARY KEY,
 product_name VARCHAR(100),
 price DECIMAL(10, 2)
);
Q16
Single Choice

In PostgreSQL, when would you prefer using a Business Key over a system-generated key?

Q17
Single Choice

How would you define both a Natural Key and a Candidate Key in a PostgreSQL table?

SQL Code
CREATE TABLE orders (
 order_id SERIAL PRIMARY KEY,
 order_number VARCHAR(20),
 customer_id INT,
 UNIQUE(order_number, customer_id)
);
Q18
Single Choice

Which of the following best describes the relationship between a Business Key and a Candidate Key in PostgreSQL?

Q19
Single Choice

Consider the following PostgreSQL table schema:

SQL Code
CREATE TABLE vehicles (
 vin VARCHAR(17) PRIMARY KEY,
 registration_number VARCHAR(10) UNIQUE,
 engine_number VARCHAR(20) UNIQUE
);
Which of the following is true?
Q20
Single Choice

In a PostgreSQL system where both Business Key and Candidate Key are defined, how should a developer decide which key to use in a JOIN operation?

Q21
Multiple Choice

Which of the following best defines a Business Key in PostgreSQL?

SQL Code
Given the 'employees' table with columns 'employee_id', 'social_security_number', 'email', and 'phone_number', identify the Business Key and implement a unique constraint to ensure its uniqueness.

```sql
CREATE TABLE employees (
 employee_id SERIAL PRIMARY KEY,
 social_security_number VARCHAR(11) UNIQUE,
 email VARCHAR(255) UNIQUE,
 phone_number VARCHAR(15) UNIQUE
);
```
Q22
Multiple Choice

What distinguishes a Natural Key from a Surrogate Key in PostgreSQL?

SQL Code
Consider a 'customers' table with columns 'customer_id', 'national_id', and 'email'. Implement the table using a Natural Key and a Surrogate Key, and explain the difference.

```sql
CREATE TABLE customers (
 customer_id SERIAL PRIMARY KEY,
 national_id VARCHAR(20) UNIQUE,
 email VARCHAR(255) UNIQUE
);
```
Q23
Multiple Choice

Which key can serve as both a Natural Key and a Candidate Key in PostgreSQL?

SQL Code
Given a 'products' table with columns 'product_id', 'product_code', and 'product_name', identify the Natural Key and the Candidate Key. Implement the constraints.

```sql
CREATE TABLE products (
 product_id SERIAL PRIMARY KEY,
 product_code VARCHAR(50) UNIQUE,
 product_name VARCHAR(255) NOT NULL
);
```
Q24
Multiple Choice

How can you enforce uniqueness on a Candidate Key in PostgreSQL?

SQL Code
Consider a 'departments' table with columns 'department_id', 'department_code', and 'department_name'. Enforce uniqueness on the Candidate Key using a unique constraint.

```sql
CREATE TABLE departments (
 department_id SERIAL PRIMARY KEY,
 department_code VARCHAR(10) UNIQUE,
 department_name VARCHAR(100)
);
```
Q25
Multiple Choice

When would you choose a Surrogate Key over a Natural Key in PostgreSQL?

SQL Code
Consider a 'transactions' table with columns 'transaction_id', 'account_number', 'transaction_date', and 'amount'. Implement the table using a Surrogate Key and discuss when it is preferred over a Natural Key.

```sql
CREATE TABLE transactions (
 transaction_id SERIAL PRIMARY KEY,
 account_number VARCHAR(20),
 transaction_date DATE,
 amount NUMERIC(10, 2)
);
```
Q26
Multiple Choice

Which scenario would benefit from using a Candidate Key in PostgreSQL?

SQL Code
Given a 'suppliers' table with columns 'supplier_id', 'supplier_code', and 'contact_email', enforce a Candidate Key on 'supplier_code' and explain its importance in the context.

```sql
CREATE TABLE suppliers (
 supplier_id SERIAL PRIMARY KEY,
 supplier_code VARCHAR(50) UNIQUE,
 contact_email VARCHAR(255)
);
```
Q27
Multiple Choice

In what situations would you use a Natural Key as a Candidate Key in PostgreSQL?

SQL Code
Consider a 'library_books' table with columns 'book_id', 'isbn', and 'title'. Implement 'isbn' as a Natural Key and explain when it is suitable to use a Natural Key as a Candidate Key.

```sql
CREATE TABLE library_books (
 book_id SERIAL PRIMARY KEY,
 isbn VARCHAR(13) UNIQUE,
 title VARCHAR(255)
);
```
Q28
Multiple Choice

What are the benefits of using a Business Key as a Natural Key in PostgreSQL?

SQL Code
Consider a 'employees' table with columns 'employee_id', 'email', and 'employee_number'. Implement 'employee_number' as a Business Key and discuss its benefits.

```sql
CREATE TABLE employees (
 employee_id SERIAL PRIMARY KEY,
 email VARCHAR(255) UNIQUE,
 employee_number VARCHAR(10) UNIQUE
);
```
Q29
Multiple Choice

How does the choice between a Surrogate Key and a Natural Key impact database design in PostgreSQL?

SQL Code
Given a 'orders' table with columns 'order_id', 'customer_id', and 'order_date', discuss the design implications of using 'order_id' as a Surrogate Key versus using a composite Natural Key consisting of 'customer_id' and 'order_date'. Implement both designs.

```sql
CREATE TABLE orders (
 order_id SERIAL PRIMARY KEY,
 customer_id INT,
 order_date DATE
);

-- Alternative design
CREATE TABLE orders (
 customer_id INT,
 order_date DATE,
 PRIMARY KEY (customer_id, order_date)
);
```
Q30
Multiple Choice

What challenges arise when using a composite Natural Key in PostgreSQL?

SQL Code
Consider a 'student_courses' table with columns 'student_id', 'course_id', and 'enrollment_date'. Implement a composite Natural Key using 'student_id' and 'course_id'. Discuss the challenges of this approach.

```sql
CREATE TABLE student_courses (
 student_id INT,
 course_id INT,
 PRIMARY KEY (student_id, course_id)
);
```