SQL Intro 5.slides
SQL Intro 5.slides
1. Transactions
2. Views
3. Access Rights in SQL
Transactions
A logical unit of work consisting of one or more SQL statements
Atomic transaction
Fully executed or
Rolled back as if it never occurred
Isolation from concurrent transactions
Changes made by a transaction are not visible to other concurrently executing transactions until the transaction completes
VIEWS
Definition
View Creation and Destruction
Updating Views
Types of Views
Views
One database often supports multiple applications
Slightly different pictures of the world.
Views help accommodate this variation without storing redundant data.
Views
In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.)
Example:
Employee(ssn, name, department, project, salary)
Consider a person who needs to know the name and project of employees in the 'Development' department, but not the salary. This person
should see a relation described, in SQL, by
SELECT name, project
FROM Employee
WHERE department = 'Development'
Views
Provide a mechanism to hide certain data from the view of certain users.
Any relation that is not part of the conceptual model but is visible to a user as a “virtual relation” is called a view.
Not physically stored.
Levels of Abstraction
Multiple views
A single Conceptual (Logic) Schema
A single Physical Schema
Levels of Abstraction
Physical Level
Lowest level
How the data is physically stored
It includes
Where the data is located
File structures
Access methods
Indexes
Managed by the Database Administrator
Levels of Abstraction
Levels of Abstraction
Data Independence
A database model exhibits
data independence if:
Destruction
DROP VIEW <view_name>
Data Partitioning
Sometimes the data of a database is partitioned.
Another Example
Consider the following relations
Person(name, city)
Purchase(buyer, seller, product, store)
Product(name, maker, category)
When you enter a query that mentions a view in the FROM clause, the DBMS expands/rewrites your query to include the view definition.
View Expansion
Query using a view
SELECT name, SeattleView.store
FROM SeattleView, Product
WHERE SeattleView.product = Product.name
AND Product.category = 'shoes'
Expanded query
SELECT name, Purchase.store
FROM Person, Purchase, Product
WHERE Person.city = 'Seattle'
AND Person.name = Purchase.buyer
AND Purchase.product = Product.name
AND Product.category = 'shoes'
Another Example
Query using a view
SELECT buyer, seller
FROM SeattleView
WHERE product= 'gizmo'
Expanded query
SELECT buyer, seller
FROM Person, Purchase
WHERE Person.city = 'Seattle'
AND Person.name = Purchase.buyer
AND Purchase.product= 'gizmo'
Updating Views
How can we insert a tuple into a table that doesn't exist?
Updating Views
How can we insert a tuple into a table that "doesn't exist"?
Example:
Employee(ssn, name, department, project, salary)
Developers(name, project)
becomes:
INSERT INTO Employee
VALUES(NULL, 'Joe', NULL, 'Optimizer', NULL)
Non-Updateable Views
Consider the relations
Person(name, city)
Purchase(buyer, seller, product, store)
Updating Views
Most SQL implementations allow updates only on simple views.
The FROM clause has only one database relation.
The SELECT clause contains only attribute names of the relation.
No expressions, aggregates, or distinct specification.
Any attribute not listed in the SELECT clause can be set to NULL.
The query does not have a GROUP BY or HAVING clause.
Updating Views
SQLite views are read-only and thus you may not be able to execute a DELETE, INSERT or UPDATE statement on a view.
A workaround exists
Not in the scope of this class
This is why WITH CHECK OPTION is not implemented
Types of Views
Virtual views
Used in databases
Computed only on-demand – slow at runtime
Always up to date
Types of Views
Materialized views
A physical table containing all the tuples in the result of the query defining the view
Used in Data Warehouses (but recently also in DBMS)
Precomputed offline – fast at runtime
If relations used in the query are updated, the materialized view result becomes out of date
Need to maintain the view, by updating the view whenever the underlying relations are updated.
Data Warehouse
A relational database designed for query and analysis rather than for transaction processing.
Usually contains historical data derived from transaction data.
Separates analysis workload from transaction workload.
Enables an organization to consolidate data from several sources.
Advantages/Disadvantages of Views
ADVANTAGES DISADVANTAGES
Data independence Update restriction
Currency Structure restriction
Improved security Performance
Reduced complexity
Convenience
Customization
Data integrity
Summary
A view is a stored query definition
Views can be very useful
Privacy
Easier query writing
Extensibility
Not all views can be updated unambiguously
Three levels of abstraction in a relational DBMS
Yields data independence, logical and physical
Terminology
Privacy Users should not be able to see and use data they are not supposed to.
Security No one should be able to enter the system and / or impact its behavior without being authorized to do so.
Integrity Authorized users should not be able to modify things they are not supposed to.
Availability Users should be able to see and modify things they are allowed to.
Privileges
Right to perform SQL statement type on objects
Assigned to users or roles (authorization IDs)
Creator of object: all privileges for that object
Administrator: management of system privileges
Privileges
The privileges defined by the ISO standard:
SELECT - retrieve data from a table
INSERT - insert new rows into a table
UPDATE - modify rows of data in a table
DELETE - delete rows of data from a table
REFERENCE - reference columns of a named table in integrity constraints
USAGE - use domains, collations, character sets, and translations
Grant Privileges
Syntax
GRANT <privileges> ON <object>
TO [<users>|<role>]
[WITH GRANT OPTION]
Access matrix : < user > has < right > on < object >
Examples
GRANT INSERT, SELECT ON Movie TO Klaus
Revoke Privileges
Syntax
REVOKE <privileges>
ON <object>
FROM <users>
[RESTRICT | CASCADE]
RESTRICT: only revoke if none of the privileges have been granted by these users.
CASCADE: revoke from all users that have been granted the privilege by these users.
Privilege given from different users must be revoked from all users to loose privilege.
Summary
Security of DB and their applications is extremely important.
Roles make privileges with many users manageable.
Views also play an important role.
Fine granular access restriction on objects is very important.
https://www.xkcd.com/327/