Challenge your understanding of SQLite views and triggers with these beginner-friendly questions. This quiz covers key concepts, practical uses, and basic syntax related to working with views and triggers in SQLite databases.
Which statement best describes the main purpose of a view in SQLite?
Explanation: A view in SQLite is a virtual table created by a query, allowing users to see data from one or more tables as if it were a single table. It does not store data physically but refers to data dynamically. A physical copy or a temporary table is not created when you define a view, so the other options are incorrect. Enforcing data integrity constraints is not the primary purpose of a view; that is more the role of triggers or constraints.
How would you create a simple view that shows only the 'name' and 'age' columns from a 'people' table?
Explanation: The correct syntax to create a view in SQLite uses the 'CREATE VIEW' statement followed by the desired SELECT statement. 'NEW VIEW', 'INSERT VIEW', and 'VIEW ... :=' are not valid SQLite syntax and will cause syntax errors. Only the first option correctly represents the process for creating a view.
What is the primary function of a trigger in SQLite, for example when automatically updating a timestamp after a record changes?
Explanation: Triggers are designed to automatically run predefined actions such as updating values after inserts, updates, or deletions. They are not intended for storing queries, backing up databases, or managing user permissions, which require different features or external tools. The other choices are unrelated to the purpose of triggers.
If you try to update data using a view in SQLite, what must be true for the update to succeed?
Explanation: For a view to be updatable in SQLite, it should be based solely on a single table and not involve compound features like joins, groupings, or aggregates. If a view is based on multiple tables, or if it's particularly complex, updates through the view typically will not work. ORDER BY clauses or subqueries do not determine whether a view is updatable.
Which event can a trigger in SQLite respond to?
Explanation: SQLite triggers can be set to activate when an INSERT, UPDATE, or DELETE is performed on a specific table. They do not respond to user creation, schema alterations, or export actions. The other options are not related to events that SQLite triggers can handle.
What is a main use of an INSTEAD OF trigger in SQLite, such as when working with complex views?
Explanation: INSTEAD OF triggers are often used on views in SQLite because views cannot normally be directly modified. These triggers allow you to define what happens when an insert, update, or delete is attempted on a view by redirecting the operation as desired. The other options describe unrelated features not applicable to INSTEAD OF triggers.
Which statement will remove a view named 'summary' from a SQLite database?
Explanation: The correct way to permanently remove a view in SQLite is to use the 'DROP VIEW' statement. 'DELETE VIEW' or 'REMOVE VIEW' are not valid SQL syntax, and 'ALTER VIEW ... DROP' does not exist as a command. Therefore, only the first option is appropriate.
If you want to automatically log changes after a record is updated in a table, which trigger should you use in SQLite?
Explanation: An AFTER UPDATE trigger runs after a row has been updated and is ideal for tasks such as logging changes. BEFORE INSERT and INSTEAD OF DELETE are used for different events, while BEFORE SELECT triggers do not exist in SQLite. Only the AFTER UPDATE trigger is suitable for this scenario.
What happens to the data shown by a view in SQLite when the underlying tables are changed?
Explanation: Because views in SQLite are virtual, they always show the most current data from their base tables when queried. Views do not hold their own persistent data, so manual refreshing, snapshots, or recreating the view are not required to see updated data. The other options describe behaviors of materialized views, not standard SQLite views.
In which situations can you specify a trigger to execute in SQLite, for example, BEFORE or AFTER a database event?
Explanation: SQLite allows you to specify whether a trigger fires BEFORE or AFTER an insert, update, or delete event on a table. Triggers cannot be tied directly to backups, database startup, or restricted only to AFTER events. Thus, the correct answer is that both BEFORE and AFTER timing are supported.