Explore key concepts of primary keys, foreign keys, and constraints in SQLite with this quiz. Strengthen your foundational SQL knowledge and understand how data integrity is maintained using unique identifiers and relational links.
What is the main purpose of declaring a PRIMARY KEY on a column in an SQLite table?
Explanation: A PRIMARY KEY uniquely identifies each row and requires every value to be unique and non-null. It does not allow duplicate or null values, ensuring data integrity. Allowing duplicate values would defeat the purpose of a primary key, and it is unrelated to storing large text values or preventing updates, which are handled differently.
When using a composite primary key in SQLite, which of the following is true?
Explanation: A composite primary key ensures that the combined values of the specified columns are unique for each row, not the individual columns. Each column does not need to be unique separately, and typically neither can have null values. An index is created for the combination, not for only one column.
Which keyword is used in SQLite to create a foreign key relationship between two tables?
Explanation: The FOREIGN KEY keyword defines a relationship between columns in two tables, referencing the primary key in the related table. OUTER KEY, DIFFERENT KEY, and SIDE KEY have no meaning in this context and are not valid SQLite keywords.
What happens if you try to insert a value into a child table's foreign key column that does not exist in the referenced parent table in SQLite?
Explanation: If a foreign key value does not match an existing value in the parent table, SQLite enforces referential integrity and prevents the insertion, returning an error. Values are not added automatically, they are not ignored, and the row is not inserted with a null foreign key unless null is allowed and no constraint is violated.
In SQLite, which constraint would you apply to a column so that all its values must be different from each other, but null values are allowed?
Explanation: The UNIQUE constraint allows any number of null values but enforces all non-null values to be distinct. PRIMARY KEY also enforces uniqueness but does not allow nulls, while FOREIGN KEY defines relationships, not uniqueness. DEFAULT sets a value and does not enforce uniqueness.
What is the effect of adding a NOT NULL constraint to an SQLite column?
Explanation: Adding a NOT NULL constraint ensures that null values cannot be entered in the column, maintaining data completeness. It does not make the column a primary key or interfere with uniqueness. Enabling only null values would contradict the NOT NULL constraint.
If a foreign key in SQLite is defined with ON DELETE CASCADE, what happens when a referenced row in the parent table is deleted?
Explanation: The ON DELETE CASCADE action ensures that deleting a parent row will automatically delete all related child rows. It does not drop foreign key constraints or simply prevent deletion. Setting related rows to null is accomplished using ON DELETE SET NULL.
Which SQLite constraint can be used to set a predefined value if no value is provided for a column during insertion?
Explanation: The DEFAULT constraint assigns a specified value to the column when none is provided during the insert. PRIMARY KEY and UNIQUE control row uniqueness, and AUTO does not exist as a constraint in SQLite.
How can you make an integer primary key in SQLite automatically get a unique value for each new row?
Explanation: INTEGER PRIMARY KEY AUTOINCREMENT ensures each new row automatically receives the next available integer value. CHAR PRIMARY KEY and TEXT PRIMARY KEY require manual value entry. The UNIQUE constraint does not provide auto-incrementing functionality.
If you try to insert duplicate values into a column with a UNIQUE constraint in SQLite, what will be the outcome?
Explanation: Inserting duplicates into a UNIQUE column is not allowed; SQLite will reject the operation with an error. Accepting duplicates or deleting them silently would violate the uniqueness rule, and applying a UNIQUE constraint does not turn the column into a primary key.