Q1
True / FalseIn PostgreSQL, a business key is a key that uniquely identifies a record using business logic.
A business key is a unique identifier derived from business logic, often meaningful in the context of the business, such as an email address or a product code.
Q2
True / FalseIn PostgreSQL, a natural key is always the best choice for a primary key.
While natural keys can be used as primary keys, they are not always the best choice due to potential changes in business requirements or data.
Q3
True / FalseIn PostgreSQL, a candidate key is any column or set of columns that can uniquely identify a row in a table.
A candidate key is an attribute or a set of attributes that uniquely identifies a row and can potentially serve as the primary key.
Q4
True / FalseIn PostgreSQL, a table can have multiple candidate keys.
A table can have multiple candidate keys, but only one of them can be chosen as the primary key.
Q5
True / FalseIn PostgreSQL, a natural key is always a candidate key.
A natural key is a candidate key because it is an attribute that can uniquely identify a row in a table.
Q6
True / FalseIn PostgreSQL, a surrogate key is the same as a business key.
A surrogate key is an artificial key, usually an auto-incremented number, that uniquely identifies a record, whereas a business key is derived from business data.
Q7
True / FalseIn PostgreSQL, using a natural key as a primary key can lead to issues if the business logic changes.
If the business logic changes, natural keys might need to be updated, which can cause complications and potential integrity issues.
Q8
True / FalseIn PostgreSQL, a business key can never be a surrogate key.
A business key is inherently tied to business logic and meaningful data, while a surrogate key is an arbitrary identifier.
Q9
True / FalseIn PostgreSQL, a composite key can be a candidate key.
A composite key, which consists of multiple columns, can serve as a candidate key if it uniquely identifies a row.
Q10
True / FalseIn PostgreSQL, a table must have at least one candidate key that can act as a primary key.
A table must have at least one candidate key to ensure that there is a way to uniquely identify each row, which is a fundamental requirement for defining a primary key.
Q21
Multiple ChoiceWhich 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
);
```
A Business Key is a unique identifier that has a real-world meaning and is used within the business domain. In this case, 'social_security_number', 'email', and 'phone_number' can all serve as Business Keys.
Q25
Multiple ChoiceWhen 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)
);
```
A Surrogate Key is preferred when there is no stable Natural Key or when the Natural Key is complex, lengthy, or prone to change. It is also useful in cases where the Natural Key is sensitive or needs to be kept confidential.
Q29
Multiple ChoiceHow 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)
);
```
The choice between a Surrogate Key and a Natural Key can impact the complexity of the database schema, query performance, and data integrity. Surrogate Keys provide simplicity and immutability, while Natural Keys offer meaningful, business-related identifiers but may complicate the schema.