FOREIGN KEY CONSTRAINT의 MASTER TABLE을 REFERENCE하는 TABLE 찾기
제품 : SQL*PLUS
작성날짜 : 2003-12-17
FOREIGN KEY CONSTRAINT의 MASTER TABLE을 REFERENCE하는 TABLE 찾기
================================================================
Master table 이 dept2일 때, 이 테이블을 참조하는 table들을 찾는 SQL
select x.table_name "reference table"
from
(select distinct r_constraint_name,table_name
from all_constraints
where constraint_type='R' ) x, all_constraints a
where a.table_name = 'DEPT2'
and x.r_constraint_name = a.constraint_name;
Similar Messages
-
FOREIGN KEY CONSTRAINT 의 MASTER TABLE NAME 의 확인
제품 : ORACLE SERVER
작성날짜 : 1995-11-02
FOREIGN KEY CONSTRAINT 의 MASTER TABLE NAME 의 확인
===================================================
다음은 FOREIGN KEY CONSTRAINT 이름으로 REFERENCE 하는 TABLE
(MASTER TABLE)을 찾는 SQL SCRIPT이다.
col Primary_key_table format a20
col Constraint_name format a20
select a.object_name Primary_Key_table,
c.name Constraint_name
from dba_objects a,
sys.cdef$ b,
sys.con$ c
where c.name = 'EMP_FOREIGN_KEY' -- CONSTRAINT NAME
and b.con# = c.con#
and b.robj# = a.object_id
/The set of constraints as you show it is valid, but will likely result in a lot of violations since both child columns are larger than the parent. The Oracle 2256 error has nothing to do with data validation, nor with the different lengths of the columns. The documentation says
02256, 00000, "number of referencing columns must match referenced columns"
// *Cause: The number of columns in the foreign-key referencing list is not
// equal to the number of columns in the referenced list.
// *Action: Make sure that the referencing columns match the referenced
// columns.Look at the actual statement that the client ran. It will be different than the one you posted. one of the two column lists will have more columns than the other. -
Using FOreign key constraints on tables in database.
I am student and novice in the field of ORACLE and PL/SQL and Database Creation. I had created a database consisting tables and got problem while applying foreign key constraints.
CUST_MSTR
CREATE TABLE "DBA_BANKSYS"."CUST_MSTR"("CUST_NO" VARCHAR2(10),
"FNAME" VARCHAR2(25), "MNAME" VARCHAR2(25), "LNAME" VARCHAR2(25),
"DOB_INC" DATE NOT NULL, "OCCUP" VARCHAR2(25), "PHOTOGRAPH" VARCHAR2(25),
"SIGNATURE" VARCHAR2(25), "PANCOPY" VARCHAR2(1), "FORM60" VARCHAR2(1));
(CUST_NO is PRIMARY KEY, )
-- EMP_MSTR
CREATE TABLE "DBA_BANKSYS"."EMP_MSTR"("EMP_NO" VARCHAR2(10),
"BRANCH_NO" VARCHAR2(10), "FNAME" VARCHAR2(25), "MNAME" VARCHAR2(25),
"LNAME" VARCHAR2(25), "DEPT" VARCHAR2(30), "DESIG" VARCHAR2(30));
(EMP_NO is primary key )
--NOMINEE_MSTR
CREATE TABLE "DBA_BANKSYS"."NOMINEE_MSTR"("NOMINEE_NO" VARCHAR2(10),
"ACCT_FD_NO" VARCHAR2(10), "NAME" VARCHAR2(75), "DOB" DATE,
RELATIONSHIP" VARCHAR2(25));
(NOMINEE_NO is primary key )
--ADDR_DTLS
CREATE TABLE "DBA_BANKSYS"."ADDR_DTLS"("ADDR_NO" NUMBER(6),
"CODE_NO" VARCHAR2(10), "ADDR_TYPE" VARCHAR2(1), "ADDR1" VARCHAR2(50),
"ADDR2" VARCHAR2(50), "CITY" VARCHAR2(25), "STATE" VARCHAR2(25),
"PINCODE" VARCHAR2(6));
( ADDR_NO is primary key )
Problem: I want to apply foreign key constraints on ADDR_DTLS table so that Before inserting value in ADDR_DTLS table it must check, VALUE in ADDR_DTLS.CODE_NO must be PRESENT either in attribute value CUST_MSTR.CODE_NO or EMP_MSTR.CODE_NO or NOMINEE_MSTR.CODE_NO table .
I applied the foreign key constraints using this syntax
CREATE TABLE "DBA_BANKSYS"."ADDR_DTLS"("ADDR_NO" NUMBER(6),
"CODE_NO" VARCHAR2(10), "ADDR_TYPE" VARCHAR2(1), "ADDR1" VARCHAR2(50),
"ADDR2" VARCHAR2(50), "CITY" VARCHAR2(25), "STATE" VARCHAR2(25),
"PINCODE" VARCHAR2(6),
constraints fk_add foreign key CODE_NO references CUST_MSTR. CODE_NO,
constraints fk_add1 foreign key CODE_NO references EMP_MSTR. CODE_NO,
constraints fk_add2 foreign key CODE_NO references NOMINEE_MSTR.CODE_NO);
(foreign key)
ADDR_DTLS.CODE_NO ->CUST_MSTR.CUST_NO
ADDR_DTLS.CODE_NO ->NOMINEE_MSTR.NOMINEE_NO
ADDR_DTLS.CODE_NO ->BRANCH_MSTR.BRANCH_NO
ADDR_DTLS.CODE_NO ->EMP_MSTR.EMP_NO
When I applied foreign key constraints this way, its gives a error called foreign key constraints violation. (I understand that, its searches the attribute value of ADDR_DTLS.CODE_NO in all the three tables must be present then the value will be inserted. But I want, if the value is in any of the three table then its should insert the value or its gives an error.)
Please help me out, though i put the question and i want too know how to apply the forign key in this way. and is there any other option if foreign key implementation is not pssible.If you are on 11g you can use ON DELETE SET NULL:
CREATE TABLE addr_dtls
( addr_no NUMBER(6) CONSTRAINT addr_pk PRIMARY KEY
, addr_cust_no CONSTRAINT addr_cust_fk REFERENCES cust_mstr ON DELETE SET NULL
, addr_emp_no CONSTRAINT addr_emp_fk REFERENCES emp_mstr ON DELETE SET NULL
, addr_nominee_no CONSTRAINT addr_nominee_fk REFERENCES nominee_mstr ON DELETE SET NULL
, addr_type VARCHAR2(1)
, addr1 VARCHAR2(50)
, addr2 VARCHAR2(50)
, city VARCHAR2(25)
, state VARCHAR2(25)
, pincode VARCHAR2(6) );In earlier versions you'll need to code some application logic to do something similar when a parent row is deleted, as otherwise the only options are to delete the dependent rows or raise an error.
btw table names can be up to 30 characters and don't need to end with MSTR or DTLS, so for example CUSTOMERS and ADDRESSES might be more readable than CUST_MSTR and ADDR_DTLS. Also if the Customer/Employee/Nominee PKs are generated from a sequence they should be numeric.
Edited by: William Robertson on Aug 15, 2010 6:47 PM -
FOREIGN KEY Constraint And Table Lock
According to Db Concepts on Data Integrity->Types of Integrity Contraints->Referential Integrity Constraints, if the FK column is not indexed, delete/update of parent table would cause child table share locked.
I can understand the reason behind this behaviour because if parent's PK is modified but not commited yet, we don't want another transaction to insert/update the child's FK column to use the deleted parent PK value and this requires a table share lock.
The problem is, I don't understand why if the child's FK is indexed, no table share lock is acquired. The same condition could happen and we still need to prevent another transaction from using the uncommitted deleted PK in parent as the child's FK.
Appreciate and advice.hi
When PK column change, Oracle is going to have to do a
referential integrity check on any referenced columns ,in
the absence of an index on the child end of the foreign
key, that would seem to result in a full table scan of the table which cause locks the entire table for any
Delete or Update on Parent table.
Khurram -
Import table data in right order to avoid violating foreign key constraints
Gentlemen
I am trying to import table data into an existing 10g schema using datapump import in table mode.
However, in order to avoid violating foreign key constraints, the tables must be loaded in a specified order. I tried specifying the order in the TABLES parameter:
TABLES=table1,table2,table3 etc.
However, datapump seems to chose its own order leading to errors like the following:
ORA-31693: Table data object "SCHEMAX"."TABLE3" failed to load/unload and is being skipped due to error:
ORA-02291: integrity constraint (SCHEMAX.TABLE3_TABLE1#FK) violated - parent key not found
I want to try to avoid having to disable all foreign keys because there are hundreds of them.
Any advice?
Yours
Claus Jacobsen, DenmarkThanks Anantha.
Since I am only loadding data (the constraints are already defined in the target database), I am not sure whether this approach would work. Meanwhile I have solved the problem of moving data from one system to another using another, tedious and far from elegant approach that I would prefer to not eloborate on:-)
However, I have also discovered another probable reason why the foreign key constraints were violated, other than wrong order of table data loading. It turns out almost every single table in the schema contains a trigger supposed to generate a unique row ID from a sequence on insert such as:
CREATE OR REPLACE TRIGGER "SCHEMAX"."TABLEX#B_I_R"
BEFORE INSERT
ON TABLEX
FOR EACH ROW
DECLARE
BEGIN
SELECT tablex_seq.nextval INTO :NEW.ID FROM dual;
END;
If the import mechanism fires this trigger, and the sequences in the source and the target systems are not synchronized, then I guess that referred records a more than likely to end up with wrong ID's compared to the row ID's in the referring rows?
Spooky. Anybody can confirm this theory?
Yours
Claus
Message was edited by:
user586249 -
Creating a foreign key constraint on a synonym of table in another schema.
Hi,
I am having two user operapps and oper
owner of table po_vendors is operapps ,i have created a synonym in oper with select permission.
now i am trying to create a foreign key.
ALTER TABLE OPS_BR_VENDORS ADD ( FOREIGN KEY (VENDOR_ID) REFERENCES PO_VENDORS (VENDOR_ID));
the bolded po_vendors is the synonym for the table po_vendors in operapps.
i am getting the below error message.
SQL> ALTER TABLE OPS_BR_VENDORS ADD ( FOREIGN KEY (VENDOR_ID) REFERENCES PO_VENDORS (VENDOR_ID));
ALTER TABLE OPS_BR_VENDORS ADD ( FOREIGN KEY (VENDOR_ID) REFERENCES PO_VENDORS (VENDOR_ID))
ERROR at line 1:
ORA-01031: insufficient privileges.
i have given dba privileges to oper user.
Please advice.1) You cannot create a constraint on a synonym. You have to specify a physical table (i.e. OPERAPPS.PO_VENDORS)
2) The owner of the OPS_BR_VENDORS table will need to have the REFERENCES permission on the PO_VENDORS table granted directly (not via a role).
Justin -
Foreign Key Constraint Failure on Self-Referencing Table
In a recent data deletion project, I ran into a problem where Oracle allowed for a record (the parent) to be deleted when there still existed a child record in the same table which referred to the parent.
An abbreviated version of the table is as follows:
create table test (
template_id number not null,
customer_id number null,
parent_id number null,
constraint pk_test primary key (template_id),
constraint r_parent foreign key (parent_id) references test (template_id));
The parent and child records are as follows:
Parent: template_id = 100, customer_id = 200, parent_id = null
Child: template_id = 101, customer_id = null, parent_id = 100
Records were deleted from this table using:
DELETE FROM test WHERE customer_id = 200;
When this statement is executed, is it being executed as part of 155 delete statements using PL/SQL, and tens of thousands of records from 155 tables are being deleted. These delete statements have been ordered taking into account foreign key constraints. All 155 statements are being deleted in a single transaction. The delete statement above was the 23rd statement of the set to be executed. I would have expected when this delete statement was executed, an error would have been thrown. However, all 155 delete statements successfully complete, and after committing the results, the child record above still exists in the DB as an orphan. I also tried executing the first 23 statements manually in SQL*Plus (still one transaction), and the problem still occurs: the parent is deleted and the child is orphaned.
If I execute the very simple example above, I do indeed get an referential constraint error.
Has anyone ever encountered this situation? And does anyone have any ideas how to go about troubleshooting this problem. We need to know the cause of this, as we have a small handful of table with a similar self-referential foreign key constraint set-up.
Thank you very much in advance,
MarkFirst of all, the very simple example that I outlined above I have already tried and it worked: when I attempted to delete the parent a referential integrity error was thrown as one would expect. This small example was my test case to see if I could reproduce what I'm seeing in our application in a more manageable and demonstrable example. But as it worked, I'm still perplexed in reconciling the differences between what I'm seeing in our application (which I can't demonstrate in this help group) and the test case. When I used the test case in my first posting in this thread, I used it to help describe the problem I'm encountered, even though that small example works.
Below is more of the table you've asked for.
OWNER CONSTRAINT_NAME CONSTRAINT_TYPE TABLE_NAME SEARCH_CONDITION R_OWNER R_CONSTRAINT_NAME DELETE_RULE STATUS DEFERRABLE DEFERRED VALIDATED GENERATED BAD RELY LAST_CHANGE INDEX_OWNER INDEX_NAME INVALID VIEW_RELATED
"DOCPADMIN" "R_TEMPLATE_PARENT" "R" "DCR_TEMPLATE" (null) "DOCPADMIN" "XPKDCR_TEMPLATE" "NO ACTION" "ENABLED" "NOT DEFERRABLE" "IMMEDIATE" "VALIDATED" "USER NAME" (null) (null) "2010-01-13 19:01:21" (null) (null) (null) (null)
"DOCPADMIN" "XPKDCR_TEMPLATE" "P" "DCR_TEMPLATE" (null) (null) (null) (null) "ENABLED" "NOT DEFERRABLE" "IMMEDIATE" "VALIDATED" "USER NAME" (null) (null) "2010-01-13 15:48:31" (null) "XPKDCR_TEMPLATE" (null) (null)Overall, I haven't said that Oracle's FK mechanism is broken. What I've said is that I've got a situation that I'm trying to understand. My first attempt at my test case didn't work because the test case worked as it should. I'm trying to figure out another course of action to try and figure this situation out. -
Query the name of the parent table in a foreign key constraint
Hello,
Does anyone know how to query for the parent table name in a foreign key constraint? I don't see that relationship in ALL_CONS_COLUMNS or ALL_CONSTRAINTS.
Thanks in advance,
Michaelor try this...
SELECT rc.TABLE_NAME "PK_Table_Name",cc.TABLE_NAME "FK_Table_Name",
case when cc.column_name = rc.column_name
then c.TABLE_NAME || '(' || cc.COLUMN_NAME || ')'
else r.TABLE_NAME || '(' || rc.COLUMN_NAME || ') = ' ||c.TABLE_NAME || '(' || cc.COLUMN_NAME || ')' end as "TABLE_NAME(COLUMN_NAME)"
from all_constraints c,
all_constraints r,
all_cons_columns cc,
all_cons_columns rc
WHERE
r.table_name = upper('emp')
and c.CONSTRAINT_TYPE = 'R'
and c.R_OWNER = r.OWNER
and c.R_CONSTRAINT_NAME = r.CONSTRAINT_NAME
and c.CONSTRAINT_NAME = cc.CONSTRAINT_NAME
and c.OWNER = cc.OWNER
and r.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
and r.OWNER = rc.OWNER
and cc.POSITION = rc.POSITION
ORDER BY r.TABLE_NAME; -
How to alter or add foreign key constr to table having child rows or data
Hi,
I have a table by name DEPT in which the column DEPTNO is a primary key column and have data in it for all the columns . Now I have a EMP table where in the column DEPTNO is not having any constraint including foreign key constraint too. Now i have data in the EMP table for all the columns . now i would like to add the foreign key constraint to DEPTNO column in the EMP table referencing the DEPTNO column in the DEPT table. Both the table do not have any null values .
I need to add the foreign key without deleting the data in the EMP table.
Please advice.
Regards,
VineshHi,
I need to add the foreign key without deleting the data in the EMP table.Foreign key constraint can have only those values which are present in the primary key of the parent table or null.
When your tables are populated with values and then you are adding the constraint then,
the parent table data is only present in child table or null values.
Example
SQL> create table emp_bkp as select * from emp;
Table created.
SQL> create table dept_bkp as select * from dept;
Table created.
SQL> alter table dept_bkp
2 add constraint pk_deptno primary key(deptno);
Table altered.
SQL> select distinct deptno from emp_bkp;
DEPTNO
30
20
40
10
SQL> select deptno from dept_bkp;
DEPTNO
10
20
30
40
55
SQL> alter table emp_bkp
2 add constraint fk_deptno_bkp foreign key(deptno)
3 references dept_bkp(deptno);
Table altered.
SQL> alter table emp_bkp
2 drop constraint fk_deptno_bkp;
Table altered.
SQL> insert into emp_bkp(empno,ename,deptno) values(142,'Fenny',90);
1 row created.
SQL> alter table emp_bkp
2 add constraint fk_deptno_bkp foreign key(deptno)
3 references dept_bkp(deptno);
add constraint fk_deptno_bkp foreign key(deptno)
ERROR at line 2:
ORA-02298: cannot validate (SCOTT.FK_DEPTNO_BKP) - parent keys not found
SQL> update emp_bkp
2 set deptno=null
3 where empno=142;
1 row updated.
SQL> alter table emp_bkp
2 add constraint fk_deptno_bkp foreign key(deptno)
3 references dept_bkp(deptno);
Table altered.
SQL> insert into dept_bkp (deptno,dname)values(90,'IT');
1 row created.
SQL> update emp_bkp
2 set deptno=90
3 where empno=142;
1 row updated.Twinkle -
Foreign keys to same table / Who columns in User table???
Hi All,
We are designing a database for our client and we are using common columns in all tables like CreatedBy and UpdatedBy which are foreign keys to USER table. My question is can we use these two columns in USER table and have the foreign key constraints to the same tables?
USER table script will look like following..
CREATE TABLE XX_USER
User_ID VARCHAR2(10)
, CreatedBy NUMBER(10)
, UpdatedBy NUMBER(10)
, User_Name VARCHAR2(100) NOT NULL
, Designation VARCHAR2(100) NOT NULL
, CONSTRAINT USER_ISACTIVE_CHECK CHECK (Is_Active in ('Y', 'N'))
, CONSTRAINT USER_KEY PRIMARY KEY (User_ID),
, CONSTRAINT USER_CREATED_BY FOREIGN KEY (CREATED_BY)
REFERENCES XX_USER (USER_ID)
, CONSTRAINT USER_UPDATED_BY FOREIGN KEY (UPDATED_BY)
REFERENCES XX_USER (USER_ID)
);I think what I am doing (the above script) is not correct. Could someone please suggest me how can solve this issue?
Thanks in advance,
Oraebsuser8644385 wrote:
We are designing a database for our client and we are using common columns in all tables like CreatedBy and UpdatedBy which are foreign keys to USER table. My question is can we use these two columns in USER table and have the foreign key constraints to the same tables?You can. After fixing synatx errors:
SQL> CREATE TABLE XX_USER
2 (
3 User_ID VARCHAR2(10)
4 , CreatedBy NUMBER(10)
5 , UpdatedBy NUMBER(10)
6 , User_Name VARCHAR2(100) NOT NULL
7 , Designation VARCHAR2(100) NOT NULL
8 , Is_Active VARCHAR2(1)
9 , CONSTRAINT USER_ISACTIVE_CHECK CHECK (Is_Active in ('Y', 'N'))
10 , CONSTRAINT USER_KEY PRIMARY KEY (User_ID)
11 , CONSTRAINT USER_CREATED_BY FOREIGN KEY (CreatedBy)
12 REFERENCES XX_USER(USER_ID)
13 , CONSTRAINT USER_UPDATED_BY FOREIGN KEY (UpdatedBy)
14 REFERENCES XX_USER(USER_ID)
15 )
16 /
Table created.
SQL> Now USER_ID is a string while CreatedBy/UpdatedBy is a number. Even though it is allowed, it limits USER_ID to numeric strings and causes implicit conversions while validating. So I would change CreatedBy/UpdatedBy data type to VARCHAR2(10).
SY. -
Delete a user from a table whose name is a foreign key in other tables
Dear All;
I am trying to figure out an easy way to do this. I just recently took someone application who utilized 500 tables. I am trying to delete a user from a table called member_table. However, I am having problems doing so because the user name is a foreign key in other tables which has a relationship with this member_table. I really can't naviagte through all 500 different tables and start deleting the user from each table . hence, I would like to figure out a way to delete the user from the member_table without getting the error message
ORA - 02292 "Integrity Constraint (....) violated child record foundUnless you want to find and re-create all of the FK's that point to that field so you can make them ON DELETE CASCADE (note it is the FK not the PK that has that attribute), you will need to either delete that member id from each of the child tables individually or update each one individually to either null or some valid value in member_table before you can delete the id from member_table.
You can find all of the tables, and the corresponding column_name that have an FK relationship to memeber_table with the following:
SELECT c.table_name, col.column_name
FROM user_constraints c, user_cons_columns col
WHERE c.constraint_name = col.constraint_name and
c.r_constraint_name = (SELECT constraint_name
FROM user_constraints
WHERE table_name = 'MEMBER_TABLE' and
constraint_type = 'P') and
c.constraint_type = 'R';If there are a lot of these, you could use something similar to generate the set of delete/update statements that would be needed.
John -
Foreign keys at the table partition level
Anyone know how to create and / or disable a foreign key at the table partition level? I am using Oracle 11.1.0.7.0. Any help is greatly appreciated.
Hmmm. I was under the impression that Oracle usually ignores indices on columns with mostly unique and semi-unique values and prefers to do full-table scans instead on the (questionable) theory that it takes almost as much time to find one or more semi-unique entries in an index with a billion unique values as it does to just scan through three billion fields. Though I tend to classify that design choice in the same category as Microsoft's design decision to start swapping ram out to virtual memory on a PC with a gig of ram and 400 megs of unused physical ram on the twisted theory that it's better to make the user wait while it needlessly thrashes the swapfile NOW than to risk being unable to do it later (apparently, a decision that has its roots in the 4-meg win3.1 era and somehow survived all the way to XP).
-
Update primary key thats also a foreign key in another table
Hi Developers,
I need to update the primary key for a record but it's also the foreign key in another table.
Example,
Table 1 Details
Name : Parent_Table
Columns : ID, Name, Age
Primary Key : ID
Table 2 Details
Name : Child_Table
Columns : ID, Parent_ID, Name, Age
Primary Key : ID
Foreign Key : Parent_ID (Primary Key in Parent_Table)
Parent_Table
ID Name Age
1001 Sam 26
1002 George 25
Child_Table
ID Parent_ID Name Age
1010 1001 Sam 26
1020 1002 George 25
Now I want to update ID (1001) in Parent_table as 2001 and also, I want to update Parent_ID (1001) in Child_Table as 2001.
How we will write the java code to update these columns.
Thanks in advance.dcminter wrote:
If you're looking at changing the primary keys in your data then there's probably something wrong with your data structure.Depends how you feel about business primary keys versus surrogates. Personally I prefer the latter so I'm with you in theory. In practice, however, a DB that uses a business PK may well find that it's a legitimate use case to change the key value (but then that's why I like surrogates in the first place!)Primary keys should not have meaning. -
Foreign key constraint on multi-column primary key accepts 1 empty column!?
Hi, we have a reference table with a two-column primary key. In the child table there is a non-mandatory foreign key constraint to this table. So if both child columns are null it's ok too. But now we see that if one of the two child table columns that build up the foreign key is null, the other column can have any (non-existant in the master-tabel) value you like!? That does not make sense.
Can anyone explain this to me???
Regards, Paul.Paul, I believe that this is in accordance to the ANSI SQL standard requirement for the treatment of nulls in a multi-column FK. In any case Oracle specifically states this is the way FK work From the 10 Concepts manual, Ch 21 Data Integrity, topic Nulls and Foreign Keys:
The relational model permits the value of foreign keys either to match the referenced primary or unique key value, or be null. If any column of a composite foreign key is null, then the non-null portions of the key do not have to match any corresponding portion of a parent key. <<HTH -- Mark D Powell -- -
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 CLARIFYInteresting.
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
Maybe you are looking for
-
How to write a method without knowing how many parameters passed in?
hi there, i m writing a method which is to find out the max value of a set of values, e.g. int max(var1, var2, var3, var4) { here is the code to find the max value, and return it; ......in these case, there are 4 parameters, but what i m try
-
Hi, I have several netware servers that have been experience abends multiple times during the course of the day. I have applied several service packs n65nss8c, tcpip nwsp8, and one server (they are vm's running on esxi 4.10. I even went so far as to
-
Variable declaration in the custom tag
Hey, I am using JSP2.0 to write a custom tag. and I need to declare a script variable in my tag so that it can be referenced in a jsp file. At the beginning, I used name-given and my tag works as it supposed to do. But I don't like to make the variab
-
Centered text in JTextArea (java)
how to center multiple line of text both vertically and horizontally in JTextArea ?
-
How to put two files with the same name into the same folder?
How can I put two files with the same name and the same extention into the same directory? Is this even possible? Thanks in advance.