Constraints.....in oracle

Difference between them
CANDIDATE KEY
COMPOSITE KEY
ALTERNATE KEY
UNIQUE KEY
and plzz give me some example...

Sukanta wrote:
Difference between them
CANDIDATE KEY
COMPOSITE KEY
ALTERNATE KEY
UNIQUE KEY
and plzz give me some example...www.google.com
Should have all the answers you need, it may take some effort, but you'll learn a lot more in the process.
Good luck!

Similar Messages

  • Using constraints in Oracle 10g

    Hi,
    I am confused on with this thing w.r.t to Oracle 10g.
    Whenever I try and create a constraint on a value like
    CONSTRAINT "Const1" CHECK (status in (0,1,-1))
    It creates a problem for me if I use the status variable with where clause
    like
    select * from table where status=0
    I get a
    ERROR at line 1:
    ORA-03113: end-of-file on communication channel
    Now this works fine if i create the above constraint as
    CONSTRAINT "Const1" CHECK (status in (-1,0,1))
    I am unable to find any reason for this.
    Please help.
    regards
    Gaurav
    Message was edited by:
    masand

    Hi,
    I'am using 10g 10.2.0.1.0 database. I had a query which select data from database. the query uses CAST(MULTISET to cast subquery into a scalar type. When I ran the query without ORDER BY statement, it works fine but the result is in random order. Now, when I put ORDER BY statement, I was given ora-03113 end-of-communication channel error.
    Can anyone tell why? Is it a bug? what is the workaround?
    Here is my query that without ORDER BY:
    SELECT a.assignment_no || '-' || TO_CHAR(a.item_seq) as "ASSIGNNO",
    A.photo_id as "PHOTOID",
    CAST(MULTISET
    (SELECT S.SIZE_DESC as "SizeDesc",
    S.SIZE_CODE as "SizeCode",
    D.QUANTITY Quantity,
    D.AMOUNT Amount,
    null REMARKS,
    S.SIZE_ORDER AS "SIZEORDER"
    FROM PHOTO_ORDER_DETAIL D, PHOTO_SIZE S
    WHERE S.SIZE_TYPE = 'FILMPRO'
    AND S.ODD_SIZE_FLAG = 'N'
    AND D.SIZE_CODE (+)= S.SIZE_CODE
    AND D.INPUT_YEAR (+)= 2006
    AND D.ORDER_NO (+)= 120
    AND D.PHOTO_ID (+)=A.PHOTO_ID
    AND D.YEAR (+)= A.YEAR
    ) AS order_size_list_t
    ) as "ColorPrint"
    FROM PHOTO_ARCHIVE A
    WHERE EXISTS (
    SELECT 1
    FROM PHOTO_ORDER_DETAIL E, PHOTO_SIZE B
    WHERE E.ORDER_NO = 120
    AND E.INPUT_YEAR = 2006
    AND B.SIZE_CODE=E.SIZE_CODE
    AND B.SIZE_TYPE='FILMPRO'
    AND B.ODD_SIZE_FLAG = 'N'
    AND a.YEAR=E.YEAR
    AND a.PHOTO_ID = E.PHOTO_ID);
    It's result OK but not in the order I desire
    Here is query with ORDER BY:
    SELECT a.assignment_no || '-' || TO_CHAR(a.item_seq) as "ASSIGNNO",
    A.photo_id as "PHOTOID",
    CAST(MULTISET
    (SELECT S.SIZE_DESC as "SizeDesc",
    S.SIZE_CODE as "SizeCode",
    D.QUANTITY Quantity,
    D.AMOUNT Amount,
    null REMARKS,
    S.SIZE_ORDER AS "SIZEORDER"
    FROM PHOTO_ORDER_DETAIL D, PHOTO_SIZE S
    WHERE S.SIZE_TYPE = 'FILMPRO'
    AND S.ODD_SIZE_FLAG = 'N'
    AND D.SIZE_CODE (+)= S.SIZE_CODE
    AND D.INPUT_YEAR (+)= 2006
    AND D.ORDER_NO (+)= 120
    AND D.PHOTO_ID (+)=A.PHOTO_ID
    AND D.YEAR (+)= A.YEAR
    ORDER BY S.SIZE_ORDER
    ) AS order_size_list_t
    ) as "ColorPrint"
    FROM PHOTO_ARCHIVE A
    WHERE EXISTS (
    SELECT 1
    FROM PHOTO_ORDER_DETAIL E, PHOTO_SIZE B
    WHERE E.ORDER_NO = 120
    AND E.INPUT_YEAR = 2006
    AND B.SIZE_CODE=E.SIZE_CODE
    AND B.SIZE_TYPE='FILMPRO'
    AND B.ODD_SIZE_FLAG = 'N'
    AND a.YEAR=E.YEAR
    AND a.PHOTO_ID = E.PHOTO_ID);
    The result is
    ORA-03113: end-of-file on communication channel

  • Doubt on Constraints in Oracle?

    Hi Friends,
    Constraints are the business rules that are enforced on the data that is stored in the tables.
    We can define constraints in three forms :
    1.using Constraint feature provided by Oracle i.e CHECK,NOT NULL,PRIMARY KEY .....
    2.Using Triggers in the database
    3.Writing the logic at the Application Level.
    Among all these only (1) is preferred,why?
    what exactly is the difference in the above approaches?
    Are the remaining approaches time consuming ?

    The main reason why CONSTRAINTS are to be preferred over TRIGGERS or other approaches is that it is the accepted approach to solving the problem. The fact that constraints are faster than triggers is noteworthy but irrelevant. Relational theory uses keys to assert relational integrity: constraints are the way to implement those keys. If I was looking at a new system I would expect to see keys in the data model implemented as constraints in the schema. I would not expect nor want to have to look at triggers to see what relational integrity is enforced.
    Using triggers instead of constraints is possible but it is like driving on the wrong side of the motorway: it's harder, it's dangerous and only a fool or a drunk would choose to do it.
    Cheers, APC
    blog: http://radiofreetooting.blogspot.com

  • Analyzing foreign keys constraints in Oracle.

    Hello,
    I want to analyze the fk constraints between tables and a directed graph should be the result.
    I can make the query
    select * from all_constraints where owner = 'XYZ' and constraint_type = 'R';
    But how can I retrieve the information about the reference tables? The tuple in the result of this query contains only one table name.

    http://www.morganslibrary.org/library.html
    Look up constraints
    Scroll to the bottom of the page
    There are several SQL statements there that will return the results you are asking for.

  • An approach to processing "constraint violation" oracle errors.

    Hello,
    We are using Toplink to map our classes to the tables. What would be the best way in the framework to map "constraint violation" sql exceptions to readable error messages sent back to the users?
    Say, we need to insert new Member record. The Member table has 10 foreign keys for lookup tables such as member_type, member_level, etc...The straight forward approach is to check all the FKs using queries before inserting the Member record and generate corresponding error messages if any of the FKs fails.
    Is there a more generic and ellegant approach for this?
    thank you

    This is something that is generally left up to the application. Many approach this by issuing the insert and getting the exception then. Others do as you described.

  • Cascade Update in Oracle Constraint ?

    How can I create a Constraint in Oracle 9i with Cascade Update Option like On Delete Cascade.
    It means, if I change the primary key of master table, the foreign key of all the relevant child tables should be updated automatically.
    Any idea please ?
    Best Regards,
    Luqman

    Oracle doesn't intrinsically have the concept of a CASCADE UPDATE.
    From a data modeling standpoint, primary keys should never, ever change under any circumstances. If a column can ever change, it should not be used as a primary key.
    Tom Kyte has a package and some code to generate triggers that would do this sort of thing
    http://asktom.oracle.com/~tkyte/update_cascade/index.html
    but I would strongly suggest you change the data model rather than going down that path.
    Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • PL/SQL Automatic Constraint Handler(Code)

    For those interested...
    I created the following database code that automatically does the constraint check/return error message for you.
    You could even take this a step further and create your own message repository...
    This guy even grabs/checks for foreign key violations:
    First part of code builds/returns dictionary data constraints for table that you specify.
    It then loads this data into object type.
    This object type will be referenced later in the database trigger that you create for table where you want to validate constraints
    return DbCons_documents_Array
    as
    dbdata DbCons_documents_Array := DbCons_documents_Array();
    i NUMBER := 1;
    begin
    FOR cns_rec IN(select child.child_cons_name clchild_cons_name,
    child.parent_cons_name clparent_cons_name,
    child.child_columns clchild_columns,
    parent.parent_cons_name pparent_cons_name,
    parent.parent_tname pparent_tname,
    child.child_tname clchild_tname,
    CONCAT(CONCAT('"',replace(replace(parent.parent_columns,'"',''),'''','')),'"') pparent_columns,
    child.parent_cons_type pparent_cons_type
    --CONCAT(CONCAT('''',web_form_array(find_web_frm_val).db_col_value),'''');
    FROM (
    select a.table_name child_tname,
    a.constraint_name child_cons_name,
    NVL(b.r_constraint_name,a.constraint_name) parent_cons_name,
    b.constraint_type parent_cons_type,
    max(decode(position, 1, '"'||column_name||'"',NULL)) ||
    max(decode(position, 2,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 3,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 4,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 5,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 6,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 7,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 8,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 9,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,10,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,11,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,12,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,13,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,14,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,15,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,16,', '||'"'||column_name||'"',NULL))
    child_columns
    from user_cons_columns a, user_constraints b
    where a.constraint_name = b.constraint_name
    and b.constraint_type IN ( 'U', 'R' )
    --and nvl(B.r_constraint_name,B.constraint_name) = A.constraint_name
    -- and nvl(a.r_owner,a.owner) = b.owner
    group by a.table_name, a.constraint_name, b.r_constraint_name,b.constraint_type ) child,
    ( select a.constraint_name parent_cons_name,
    a.table_name parent_tname,
    max(decode(position, 1, '"'||column_name||'"',NULL)) ||
    max(decode(position, 2,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 3,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 4,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 5,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 6,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 7,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 8,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position, 9,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,10,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,11,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,12,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,13,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,14,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,15,', '||'"'||column_name||'"',NULL)) ||
    max(decode(position,16,', '||'"'||column_name||'"',NULL))
    parent_columns
    from user_cons_columns a, user_constraints b
    where a.constraint_name = b.constraint_name
    and b.constraint_type in ( 'P', 'U', 'R' )
    group by a.table_name, a.constraint_name ) parent
    where child.parent_cons_name = parent.parent_cons_name
    and child.child_tname = upper('YOUR TABLE NAME HERE')
    LOOP
    dbdata.extend(i);
    dbdata(i) := cdrl5.dbconstraint_documents_rec(cns_rec.pparent_cons_name,cns_rec.clchild_tname,cns_rec.pparent_tname,cns_rec.pparent_columns,cns_rec.pparent_cons_type);
    --dbdata(i) := '';
    -- dbdata(i) := dbconstraint_rec('test','test','test','test');
    --v_sql :=
    -- dbdata(i).constraint_name;
    i := i+1;
    END LOOP;
    FOR d IN 1..dbdata.COUNT
    LOOP
    IF dbdata(d).constraint_name IS NULL
    THEN
    dbdata.delete(d);
    END IF;
    END LOOP;
    -- dbdata.delete;
    return dbdata;
    END;
    Second part is code that is in trigger of table you are inserting into. Note that you have to store the table name/column name(that make up the constraint)/actual form values into pl/sql table:
    DECLARE
    v_sql VARCHAR2(32000);
    v_literal_value VARCHAR2(100);
    v_error_relay VARCHAR2(4000);
    v_db_values NUMBER := 1;
    v_dup_count NUMBER;
    nforeign_key EXCEPTION;
    TYPE DupRecordType IS REF CURSOR;
    dup_rec_cv DupRecordType;
    dbdata DbCons_Documents_Array := DbCons_Documents_Array(cdrl5.dbconstraint_documents_rec(null,null,null,null,null));
    TYPE tokenTableType is TABLE of varchar2(4000) -- table for Stringtoken
    index by binary_integer;
    tokenChar VARCHAR2(4000) := '," ';
    tokens tokenTableType;
    vCnt integer := 1;
    myLine varchar2(4000) := null;
    Line varchar2(4000) := null;
    vPos integer := 1;
    TYPE form_rec IS RECORD
    table_name VARCHAR2(100),
    db_column VARCHAR2(100),
    db_col_value VARCHAR2(1000)
    TYPE form_type IS TABLE OF form_rec
    INDEX BY binary_integer;
    web_form_array form_type;
    BEGIN
    dbdata.DELETE;
    -- IF :new.DOC_ID IS NOT NULL
    -- THEN
    NOTE: Load the below table(with values specified) for each table constraint
    web_form_array(v_db_values).table_name :=
    'YOUR TABLE NAME';
    web_form_array(v_db_values).db_column :=
    'YOUR FIELD NAME';
    web_form_array(v_db_values).db_col_value :=
    :new.;
    v_db_values := v_db_values +1;
    FOR cns_rec IN(select *
    from TABLE ( cast( documents_cons_dml() as DbCons_documents_Array ))
    LOOP
    line :=
    cns_rec.constraint_where;
    v_sql := 'SELECT count(*) FROM '||cns_rec.constraint_column;
    IF INSTR(line,',',1,1) <= 0
    THEN
    FOR find_web_frm_val IN 1..web_form_array.COUNT
    LOOP
    IF cns_rec.table_name = web_form_array(find_web_frm_val).table_name
    AND replace(line,'"','') = web_form_array(find_web_frm_val).db_column
    THEN
    v_literal_value :=
    CONCAT(CONCAT('''',web_form_array(find_web_frm_val).db_col_value),'''');
    v_sql := v_sql||' WHERE '||' '||web_form_array(find_web_frm_val).db_column||' = '||v_literal_value;
    END IF;
    END LOOP;
    ELSE
    while (vPos <= length(line))
    loop
    if (length(replace(tokenChar,substr(line, vPos, 1), '')) = length(tokenChar))
    then
    myLine := myLine || substr(line, vPos, 1);
    elsif (myLine is not NULL) then
    tokens(vCnt) := myLine;
    FOR find_web_frm_val IN 1..v_db_values
    LOOP
    IF cns_rec.table_name = web_form_array(find_web_frm_val).table_name
    AND MYLine = web_form_array(find_web_frm_val).db_column
    THEN
    v_literal_value :=
    CONCAT(CONCAT('''',web_form_array(find_web_frm_val).db_col_value),'''');
    END IF;
    end loop;
    IF vCnt = 1
    THEN
    v_sql := v_sql||' WHERE '||' '||myLine||' = '||v_literal_value;
    ELSE
    v_sql := v_sql||' AND '||' '||myLine||' = '||v_literal_value;
    END IF;
    myLine := null;
    vCnt := vCnt + 1;
    end if;
    vPos := vPos + 1;
    end loop;
    if (myLine is not NULL) then
    tokens(vCnt) := myLine;
    end if;
    vCnt := 1;
    vPos := 1;
    END IF;
    OPEN dup_rec_cv FOR v_sql;
    FETCH dup_rec_cv
    INTO v_dup_count;
    CLOSE dup_rec_cv;
    IF cns_rec.column_position = 'R'
    THEN
    IF v_dup_count <= 0
    THEN
    v_error_relay :=
    'The form field value for Database Column '||' '||line||' '||' that was entered does not exist';
    RAISE nforeign_key;
    END IF;
    ELSIF cns_rec.column_position = 'U'
    THEN
    IF v_dup_count > 0
    THEN
    RAISE DUP_VAL_ON_INDEX;
    END IF;
    END IF;
    -- commit_trans(v_dup_count);
    -- INSERT INTO test
    -- values(line||' '||INSTR(line,',',1,1));
    -- VALUES(cns_rec.constraint_name||' '||cns_rec.table_name||' '||cns_rec.constraint_column||' '||cns_rec.constraint_where);
    -- values(v_sql||' '||vPos||' '||length(line));
    -- VALUES(web_form_array(i).table_name||' '||CONCAT(CONCAT('''',web_form_array(i).db_col_value),'''')||' '||i);
    --commit;
    -- i := i+1;
    END LOOP;
    EXCEPTION
    WHEN DUP_VAL_ON_INDEX
    THEN
    raise_application_error(-20101, 'Attempted to update duplicate. Cannot update '||:new.);
    WHEN nforeign_key
    THEN raise_application_error(-20101, v_error_relay||' '||' YOUR MESSAGE HERE '||:new.);
    END;

    Mark:
    Ordinarily, I not really a critical guy, but that thing is such a mess that I have to comment. I won't mention the complete lack of bind variables (which leads to the shared pool and hard parsing concerns), but as posted it won't even compile. I had some time to kill, so.
    For starters, the big query in your function gets:
    ORA-01467: sort key too long
    on my 9.2.0.6 instance with 16K blocksize, and on my 9.2.0.1 instance with an 8K blocksize. Interestingly, it does work on my 8.1.7.4 instance with 8K blocks. So, I had to limit it to 15 columns.
    You give no function name at all. Based on the trigger code, I assume the function should be documents_cons_dml. I also added a paramter to pass the table name since one could conceivably have more than one table in an application.
    I also took the liberty of fixing your extend problem. In your code, you extend the array by one in the first iteration through the loop, by two in the second and so on. Which is why you need the delete loop at the end.
    There is no definition for the DbCons_documents_Array that the function returns, so I assumed something like:
    SQL> CREATE TYPE dbconstraint_documents_rec AS OBJECT (
      2     constraint_name   VARCHAR2(30),
      3     table_name        VARCHAR2(30),
      4     constraint_column VARCHAR2(30),
      5     constraint_where  VARCHAR2(4000),
      6     column_position   VARCHAR2(1) );
      7  /
    Type created.
    SQL> CREATE TYPE DbCons_documents_Array AS
      2     VARRAY(50) OF dbconstraint_documents_rec;
      3  /
    Type created.I used the names that you used in the trigger, although the names don't seem to match with the column contents, must be code re-use.
    So, now the function looks like:
    SQL> CREATE FUNCTION documents_cons_dml (p_table_name IN VARCHAR2)
      2    RETURN DbCons_documents_Array AS
      3
      4     dbdata DbCons_documents_Array := DbCons_documents_Array();
      5     i NUMBER := 1;
      6  BEGIN
      7     FOR cns_rec IN(SELECT child.child_cons_name clchild_cons_name,
      8                           child.parent_cons_name clparent_cons_name,
      9                           child.child_columns clchild_columns,
    10                           parent.parent_cons_name pparent_cons_name,
    11                           parent.parent_tname pparent_tname,
    12                           child.child_tname clchild_tname,
    13                           CONCAT(CONCAT('"',REPLACE(REPLACE(parent.parent_columns,'"',''),'''','')),'"') pparent_columns,
    14                           child.parent_cons_type pparent_cons_type
    15                    FROM (SELECT a.table_name child_tname,
    16                                 a.constraint_name child_cons_name,
    17                                 NVL(b.r_constraint_name,a.constraint_name) parent_cons_name,
    18                                 b.constraint_type parent_cons_type,
    19                                 MAX(DECODE(position, 1, '"'||column_name||'"',NULL)) ||
    20                                 MAX(DECODE(position, 2,', "'||column_name||'"',NULL)) ||
    21                                 MAX(DECODE(position, 3,', "'||column_name||'"',NULL)) ||
    22                                 MAX(DECODE(position, 4,', "'||column_name||'"',NULL)) ||
    23                                 MAX(DECODE(position, 5,', "'||column_name||'"',NULL)) ||
    24                                 MAX(DECODE(position, 6,', "'||column_name||'"',NULL)) ||
    25                                 MAX(DECODE(position, 7,', "'||column_name||'"',NULL)) ||
    26                                 MAX(DECODE(position, 8,', "'||column_name||'"',NULL)) ||
    27                                 MAX(DECODE(position, 9,', "'||column_name||'"',NULL)) ||
    28                                 MAX(DECODE(position,10,', "'||column_name||'"',NULL)) ||
    29                                 MAX(DECODE(position,11,', "'||column_name||'"',NULL)) ||
    30                                 MAX(DECODE(position,12,', "'||column_name||'"',NULL)) ||
    31                                 MAX(DECODE(position,13,', "'||column_name||'"',NULL)) ||
    32                                 MAX(DECODE(position,14,', "'||column_name||'"',NULL)) ||
    33                                 MAX(DECODE(position,15,', "'||column_name||'"',NULL)) child_columns
    34                          FROM user_cons_columns a, user_constraints b
    35                          WHERE a.constraint_name = b.constraint_name and
    36                                b.constraint_type IN ( 'U', 'R' )
    37                          GROUP BY a.table_name, a.constraint_name,
    38                                   b.r_constraint_name,b.constraint_type ) child,
    39                         (SELECT a.constraint_name parent_cons_name,
    40                                 a.table_name parent_tname,
    41                                 MAX(DECODE(position, 1, '"'||column_name||'"',NULL)) ||
    42                                 MAX(DECODE(position, 2,', "'||column_name||'"',NULL)) ||
    43                                 MAX(DECODE(position, 3,', "'||column_name||'"',NULL)) ||
    44                                 MAX(DECODE(position, 4,', "'||column_name||'"',NULL)) ||
    45                                 MAX(DECODE(position, 5,', "'||column_name||'"',NULL)) ||
    46                                 MAX(DECODE(position, 6,', "'||column_name||'"',NULL)) ||
    47                                 MAX(DECODE(position, 7,', "'||column_name||'"',NULL)) ||
    48                                 MAX(DECODE(position, 8,', "'||column_name||'"',NULL)) ||
    49                                 MAX(DECODE(position, 9,', "'||column_name||'"',NULL)) ||
    50                                 MAX(DECODE(position,10,', "'||column_name||'"',NULL)) ||
    51                                 MAX(DECODE(position,11,', "'||column_name||'"',NULL)) ||
    52                                 MAX(DECODE(position,12,', "'||column_name||'"',NULL)) ||
    53                                 MAX(DECODE(position,13,', "'||column_name||'"',NULL)) ||
    54                                 MAX(DECODE(position,14,', "'||column_name||'"',NULL)) ||
    55                                 MAX(DECODE(position,15,', "'||column_name||'"',NULL)) parent_columns
    56                          FROM user_cons_columns a, user_constraints b
    57                          WHERE a.constraint_name = b.constraint_name and
    58                                b.constraint_type IN ( 'P', 'U', 'R' )
    59                          GROUP BY a.table_name, a.constraint_name ) parent
    60                    WHERE child.parent_cons_name = parent.parent_cons_name and
    61                          child.child_tname = upper(p_table_name)) LOOP
    62        dbdata.extend;
    63        dbdata(i) := dbconstraint_documents_rec(cns_rec.pparent_cons_name,
    64                                                cns_rec.clchild_tname,
    65                                                cns_rec.pparent_tname,
    66                                                cns_rec.pparent_columns,
    67                                                cns_rec.pparent_cons_type);
    68        i := i+1;
    69     END LOOP;
    70     RETURN dbdata;
    71  END;
    72  /
    Function created.
    SQL> CREATE TABLE t (ID NUMBER PRIMARY KEY, descr VARCHAR2(10));
    Table created.
    SQL> CREATE TABLE t1 (idt1 NUMBER PRIMARY KEY, descr VARCHAR2(10));
    Table created.
    SQL> ALTER TABLE t ADD CONSTRAINT t_fk
      2  FOREIGN KEY (id) REFERENCES t1 (idt1);
    Table altered.Now for the trigger. Once I got rid of the schema owner cdrl5 in the dbconstraint_documents_rec call it actually compiled first time. So, my trigger looks like:
    SQL> CREATE OR REPLACE TRIGGER t_bi
      2  BEFORE INSERT ON t
      3  FOR EACH ROW
      4  DECLARE
      5     v_sql VARCHAR2(32000);
      6     v_literal_value VARCHAR2(100);
      7     v_error_relay VARCHAR2(4000);
      8     v_db_values NUMBER := 1;
      9     v_dup_count NUMBER;
    10     nforeign_key EXCEPTION;
    11
    12     TYPE DupRecordType IS REF CURSOR;
    13     dup_rec_cv DupRecordType;
    14
    15     dbdata DbCons_Documents_Array := DbCons_Documents_Array(dbconstraint_documents_rec(null,null,null,null,null));
    16
    17     TYPE tokenTableType IS TABLE OF VARCHAR2(4000) INDEX BY BINARY_INTEGER;
    18     tokenChar VARCHAR2(4000) := '," ';
    19     tokens tokenTableType;
    20     vCnt INTEGER := 1;
    21     myLine VARCHAR2(4000) := NULL;
    22     Line VARCHAR2(4000) := NULL;
    23     vPos INTEGER := 1;
    24
    25     TYPE form_rec IS RECORD (
    26        table_name VARCHAR2(100),
    27        db_column VARCHAR2(100),
    28        db_col_value VARCHAR2(1000));
    29
    30     TYPE form_type IS TABLE OF form_rec INDEX BY BINARY_INTEGER;
    31     web_form_array form_type;
    32
    33  BEGIN
    34     dbdata.DELETE;
    35  -- NOTE: Load the below table(with values specified) for each table constraint
    36
    37     web_form_array(v_db_values).table_name := 'T';
    38     web_form_array(v_db_values).db_column := 'ID';
    39     web_form_array(v_db_values).db_col_value := :new.ID;
    40     v_db_values := v_db_values +1;
    41
    42     FOR cns_rec IN(SELECT *
    43                    FROM TABLE ( CAST(documents_cons_dml('T') AS DbCons_documents_Array ))) LOOP
    44        line := cns_rec.constraint_where;
    45        v_sql := 'SELECT count(*) FROM '||cns_rec.constraint_column;
    46        IF INSTR(line,',',1,1) <= 0 THEN
    47           FOR find_web_frm_val IN 1 .. web_form_array.COUNT LOOP
    48              IF cns_rec.table_name = web_form_array(find_web_frm_val).table_name AND
    49                 REPLACE(line,'"','') = web_form_array(find_web_frm_val).db_column THEN
    50
    51                 v_literal_value :=CONCAT(CONCAT('''',web_form_array(find_web_frm_val).db_col_value),'''');
    52                 v_sql := v_sql||' WHERE '||' '||web_form_array(find_web_frm_val).db_column||' = '||v_literal_value;
    53              END IF;
    54           END LOOP;
    55        ELSE
    56           WHILE (vPos <= LENGTH(line)) LOOP
    57              IF (LENGTH(REPLACE(tokenChar,substr(line, vPos, 1), '')) = LENGTH(tokenChar)) THEN
    58                 myLine := myLine || substr(line, vPos, 1);
    59              ELSIF (myLine IS NOT NULL) THEN
    60                 tokens(vCnt) := myLine;
    61                 FOR find_web_frm_val IN 1 .. v_db_values LOOP
    62                    IF cns_rec.table_name = web_form_array(find_web_frm_val).table_name AND
    63                          MYLine = web_form_array(find_web_frm_val).db_column THEN
    64                       v_literal_value := CONCAT(CONCAT('''',web_form_array(find_web_frm_val).db_col_value),'''');
    65                    END IF;
    66                 END LOOP;
    67                 IF vCnt = 1 THEN
    68                    v_sql := v_sql||' WHERE '||' '||myLine||' = '||v_literal_value;
    69                 ELSE
    70                    v_sql := v_sql||' AND '||' '||myLine||' = '||v_literal_value;
    71                 END IF;
    72                 myLine := null;
    73                 vCnt := vCnt + 1;
    74              END IF;
    75              vPos := vPos + 1;
    76           END LOOP;
    77
    78           IF (myLine IS NOT NULL) THEN
    79              tokens(vCnt) := myLine;
    80           END IF;
    81           vCnt := 1;
    82           vPos := 1;
    83        END IF;
    84
    85        OPEN dup_rec_cv FOR v_sql;
    86        FETCH dup_rec_cv INTO v_dup_count;
    87        CLOSE dup_rec_cv;
    88        IF cns_rec.column_position = 'R' THEN
    89           IF v_dup_count <= 0 THEN
    90              v_error_relay := 'The form field value for Database Column '||' '||line||' '||' that was entered does not exist';
    91              RAISE nforeign_key;
    92           END IF;
    93        ELSIF cns_rec.column_position = 'U' THEN
    94           IF v_dup_count > 0 THEN
    95              RAISE DUP_VAL_ON_INDEX;
    96           END IF;
    97        END IF;
    98     END LOOP;
    99  EXCEPTION
    100     WHEN DUP_VAL_ON_INDEX THEN
    101        raise_application_error(-20101, 'Attempted to update duplicate. Cannot update '||:new.ID);
    102     WHEN nforeign_key THEN
    103        raise_application_error(-20101, v_error_relay||' '||' YOUR MESSAGE HERE '||:new.ID);
    104  END;
    105  /
    Trigger created.So, lets try this puppy out.
    SQL> INSERT INTO t1 VALUES (1, 'T1 ONE');
    1 row created.
    SQL> COMMIT;
    Commit complete.I want at least one valid value to make sure I can actually insert something without dying on the trigger.
    SQL> INSERT INTO t VALUES (1, 'T One');
    1 row created.
    SQL> COMMIT;
    Commit complete.Which we can. Now lets test the Primary Key.
    SQL> INSERT INTO t VALUES (1, 'T One');
    INSERT INTO t VALUES (1, 'T One')
    ERROR at line 1:
    ORA-00001: unique constraint (OPS$ORACLE.SYS_C0027113) violatedOOPS, that's Oracle's message. Well, maybe it's because you are only checking cns_rec.column_position for R or U and my constraint is a P, and by the way, your function does not return the Primary Key for the table passed at all.
    So, lets try the foreign key:
    SQL> INSERT INTO t VALUES (2, 'T Two');
    INSERT INTO t VALUES (2, 'T Two')
    ERROR at line 1:
    ORA-02291: integrity constraint (OPS$ORACLE.T_FK) violated - parent key not foundOOPS. So, for every valid insert I am doing a huge amount of extra work, and I still get Oracle's errors when it's wrong. If I have screwed something up in the set-up, please feel free to post a working example exactly as I did here.
    John

  • FOREIGN KEY CONSTRAINT

    HI GUYS
    I DID THE FOLL
    CREATE TABLE MASTER(EMPNO NUMBER PRIMARY KEY,
    ENAME VARCHAR2(12));
    CREATE TABLE CHILD(EMPNO NUMBER);
    ALTER TABLE CHILD ADD CONSTRAINT FOREIGN KEY REFERNCES MASTER(EMPNO)
    NOW WHEN I DOTHE ABOVE AND THE DO DESC CHILD
    I GET THE FOLL
    EMPNO NUMBER
    ENAME VARCHAR2(12)
    KEY NUMBER
    WHAT IS THIS KEY COLUMN ? WHY IS IT COMING
    KINDLY CLARIFY

    Interesting.
    The correct syntax to add a FK constraint is:
    ALTER TABLE child ADD CONSTRAINT
       FOREIGN KEY (empno) REFERENCES master (empno);I can replicate your behaviour in 9.2.0.6, and in 8.1.7.4. It appears that in the absence of a column list in parens, Oracle takes the word after FOREIGN and adds a column with that name and the appropriate data type to the table and uses that in the FK.
    SQL> CREATE TABLE master (empno NUMBER PRIMARY KEY,
      2                       ename VARCHAR2(12));
    Table created.
    SQL> CREATE TABLE child (empno NUMBER);
    Table created.
    SQL> ALTER TABLE child ADD CONSTRAINT
      2     FOREIGN new_col REFERENCES master (empno);
    Table altered.
    SQL> desc child
    Name                                      Null?    Type
    EMPNO                                              NUMBER
    NEW_COL                                            NUMBERJust to show that the constraint is enforced:
    SQL> INSERT INTO child VALUES(1,1);
    INSERT INTO child VALUES(1,1)
    ERROR at line 1:
    ORA-02291: integrity constraint (OPS$ORACLE.SYS_C0070911) violated -
    parent key not foundBut, this only works when there is no KEY keyword:
    SQL> DROP TABLE child;
    Table dropped.
    SQL> CREATE TABLE child (empno NUMBER);
    Table created.
    SQL> ALTER TABLE child ADD CONSTRAINT
      2     FOREIGN KEY new_col REFERENCES master (empno);
       FOREIGN KEY new_col REFERENCES master (empno)
    ERROR at line 2:
    ORA-00902: invalid datatypeOn a fast scan of the documentation I don't see where this behaviour is documented, and I have certainly never seen this before. Anyone out there have any thoughts?
    John

  • Oracle client 9.2.0.7 ora-01403 random error

    I updated my Oracle Client to 9.2.0.7 version and since this time I have a random error.
    It seems that the transaction abort some time, because I have ORA-01403 when I create the insert and just after the select into clause has the error.
    I don't want to trap error for security.
    Is anybody has the same type of error and how to resolve it?
    Thanks

    "No No No pl/sql codes are changed in the Db and No line of code are changed in .Net application"
    But all of the errors you have listed so far are data errors. Has any data changed in the last 3 years?
    ORA-02291 means that you are trying to insert a record into a child table that has no corresponding record in the parent table.
    SQL> CREATE TABLE t (id NUMBER NOT NULL PRIMARY KEY,
      2                  descr VARCHAR2(10));
    Table created.
    SQL> CREATE TABLE t1 (id NUMBER NOT NULL REFERENCES t(id),
      2                   other_desc VARCHAR2(10));
    Table created.
    SQL> INSERT INTO t1 VALUES (1, 'One');
    INSERT INTO t1 VALUES (1, 'One')
    ERROR at line 1:
    ORA-02291: integrity constraint (OPS$ORACLE.SYS_C0099966) violated - parent key not foundNo triggers, not even an Oracle client since I am logged into the database server.
    One more time, your random errors have nothing to do with the client version, and everything to do with the data in your database (or the lack of it for the 1403), or the data you are trying to put in (for the 2291).
    John

  • Drop system generated constraint

    Hi All,
    In a table I am having primary key called abc_pk for column. Mistakenly i have given the same name for foreign key as abc_pk and executed. As of me if same name for constraint then oracle will generate own constraint name such as sys_co4537.. if it is wrong correct me...
    if oracle itself generated constraint name then how to find constraint correct constraint name....
    case 1: if it single database i can find it in corresponding table with reference key..as (sys_co4537)
    case 2: If i am having more than 2 database then it difficult to find because each database will create different name...
    pls suggest me how to drop sys_* constraint from different database..
    Thanks & Regards
    Sami

    Hi All,
    select * from USER_CONSTRAINTS
    where table_name=<<table_name>>
    by getting the constraint name, you can drop the constraint by
    alter table <<table name>> drop constraint <<constraint name>>;By using above statments user needs to select the constraint name, after that needs to drop it. Instead of that user needs to run as script.. so i have created as block script
    as
    DECLARE
    V_CONSTRAINT_NAME VARCHAR2(30);
    BEGIN
    SELECT CONSTRAINT_NAME INTO V_CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'ABC' AND R_CONSTRAINT_NAME='ABC_PK';
    EXECUTE IMMEDIATE 'ALTER TABLE DEPOSIT_ACCOUNT_INTEREST DROP CONSTRAINT '|| V_CONSTRAINT_NAME;
    EXECUTE IMMEDIATE 'ALTER TABLE DEPOSIT_ACCOUNT_INTEREST ADD CONSTRAINT ABC_FK FOREIGN KEY (XXX_ID) REFERENCES DEPOSIT_ACCOUNT XXX_ID) DEFERRABLE INITIALLY DEFERRED';
    END;I think this might me usefull to some one..
    Thanks & Regards
    Sami.

  • Constraint trouble

    I have disabled a foreign key constraint and deleted all parent records in table A. I tried to re-enable the constraint and PLSQL complained about children in table B missing parent records. I want to tie the orphaned child records in table B to newly created records in Table C via a separate column. Is there anything I can do to reactivate the FK short of deleting the child records?

    You cannot have the same column constrained against two different tables, so you may be out of luck. However, if you need to constraint those orphans against a different column that will be null for the other records, you have two choices.
    First, you could insert a dummy parent in the parent table and assign the current orphans to that dummy parent. The other option would be to enable the constraint with the novalidate option.
    I assume you situation is something like this:
    SQL> CREATE TABLE t (id NUMBER, descr VARCHAR2(10));
    Table created.
    SQL> CREATE TABLE t1 (t1_id NUMBER, t_id NUMBER, descr VARCHAR2(10));
    Table created.
    SQL> ALTER TABLE t1 ADD CONSTRAINT t1_t_fk
      2     FOREIGN KEY (t_id) REFERENCES t (id);
    Table altered.
    SQL> INSERT INTO t VALUES(1, 'One');
    1 row created.
    SQL> INSERT INTO t VALUES(2, 'Two');
    1 row created.
    SQL> INSERT INTO t VALUES(3, 'Three');
    1 row created.
    SQL> INSERT INTO t1 VALUES (1, 1, 'T1One');
    1 row created.
    SQL> INSERT INTO t1 VALUES (2, 2, 'T1Two');
    1 row created.
    SQL> INSERT INTO t1 VALUES (3, 3, 'T1Three');
    1 row created.
    SQL> COMMIT;
    Commit complete.
    SQL> ALTER TABLE t1 DISABLE CONSTRAINT t1_t_fk;
    Table altered.
    SQL> DELETE FROM t WHERE id = 3;
    1 row deleted.
    SQL> COMMIT;
    Commit complete.You are here now. Creating a "dummy" parent requires an insert into parent (t in my example) and and update to child (t1 in my example). The novalidate option would go lke:
    SQL> ALTER TABLE t1 ENABLE NOVALIDATE CONSTRAINT t1_t_fk;
    Table altered.
    SQL> INSERT INTO t1 VALUES (4, 3, 'T13Again');
    INSERT INTO t1 VALUES (4, 3, 'T13Again')
    ERROR at line 1:
    ORA-02291: integrity constraint (OPS$ORACLE.T1_T_FK) violated - parent key not foundSo you cannot enter new orphans, but the existing ones will be fine. You can even update them if neccessary:
    SQL> UPDATE t1 SET descr = 'Will Fail'
      2  where t1_id = 3;
    1 row updated.
    SQL> COMMIT;
    Commit complete.HTH
    John

  • Row level constraint

    is there any row level constraint in oracle.
    if it is possible plz send me the statment of that constraint.
    OR how to implment row level secuirty.
    best wishes

    Duplicate ... lets stick with the other one in this forum,.

  • EjbRemove fails with deferred constraints in 8.1.6

    I get a constraint violation under certain conditions from Oracle.
    IBM ejb container hides the exception, and raises a CSI_ROLLBACK exception. The only workaround I can think of, is disabling all constraints in Oracle, and trusting IBMs Application server to guarantee the
    referential integrity of the database.
    Does anyone else notice these problems and found a resolution?
    Oracle introduced deferred constraints to allow the constraint violation checking at the end of an transaction.
    For the IBM implementation of entity EJBs this is essential,
    because any the "DELETE FROM WHERE id=" which result from an ejbRemove() happens >>>before<<< an ejbStore() which results in "UPDATE SET foreignkey = NULL ".
    I am using the jdbc driver from classes12.zip, and noticed that
    you get an constraint violation on a commit, when the record has been created in a transaction, is updated in a transaction, and then is deleted in a transaction. The transactions are isolated.
    A rollback, and deleting again resolves the problem.
    But the problem is that an Websphere Application server is in between the
    delete from a user, and the jdbc driver accessed through a datasource.
    null

    Does anyone know where to configure and find required settings (e.g. the number of open_cursors) for Oracle 8.1.6 server when using the jdbc thin driver ?
    null

  • Speeding up enable of constraints

    I have various constraints that used to take seconds and now are taking up to half an hr to enable. The data in the tables is static, it has increased or decreased. Does anyone know how to speed up the enabling of constraints on a table? What goes on behind the scenes after issuing alter table <table_name> enable constraint <constraint_name>;
    Thankis

    Dear HouseofHunger,
    Please tell me if that online documentation is enough for you or not?
    http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_3001.htm#i2183600
    +"+
    +Restrictions on Modifying Constraints Modifying constraints is subject to the following restrictions:+
    +*You cannot change the state of a NOT DEFERRABLE constraint to INITIALLY DEFERRED.+
    +*If you specify this clause for an index-organized table, then you cannot specify any other clauses in the same statement.+
    +*You cannot change the NOT NULL constraint on a foreign key column of a reference-partitioned table, and you cannot change the state of a partitioning referential constraint of a reference-partitioned table.+
    +See Also:+
    +"*Changing the State of a Constraint: Examples*"+
    +"+
    +"+
    +Changing the State of a Constraint: Examples The following statement places in ENABLE VALIDATE state an integrity constraint named emp_manager_fk in the employees table:+
    +ALTER TABLE employees+
    +ENABLE VALIDATE CONSTRAINT emp_manager_fk+
    +EXCEPTIONS INTO exceptions;+
    +Each row of the employees table must satisfy the constraint for Oracle Database to enable the constraint. If any row violates the constraint, then the constraint remains disabled. The database lists any exceptions in the table exceptions. You can also identify the exceptions in the employees table with the following statement:+
    +SELECT e.*+
    +FROM employees e, exceptions ex+
    +WHERE e.rowid = ex.row_id+
    +AND ex.table_name = 'EMPLOYEES'+
    +AND ex.constraint = 'EMP_MANAGER_FK';+
    +The following statement tries to place in ENABLE NOVALIDATE state two constraints on the employees table:+
    +ALTER TABLE employees+
    +ENABLE NOVALIDATE PRIMARY KEY+
    +ENABLE NOVALIDATE CONSTRAINT emp_last_name_nn;+
    +This statement has two ENABLE clauses:+
    +*The first places a primary key constraint on the table in ENABLE NOVALIDATE state.+
    +*The second places the constraint named emp_last_name_nn in ENABLE NOVALIDATE state.+
    +In this case, Oracle Database enables the constraints only if both are satisfied by each row in the table. If any row violates either constraint, then the database returns an error and both constraints remain disabled.+
    +Consider the foreign key constraint on the location_id column of the departments table, which references the primary key of the locations table. The following statement disables the primary key of the locations table:+
    +ALTER TABLE locations+
    +MODIFY PRIMARY KEY DISABLE CASCADE;+
    +The unique key in the locations table is referenced by the foreign key in the departments table, so you must specify CASCADE to disable the primary key. This clause disables the foreign key as well.+
    +"+
    Hope That Helps.
    Ogan

  • Enabling Constraints

    Hi All,
    Some generic conceptual questions.
    Oracle Database Version: 11.1.0.7
    1) Is there any time difference between enabling constraint on a Non-partitioned table and enabling constraint on a partitioned table, both tables having same no. of records?
    2) Suppose, we have 3 tables A, B and C.
    B and C have FK on Table A. After loading data I want to enable constraints on table B and C. Can I do that in parallel? Or do I have to wait for one table to enable constraint and then start enabling FK constraint for the second table? Is there some locking mechanism, which enables lock on Table A while enabling FK on table B is happening?
    Thanks,
    Ishan
    Edited by: Ishan on May 12, 2012 3:54 PM

    >
    Hi,
    1) Is there any time difference between enabling constraint on a Non-partitioned table and
    enabling constraint on a partitioned table, both tables having same no. of records? http://docs.oracle.com/cd/B10501_01/server.920/a96521/partiti.htm
    =====================
    Partitions and subpartitions of a table or index all share the same logical attributes. For example, all partitions (or subpartitions) in a table share the same column and constraint definitions, and all partitions (or subpartitions) of an index share the same index options. They can, however, have different physical attributes (such as TABLESPACE).
    ======================
    2) Suppose, we have 3 tables A, B and C.
    B and C have FK on Table A. After loading data I want to enable constraints on table B and C.
    Can I do that in parallel? Or do I have to wait for one table to enable constraint and then start
    enabling FK constraint for the second table? Is there some locking mechanism, which enables
    lock on Table A while enabling FK on table B is happening?Why in parallel? AFAICS from the docco above, enabling CONSTRAINTs is a "binary" thing for all partitions - it's
    either enabled or it's not.
    Are you enabling this on a production server while updates are taking place? You might wish to consider
    looking at DEFERRABLE? But, you use the term "loading" so this isn't prod?
    Googling "enable constraint partitioned table" and "constraint deferrable oracle partitioned table" gave several
    interesting pages which may be of interest, but I don't know enough about your case to help further.
    HTH all the same,
    Paul...
    IshanEdited by: Paulie on 12-May-2012 20:42

  • Checking status of Deferred/Immediate constraints

    I know I can check the status of a Disabled/Enabled constraint in oracle by:
    select status from <constraint table> where <constraint name> and that's peachy, what i'm trying to do is check the status of a constraint that i've deferred that is normally immediate and so far i've come up empty handed.
    for example: the constraint resides in user_constraints, the columns deferrable, deferred and status are of no help since they contain information about the normal state of the constraint.
    so doing
    select constraint_name, deferred from user_constraints;
    set constraint cons_name deferred;
    select constraint_name, deferred from user_constraints;yields the same information (ie..useless)
    any ideas?

    The application code is doing a SET CONSTRAINT call at some appropriate place in the code. One application module may decide to defer it to continue processing whereas some other module may decide to leave it as-is.
    At one point in time, one transaction may have issued SET CONSTRAINT to defer it and other transaction involving same table may not have done so. You cannot look at the dictionary table to see what is the state, since the state is being altered by the transaction in progress, and there may be many transactions at the same time - one status cannot give information about all.

Maybe you are looking for

  • My first gen ipod nano is not recognized by win or itunes. Plz help me!!!

    After i plug my ipod it appears on my computer after 10 min as removable disk not labeled as (ipod drive H) and i am not able to access it, when i try the comp get frozen for some time and then i get alert that i should insert disk into drive H. When

  • How to have a value survive logout and destruction of the session

    Hi, Currently I'm facing the following challenge: For a customer we are building a webcenter portal app (version 11.1.1.6). Depending on where you come from, customer wants to have different styles and template. I have done this by adding a URL param

  • Problems with effects in PSE 10 (was from cappy pappy)

    I have an IMAC and use PSE 10 for my photoshop.  I have several Florabella add ons that I use and was trying to add some new ones last night.  So I went through the usual steps but when I went from photo creations to photo effect there was no photo e

  • Downloaded songs lost

    i purchased an album on iTunes and was having problems with my internet connection and downloading. before it could finish my computer froze and i was forced to restart. when i reopened iTunes, it didn't have the album. does anyone know how i can eit

  • How to put Author name and other book/magazine information into iTunes

    Hi, I'm trying to organize my pdf's in iTunes (and while I'm at it, I'm putting some web magazines in as well).  I know to highlight the title and press Command/I to edit information, but categories like "author", "publisher" don't come up even thoug