To define a view constraint in oracle9i

Source: Internet
Author: User
Tags define join query

Before oracle9i, Oracle views are always generated dynamically from their base tables (base tables), and the view cannot contain constraints. The Oracle view simplifies complex queries by hiding all the internal table join operations.

For example, the following view displays ordering information for hardware (widget) products.

Create or Replace view
Widget_orders
As
Select
Cust_name,
Order_date,
Product_Name,
SUM (qty*price) Total cost
From
Customer
Natural Join
Orders
Natural Join
Order_item
Natural Join
Product
where
Product_type = ' widget ';

After we have defined this view, we can make a complex query about it.

SELECT * from Widget_orders where order_date > sysdate-5;

The problem with traditional views is that we cannot define referential integrity constraints (referential integrity constraints) on the view. Starting with Oracle9i, Oracle supports the following view constraints.

Non-null (NOT NULL): This constraint is always inherited from the base table in which the view was created.
Uniqueness constraint (UNIQUE constraints): Oracle9i allows you to define a uniqueness constraint on any column of the view.
Primary KEY (Primary key): We can define primary key constraints directly to the view.
Foreign key (Foreign key): As long as the view has foreign keys that depend on other base tables, foreign key referential integrity is directly present.
As you know, managing a reference complete constraint on a view can greatly affect the performance of a query.

In oracle9i, we can avoid the problems caused by the unconstrained view. The following example creates a PRIMARY key constraint on a view.

Alter view
Widget_orders
Add constraint
Widget_orders_pk
Primary key
(Cust_name, Order_date, Product_Name)
Disable novalidate;



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.