Explore key concepts of SQLite schema design, including data types, normalization, primary keys, and indexing strategies. This quiz helps users evaluate their understanding of efficient schema creation and best practices for optimizing SQLite database structures.
Why is it recommended to use an INTEGER PRIMARY KEY for the main identifier in an SQLite table?
Explanation: Using an INTEGER PRIMARY KEY in SQLite automatically assigns a unique row id to each record and makes indexing more efficient. Limiting the table to positive numbers or reducing storage size by half compared to TEXT is incorrect; these are not direct effects of this key choice. Having an INTEGER PRIMARY KEY is not required for foreign key support but can simplify relationships. The benefit lies mainly in performance and uniqueness.
In SQLite, which storage class is recommended for storing true or false values?
Explanation: Storing true or false values as INTEGER (typically 0 and 1) is the recommended approach in SQLite. SQLite does not have a built-in BOOLEAN storage class, even though 'BOOLEAN' may be accepted as an alias. TEXT and DATE are not appropriate for representing boolean logic as they imply either string data or specific date formatting.
What is the main advantage of adding NOT NULL constraints to columns in an SQLite table?
Explanation: The NOT NULL constraint ensures that each entry in the column contains a value, supporting data reliability. It does not speed up SELECT queries directly, make the database read-only, or prevent duplicates (which requires a UNIQUE constraint). Its primary purpose is to maintain data completeness and consistency.
When defining a table, which best practice should you follow for default values in columns?
Explanation: Defaults help prevent errors when certain columns are omitted in INSERT statements, especially where missing values could cause issues. Setting defaults for every column or always using NULL is unnecessary and can introduce ambiguity. Avoiding default values just to save storage disregards data integrity and application logic.
Why is normalizing your database schema important in SQLite design?
Explanation: Normalization focuses on organizing tables to eliminate redundancy, which reduces inconsistencies and storage requirements. It does not inherently make queries faster, eliminate the need for indexing, or restrict foreign key use. Proper normalization supports efficient, reliable databases.
What is a best practice regarding indexes on foreign key columns in SQLite?
Explanation: Indexing foreign key columns improves JOIN and lookup performance, which is important for relational database efficiency. Avoiding indexes can slow down these operations. While primary keys are indexed by default, foreign keys benefit from manual indexing. Removing all reference column indexes is not advised for performance.
When should you use the AUTOINCREMENT keyword with INTEGER PRIMARY KEY in SQLite?
Explanation: AUTOINCREMENT ensures that row ids are not reused, useful in certain scenarios requiring strict sequence guarantees. Using it all the time can lead to slower inserts and larger database files. It does not provide random values or automatic table sorting. It should be reserved for specific needs.
Which SQLite table naming convention is typically considered best practice for clarity and consistency?
Explanation: Singular, lowercase table names increase clarity and consistency, making schema management easier. Using uppercase letters, spaces, or special characters can complicate queries and reduce readability. While variations are possible, simple lowercase names are widely preferred.
What is a composite primary key in the context of SQLite schema design?
Explanation: A composite primary key uses two or more columns in combination to make each row unique. It is not a foreign key referencing two tables and does not automatically combine unique and NOT NULL constraints. Composite keys do not require numeric columns exclusively; any appropriate columns can be used.
If you need to remove a column from an existing SQLite table, what is considered the recommended procedure?
Explanation: Because SQLite does not support the DROP COLUMN statement in most versions, the best practice is to create a new table without the column, transfer the data, and rename the table. There is no direct DROP COLUMN support as in other databases. Deleting rows or restarting the server does not affect the schema structure.