Q1
True / FalseIn PostgreSQL, a foreign key is used to link two tables together.
A foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table, creating a link between the two tables.
Q2
True / FalseA foreign key in PostgreSQL can reference columns in the same table.
This is known as a self-referential foreign key, where a table's foreign key references its own primary key.
Q3
True / FalseIn PostgreSQL, foreign keys do not enforce referential integrity.
Foreign keys enforce referential integrity by ensuring that the value in the foreign key column(s) matches a value in the referenced table's primary key column(s).
Q4
True / FalseIn PostgreSQL, you can define a foreign key constraint using the ALTER TABLE statement.
You can add a foreign key constraint to an existing table using the ALTER TABLE statement.
Q5
True / FalseA foreign key in PostgreSQL can reference a unique constraint instead of a primary key.
PostgreSQL allows foreign keys to reference columns with unique constraints as well as primary keys.
Q6
True / FalsePostgreSQL automatically creates an index on the foreign key column.
PostgreSQL does not automatically create an index on the foreign key column, though creating an index can improve performance.
Q7
True / FalseIn PostgreSQL, ON DELETE CASCADE ensures that when a referenced row is deleted, all rows with matching foreign key values are also deleted.
ON DELETE CASCADE is an action that can be defined for a foreign key to automatically delete rows in the referencing table when the referenced row is deleted.
Q8
True / FalsePostgreSQL allows foreign key constraints to be deferred.
PostgreSQL supports deferrable foreign key constraints, which can be set to check constraints at the end of the transaction rather than immediately.
Q9
True / FalseIn PostgreSQL, you cannot define a foreign key constraint that references multiple columns (composite key).
PostgreSQL supports composite foreign keys that reference multiple columns.
Q10
True / FalseIn PostgreSQL, foreign key constraints can reference tables in other databases.
Foreign key constraints in PostgreSQL can only reference tables within the same database. They cannot reference tables in different databases.
Q25
Multiple ChoiceCan 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)
);
In PostgreSQL, a foreign key can reference a unique constraint in another table, not just the primary key. This allows flexibility in database design.
Q29
Multiple ChoiceCan 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);
In PostgreSQL, a foreign key can reference a primary key in the same table, commonly used for hierarchical data structures like employee-manager relationships.