FOREIGN KEY across 2 schemas with ON DELETE CASCADE

I found I could define
ALTER TABLE user2.foo ADD CONSTRAINT fk_bar bar REFEERENCES
user1.foobar;
if user1 hat granted SELECT and REFERENCES to users2.
But what, if I want to have
ALTER TABLE user2.foo ADD CONSTRAINT fk_bar bar REFEERENCES
user1.foobar ON DELETE CASCADE;
I made
GRANT DELETE ON foo to users1
as user2
But I get ORA-02292 (child-record exists) if I delete a row in user1.bar wich ha s a related row in user2.foo
Any idea what to do ?

Sorry i made a mistakte in my test. It will allso work with ON DELETE CASCADE

Similar Messages

  • Drop foreign keys and recreate foreign keys with on delete cascade dymanica

    I need to drop foreign keys and recreate foreign keys with on delete cascade dymanically via a script. Does anyone have a script available?

    You could also disable the integrity contraints.
    SET PAGESIZE 0
    SET FEEDBACK OFF
    SET PAUSE OFF
    -- create a disable script
    SPOOL do_disable.sql
    SELECT 'ALTER TABLE '
           || table_name
            || chr(10) ||
           'DISABLE CONSTRAINT '
            || constraint_name
            || ';'
      FROM user_constraints
    WHERE constraint_type = 'R'
       AND status = 'ENABLED'
    select 'alter trigger '
           || trigger_name
            || ' disable;'
      from user_triggers
    where status = 'ENABLED'
    SPOOL OFF
    -- now create a enable script as well
    SPOOL do_enable.sql
    SELECT 'ALTER TABLE '
           || table_name
            || chr(10)
            || 'ENABLE CONSTRAINT '
            || constraint_name
            || ';'
      FROM user_constraints
    WHERE constraint_type = 'R'
       AND status = 'ENABLED'
    SELECT 'ALTER trigger '
           || trigger_name
            || ' ENABLE;'
      FROM user_triggers
    WHERE status = 'ENABLED'
    SPOOL OFF
    SET FEEDBACK ONif you run this script on a TESTdatabase you will end up with a do_disable.sql and a do_enable.sql script on youre local directory.
    Running do disable will disable all the currently enabled triggers and contraints.
    Running do_enable.sql will enable the previously disables triggers and contraints.
    be sure to test before executing this on a real live production database.
    Make sure you have a good backup!

  • Syntax for creating foreign key across users in a database

    There are two user present A,B.They are granted all privileges.Now in USER A, there is a table PARENT whose primary key is PARENT_NO.In USER B I have created a table CHILD whose primary key is CHILD_NO.
    In the CHILD table of USER B, I want to create a foreign key relation to the PARENT table of USER A.For this I have created a column CHILD_PARENT_NO in the CHILD table.If anybody knows the syntax please post the syntax for creating the required foreign key relationship?

    Please post your code. Cut'n'paste from SQL*Plus like this...
    SQL> conn a/a
    Connected.
    SQL> desc t1
    Name                                      Null?    Type
    COL1                                               NUMBER
    COL2                                               VARCHAR2(10)
    SQL> grant references on t1 to b;
    Grant succeeded.
    SQL> conn b/b
    SQL> create synonym a_t for a.t1;
    Synonym created.
    SQL> alter table test add constraint fk foreign key (n) references a_t(col1);
    Table altered.
    SQL> Note that Oracle will translate the synonym anyway...
    SQL> select constraint_name, r_owner, r_constraint_name
      2  from  user_constraints
      3  where table_name = 'TEST'
      4  /
    CONSTRAINT_NAME R_OWNER R_CONSTRAINT_NAME
    FK              A       T1_PK
    SQL> By the way, this ...
    GRANT ALL PRIVILEGES TO B;... is a mindbendingly unsafe way of proceeding. In real life you would have given user B the power to utterly destroy your database. It's always easier to start with good habits than to break bad ones so please get used to granting only the minimum set of privileges necessary.
    Cheers, APC

  • Detect the fields which are primary/foreign keys for each schema's tables

    Hello everybody,
    I'd like to know if it's possible to select a field in one of the dictionary tables to identify the fields of a table which are PK or FK in this one. I'd like to do this to generate the fields that I need to put in a technical design.
    Actually I have this (if it can help you to understand what I'd like to do) :
    select table_name "Table name",
    column_name "Physical field name",
    case when data_type = 'NUMBER' then data_type || '(' || NVL(data_precision,0) || ',' || NVL(data_scale,0) || ')'
    when data_type = 'VARCHAR2' then data_type || '(' || data_length || ')'
    else data_type
    end "Datatype",
    case when nullable = 'Y' then 'FALSE'
    when nullable = 'N' then 'TRUE'
    end "Not null",
    /* Here I'd like to retrieve TRUE or FALSE if the field to display is a primary key or not and the same for foreign keys*/
    from all_tab_columns
    where table_name in (select table_name
    from all_tables
    where table_name like '%project_name%'
    and table_name not like '%TMP%');
    Thanks a lot for your help,
    Florent

    Please investigate all_constraints and all_cons_colums.
    http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14237/toc.htm

  • Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths

    I am trying to introduce a constraint on the sales_details table
    ALTER TABLE [dbo].[SALES_DETAILS] WITH CHECK ADD constraint fk_sd_vat FOREIGN KEY([VAT])
    REFERENCES [dbo].[VAT Rate] ([VAT]) on update cascade on delete cascade
    GO
    I am getting the error saying
    Msg 1785, Level 16, State 0, Line 1
    Introducing FOREIGN KEY constraint 'fk_sd_vat' on table 'SALES_DETAILS' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
    Msg 1750, Level 16, State 0, Line 1
    Could not create constraint. See previous errors.
    What could be the reason,Please suggest me the solution for  this,
    Thanks,

    Hi Erland,
    This is my DDl
    CREATE TABLE [dbo].sales_details(
    [BNUM] [varchar](20) NOT NULL,
    [CNAME] [varchar](30) NOT NULL,
    [CNUM] [int] NOT NULL FOREIGN KEY([CNUM])REFERENCES [dbo].[Customer] ([CNum]),
    [ITNAME] [varchar](100) NOT NULL,
    [ITEM#] [int] NOT NULL ,
    [QTY] [int] NOT NULL,
    [UNIT] [varchar](5) NOT NULL,
    [PRICE] [float] NOT NULL,
    [BASIC] [float] NOT NULL,
    [DISCOUNT] [float] NOT NULL,
    [FRQTY] [int] NOT NULL,
    [BADDR] [varchar](300) NULL,
    [CADDR] [varchar](300) NOT NULL,
    [BDATE] [datetime] NOT NULL,
    [VAT] [float] NOT NULL
    ALTER TABLE [dbo].[SALES_DETAILS] WITH CHECK ADD constraint cons1 FOREIGN KEY([ITNAME])
    REFERENCES [dbo].[New Item] ([Item Description]) on cascade update
    GO
    ALTER TABLE [dbo].[SALES_DETAILS] WITH CHECK ADD constraint FOREIGN KEY([VAT])
    REFERENCES [dbo].[VAT Rate] ([VAT]) on cascade update
    GO
    I am getting the error when i execute second Alter statement.
    Thanks,
    Check if VAT Rate table has any FK with cascaded options?
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Distinctcount across star schema with boolean filter

    Hi,
    I have a fact table with CustomerNo, DateKey, ProductKey, SupplierKey, Sales.
    This is related to a Calendar table, Product, and Supplier Table.
    I'm trying to calculate a distinct count of customer where the sales = 0 and keeping the original filter conditions on related tables. 
    I've tried :  
    Calculate(Distinctcount(fact[CustomerNo), Fact[Sales]=0)
    The figures are wrong.  It's hard for me to evaluate what rows are actually being returned due to the size of the table. I think it may be related to the the DistinctCount running a groupby first and the filter being run second.
    So I tried :
    Calculate(Distinctcount(fact[CustomerNo), Filter(Fact, Fact[Sales]=0))
    This returns the same value as the one above.
    It's very weird as the slicers on the Pivot do interact with the measure so I know the measures are keeping the existing filter contexts.
    Any ideas what is happening? 
    Lee Hawthorn ACMA, CGMA View my Performance Management blog at leehbi.com

    well, the formula you provided is correct and should return the correct result
    its hard to say why it would return wrong results without knowing/seeing the data
    the only thing i could think of is the following:
    a customer made two sales 1) with Sales=123 2) with Sales=0
    the formula would of course count that customer as a distinct customer with Sales=0
    maybe thats the reason for your "wrong" results?
    you may also check this link: http://www.daxpatterns.com/distinct-count/
    hth,
    gerhard
    Gerhard Brueckl
    blogging @ http://blog.gbrueckl.at
    working @ http://www.pmOne.com

  • Assigning a foreign key

    i have created a database which is administered by ora1 who created 2 tables called student and instructor then created a new user called dev1 with dba priviliges dev1 created a table called university assigned a primary key to the university_id column and has added new columns to the instructor and student tables owned by ora1 the problem i am having is i am unable to assign foreign keys to the 2 tables owned by ora1 even though dev1 could add the extra university_id columns to the 2 tables the code i am using to add extra columns and assign the foreign keys is as follows:
    alter table ora1.student
    add (university_id varchar2 (5));
    alter table ora1.student add
    constraint student_fk1
    foreign key (university_id)
    references university (university_id)
    on delete cascade;
    I keep getting error message saying table or view does not exist

    hi u are using the right format
    ALTER TABLE <table_name>
    ADD <constraint_name> FOREIGN KEY
    (<column_name1> ,
    <column_name2> )
    REFERENCES <table_name>
    (<column_name1> ,
    <column_name2>)
    ALTER TABLE ora1.student
    ADD student_fk1 FOREIGN KEY
    (university_id)
    REFERENCES university (university_id)the problem may be your table doesnot exist or u are refering the table which is in different schema
    Regards
    Hitesh

  • Foreign Key Error Message ORA - 01430

    I have a table name LSU_INTER_RESOURCES_T. I would like to add a foreign key constraint with the following command:
    alter table lsu_inter_message_t
    add constraint inter_message_id_fk
    inter_request_id REFERENCES lsu_inter_requests_t(inter_request_id) ON DELETE CASCADE;
    The system returned the error message:
    ORA - 01430: column being added already exists.
    if I drop the column inter_request_id I will lose data and when I create the constraint it places the column at the end of the table. Losing data is not an issue now because the table has no data but down the line I can see that this will be a real real hassle.
    I would like to have the foreign key column defined in the sequence I chose when I designed the table layout. That is to say I want all of the key columns placed at the begining of the table row layout.
    Can I move the column after I define it and how is this done? Is this a 10g feature?
    Thank you, Ned

    Try
    alter table lsu_inter_message_t
    add constraint inter_message_id_fk
    foreign key(inter_request_id ) REFERENCES lsu_inter_requests_t(inter_request_id) ON DELETE CASCADE;

  • Accurate statistics on skewed foreign key when coming from parent

    Hi,
    I will be as short as possible:
    - we have two tables in a parent - child relationship. The foreign key is very skewed and we need to get child details coming from the parent
    - CBO is not considering the skeweness from histograms on foreign key column (I suppose it cannot as it is the example without going into tables) and it calculates the expected returning rows in the execution plan as no_rows / distinct_values => 10,000 / 3 = 3,333 rows for both execution plans, even if they returns effectively 1 row / 9,999 rows. In histograms exists this information but it cannot be extracted by CBO as it is.
    Any idea how to help CBO to choose separate execution plans for query 1 and query 2 excluding hints or query modifications?
    -- Oracle Database 10.2.0.3
    --DROP TABLE child;
    --DROP TABLE parent;
    CREATE TABLE parent AS SELECT LEVEL parent_num, 'parent_' || LEVEL parent_id FROM DUAL CONNECT BY LEVEL <= 10;
    ALTER TABLE parent ADD CONSTRAINT parent_pk PRIMARY KEY (parent_num);
    CREATE UNIQUE INDEX parent_id_ix ON PARENT (parent_id, parent_num);
    CREATE TABLE child AS SELECT DECODE (LEVEL, 1, 1, 10000, 10, 5) parent_num, 'child_' || LEVEL child_id FROM DUAL CONNECT BY LEVEL <= 10000;
    ALTER TABLE child ADD FOREIGN KEY (parent_num) REFERENCES parent (parent_num) ON DELETE CASCADE;
    CREATE INDEX child_parent_num_ix ON CHILD(parent_num);
    exec dbms_stats.gather_table_stats(ownname=>'***REPLACE WITH YOUR OWN USER***',tabname=>'PARENT',estimate_percent=>100,cascade=>TRUE,method_opt=>'FOR ALL COLUMNS SIZE 1');
    exec dbms_stats.gather_table_stats(ownname=>'***REPLACE WITH YOUR OWN USER***',tabname=>'CHILD',estimate_percent=>100,cascade=>TRUE,method_opt=>'FOR COLUMNS parent_num SIZE 3, child_id SIZE 1');
    -- QUERY 1: this query should be very selective (1 row), using index
    SELECT c.child_id FROM child c, parent p WHERE c.parent_num = p.parent_num AND p.parent_id = 'parent_1';
    -- QUERY 2: it should return 99.9999% of the rows (9,999 rows), so it should use full table scan
    SELECT c.child_id FROM child c, parent p WHERE c.parent_num = p.parent_num AND p.parent_id = 'parent_5';
    In reality both queries have the same execution plan as below:
    Execution Plan
    Plan hash value: 4033709836
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 3333 | 83325 | 6 (0)| 00:00:01 |
    | 1 | NESTED LOOPS | | 3333 | 83325 | 6 (0)| 00:00:01 |
    | 2 | TABLE ACCESS BY INDEX ROWID| PARENT | 1 | 12 | 1 (0)| 00:00:01 |
    |* 3 | INDEX UNIQUE SCAN | PARENT_ID_IX | 1 | | 0 (0)| 00:00:01 |
    |* 4 | TABLE ACCESS FULL | CHILD | 3333 | 43329 | 5 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    3 - access("P"."PARENT_ID"='parent_1')
    4 - filter("C"."PARENT_NUM"="P"."PARENT_NUM")
    Execution Plan
    Plan hash value: 4033709836
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 3333 | 83325 | 6 (0)| 00:00:01 |
    | 1 | NESTED LOOPS | | 3333 | 83325 | 6 (0)| 00:00:01 |
    | 2 | TABLE ACCESS BY INDEX ROWID| PARENT | 1 | 12 | 1 (0)| 00:00:01 |
    |* 3 | INDEX UNIQUE SCAN | PARENT_ID_IX | 1 | | 0 (0)| 00:00:01 |
    |* 4 | TABLE ACCESS FULL | CHILD | 3333 | 43329 | 5 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    3 - access("P"."PARENT_ID"='parent_5')
    4 - filter("C"."PARENT_NUM"="P"."PARENT_NUM")
    column table_name format A10
    column column_name format A11
    column endpoint_actual_value format A21
    select * from user_tab_histograms where table_name='CHILD' and column_name='PARENT_NUM';
    TABLE_NAME COLUMN_NAME ENDPOINT_NUMBER ENDPOINT_VALUE ENDPOINT_ACTUAL_VALUE
    CHILD PARENT_NUM 1 1
    CHILD PARENT_NUM 9999 5
    CHILD PARENT_NUM 10000 10
    3 rows selected.
    I am sorry about the formatting but this is the best I could following plain text help.
    Thank you,
    Eugen
    Edited by: Eugen Iacob on Dec 13, 2011 3:21 AM
    Edited by: Eugen Iacob on Dec 13, 2011 12:36 PM
    Edited by: Eugen Iacob on Dec 13, 2011 12:37 PM
    Edited by: Eugen Iacob on Dec 14, 2011 2:13 AM

    Hi,
    you can create a function based index on child table, and query that value instead of parent_id from parent table.
    SQL> select * from v$version;
    BANNER
    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
    PL/SQL Release 10.2.0.1.0 - Production
    CORE     10.2.0.1.0     Production
    TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
    NLSRTL Version 10.2.0.1.0 - Production
    CREATE TABLE parent AS SELECT LEVEL parent_num, 'parent_' || LEVEL parent_id, 'some text ' || level parent_name
    FROM DUAL CONNECT BY LEVEL <= 10;
    ALTER TABLE parent ADD CONSTRAINT parent_pk PRIMARY KEY (parent_num);
    CREATE UNIQUE INDEX parent_id_ix ON PARENT (parent_id, parent_num);
    CREATE TABLE child AS SELECT DECODE (LEVEL, 1, 1, 10000, 10, 5) parent_num, 'child_' || LEVEL child_id,
    'some text ' || level child_name
    FROM DUAL CONNECT BY LEVEL <= 10000;
    ALTER TABLE child ADD FOREIGN KEY (parent_num) REFERENCES parent (parent_num) ON DELETE CASCADE;
    CREATE INDEX child_parent_num_ix ON CHILD(parent_num);
    CREATE OR REPLACE function get_parent_id( in_parent_num in number )
    RETURN parent.parent_id%TYPE
    DETERMINISTIC
    IS
      ret parent.parent_id%TYPE;
    BEGIN
      SELECT parent_id INTO ret
      FROM parent
      WHERE parent_num = in_parent_num;
      RETURN ret;
    END;
    CREATE INDEX child_parent_id_idx ON child( get_parent_id( parent_num ) );
    exec dbms_stats.gather_table_stats(ownname=>user,tabname=>'CHILD',estimate_percent=>100,cascade=>TRUE,method_opt=>'FOR ALL HIDDEN COLUMNS');
    explain plan for
    SELECT p.parent_name, c.child_name
    FROM child c, parent p
    WHERE c.parent_num = p.parent_num AND get_parent_id(c.parent_num ) = 'parent_1';
    select * from table( dbms_xplan.display );
    plan FOR succeeded.
    PLAN_TABLE_OUTPUT                                                                                                                                                                                                                                                                                           
    Plan hash value: 3790918082                                                                                                                                                                                                                                                                                 
    | Id  | Operation                    | Name                | Rows  | Bytes | Cost (%CPU)| Time     |                                                                                                                                                                                                        
    |   0 | SELECT STATEMENT             |                     |     1 |    77 |     3   (0)| 00:00:01 |                                                                                                                                                                                                        
    |   1 |  NESTED LOOPS                |                     |     1 |    77 |     3   (0)| 00:00:01 |                                                                                                                                                                                                        
    |   2 |   TABLE ACCESS BY INDEX ROWID| CHILD               |     1 |    37 |     2   (0)| 00:00:01 |                                                                                                                                                                                                        
    |*  3 |    INDEX RANGE SCAN          | CHILD_PARENT_ID_IDX |     1 |       |     1   (0)| 00:00:01 |                                                                                                                                                                                                        
    |   4 |   TABLE ACCESS BY INDEX ROWID| PARENT              |     1 |    40 |     1   (0)| 00:00:01 |                                                                                                                                                                                                        
    |*  5 |    INDEX UNIQUE SCAN         | PARENT_PK           |     1 |       |     0   (0)| 00:00:01 |                                                                                                                                                                                                        
    Predicate Information (identified by operation id):                                                                                                                                                                                                                                                         
       3 - access("TEST"."GET_PARENT_ID"("PARENT_NUM")='parent_1')                                                                                                                                                                                                                                              
       5 - access("C"."PARENT_NUM"="P"."PARENT_NUM")                                                                                                                                                                                                                                                            
    Note                                                                                                                                                                                                                                                                                                        
       - dynamic sampling used for this statement                                                                                                                                                                                                                                                               
    22 rows selected
    explain plan for
    SELECT  p.parent_id, c.child_id
    FROM child c, parent p
    WHERE c.parent_num = p.parent_num AND get_parent_id(c.parent_num ) = 'parent_5';
    select * from table( dbms_xplan.display );
    plan FOR succeeded.
    PLAN_TABLE_OUTPUT                                                                                                                                                                                                                                                                                           
    Plan hash value: 2656363181                                                                                                                                                                                                                                                                                 
    | Id  | Operation          | Name         | Rows  | Bytes | Cost (%CPU)| Time     |                                                                                                                                                                                                                         
    |   0 | SELECT STATEMENT   |              |  9998 |   732K|    23  (27)| 00:00:01 |                                                                                                                                                                                                                         
    |*  1 |  HASH JOIN         |              |  9998 |   732K|    23  (27)| 00:00:01 |                                                                                                                                                                                                                         
    |   2 |   INDEX FULL SCAN  | PARENT_ID_IX |    10 |   380 |     1   (0)| 00:00:01 |                                                                                                                                                                                                                         
    |*  3 |   TABLE ACCESS FULL| CHILD        |  9998 |   361K|    21  (24)| 00:00:01 |                                                                                                                                                                                                                         
    Predicate Information (identified by operation id):                                                                                                                                                                                                                                                         
       1 - access("C"."PARENT_NUM"="P"."PARENT_NUM")                                                                                                                                                                                                                                                            
       3 - filter("GET_PARENT_ID"("C"."PARENT_NUM")='parent_5')                                                                                                                                                                                                                                                 
    Note                                                                                                                                                                                                                                                                                                        
       - dynamic sampling used for this statement                                                                                                                                                                                                                                                               
    20 rows selected

  • How to create a foreign key for the table from two different tables?

    Hi All,
    I have a three table like below. In the below table SAMPLE_CONS_CHECK and SAMPLE_CONS2_CHECK will be having the primary key for NAME column. The same SAMPLE_CONS3_CHECK table also having the primary key for NAME column and forieign key for SAMPLE_CONS_CHECK and SAMPLE_CONS2_CHECK tables. See the below code 2
    code 1:
    CREATE TABLE SAMPLE_CONS_CHECK
            (NAME VARCHAR2(10),
            SERIES  VARCHAR2(5)
    CREATE TABLE SAMPLE_CONS2_CHECK
            (NAME  VARCHAR2(5),
             MODEL  NUMBER
    CREATE TABLE SAMPLE_CONS3_CHECK
            (NAME  VARCHAR2(5),
             MODEL_NO  NUMBER
            )code 2
    alter table SAMPLE_CONS_CHECK
    add constraint SAMPLE_CONS_CHECK_pk primary key (NAME)
    alter table SAMPLE_CONS2_CHECK
    add constraint SAMPLE_CONS2_CHECK_pk primary key (NAME)
    alter table SAMPLE_CONS3_CHECK
    add constraint SAMPLE_CONS3_CHECK_pk primary key (NAME)
    ALTER TABLE SAMPLE_CONS3_CHECK ADD
    CONSTRAINT SAMPLE_CONS3_CHECK_FK1 FOREIGN KEY
         NAME
    ) REFERENCES SAMPLE_CONS_CHECK
        NAME
    ) ON DELETE CASCADE;
    ALTER TABLE SAMPLE_CONS3_CHECK ADD
    CONSTRAINT SAMPLE_CONS3_CHECK_FK2 FOREIGN KEY
         NAME
    ) REFERENCES SAMPLE_CONS2_CHECK
        NAME
    ) ON DELETE CASCADE;From the above schenario i am able to insert the data to SAMPLE_CONS3_CHECK table. But the parent data is already available in the parent table. The problem is here two different constarints from two different tables. While inserting, it is checking from both the tables whether the parent is exist or not.
    How can i solve this problem? Can anyone halp me about this?
    Thanks
    Edited by: orasuriya on Aug 8, 2009 2:02 AM

    Actually the design is completely incorrect.
    What you say is
    I have
    'foo', 'foo series'
    'foo','foo model'
    'foo',666
    By virtue of table3 referring to both table1 and table2.
    This means you actually need to have 1 (one) table:
    'foo','foo series','foo model', 666
    And the 'problem' disappears.
    Sybrand Bakker
    Senior Oracle DBA

  • How to use Concatenation option in Foreign Key

    Hi,
    I have 2 tables.
    Master Table : Father(a varchar2(10),b varchar2(10)
    Child Table : Child (c varcahr2(20))
    In the child Table i have stored the following Father.A||Father.B
    Child.C = Father.a + Father.B
    I want to create a Foreign Key Relation for this.
    I cant use '||' option.
    Can any one please guide me.
    ALTER TABLE child ADD CONSTRAINT fk_child FOREIGN KEY (c) REFERENCES father(a||b)
    ON DELETE CASCADE;
    This statement throws error.
    Pls help me in this

    Why not create a third column on the Master table ?
    alter table Father add c varchar2(20);
    -- then perform an update
    update Father set c := a|| b
    commit
    /and then create the constraint as;
    ALTER TABLE child ADD CONSTRAINT fk_child FOREIGN KEY (c) REFERENCES father (c) ON DELETE CASCADE;

  • How to alter the constraint for on delete cascade

    Hi Guys,
    I have a table which has 5 level of child tables .But all the child tables have the foreign keys without on delete cascade.But I have to delete a data in the parent table .One method I thought to change the child table constraints to ON DELETE CASCADE then delete the record and after that again to change the constraints with out the ON DELETE CASCADE.Please let me know wheather these is possible or not .If not is there any other alternatives to achive this.
    Any suggestions would be highly appreciated.
    Thanks in advance
    Prafulla

    I think you need to drop and recreate your FKs.
    See my example here.
    create table TEST1
      id       NUMBER(12) not null,
      anything VARCHAR2(20)
    alter table TEST1
      add constraint PK_TEST1 primary key (ID);
    create table TEST2
      id2  NUMBER(12) not null,
      fk1  NUMBER(12),
      info VARCHAR2(50)
    alter table TEST2
      add constraint PK_TEST2 primary key (ID2);
    alter table TEST2
      add constraint FK_TEST2_TO_TEST1 foreign key (FK1)
      references TEST1 (ID);
    -- and here is the code when I add on delete cascade for FK
    alter table TEST2
      drop constraint FK_TEST2_TO_TEST1;
    alter table TEST2
      add constraint FK_TEST2_TO_TEST1 foreign key (FK1)
      references TEST1 (ID) on delete cascade;

  • Problem on DELETE CASCADE

    my following tables not allow me to delete the parent table student and the corperate recode in child table also get delete , why
    create table Student(       student_id  int(10) not null  ,       sure_name VARCHAR(25)  not null,       english_name Varchar(25) default null,       chinese_name varchar(35) CHARACTER SET utf8 COLLATE utf8_general_ci not NULL,       student_hkid  varchar(12) not null,       address varchar(100) default null,       mobile_No  varchar(15) default null,       home_Telephone  varchar(15) default null,       email    varchar(20) not null,       faculty  varchar(70) default null,       department varchar(100) default null,       local_student varchar(3) default 'No',       fresh_student varchar(3) default 'No',       fulltime_student varchar(3) default 'No',       first_Ustudent varchar(3) default 'No',        userIp  VARCHAR(45)  not NULL,       accessTime  VARCHAR(45)  not NULL,       primary key(student_id))TYPE=MyISAM CHARACTER set utf8; create table Student_financial_info(   student_id  int(10) not null,   grant_loan varchar(3) default 'No',   uploadfile  text default null,   notification_time varchar(10) default null,   father_name  varchar(25) default null,   father_HKID  varchar(12) default null,   father_Salary decimal(6,2) ,   father_live_together varchar(3),   mother_name  varchar(25) default null,   mother_HKID  varchar(12) default null,   mother_Salary decimal(6,2) ,   mother_live_together varchar(3),   1th_sibling_name  varchar(25) default null,   1th_sibling_HKID  varchar(12) default null,   1th_sibling_Salary decimal(6,2) ,   1th_sibling_together varchar(3),   2th_sibling_name  varchar(25) default null,   2th_sibling_HKID  varchar(12) default null,   2th_sibling_Salary decimal(6,2) ,   2th_sibling_together varchar(3),   3th_sibling_name  varchar(25) default null,   3th_sibling_HKID  varchar(12) default null,   3th_sibling_Salary decimal(6,2) ,   3th_sibling_together varchar(3),   4th_sibling_name  varchar(25) default null,   4th_sibling_HKID  varchar(12) default null,   4th_sibling_Salary decimal(6,2) ,   4th_sibling_together varchar(3),   addition_income Varchar(200) default null,   primary key(student_id ),   FOREIGN KEY(student_id) REFERENCES student(student_id) ON DELETE CASCADE ON UPDATE CASCADE)TYPE=MyISAM CHARACTER set utf8;

    HJava wrote:
    hi thank you!
    I change my table Engine under Mysql Query Browser, but Foreign Key settinis not action at all ( thank you you mention the foreign key is ignore), quite stronge for me, I have to drop the table and change the table engine to InnoDB in my create table code and run it under command line client,
    now, It work.
    are there any different to  use command line Client and Mysql Query Brower?
    thank you!Not really, other than appearance. But, the "GUI" might let you simply change the type of table on an already created database (I don't know, haven't tried it), unfortunately, that won't activate any foreign keys when changing a MyISAM to InnoDB (although it will destroy keys on an InnoDB when changing to MyISAM), as, like the above quote says, the keys are not only not used, they are not even stored. I.E. MySQL no longer even knows, nor cares, that you even attempted to create a foreign key for that table. So, you must drop and recreate the table, you can't simply "change" it. Although you could change it (if the GUI even does this) and then add the foreign key constraints.

  • Add ON DELETE CASCADE

    hi every body.
    i want to add the constraint "ON DELETE CASCADE" on foreign key value in child table like.
    ALTER table employee
    Add CONSTRAINT cscade_dept_no ON DELETE CASCADE (Deptno)
    but it display error message, so please help me in this regards.
    Thanks
    Edited by: BilalKhan on Feb 7, 2011 11:53 PM

    Usually, will be usefull look the error displayed.
    Example:
    ALTER table employee Add CONSTRAINT cscade_dept_no ON DELETE CASCADE (Deptno);
    Error SQL: ORA-00904: :
    00904. 00000 -  "%s: invalid identifier"
    *Cause:   
    *Action:Then we can say to you... Your syntaxis is wrong, you must use
    ALTER TABLE table_name
    add CONSTRAINT constraint_name
    FOREIGN KEY (column1, column2, ... column_n)
    REFERENCES parent_table (column1, column2, ... column_n)
    ON DELETE CASCADE;
    example
    ALTER TABLE employee
    add CONSTRAINT cscade_dept_no
      FOREIGN KEY (Deptno)
      REFERENCES Department (Deptno)
      ON DELETE CASCADE;

  • Can't Delete Parent Record - On Delete Cascade is set

    Greetings all,
    I have a parent and child table and when I attempt to delete the parent record, I get the following error:
    ORA-02292: integrity constraint (WFSTEADMAN.INCIDENT_NOTES_TBL_FK) violated - child record found
         Error      Unable to process row of table MYTEST_INCIDENTS.
    I have the the FK set to ON DELETE CASCADE so I am not sure what the issue might be.
    Below are the structures of my parent and child tables:
    I am using apex.oracle.com so the version is: Application Express 4.0.2.00.06
    Any assistance would be appreciated.
    Wally
    PARENT TABLE:
    =============================================================
    CREATE TABLE "MYTEST_INCIDENTS"
    (     "INCIDENTID" NUMBER NOT NULL ENABLE,
         "INCIDENTCREATED" DATE NOT NULL ENABLE,
         "INCIDENT_OWNER" VARCHAR2(200) NOT NULL ENABLE,
         "DTGSTART" DATE NOT NULL ENABLE,
         "DTGEND" DATE,
         "INCIDENT_SERVICES" VARCHAR2(4000),
         "INCIDENT_CUSTOMERS" VARCHAR2(4000),
         "INCIDENT_DESCRIPTION" VARCHAR2(4000),
         "INCIDENT_TIMELINE" VARCHAR2(4000),
         "INCIDENT_TRACKING" VARCHAR2(500),
         "INCIDENT_RESTORE" VARCHAR2(4000),
         "INCIDENT_FOLLOWUP" VARCHAR2(4000),
         "INCIDENT_LEVEL" NUMBER,
         "INCIDENT_TITLE" VARCHAR2(500),
         "EMAIL_NOTIFY" VARCHAR2(4000),
         "TEXT_NOTIFY" VARCHAR2(4000),
         "LEVEL_TYPE" VARCHAR2(25),
         CONSTRAINT "MYTEST_INCIDENTS_PK" PRIMARY KEY ("INCIDENTID") ENABLE
    CREATE OR REPLACE TRIGGER "BI_MYTEST_INCIDENTS"
    before insert on "MYTEST_INCIDENTS"
    for each row
    begin
    if :NEW."INCIDENTID" is null then
    select "MYTEST_INCIDENTS_SEQ".nextval into :NEW."INCIDENTID" from dual;
    end if;
    end;
    ALTER TRIGGER "BI_MYTEST_INCIDENTS" ENABLE;
    CHILD TABLE
    =============================================================
    CREATE TABLE "MYTEST_INC_NOTES"
    (     "NOTEID" NUMBER NOT NULL ENABLE,
         "INCIDENT_ID" NUMBER NOT NULL ENABLE,
         "NOTE_DTG" DATE,
         "NOTE_OWNER" VARCHAR2(200),
         "NOTE_COMMENTS" VARCHAR2(4000),
         CONSTRAINT "MYTEST_INC_NOTES_PK" PRIMARY KEY ("NOTEID") ENABLE
    ) ;ALTER TABLE "MYTEST_INC_NOTES" ADD CONSTRAINT "MYTEST_INC_NOTES_CON" FOREIGN KEY ("INCIDENT_ID")
         REFERENCES "MYTEST_INCIDENTS" ("INCIDENTID") ON DELETE CASCADE ENABLE;
    CREATE OR REPLACE TRIGGER "BI_MYTEST_INC_NOTES"
    before insert on "MYTEST_INC_NOTES"
    for each row
    begin
    if :NEW."NOTEID" is null then
    select "MYTEST_INC_NOTES_SEQ".nextval into :NEW."NOTEID" from dual;
    end if;
    end;
    ALTER TRIGGER "BI_MYTEST_INC_NOTES" ENABLE;
    CREATE OR REPLACE TRIGGER "MYTEST_INC_NOTES_T1"
    BEFORE
    insert or update on "MYTEST_INC_NOTES" REFERENCING NEW AS NEW OLD AS OLD
    for each row
    begin
    :NEW.NOTE_DTG := SYSDATE;
    end;
    ALTER TRIGGER "MYTEST_INC_NOTES_T1" ENABLE;

    Yeah there was a rampant child table still left out there. Noticed the issue and deleted the child and all is well now. :). Should have looked for a couple more minutes before posting.
    Thanks
    Wally

Maybe you are looking for

  • Dual monitors on MacBook Pro 15' Mid-2012

    I have two Apple HD Cinema Displays (23-inch DVI 2005 model) and I want to connect both them to my MacBook Pro 15' using the MiniDisplayport without any lag. I know my mac only supports up to one monitor and I am currently using a USB to DVI to conne

  • Search for a String in a list of files

    Could anyone please send me a java sample code which searches for a string from a list of files and returns me the filename and the line in which the Search String was found ? Thanks in advance RR

  • What is error code 3014? And How to fix it?

    What is error code 3014? And How to fix it?

  • Microsoft Publisher to Photoshop?

    We are new to Photoshop and need to transfer our materials. Is it possible to copy a microsoft publisher document into a new photoshop document?

  • Web Auth with AAA (RAIDUS) Failure

    Hi Guys, We are having an issue with out Web Auth Using AAA Servers. We get the following error: AAA Authentication Failure for UserName:14t.park User Type: WLAN USER, This error is from the Web Interface, I have been looking at the debug settings to