Database Constraints and Triggers

Implementation and management of database constraints and triggers for maintaining data integrity and automating database operations

Category
Database Management
Level
intermediate
Languages
en → en
Cards
48
Markdown
Database Constraints and Triggers (.md)

Flashcards (48)

#Front (Term / Question)Back (Definition / Answer)ExampleMultiple-Choice Options
1What is a trigger in a database?A trigger is a set of instructions that automatically executes in response to certain events on a table.
2How do constraints help maintain data integrity?Constraints enforce rules on data entry, preventing invalid data from being stored in the database.
3What is a trigger body?The trigger body is the section of the trigger that contains the SQL statements to be executed when the trigger is fired.
4What is the purpose of the SET NULL action in foreign key constraints?The SET NULL action sets the foreign key column to NULL when the referenced record is deleted.
5What is a check constraint?A check constraint is a rule that specifies a condition that must be met for values in a column.
6What is the purpose of the SET DEFAULT action in foreign key constraints?The SET DEFAULT action sets the foreign key column to its default value when the referenced record is deleted.
7What is the purpose of the RAISE statement in a trigger?The RAISE statement is used to generate an error or warning message within a trigger.
8Explain the difference between a primary key and a unique key.A primary key cannot contain NULL values and uniquely identifies a record, while a unique key can contain one NULL value and also ensures uniqueness.
9How can you prevent infinite loops with triggers?To prevent infinite loops, ensure that triggers do not cause actions that would re-invoke themselves.
10Define PRIMARY KEY constraint.A PRIMARY KEY constraint uniquely identifies each record in a table and cannot contain NULL values.
11Give an example of a check constraint.An example of a check constraint is ensuring that the age column must have values greater than 0.
12What are the types of triggers?The main types of triggers are BEFORE triggers, AFTER triggers, and INSTEAD OF triggers.
13What is a performance consideration when using triggers?Triggers can slow down data operations due to additional processing, so they should be used judiciously.
14What is a trigger event?A trigger event is the specific action that causes the trigger to execute, such as INSERT, UPDATE, or DELETE.
15What is the impact of constraints on performance?While constraints ensure data integrity, they can also impact performance due to additional checks during data operations.
16How do you drop a constraint in SQL?You can drop a constraint using the ALTER TABLE statement followed by DROP CONSTRAINT.
17What is a trigger for auditing purposes?A trigger for auditing purposes records changes made to a table, such as who made the change and when.
18What is the purpose of the UNIQUE constraint?The UNIQUE constraint ensures that all values in a column are different from each other.
19What is a cascading update?A cascading update automatically updates related records in child tables when a record in the parent table is updated.
20What happens when a UNIQUE constraint is violated?When a UNIQUE constraint is violated, the database rejects the insert or update operation that causes the duplication.
21Name the four main types of database constraints.The four main types are NOT NULL, UNIQUE, PRIMARY KEY, and FOREIGN KEY.
22What is a FOREIGN KEY constraint?A FOREIGN KEY constraint establishes a relationship between two tables by ensuring that a value in one table matches a value in another.
23Can a trigger call another trigger?Yes, a trigger can call another trigger, but this can lead to complex interactions and should be managed carefully.
24What is the purpose of using triggers in data synchronization?Triggers can be used to automatically synchronize data between tables or databases when changes occur.
25What is a domain constraint?A domain constraint specifies the allowable values for a column, such as data type and range.
26What is a self-referencing foreign key?A self-referencing foreign key is a foreign key that refers to the same table it is defined in.
27When is a BEFORE trigger executed?A BEFORE trigger is executed before an insert, update, or delete operation on a table.
28What is a trigger timing?Trigger timing refers to whether the trigger is executed before or after the triggering event.
29What is the purpose of an AFTER trigger?An AFTER trigger is executed after an insert, update, or delete operation, often used for auditing or logging changes.
30How can triggers help with data validation?Triggers can enforce complex validation rules that cannot be implemented with standard constraints.
31What is a trigger condition?A trigger condition is a specific criterion that must be met for the trigger to execute.
32What is a composite key?A composite key is a combination of two or more columns in a table that together uniquely identify a record.
33What is the difference between a trigger and a stored procedure?A trigger is automatically invoked by database events, while a stored procedure must be explicitly called by a user or application.
34What is a cascading delete?A cascading delete automatically removes related records in child tables when a record in the parent table is deleted.
35What does an INSTEAD OF trigger do?An INSTEAD OF trigger replaces the standard action of an insert, update, or delete operation.
36How do you create a trigger in SQL?You create a trigger using the CREATE TRIGGER statement followed by the trigger name and its specifications.
37What is the difference between a trigger and a constraint?A trigger automates actions based on events, while a constraint enforces rules on data integrity.
38What is a trigger log?A trigger log is a record of actions performed by triggers, often used for debugging or auditing.
39What is a trigger variable?Trigger variables are special variables that hold information about the triggering event, such as OLD and NEW values.
40What is the purpose of the DEFAULT constraint?The DEFAULT constraint provides a default value for a column when no value is specified during an insert.
41What is the role of triggers in enforcing business rules?Triggers can enforce complex business rules that go beyond simple constraints, ensuring compliance with business logic.
42What is a row-level trigger?A row-level trigger executes once for each row affected by the triggering event.
43What is cascading in the context of foreign keys?Cascading refers to automatically updating or deleting related records in child tables when a record in the parent table is updated or deleted.
44What does the NOT NULL constraint do?The NOT NULL constraint ensures that a column cannot have a NULL value.
45What is a database constraint?A database constraint is a rule that limits the types of data that can be inserted into a table to ensure data integrity.
46What is a trigger action?A trigger action is the specific operation that the trigger performs when it is fired.
47What is a statement-level trigger?A statement-level trigger executes once for the entire SQL statement, regardless of how many rows are affected.
48What is the significance of the order of triggers?The order of triggers can affect the outcome of operations, as they execute in the order they are defined.