ViewsViews are one of the greatest things invented since sliced bread. The main beauty of a view is that it can be used like a table in most situations, but unlike a table, it can encapsulate very complex calculations and commonly used joins. It can also use pretty much any object in the db except for stored procedures. Views are most useful when you always need to join the same set of tables say an Order with an Order Detail to get summary calculation fields etc.
| Feature | Rating |
| Works in various kinds of databases | 4 (many databases such as DB II, Oracle, SQL Server support them, MySQL 5, PostGreSQL, SQLite, MSAccess (calls it a query)). There are also some that don’t e.g. MySQL < 5.0 |
| Can be called by multiple applications and interfaces | 5 (generally they can be called anywhere where you can call a table which is pretty much everywhere) |
| Can take an undefined number of arguments | 5 (you can select subsets of columns and rows from a view just like you can from a table) |
| Reusability within the database | 5 (you can reuse them in other views, in stored functions and stored procedures). |
| Can be used to change data in a table without giving rights to a user to change table directly. | 3 In many databases Views are read-only and complex views are rarely updateable. Note that for example some databases such as Oracle,PostgreSQL, MS SQL Server , SQLite will allow you to update even a complex view by using an instead of trigger or rule against the view. MySQL 5, MS SQL Server and some others automatically make one table views updateable. MS Access has a fairly sophisticated update algorithm for queries in that it automatically makes one table and even multi-table queries updateable and allows deletes if you define primary keys appropriately. Also depending on the field a column comes from it will update that and also automatically create child records in child tables if you try to update a child column when there is no record. |
| Can return varying number of fields given different arguments. | 4 – (you can select subsets of columns and rows from a view just like you can from a table). However you can't change the underlying structure e.g. return records from a different set of tables like you can with a stored procedure or function. |
| Long stretches of SQL easy to read. | 3 (A view can often be defined with an administrative designer or using a color coded sql editor so is fairly easy to read) - in general though it is limited to only one select statement or unioned select statements. |
Triggers And RulesTriggers are objects generally tied to a table or view that run code based on certain events such as inserting data, before inserting data, updating/deleting data and before these events happen.
Triggers can be very great things and very dangerous things. Dangerous in the sense that they are tricky to debug, but powerful because no update to a table with a trigger can easily escape the trigger.
They are useful for making sure certain events always happen when data is inserted or updated - e.g. set complex default values of fields, inserting logging records into other tables.
Triggers are especially useful for one particular situation and that is for implementing instead of logic. For example as we said earlier, many views involving more than one table are not updateable. However in DBMS such as PostgreSQL, you can define a rule on a view that occurs when someone tries to update or insert into the view and will occur instead of the insert. The rule can be fairly complex and can layout how the tables should be updated in such a situation. MS SQL Server and SQLite let you do something similar with INSTEAD OF triggers. Note the term Rule is a little confusing in DBMS because they mean quite different things. In Microsoft SQL Server for example a Rule is an obsolete construct that was used to define constraints on tables. In PostgreSQL a Rule is very similar to a trigger except that it does not get triggered per row event and is defined without need of a handling function.
| Feature | Rating |
| Works in various kinds of databases | 2 (many databases such as DB II, Oracle, SQL Server support them, MySQL 5, PostGreSQL,). There are lots that don't e.g. MySQL < 5.0, MySQL 5 limited, MS Access |
| Can be called by multiple applications and interfaces | 5 (it just happens behind the scenes. No application can escape them) |
| Can take an undefined number of arguments | 0 ( strictly for updating data and against a table or view ) |
| Reusability within the database | 0 - No |
| Can be used to change data in a table without giving rights to a user to change table directly. | 4 In general yes for databases that support them |
| Can return varying number of fields given different arguments. | 0 –Triggers are strictly for updating data |
| Long stretches of SQL easy to read. A trigger can often be defined with an administrative designer or using a color coded sql editor so is fairly easy to read | 5 |