You are on page 1of 9

1

Relational system

Briefly, a relational system is a system in which: (a) The data is perceived by the user as tables (and nothing but tables); and (b)The operators at the user’s disposal (e.g., for data retrieval) are operators that generate new tables from old. The relational model is divided into three parts, having to do with 1. data structure, 2. data integrity, and 3. data manipulation, respectively All three parts have their own special terms. The most important of the terms used in connexion with the data structure part are shown in Fig. Shown below, • A relation corresponds is a table. • A tuple corresponds to a row of such a table and an attribute to a column. The number of tuples is called the cardinality and the number of attributes is called the degree. • The primary key is a unique identifier for the table—that is, a column or column combination with the property that, at any given time, no two rows of the table contain the same value in that column or column combination. • Finally, a domain is a pool of values, from which one or more attributes (columns) draw their actual values. For example, the domain labeled S# in Fig. 11.1 is the set of all legal supplier numbers
These terms are summarized in the table (Data structure terminology) Formal relational term relation tuple cardinality attribute degree primary key domain Informal equivalents table row or record number of rows column or field number of columns unique identifier pool of legal values

Page 1 of 9

Table

A table in a relati ional syste em consist ts of Head: a row of co olumn hea adings, tog gether with Body: zero z or mo ore rows of o data valu ues.
Base Tab ble (Base relations) A base ta able is table that the tabl le exists in it ts own right. It given a name via an a appropria ate CREATE E statement.

Kind Of f Relations s 1- Base e relations s
Base relations (a also called real r relation ns): A base relation co orresponds to t what SQ QL e—i.e., it is a named, au utonomous relation. r calls a base table

2- View ws (also ca alled virtual relation ns):
med, derived relation r that represented d within the s system purel ly by its A view is a nam nition in term ms of other named n relatio ons- it does not have any y separate, defin distin nguishable stored s data of o its own (un nlike base relation). r Fo or example

EATE VEIW W GOOD-SU UPPLIERS CRE AS S SELECT S#, S STATUS, CITY FROM S RE STATUS S > 15; WHER Pag ge 2 of 9

3-

Snapshots:

A snapshot is also a named, derived relation, like a view. Unlike a view, however, a snapshot is real, not virtual—i.e., it is represented not only by its definition in terms of other named relations, but also by its own stored data. an example (pseudoSQL): CREATE SNAPSHOT Sc AS SELECT S#, CITY FROM S REFRESH EVERY DAY Creating a snapshot is much like executing a query, except that (i) the result of that query is stored in the database under the specified

name (SC in the example ) as a read-only relation, and (ii) periodically (EVERY DAY in the example) the sbapshot is “refreshed”—i.e., its current value is discarded, the query is executed a and the result of that new execution becomes the new value of the snapshot. 4- Query results: 5Intermediate results:
An intermediate result is a relation (typically unnamed) that results from some relational expression that is nested within some larger expression. For example, consider the following SQL statement:

SELECT DISTINCT S.CITY FROM S WHERE S.S# IN (SELECT SP.S# FROM SP WHERE SP.P# ‘P2’);
The relation resulting from the sub query here is an intermediate result. Like a final query result, it has no persistent existence within the database.

6- Temporary relations
A temporary relation is a named relation, like a base relation or view or snapshot, but (unlike a base relation or view or snapshot) one that automatically destroyed at some appropriate time (e.g., at the end of an interactive session). Base relations, snapshots, and views, by contrast, are more permanent, the sense that they are only destroyed as a result of some explicit user action.

Page 3 of 9

INTRODUCTION TO VIEWS
A view in SQL terminology is a single table that is derived from other tables. These other tables could be base tables or previously defined views. A view is a predefined query on one or more tables called base tables. • A view is not stored as a physical table. • Oracle stores the information you use to create a view, its definition, in its system tables. When you use a view, Oracle activates its definition and perform SELECT statements based on the view’s definition. Views allow you to define exactly what information is delivered from underlying base tables upon which the views are defined Views provide a type of security known as discretionary access control, means that different users can see different rows and columns from the same base tables. Views act like filters, allowing certain information to pass through the filter to users but preventing other information from passing. A column subset view hides selected columns from some users but make them accessible to others. A row subset view users rows meeting stated criteria inaccessible to selected user groups

• • • • •

Types of Views
There are two fundamental types of views: • Complex views;. Contains a subquery that retrieve data from multiple tables, or groups rows using GROUP BY or DISTINCT clause, or contains a function call. • Simple views Retrieve rows from one base table.

Page 4 of 9

What Views Provide
You create views by specifying a SELECT statement that prescribe what rows, columns, and tables are accessed to create the view. Some of the benefits of a view are: – Security – Query Simplification – Privacy – Data Independence Security: Views can restrict a user’s access to base table rows and columns, hiding the information the user should not see. Query Simplification: When a complex multi-table SELECT statement is required to deliver information, you can capture the query as a view, allowing users to access information through the simpler view. Privacy: Views can hide the names of the base tables the constitute the view from users.

Updates on Views
Update mean, add (insert), remove (delete), and update You can not perform remove or add a row if the view contains the following • Group functions • A Group By clause • The DISTINCT keyword • The pseudo column ROWNUM keyword another case where addition record is not permitted which is: • NOT NULL columns in the base tables that are not selected by the view.

Destroying/Altering Tables and Views
• If we decide that we no longer need a base table and want to destroy it, we can use the DROP TABLE command. DROP TABLE Students RESTRICT • • • • Destroys the “Student” table unless some view or integrity constraint refers to Student, if so, the command fails. If the keyword RESTRICT is replaced by CASCADE, Students is dropped and any referencing views or integrity constraints are dropped as well. A view can be dropped using the DROP VIEW command. ALTER TABLE modifies the structure of an existing table.

Page 5 of 9

Defining and Querying One-Table Views
• To create (define) a view, issue the CREATE VIEW statement. CREATE [OR REPLACE] [FORCE | NOFORCE] VIEW <view-name> [(<column-name>,…,<column-name>)] AS <subquery> [WITH CHECK OPTION [CONSTRAINT <constraint-name>]] [WITH READ ONLY]; • • • • • • • OR REPLACE indicates that Oracle should replace an existing view by the same name in the user’s schema. The FORCE option allows you to create a view on a base table that doesn’t yet exist. NOFORCE means that the view cannot be created unless the table it refers to exists already. Subquery is the subquery that retrieves the data from the base table. WITH CHECK OPTION specifies that only rows that satisfy the subquery WHERE clause criteria can be inserted, updated, or deleted. READ ONLY specifies that rows may only be read with the view, not updated, deleted, or inserted. <constraint-name> specifies the name of the WITH CHECK OPTION constraint.

Examples CREATE OR REPLACE VIEW MaleEmployees AS SELECT * FROM Employee WHERE Sex = ‘M’ WITH READ ONLY;

CREATE OR REPLACE VIEW Employee_View AS SELECT SSN, FirstName, LastName, BDate FROM employee;

In SQL, the clause WITH CHECK OPTION must be added at the end of the view definition if a view is to be updated. This allows the system to check for view updatability and to plan an execution strategy for view updates.

Page 6 of 9

Figure 0-1: Schema diagram for the COMPANY relational database schema.

Producing Complex Views
• • You can create a complex view with the same CREATE VIEW you use for a simple one-table view. Complex view contains a subquery that retrieves from – Multiple base tables, – Groups rows using a GROUP BY or – A DISTINCT clause, or – Contains one or more functions or expressions. Complex views are different from simple views because DML operations are not allowed on some complex views.

Page 7 of 9

In VI, we did not specify any new attribute names for the view WORKS_ONI (although we could have); in this case, WORKS_ONI inherits the names of the view attributes from the defining tables EMPLOYEE, PROJECT, and WORKS_ON. View V2 explicitly specifies new attribute names for the view DEPT_INFO, using a one-to-one correspondence between the attributes specified in the CREATE VIEW clause and those specified in the SELECT clause of the query that defines the view.

Removing a view

Query a View
We can now specify SQL queries on a view-or virtual table-in the same way we specify queries involving base tables. For example, to retrieve the last name and first name of all employees who work on 'ProjectX', we can utilize the WORKS_ONI view and specify the query as in QVI:

Update base table using view
The SQL-92 standard allows updates to be specified only on views that are defined on a single base table using just selection and projection, with no use of aggregate operations.

UPDATE MaleEmployees SET SALARY = salary + 50 WHERE ssn = 10041; Page 8 of 9

Listing View Definition
• • Oracle maintain information on views you create in data dictionary views. The data dictionary view user_views contains information about the views you created.

Listing View Definition • To display information about the views you can write the following SQL statement: SELECT view_name, text_length, text FROM user_views ORDER BY view_name; • If you want to list all the views to which you have access. SELECT view_name, text_length, text FROM all_views ORDER BY view_name;

Page 9 of 9