Triggers / Procedures Flashcards
What are triggers?
A trigger is a special stored procedure attached to a specific table. Managed by DBMS, supported by most RDBMS
What are the differences between triggers and stored procedures?
Give examples of triggers.
- Reduce inventory when an item is sold
- Permit changes to employee records during business hours only
- Automatically ensure the salary is within range for
- the job when an employee receives a job change
- Audit trail / activity logging
- Record changes to bank account
- Withdrawal, deposit, modifying customer profile
- Record changes to bank account
Why use triggers?
- Enforce business rules too complex for CHECK constraints or Referential Integrity
- Automate activity
- Access or modify other table
What is the downside of a trigger?
Downside: will slow down triggering operation
(INSERT, UPDATE or DELETE)
What are stored procedures?
Pieces of executable code stored in the database (small programs) Managed by DBMS and supported by most RDBMS
Give examples of stored procedures.
- Archive last year’s data
- Remove customers data when the customer hasn’t bought anything for more than 3 years
- Implement a price change across a category of products
What are the main features of stored procedures?
- Automate and standardize processes
- Compiled, therefore faster
- Usually written in Transact-SQL (T-SQL)
- Can also be written in a Common Language Runtime (CLR) programming languages: C#, Visual Basic.NET, .
What is the preferred database access method?
C#, VB.NET, Java, … can form SQL and execute it
This is not recommended
- Best practice is for programming languages to call stored procedures for database access
- Executes faster
- Reduces network traffic
- More secure
- Consistent
- DBA approve
Explain stored procedure body statements.
- SQL is non-procedural and set oriented
- Stored procedures require procedural code
- Each database vendor extends SQL with procedural code
- Microsoft uses T-SQL
- Oracle uses Procedural Language SQL (PL/SQL)
- IBM uses SQL Procedural Language (SQL PL)
- MySQL uses SQL/Persistent Stored Module (SQL/P
What are the main features of Selected T-SQL?
- CASE
- DECLARE
- EXECUTE
- FETCH
- RAISERROR
- ROLLBACK
- SET
- Control of Flow
- BEGIN/END
- BREAK
- CONTINUE
- GOTO
- IF/ELSE
- RETURN
- TRY/CATCH
- WAITFOR
- WHILE
How do you interact with stored procedures?
- Invoked by EXECUTE (EXEC) statement in T-SQL
- EXEC is optional, but a good idea
- It allows auto complete to help
- Can accept command line parameters, separated by commas
- Parameters can be positional or name
Explain system stored procedures.
- SQL Server provides many stored procedures
- Many are used for database administration
- Typing the name of a system stored procedure in a query window and pressing F1 will get help on the procedure
- Right clicking the procedure in the Object Explorer and selecting Modify will reveal the procedure code
- sp_columns, sp_help, sp_server_info
Give an example of system stored procedures.
EXEC sp_columns ‘patients’
EXEC sp_columns @table_name=’patients’
EXEC sp_help
sp_help ‘patients’
sp_server_info
What is the structure of a stored procedure?
- A stored procedure consists of:
- Procedure name
- Set of optional parameters
- Routine body
Give a sample procedure.
Increase and vendor are hard coded, not very flixible.
CREATE PROCEDURE update_items_item_cost
AS
UPDATE items
SET item_cost = item_cost * (1 + 0.25)
WHERE primary_vendor_id = 1;
How would you improve procedure flexibility?
(NOT FLIXIBLE)
PRINT ‘**********Before Update**********’
SELECT * FROM items ORDER BY primary_vendor_id;
EXEC update_items_item_cost;
PRINT ‘**********After Update**********’
SELECT * FROM items ORDER BY primary_vendor_id;
VERSUS
(FLIXIBLE) Increase and vendor are parameters,
What is the ALTER SQL Statement?
- ALTER PROCEDURE allows changing an existing stored procedure
- ALTER TABLE allows for changing table structure
- Adding/Removing/Changing of columns, constraints, …
- ALTER DATABASE, ALTER VIEW
Give an example of 3rd parameter conditional updates.
ALTER PROCEDURE update_items_item_cost
@increase DECIMAL(3, 2),
@vendor_id INT,
@update INT = 0 – If @update = 1, perform update, else show what would be updated
AS
BEGIN
IF @update = 1
UPDATE items
SET item_cost = item_cost * (1 + @increase)
WHERE primary_vendor_id = @vendor_id;
ELSE
SELECT item_id, vendor_name, item_cost AS [existing_cost], item_cost * (1 + @increase) AS [proposed_cost]
FROM items
JOIN vendors
ON primary_vendor_id = vendors.vendor_id
WHERE primary_vendor_id = @vendor_id;
END
To test;
PRINT ‘**********Before Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;
PRINT ‘**********No Update**********’
EXEC update_items_item_cost @increase=0.25, @vendor_id=1;
PRINT ‘**********After No Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;
PRINT ‘**********Update**********’
EXEC update_items_item_cost @increase=0.25, @vendor_id=1, @update=1;
PRINT ‘**********After Update**********’
SELECT * FROM items WHERE primary_vendor_id = 1;