Q1
True / FalseThe UNIQUE constraint in PostgreSQL ensures that all values in a column are different.
The UNIQUE constraint ensures that all values in a column or a group of columns are unique across the rows of the table.
Q2
True / FalseYou can have multiple UNIQUE constraints on a single table in PostgreSQL.
A table can have multiple UNIQUE constraints, each enforcing uniqueness for different columns or sets of columns.
Q3
True / FalseA UNIQUE constraint automatically creates an index in PostgreSQL.
When a UNIQUE constraint is created on a column or a set of columns, PostgreSQL automatically creates a unique index on those columns.
Q4
True / FalseYou can apply the UNIQUE constraint to multiple columns at once in PostgreSQL.
The UNIQUE constraint can be applied to multiple columns together, ensuring the combination of values in these columns is unique across the table.
Q5
True / FalseThe UNIQUE constraint in PostgreSQL can include NULL values.
In PostgreSQL, a column with a UNIQUE constraint can include multiple NULL values because NULL is not considered equal to any other NULL.
Q6
True / FalseIf you drop a UNIQUE constraint in PostgreSQL, the associated index is also dropped automatically.
Dropping a UNIQUE constraint will also drop the unique index associated with it in PostgreSQL.
Q7
True / FalseYou can add a UNIQUE constraint to an existing column with duplicate values in PostgreSQL.
You cannot add a UNIQUE constraint to an existing column that has duplicate values. All existing values must be unique before applying the constraint.
Q8
True / FalseUNIQUE constraints can be deferred in PostgreSQL, meaning their enforcement can be delayed until the end of a transaction.
PostgreSQL supports deferrable constraints, including UNIQUE constraints, allowing their enforcement to be deferred until the end of a transaction.
Q9
True / FalsePostgreSQL supports the use of the UNIQUE constraint on arrays.
PostgreSQL does not support applying the UNIQUE constraint directly on array data types.
Q10
True / FalseIn PostgreSQL, the UNIQUE constraint can be used in conjunction with the PRIMARY KEY constraint on the same column.
In PostgreSQL, a column with a PRIMARY KEY constraint automatically has a UNIQUE constraint. Applying both constraints on the same column is redundant and unnecessary.
Q22
Multiple ChoiceWhich SQL command creates a table with a UNIQUE constraint on multiple columns in PostgreSQL?
SQL Code
CREATE TABLE orders (
order_id SERIAL PRIMARY KEY,
customer_id INT NOT NULL,
product_id INT NOT NULL,
UNIQUE (customer_id, product_id)
);
This command creates an 'orders' table where the combination of 'customer_id' and 'product_id' is unique, ensuring no customer can order the same product more than once.
Q27
Multiple ChoiceWhich SQL command creates a table with a UNIQUE constraint on a computed column in PostgreSQL?
SQL Code
CREATE TABLE invoices (
invoice_id SERIAL PRIMARY KEY,
total_amount NUMERIC(10, 2),
tax_amount NUMERIC(10, 2),
UNIQUE (total_amount + tax_amount)
);
This command creates an 'invoices' table where the sum of 'total_amount' and 'tax_amount' is unique, ensuring no two invoices can have the same total plus tax value.
Q28
Multiple ChoiceWhich SQL command creates a table with a UNIQUE constraint that applies to a specific condition in PostgreSQL?
SQL Code
CREATE TABLE employees (
employee_id SERIAL PRIMARY KEY,
email VARCHAR(255),
department VARCHAR(50),
UNIQUE (email) WHERE department = 'Sales'
);
This command creates an 'employees' table where the 'email' column is unique only within the 'Sales' department, allowing duplicate emails in other departments.
Q29
Multiple ChoiceWhich SQL command creates a table with a UNIQUE constraint that ensures a column's value is unique across multiple tables in PostgreSQL?
SQL Code
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE
);
CREATE TABLE admins (
admin_id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE
);
ALTER TABLE admins ADD CONSTRAINT unique_email_global EXCLUDE USING btree (email WITH =);
This setup attempts to enforce uniqueness on the 'email' column across the 'users' and 'admins' tables, though in practice, this would require more advanced techniques like triggers.