Referential integrity based on a view

Hi,
I am having difficulty in trying to build referential integrity based on a view.
     I have explained below with an example:
     I have a table called 'vehicles' which is the master table.
     CREATE TABLE vehicles(
     type VARCHAR2(20),
     model_number NUMBER(5),
     price NUMBER(5),
     CONSTRAINT vehicles_pk PRIMARY KEY(type, model_number)
     INSERT INTO vehicles(type, model_number, price) VALUES ('CAR', 1, 100);
     INSERT INTO vehicles(type, model_number, price) VALUES ('BIKE', 1, 50);
     INSERT INTO vehicles(type, model_number, price) VALUES ('CAR', 5, 200);
     INSERT INTO vehicles(type, model_number, price) VALUES ('BIKE', 7, 50);
     I have a view called 'vu_cars' which lists only the CAR from the table 'vehicles'.
     CREATE VIEW vu_cars
     AS
     SELECT model_number
     FROM vehicles
     WHERE
          type = 'CAR';
     I have a table called 'car_properties' which contains the car properties and has referential integrity based on the view 'vu_cars'
-- This create statement throws an error, am I missing something ... ?
     CREATE TABLE car_properties(
     model_number VARCHAR2(20),
     number_of_doors NUMBER(5),
     PRIMARY KEY(model_number),
     CONSTRAINT vu_cars_fk FOREIGN KEY (model_number) REFERENCES vu_cars(model_number)
-- Actually I wanted to execute the below insert statements, but am stuck with the above create statement of the table car_properties. Is there anyway to resolve this ?
     --Below should be inserted correctly:
     INSERT INTO car_properties(model_number, number_of_doors) VALUES (1, 4);
     INSERT INTO car_properties(model_number, number_of_doors) VALUES (5, 2);
     --This should throw an error:
     INSERT INTO car_properties(model_number, number_of_doors) VALUES (7, 2);

One approach would be to:
1. Add a TYPE column to CAR_PROPERTIES.
2. Add a check constraint on the TYPE column in CAR_PROPERTIES that requires it to equal 'CAR'.
3. Create a foreign key constraint on CAR_PROPERTIES referencing both MODEL_NUMBER and TYPE in VEHICLES. This requires a primary or unique constraint on these two columns in VEHICLES, but you already have one. However, both columns should have the same data type in both tables.
It might look something like this:
SQL> CREATE TABLE vehicles(
  2  type VARCHAR2(20),
  3  model_number NUMBER(5),
  4  price NUMBER(5),
  5  CONSTRAINT vehicles_pk PRIMARY KEY(type, model_number)
  6  );
Table created.
SQL> INSERT INTO vehicles(type, model_number, price) VALUES ('CAR', 1, 100);
1 row created.
SQL> INSERT INTO vehicles(type, model_number, price) VALUES ('BIKE', 1, 50);
1 row created.
SQL> INSERT INTO vehicles(type, model_number, price) VALUES ('CAR', 5, 200);
1 row created.
SQL> INSERT INTO vehicles(type, model_number, price) VALUES ('BIKE', 7, 50);
1 row created.
SQL> CREATE TABLE car_properties(
  2  -- Note new column with both not null and check constraint
  3  type VARCHAR2(20) not null check (type = 'CAR'),
  4  -- Note change in data type to be the same as vehicles table
  5  model_number NUMBER(5) not null,
  6  number_of_doors NUMBER(5),
  7  PRIMARY KEY(model_number),
  8  -- Note new constraint referencing vehicles table
  9  CONSTRAINT vehicles_fk FOREIGN KEY (type, model_number) REFERENCES vehicles(type, model_number)
10  );
Table created.
SQL> INSERT INTO car_properties(type, model_number, number_of_doors) VALUES ('CAR', 1, 4);
1 row created.
SQL> INSERT INTO car_properties(type, model_number, number_of_doors) VALUES ('CAR', 5, 2);
1 row created.
SQL> INSERT INTO car_properties(type, model_number, number_of_doors) VALUES ('BIKE', 7, 2);
INSERT INTO car_properties(type, model_number, number_of_doors) VALUES ('BIKE', 7, 2)
ERROR at line 1:
ORA-02290: check constraint (TEMP.SYS_C0095831) violated
SQL> INSERT INTO car_properties(type, model_number, number_of_doors) VALUES ('CAR', 7, 2);
INSERT INTO car_properties(type, model_number, number_of_doors) VALUES ('CAR', 7, 2)
ERROR at line 1:
ORA-02291: integrity constraint (TEMP.VEHICLES_FK) violated - parent key not
found
SQL> INSERT INTO car_properties(type, model_number, number_of_doors) VALUES (NULL, 1, 4);
INSERT INTO car_properties(type, model_number, number_of_doors) VALUES (NULL, 1, 4)
ERROR at line 1:
ORA-01400: cannot insert NULL into ("TEMP"."CAR_PROPERTIES"."TYPE")Defining a default value for the TYPE column in CAR_PROPERTIES (to set it to 'CAR') might make this approach a little less obtrusive.

Similar Messages

  • Using geocode in a map based on a view where two datasets are joined

    Hi,
    I'd like to create a map based on a view in which two datasets are joined, where one of the datasets has a geocode column.
    Unfortunately this is not working... It seems that a custom view where two datasets are joined together where one of them has a geocode column makes the geocode column invisible for the map component.
    The following cases do work but we like to combine them so that we can combine different datasets in Studio without using the Integrator:
    - I can create a custom view in which we join two datasets > the view can be used as a source for different component but not the map component (it seems that the geocode column is not found)
    - I can create a custom view with a geocode column in it and that one can be used as a source for a map
    What I'd like to show is that it is possbile to have one datasets coming from a source system and that it is easy to combine an additional excel sheet with geo-information and maybe another excel sheet with different measures. This should be possible since you can join different datasets together in a custom view.
    Thanks in advance,
    Richard

    Oh sorry, we have two base views and one custom view:
    base view TRIS_Omzet which gets it data from an OBI server and has data like revenue, customer, time, etc.
    base view Endeca_Klanten which gets it data from an excel sheet, this view contains the name of the customer and the geocode of the customer's location
    We created one custom view (Omzet_Geo) where the two base queries are joined together using the following query:
    DEFINE V1 AS SELECT
    "Endeca_Klanten.Klanten" AS Klant,
    "Endeca_Klanten.Coordinaten" AS Coordinaten
    from Endeca_Klanten;
    DEFINE V2 AS SELECT
    "TRIS_Omzet.Naam_Consultant" AS Naam_consultant,
    "TRIS_Omzet.Jaar" AS Jaar,
    "TRIS_Omzet.Jaar_week" AS Jaar_week,
    "TRIS_Omzet.Actual" AS Actual,
    "TRIS_Omzet.Omzet" AS Omzet,
    "TRIS_Omzet.Partij" AS Partij,
    "TRIS_Omzet.Uren" AS Uren
    FROM TRIS_Omzet
    DEFINE Omzet_Geo AS SELECT
    V1.Klant AS Klant,
    V1.Coordinaten AS Coordinaten,
    V2.Naam_consultant AS Naam_consultant,
    V2.Jaar AS Jaar,
    V2.Jaar_week AS Jaar_week,
    sum(V2.Actual) AS Actual,
    sum(V2.Omzet) AS Omzet,
    V2.Partij AS Partij,
    sum(V2.Uren) AS Uren
    from V1
    right join V2
    on (V1.Klant=V2.Partij)
    group by Klant, Coordinaten, Naam_consultant,Jaar,Jaar_week,Partij
    Our view definition in the GUI looks like the following, as shown in the screenshots the datatype of the coordinated column is a Geo column both in the base view and the custom view. However the view still can't be selected to be used as a source for the map component.
    The two base views:
    The custom view:
    regards,
    Richard

  • Distributed referential integrity

    Hi, I have a question. If I am on the wrong forum please direct me as I could not find a specific forum for distrituted databases and these may be a application development question.
    I am implementing a simple distributed database application. At this point I am only concerned with creating two tables each on a different server. For simplicty I will name them table1 and table2. This would be the schema if the tables were not distributed and were located on the same server.
    CREATE TABLE table1 (
    table1_id NUMBER CONSTRAINT table1_pk PRIMARY KEY
    CREATE TABLE table2 (
    table2_id NUMBER,
    table1_id NUMBER,
    CONSTRAINTtable2_pk PRIMARY KEY (table2_id, table1_id),
    CONSTRAINT table2_fk FOREIGN KEY (table1_id) REFERENCES table1(table1_id)
    I have referred to the documentation in both Oracle® Database Administrator's Guide 10g Release 2 (10.2) and Oracle Database Application Developer's Guide. I know that I cannot use declarative referential integrity but how can I distribute the tables so that I can be sure that when I insert a row into table2, I can check to see if there is a matching row in table1 for table1_id?

    Realistically, I expect that you need to reconsider your architecture.
    It does not make sense to check for a matching row in table1 in a remote database while inserting data into table2 in a local database. The best case scenario would be that every insert into table2 would incur the overhead of a network round-trip plus the cost of querying the table in the remote database. If the network went down or if either of the two databases went down, the application would fail, which generally defeats the purpose of a distributed application. Plus, there would be all sorts of concurrency issues (i.e. I delete a row, but before I commit you query the row, see that it exists, and insert a child row. I commit, leaving your row orphaned).
    Assuming you really need a distributed architecture, you would want to replicate table1 to both the local and remote nodes. You would then declare referential integrity constraints between your local copy of table1 and table2 (as well as between table1 and table2 on the remote database, assuming you want table2 data available there as well). Your replication process (preferrably using Streams but potentially using multi-master materialized views instead) would then have to be coded to deal with errors because of the asynchronous nature of replication (i.e. to notice that database1 deleted a parent row that you just inserted a child row for and resolve the conflict appropriately).
    Justin

  • BI Publisher report based on VO (View Objects)

    Hi,
    I would like to know, can we generate BI Publiher report based on VO (view Objects ). Actually we are creating VOs for ADF and want to know if that can be used for generting BIP report.
    Thank,s
    Niraj

    Hi Niraj
    Yes, you can, there is a method on the VO to get the data from it in an XML format. Then use our APIs >> documentation to format it with a template.
    Regards, Tim

  • How to create ADF UI based on polymorphic view objects

    Hi,
    I'm using JDeveloper build JDEVADF_11.1.1.3.PS2_GENERIC_100408.2356.5660. I created a simple application based on Steve's example #10 [url http://blogs.oracle.com/smuenchadf/examples/]ViewRow and EntityObject Polymorphism.
    I added a men-only attribute "Age" to the "Men" VO and filled it with an arbitrary value in MenRowImpl.java:
    public String getAge() {
         return (String) "45";//getAttributeInternal(AGE);
    }Running the TestModule works perfectly - a row of type "Men" displays the "Age" attribute whereas a row of type "Women" doesn't.
    Afterwards, I setup an ADF ViewController Project and created a JSPX with a read-only form based on the "People" datacontrol with navigation buttons. Running that page always shows the same set of attributes independent of the row type. That's expected behaviour because the binding is defined at design time.
    So my question is: can I somehow achieve the same behaviour for polymorphic VOs as the business component tester shows?
    Kind regards,
    Markus

    Andrejus' example shows how to calculate different values for the same attribute based on overridden view objects. That's not exactly what I'm looking for. I need to display a (at least partly) different set of attributes that changes while the user scrolls through the records of the result set.

  • How to create materialized view based on a view?

    Hi,
    I hope this is not very far fetched idea.
    I have a very complex view and I would like to replicate it 'in place' that is I would like to make a materialized view that is based on this view complex view. I would like to use this materialized view (i.e table) to query data instead of using the original view, since it takes Oracle some 10-15 seconds to execute my query on the original view and I am not allowed to create indexes on most of the tables that are included in the original view.
    Can this be done?
    Best regards,
    Tamas Szecsy

    The best way to do this is to create a materialzed view based on the underlying code of the original view. If you don't have this handy, issue the following in sqlplus:
    select text
    from user_views
    where view_name = 'NAME_OF_VIEW'
    You can then cut and paste the sql statement into your create materialized view statement.
    Please note, you will probable have to set the long parameter to a higher value to reveal the complete statement for example:
    SQL> set long 2048

  • Is it possible to delete data from a block based on a view?

    I have a data block based on a view. I have set the delete allowed property to 'yes' using the object navigator, have set the database permission to allow delete, done a search through all the code to see where the delete_allowed property on the block may be reset in some trigger yet cannot delete records from the block.
    Any ideas?

    There are at least two/three ways to do this...
    Put an on-delete trigger on the block of the view with code something like this...
    delete from mytable where mytable_id = :myblock.mytable_id;
    ...or...
    dont do the above and add an instead of trigger to the view in the database. The instead of trigger intercepts the delete statement that would fail and executes code that you write in the trigger to go delete in mytable "instead of" the view.
    ... or...
    if you want to base a block on a procedure.. See this thread... Notice the metalink id in the thread..
    Re: Data block based on a procedure..
    Message was edited by:
    Mark Reichman

  • Query based on a view

    Hello,
    Software: forms 10g
    problem: I have one block in my form based on a view. It works fine for my English language selection. However, when I switch to French I encounter an error.
    Example:
    Block name: REQUESTS_VIEW
    View_name: REQUESTS_VIEW
    Table: Requests
    Item_name: Office_id
    Column_name: Office_id
    Updatable/Queryable
    Item_name: Office_Name
    Column_name: Off_Eng_name
    Query only option selected
    LOV_OFFICE attached to item
    Office table contains the following columns:
    Office_id, Off_eng_name, Off_fre_name
    In my view I use a decode function for each variable that needs to be translated so that I select the correct column from my table. I did the same for the LOV attached to the office_name item.
    Problem: I cannot set the item_property column name to point to Off_fre_name when user selects French as a language, therefore, I get an error when I query. Oracle does not allow me to do that.
    Possible solution: To redesign each table and add a language column as follows:
    Office table
    Office_id, Office_name, language
    1, Head Office, 1
    1, Bureau Chef, 0
    language table
    language_id, language_name
    0, Français
    1, English
    However, this requires a redesign of my schema. Is there another way I can do this without changing the design structure?
    Thank you very much!
    Claudia

    As of 'I cannot set the item_property column name to point to Off_fre_name when user selects French as a language, therefore, I get an error when I query', I feel that you need to have two items-one for Off_Eng_name and another one for Off_fre_name on your form regardless of the language choice, but you need per language choice to hide the other language item. This way you don't have the above headache.

  • Issue with Referential Integrity check in Oracle VPD Policy

    Hi,
    Lets assume I have two tables - Customer and Order, with cust_id in Order table referring to primary key of Customer table.
    Example Data;
    Customer
    cust_id Name
    1 abc
    2 def
    3 ghi
    Order
    Order_id cust_id Order_type
    1 1 A
    2 2 A
    3 1 B
    Now I have policies defined on both the tables;
    - for "Select, Insert, Update" queries on Customer table.
    - for "Select" queries on Order Table.
    Policy 1 on Order Table;
    Irrespective of the user, predicate = 'Order_type = ''A'''
    Policy 2 on Customer Table;
    Irrespective of the user, predicate = '(select count(1) from order o where o.cust_id = customer.cust_id and o.order_type = ''B'') > 0'
    My intention is to show only those customers who have atleast one order of type 'B'. And this policy works fine in case a user tries to read data from customer table. (for example, record for cust_id = 2 will not be returned as it don't have any orders of type "B")
    However, when a user tries to insert record in Order Table, because of the existing referential integrity constraint, the Policy on Customer table is also getting triggered. And an exception is being raised "ORA-28113: policy predicate has error".
    Could someone please explain why this is happening ?

    I'm afraid, there is no such a mean.
    At least I do not know about it.

  • ADF BC: Creating updatable VO based upon DB View with "instead of" trigger

    Hello all,
    I have got an interesting issue. I have an Oracle DB view that is used to hide some complexity in the underlying DB design (it does some unions). This view is updatable because we have created an "instead of" update trigger to update the correct table when a row is updated. This is working fine in SQL.
    Next, we have created an ADF Entity object based upon the view, specifying an appropriate PK for the DB View. Then, we have created an updatable VO based upon the EO. All well and good so far. The issue we have is in trying to commit changes to the DB - because the ADF BC framework is trying to lock the row to update (using SELECT ... FOR UPDATE), it's not working because of ORA-02014 - cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc.
    This leads me to thinking about overridding doSelect() on the EO as hinted here http://radio.weblogs.com/0118231/stories/2005/07/28/differenceBetweenViewObjectSelectAndEntityDoselectMethod.html
    As a temporary test, we have over-ridden the EO's doSelect to call super.doSelect(false) and it does work, although we will have lost update issues as detailed in Steve's article.
    My questions:
    1). Is overriding doSelect() the correct thing here? Perhaps there is a better way of handling this problem? I do have a base EO class from which all of the EO's extend, so adding this behavior should be straightforward.
    2). Does anyone have example doSelect implementation? I am thinking of overriding doSelect for my EO and calling super.doSelect (lock=false), but then I need to deal with some possible exceptions, no?
    Kind regards,
    John

    Hi John,
    I have exactly the same issue as you experienced back in January. I have a complex data modelling requirement which requires the need to pivot rows into columns using ROW_NUMBER() and PARTITION clauses. To hide the complexity from the middle tier, I have created a database view and appropriate INSTEAD OF triggers and mapped my EO to the view. I have overriden the lock() method on the EO implementation class (to avoid ORA-02014) and would like to try the same solution you used with the pl/sql call to lock the record.
    My question is, how did you manage the release of the lock if the transaction was not rolled back or committed by your application i.e. if the user closed the browser for instance.
    In my naivity, I would like to think that the BC4J framework would release any locks for the database session when it found the servlet session to be terminated however my concern is that the lock would persist and cause complications.
    Any assistance greatly appreciated (if you would be willing to supply your lock() method and pl/sql procedure logic I would be even more grateful!).
    Many thanks,
    Dave
    London

  • Error when updating data in a form based in a view

    Hi:
    i have 5 tables for enterprise data (telephone, fax and so on). i want to create a form based in all those tables, so i create a view over all the 5 tables and create the form. but when calling the portlet and trying to alter (update, insert, etc.) the data i get this error. Seems that i missed to do something with my tables but i don't know what does that non key-preserved thing mean.The error is the following:
    Error: Cannot link to the module: 1901051912 ORA-01445: cannot select ROWID from a join view without a key-preserved table (WWV-16062)
    An unexpected error occurred: ORA-01445: cannot select ROWID from a join view without a key-preserved table (WWV-16016)
    An unexpected error occurred: ORA-01445: cannot select ROWID from a join view without a key-preserved table-SELECT ROWID, IDBASE,NOMBRE,NOMBRE_FISCAL,NOTAS,BAJA,RETENCION,PREVISION_PAGOS,IVA,CIF,ID_DIRECCION,IDBASE_DIRECCION,CALLE,NUMERO,PISO,MANO,CODIGO_POSTAL,CIUDAD,ID_TELEFONO_EMP,IDBASE_TELEMP,TELEFONO_EMPRESA,ID_FAX_,IDBASE_FAXEMP,FAX,ID_EMAIL_EMP,IDBASE_EMAILEMP,E_MAIL FROM PTSS.A_VISTA where IDBASE=1 ORDER BY ROWID ASC (WWV-16016)
    Any help will be welcomed.
    Thanks in advance.
    Regards. Urko.

    You cannot update, insert, or delete records from a form that's based on a view. That is because rowid is not in the view, so it cannot lock records.

  • Creating a dynamic Selection List based on a View Object

    Hello,
    I'm new to JDeveloper and I would like to create a JSP Page with dynamic Selection List based on a runtime query or based on a view object (BC4J). The selection made by an user should serve another dynamic query with the necessary parameters that I built using createViewObjectFromQueryStmt(). By now I tried this using the InputSelectLOV from the Component Palette in JDeveloper. But without success. By the way: the selection list is not very large (5 values), so it's not necessary to have a form finding the desired value.
    Maybe someone had experience about creating this already. Please give me a tip or a little example.
    Thanks.

    http://otn.oracle.com/products/jdev/howtos/jsp/renderers.html

  • What  is self referential integrity? How does it effect the Database?

    Hello Gurus,
    What is self referential integrity?
    How could it is achieved and implemented?
    and what is effect on the Database?
    Thanks in advance.
    ~ SubbaReddy .M

    Self referential integrity simple means that the parent end of a foreign key constraint is in the same table as the child end. Consider the SCOTT.EMP table. Every manager is also an employee. Hence there is a foreign key between the MGR column and the EMPNO column.
    rgds, APC

  • Find the folder name based on the View Name

    Hi ,
    I need to find out a Folder name based on a View Name . Basically i know the view Name and want to find out the Folder that is created in Discovrer based out of that view.
    Does any one know of any EUL table that stores this relationship.
    Thanks in advance for your help
    Sunny

    Hi,
    Your requirement can be fulfilled using the following script:
    SELECT bas.ba_name,
    bas.ba_id,
    obj.obj_name folder,
    decode(obj.obj_type,
    'COBJ',
    'Complex',
    'SOBJ',
    'Simple',
    'CUO',
    'Custom',
    'Unknown') AS obj_type,
    decode(obj.obj_hidden,0,'Bare','Hidden') IS_HIDDEN,
    obj.sobj_ext_table folder_source_object
    FROM eul10g_us.eul5_ba_obj_links bol,
    eul10g_us.eul5_objs ; obj,
    eul10g_us.eul5_bas ; bas
    WHERE obj.obj_id = bol.bol_obj_id
    AND bas.ba_id = bol.bol_ba_id
    BUT
    As you can see from the SQL, only simple folders are mapped to objects.
    This means that if the object is used inside a custom folder or complex folder you will not see it.
    Tamir

  • Query based on two views

    Hello ppl, I need your help.
    I got this query which is based on two views.
    View_1: x1, x2, x3, x4, day, xy
    View_2: y1, y2, y3, y4, day, xy
    By running a SELECT query on each view takes approx 2 minutes, yes there are lots of records.
    When I combine the query most of the time it times out. I think there is something wrong with my query.
    View_1 = View_2 on col xy and day, however I have to get the records from View_2 for the following day, i.e if day = 3 from View_1 and the record from View_2 should be coming from view_2
    at present my query is like this
    SELECT a.x1, a.x2, a.x3, a.x4, a.day, a.xy, b.y1, b.y2
    FROM view_1 a, view_2 b
    WHERE b.y2= 100
    AND a.xy = b.xy
    AND b.y1 = ( SELECT b.y1
    FROM view_1 a, view_2 b
    WHERE b.day = a.day + 1)
    ORDER BY a.x1
    This query gets called in a coldfusion page.
    P.S I can also ask the DBA to make me a new view based on the same columns as the query. Would that help with speed?

    I don't see any reason why my query shouldn't give
    you what you desire.
    View_1
    x1     x2     x3     x4     Day     XY
    2      3      23      32      3      2132
    3      3      2      3      4      2132
    4      3      43      32      5      2132
    View_2
    y1     y2     y3     y4     Day     XY
    4      100      13      32      3      2132
    5      100      4      3      4      2132
    7      100      43      32      5      2132
    5      100      23      12      6      2132
    Is it a coincidence that XY has the same value in your sample? Could it be different
    and at the same time participate in the desired result?
    SELECT     a.x1, a.x2, a.x3, a.x4, a.day, a.xy, b.y1, b.y2
    FROM     view_1 a
    ,     view_2 b
    WHERE     b.y2 = 100
    AND     a.xy = b.xy
    AND     b.day = a.day + 1
    ORDER BY a.x1
    >
    X1     X2     X3     X4     Day     XY     y1     y2
    2      3      23     32      3      2132      5      100
    3      3      2      3      4      2132      7      100
    4      3      43     32      5      2132      5      100
    According to the reply to my question above, it can be ok or not.
    That is why I proposd this (in case the reply were yes):
    SELECT a.x1, a.x2, a.x3, a.x4, a.day, a.xy, b.y1, b.y2
    FROM view_1 a, view_2 b, vew_1 c
    WHERE b.y2= 100
    AND a.xy = b.xy
    AND b.day = c.day + 1
    ORDER BY a.x1

Maybe you are looking for