Explore fundamental concepts of primary keys, foreign keys, and table constraints with this easy quiz designed to enhance your understanding of relational database integrity and structure. Perfect for brushing up on key relational database management topics and ensuring your foundational SQL knowledge is strong.
Which constraint ensures that each row in a database table has a unique, non-null identifier, such as a CustomerID in a customer table?
Explanation: A PRIMARY KEY constraint ensures each record in a table is uniquely identifiable and cannot contain NULL values. FOREIGN KEY links two tables but does not guarantee uniqueness in the referencing table. UNIQUE prevents duplicate values but allows NULLs unless specified. INDEX improves performance but does not enforce uniqueness or non-null values.
What is the main role of a foreign key constraint in a relational database, for example, relating Orders.CustomerID to Customers.CustomerID?
Explanation: A foreign key constraint establishes a link between columns in two tables, enforcing referential integrity. It does not ensure uniqueness (that's handled by primary or unique keys). Foreign keys are not restricted by data type like storing text only, nor do they create temporary tables; they're about establishing relationships.
Which constraint, when applied to the 'Email' column in a table, ensures that no two records have the same email address?
Explanation: The UNIQUE constraint prevents duplicate values in a specific column or set of columns. CHECK validates data based on a given condition, INDEX assists in search performance, and FOREIGN KEY relates to enforcing relationships between tables rather than uniqueness within a column.
When defining a primary key on a table, what happens if you try to insert a record with a NULL value in the primary key field?
Explanation: A primary key does not allow NULL values, so any attempt to insert such a record results in a failure. Allowing NULL values or converting them to zero would violate the uniqueness and non-null property. A mere warning without block would undermine data integrity and is not standard SQL behavior.
If a table has a foreign key with an ON DELETE CASCADE action and the referenced row in the parent table is deleted, what happens to related rows in the child table?
Explanation: ON DELETE CASCADE automatically removes rows in the child table referencing the deleted parent row, maintaining referential integrity. If 'No action' was specified, nothing would occur; a syntax error is not the outcome of this action, and the table or database does not get locked as a result of this constraint.
Which of the following key types can a table have more than one of?
Explanation: A table can have multiple UNIQUE keys, ensuring uniqueness across multiple columns or combinations. There can only be one PRIMARY KEY per table. FOREIGN KEYS can be multiple as well, but they establish relationships rather than uniqueness. 'CLUSTERED KEY' is a specific index concept, often singular in practice.
Which of these conditions must be met for a foreign key column to reference another table’s primary key?
Explanation: For referential integrity, the data types of the foreign key and the referenced key must be compatible, though column names do not need to match. Tables do not have to be temporary, and while primary keys are always unique, the foreign key itself does not have to be unique.
Which constraint would you use to ensure only positive values can be entered in an ‘Age’ column of a table?
Explanation: A CHECK constraint can be used to enforce that only certain values, such as positive numbers, are allowed in a column. FOREIGN KEY is for referential integrity, PRIMARY KEY is for uniqueness and identity, and DEFAULT provides an initial value but does not validate user input.
What term is used when a primary key is composed of more than one column, such as using both 'OrderID' and 'ProductID' in an order details table?
Explanation: A composite key is formed from two or more columns to uniquely identify a record. 'Auto key' or 'Sequential key' are not standard terms in relational databases, and 'External key' is not a recognized term for this concept either.
Can a foreign key column in a child table contain duplicate values referencing the same parent row?
Explanation: A foreign key simply references a parent table's primary or unique key and does not require uniqueness in the child table. Multiple rows in the child can reference the same parent. Whether the column is indexed or the type of the primary key does not affect this rule.