Foreign Key Would Be Populated On Each Record / Other Would Be Null

Mar 22, 2013

I have a central entity Address. I have other entities that could have many addresses (Organization, Person for eg). The relation between Organization and Address is one to many and the relation between Person and Address is also one to many.When I modelled it, I ended up having two foreign keys in Address (Organization ID and Person ID).The question that is being raised by the client is "As only one of the foreign key would be populated on each record, the other would be null". The client is not comfortable having nullable foreign keys as they won't be able to maintain integrity. One of the requirement is that No orphan records in Address. By modeling the way above, there are chances for having some orphanned records. I tried to explain that this would be controlled by the program to ensure that no orphan records would be there but their question is that what if someone goes directly and enters a record in Address table without any foreign key value (I dont know who would have an insert/update access on the tables other than the dba's).

I am trying to get some pros and cons on using the nullable foreign keys.

View 1 Replies


ADVERTISEMENT

SQL & PL/SQL :: Primary And Foreign Key On Same Column And Foreign Key Index

Aug 25, 2010

Create table Car
(Car_cd VARCHAR2(5),
Car_Desc VARCHAR2(50)
);
alter table Car
add constraint Car_PK primary key (Car_CD);
INSERT INTO Car (Car_Cd, Car_Desc) VALUES ('CORLA','COROLLA');
Commit;
[code]....

The requirement necessitates a new table to map car to manufacturer. This mapping table may later be expanded to contain other attributes Engine, MPG, etc to hold details specific to a car.But this is in future.

Create Table Car_Mapping_Details
(Car_Cd VARCHAR2(5),
Manufacturer_Cd VARCHAR2(5));

--Primary Key Constraint
ALTER TABLE Car_Mapping_Details
ADD CONSTRAINT Car_Mapping_Details_pk PRIMARY KEY (Car_Cd );

--Not able to create this as Car_cd is already a PK in this table and therefore has Unique Index
ALTER TABLE Car_Mapping_Details ADD CONSTRAINT Car_Mapping_Details_fk1
FOREIGN KEY (Car_Cd)REFERENCES Car (Car_Cd);
[code]....

But in this case the Car_Mapping_Details.Car_cd is itself is a primary key and therefore has Unique index.Although I was able to create foreign key constraint on Car_mapping_details.car_cd column (which is also Primary Key), I was not able to create Foreign Key Index on this column. It gives me Quote:ORA-01408: such column list already indexed.In other words, not creating foreign index for foreign key column lead to table-level lock? Or will the Unique Index in that primary key column be sufficient to avoid table-level lock?

View 2 Replies View Related

SQL & PL/SQL :: How To Check Cursor Having Record Or Null

Jun 22, 2010

i want to check cursor having record or null/ if having records then count of records?

View 5 Replies View Related

SQL & PL/SQL :: Record Doesn't Contain Page Then Need To Update As Null

Jun 13, 2013

frame a query for the below values .I have few records in a below format.

1) 'CODE#ATC#YEAR#1980#NUM#4229#VOL#80##'
2) 'CODE#NSWLR#YEAR#1985#PAGE#158#VOL#4##'
3) 'CODE#WASC#YEAR#1981#MONTH#07#DAY#24#'
4) 'CODE#RPC#YEAR#1919#PAGE#296#VOL#36#'

I need only the Page from the above records.For example if i take this record

'CODE#NSWLR#YEAR#1985#PAGE#158#VOL#4##' -- I need to extract only the Page from this like 158.

If the record doesn't contain Page then i need to update as null.

View 30 Replies View Related

Forms :: Stop Saving When Detail Record Is Null?

Feb 5, 2011

I have master detail block just consider Forms A as a master and Forms B as a Detail,when my cursor is in form A i try to save the record, it is saved Form A and putting Form B data as null, i have to put a validation to stop this activity, i have to inform the user "Provide values for form B text item",

I created a post block trigger in form A

Begin

If (:FORM-B.TEXT1 is null)l or( :FORM-B.TEXT2 is null)
then
Message( 'provide value for FORM-B');
Message( 'provide value for FORM-B');
raise form_trigger_failure;
ELSE
NULL;
END IF
END;

Im Getting error message and navigation got stopped in last item of form A itself,i need to show error message and cursor control should move to first item of form -B which trigger i have to use for this.

View 1 Replies View Related

PL/SQL :: How To Insert Null Record (some Column) In Table Using Loop

Jul 5, 2012

How to insert null record (for some columns) in table using loop.

sample data of x_tab

order_id order_name

231 xxx
123
345
111 vvvv

View 5 Replies View Related

Server Utilities :: Field / Record Terminator - Trailing Null Col

Oct 7, 2010

I need to load a file with fields separated by '|^|' and at end of each record has '||*||'.

So in my ctl file what do i mention ? fields terminated by '|^|' ? for the record termination wat should I say?
Should I still mention 'trailing null col' in my ctl file...?

Sample data file:
Name|^|Age|^|city||*||
john|^|33|^|||*||
james|^||^|nyc||*||
ken|^|44|^|
washington||*||

the fields are properly terminated with |^| and the records are terminated with ||*||. Is it true that a file with |^| as field terminator cannot be loaded with sqlldr?

View 3 Replies View Related

ODP.NET :: Updating Dataset / Datarow If Data Is Populated From Stored Procedure

Mar 11, 2013

I am using:

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Microsoft Visual Studio 2010 : VB Express
Microsoft .NET Framework : Version 4.0.30319 RTMRel

When I populate dataset using select query and use OracleCommandBuilder and update dataadapter, data is saved in database.

But if I put same select in store procedure and use this to populate dataset then data is not updated in db.

I get this error

Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

at da1.Update(ds1, "req") where I am going wrong.

Dim conn As New OracleConnection
        Dim da1 As OracleDataAdapter = New OracleDataAdapter
        Dim ds1 = New DataSet
        With conn
            If .State = ConnectionState.Open Then .Close()

[Code]....

View 2 Replies View Related

PL/SQL :: Oracle Database 11g - Add Partition Based On Amount Of Data To Be Populated

Oct 30, 2013

I'm using Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production and TNS for Linux: Version 11.2.0.3.0 - Production.Requirement is to create a script to add a LIST partition to some selected tables in a schema (tables do not have data, they are not partitioned). There are about 300 such tables (can vary) and their names are maintained in a separate table. Example -Existing table

  -CREATE TABLE test_part(id number (2),
name varchar2(20),
audit_userid number (9)); 
Expected table
-CREATE TABLE test_part(id number (2), name varchar2(20), audit_userid number (9))  
PARTITION BY LIST (audit_userid)      (PARTITION p1_audit_userid VALUES (1));

 Ultimate goal is to add more partitions based on the amount of data to be populated.

View 1 Replies View Related

SQL & PL/SQL :: Hierarchical Query - Connect NON-NULL Rows To Preceding NULL Row

Aug 29, 2012

I have the following query:

select col_1,col_9 from
book_temp b
where b.col_1 is not null
order by to_number(b.col_16)
;

What I want to add is the following:

COL_9
=====
NULL
A
B
NULL
C
D
E
F
NULL
G

I need to connect the NON-NULL rows to the preceding NULL row.

View 15 Replies View Related

SQL & PL/SQL :: Counting NULL Vs NON-NULL In A GROUP BY Clause

Jun 21, 2010

I am running a GROUP BY query on a few columns of enumerated data like:

select count(*), Condition, Size
group by Condition, Size;

COUNT(*) CONDITION SIZE
-------- ---------- --------
3 MINT L
2 FAIR L
4 FAIR M
1 MINT S

Well, let's say I also have a timestamp field in the database. I cannot run a group by with that involved because the time is recorded to the milisec and is unique for every record. Instead, I want to include this in my group by function based on whether or not it is NULL.

For example:

COUNT(*) CONDITION SIZE SOLDDATE
-------- ---------- -------- ----------
3 MINT L ISNULL
2 FAIR L NOTNULL
2 FAIR M NOTNULL
2 FAIR M ISNULL
1 MINT S ISNULL

View 9 Replies View Related

SQL & PL/SQL :: Date Field - Not Null Column To NULL

Mar 16, 2011

I have a table which has a not null column. the column is date field. I am trying to change it to Null. But it is giving a error.

I am using below query.

ALTER TABLE T_test
modify (paid_to_date null)

View 9 Replies View Related

SQL & PL/SQL :: Unique Null And Multiple Non-null

Oct 24, 2013

create table test
(
id int ,
dat date
)
/

I want to implement a business rule such as we have for each id at most 1 dat null. So, I've created this unique index on test.

create unique index x_only_one_dat_cess_null on test(id, case when dat_cess is null then 'NULL' else to_char(dat_cess, 'dd/mm/yyyy') end);

insert into test values (1, sysdate);
insert into test values (1, sysdate - 1);
insert into test values (1, null);
insert into test values (1, null);
-- -----
insert into test values (2, sysdate);
insert into test values (2, sysdate - 1);
insert into test values (2, null);

The 4th insert will cause an error and this is what I wanted to implement. OK. Now the problem is that for non-null values of dat, we can't have data like this

iddat
------------
124/10/2013
123/10/2013
123/10/2013
1

because of the unique index (the 2nd and the 3rd row are equal). So just for learning purposes, how could we allow at most one null value of dat and allow duplicates for non-null values of dat.

View 2 Replies View Related

SQL & PL/SQL :: How To Modify Null Column To Not Null

Jan 9, 2012

when i follow this steps mention on this website

[URL].........

to modify column from null to not null i got this error and on this website its show successful

my steps are

first i create a table

SQL> create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(10),
2 Stu_Class varchar(10));

Table created.

Then insert some rows into Stu_Table

SQL> insert into Stu_Table (Stu_Id, Stu_Name) values(1,'Komal');

1 row created.

SQL> insert into Stu_Table (Stu_Id, Stu_Name) values(2,'Ajay');

1 row created.

SQL> insert into Stu_Table (Stu_Id, Stu_Name) values(3,'Rakesh');

1 row created.

SQL> insert into Stu_Table (Stu_Id, Stu_Name) values(4,'Bhanu');

1 row created.

SQL> insert into Stu_Table (Stu_Id, Stu_Name) values(5,'Santosh');

1 row created.

SQL> select * from Stu_Table;

ST STU_NAME STU_CLASS
-- ---------- ----------
1 Komal
2 Ajay
3 Rakesh
4 Bhanu
5 Santosh

Table Structure is like this

SQL> Describe Stu_Table
Name Null? Type
----------------------------------------- -------- ----------------------------
STU_ID VARCHAR2(2)
STU_NAME VARCHAR2(10)
STU_CLASS VARCHAR2(10)

now when i try to modify this Stu_id column to not null its give me error.

SQL>ALTER TABLE Stu_Table MODIFY Stu_Id int(3)not null;
ALTER TABLE Stu_Table MODIFY Stu_Id int(3)not null
*
ERROR at line 1:
ORA-01735: invalid ALTER TABLE option

and when i try to add new column with not null its also gives me error

SQL> ALTER TABLE Stu_Table add C1_TEMP integer NOT NULL;
ALTER TABLE Stu_Table add C1_TEMP integer NOT NULL
*
ERROR at line 1:
ORA-01758: table must be empty to add mandatory (NOT NULL) column

View 6 Replies View Related

SQL & PL/SQL :: Function In Oracle To Select Not-null Columns At Beginning And Null Columns At End?

Jul 12, 2012

I have 8 columns. Some of them might be null.I want to display all 8 columns in my result. Not null columns will be first and null at the end.Here is a sample data :

Employee table :
Employee_id Emp_fname emp_lname emp_mname dept salary emp_height emp_weight
1 aaa ddd d1 100 6 180
2 bbb ccc 120 169
3 dfe d2 5.9 223

The expected result is :
result1 result2 result3 result4 result5 result6 result7 result8
1 aaa ddd d1 100 6 180
2 bbb ccc 120 169
3 dfe d2 5.9 223

View 8 Replies View Related

SQL & PL/SQL :: Add Automatic Value In Foreign Key?

Nov 7, 2011

if have table primary key in this table empno and another table foreign key is empno and when add in the primary key 5522 how can

5522 in the empno "foreign key" automatic

View 5 Replies View Related

SQL & PL/SQL :: Replacing A Foreign Key With Another

Jul 26, 2011

I have an already populated table that refers to a wrong foreign key in another table. I have table abc that has the column fk_id. this column currently refers to column x_id1 in the table abc while it should refer to column x_id2 in xyz. I am trying to replace the data but I guess I miss something in the logic or the right way of doing it:

following is my trial:

create table abc
(
pk_id number(4) primary key,
abc_name varchar2(10),
fk_id number(4)
);

create table xyz
(
x_id1 number(4),
x_id2 number(4),
x_name varchar2(10)
);

commit;
insert all
into abc values (10,'ab1',1)
into abc values (11,'ab2',2)
into abc values (12,'ab3',5)
into abc values (13,'ab4',7)
into abc values (14,'ab5',9)
into xyz values (1,1,'d1')
into xyz values (2,2,'d2')
into xyz values (5,3,'d3')
into xyz values (7,4,'d4')
into xyz values (9,5,'d5')
select * from dual;
commit;

--this following select returns 5 rows

select * from abc, xyz where abc.fk_id = xyz.x_id1;

-- the following update only updates 3 rows and sets the other 2
-- to null!

update abc set fk_id = (select x_id2 from xyz where xyz.x_id1 = abc.fk_id);
commit;

select * from abc;

View 3 Replies View Related

SQL & PL/SQL :: Not Finding The Foreign Key?

Dec 4, 2011

I'm not really sure why oracle is not finding my Foreing Key, I'm creating an easy set of table for a company and I'm declaring all Primary keys and foreing keys as necessary and this is my

CREATE TABLE Items
(ItemID NUMBER(5) NOT NULL,
Description VARCHAR2(30),
ListPrice NUMBER(9,2) NOT NULL,
QtyOnHand Number(5),
ReorderPoint NUMBER(3),
ReorderQty NUMBER(3),
OnBackOrder Number(3),
CONSTRAINT ItemsPK

[Code] ............

There's three sets of inserts the first two successfully inserts the data NO problem but when i try to insert the data on SupplierItems comes as:

ERROR at line 1:
ORA-02291: integrity constraint (jcarter.SUPPLIERITEMSFK2) violated - parent key not found

I looked on the forum and it appears this happens when someone is doing a Insert ALL but this is not my case..

View 2 Replies View Related

SQL & PL/SQL :: Foreign Key Constraint

Feb 3, 2012

I have a parent table called dept and one child table emp which is referenced on deptno and now both have data . Now i want to truncate the dept table and i have truncated emp table after it when i am trying to truncate dept table then oracle throws a error that is-

"ORA-02266: unique/primary keys in table referenced by enabled foreign keys" ,

but when i tried to delete some records 1 by 1 then these have delete but truncate is not still working. why? if i have 10 millon records.

View 4 Replies View Related

PL/SQL :: Foreign Key Index

Mar 13, 2013

You should almost always index foreign keys because they are frequently used in joins. In addition, if you intend to delete or update unique or primary keys on the parent table, you should index the foreign keys to improve the locking of child records.what I don't understand are, I should create index on foreign key column in parent table or child table or both ?

View 1 Replies View Related

SQL & PL/SQL :: Foreign Key View In Oracle

Sep 6, 2011

Is there any view in oracle which gives the foreign key mapping.

E.g.

Table DEPT has DEPTNO as PK.
Table Emp has DEPTNO as FK to DEPT.DEPTNO.

I can get the information for DEPT table from R_CONSTRAINT_NAME column of all_constraints table.My requirement is to get the the which column of Emp table refers to DEPT.DEPTNO column .

View 3 Replies View Related

SQL & PL/SQL :: Locking And Foreign Key Indexes

Sep 1, 2013

Say we have an employee(id_emp) table with a primary key on id_emp. We have also some history tables emp_stuff with columns say (id_emp, dat_event, some_stuff) with primary key id_emp, dat_event.

This means that we have a unique index on (id_emp,dat_event). We also have a foreign key id_emp that references employee(id_emp). When we update id_emp on employee, we still have a lock on emp_stuff. According to this (end of the page) :

Quote:So, in short, with releases prior to Oracle Database 11g Release 1, you will want an index on the foreign key of the child table if you do any of the following:

Update the parent table primary key
Delete from the parent table
Merge into the parent table

So is id_emp in emp_stuff considered as indexed (through the unique index of the primary key) or do we have to add an explicit index
like this CREATE INDEX emp_stuff ON emp_stuff(id_emp) to avoide child table locks?

View 33 Replies View Related

SQL & PL/SQL :: Foreign Key - Primary Key Relation

May 2, 2012

I am trying to establish PK-FK relationship between 2 tables. Table 1 has VACC_ID as a primary key. Table 2 has VACC_ID and SCH_ID as the primary keys. I am trying to add VACC_ID from Table 2 as a FK to Table 1. This is the code I used

ALTER TABLE Table_1
ADD CONSTRAINT FK_VACC_ID
FOREIGN KEY (VACC_ID)
REFERENCES Table_2(VACC_ID)

I also tried to add it without writing the query and editing the Table 1. By default, I am getting 2 columns(both VACC_ID) in the local column being referenced to the 2 primary keys(VACC_ID and SCH_ID) of table 2. I also uploaded the screeshot of the error, when I run the query.

View 4 Replies View Related

SQL & PL/SQL :: Fetch Information For A Foreign Key

Jul 13, 2010

the view from where we can fetch information for a foreign key viz. reference table, referenced columns etc.

View 3 Replies View Related

SQL & PL/SQL :: Why Are Foreign Key Not Deferral By Default

Apr 18, 2013

In my point of view, deferral FK (DFK) are stronger than non-deferral FK (NDFK). In other words, every NDFK can be moved to DFK. Is there a performance issue ?

View 2 Replies View Related

SQL & PL/SQL :: Interchange The Value Which Has Foreign Key Dependency

Mar 29, 2011

I need to interchange the value which has foreign key depednecy,

For ex: I have a table dept

deptno| dname|dseq|
10 | CS | 1
20 | SC | 2

and in emp table

eno | ename | deptno |
1 |Raj | 10 |
2 |Ram | 10 |
3 |Vip | 10 |
4 |Muk | 10 |
5 |Sn | 20 |
6 |Snj | 20 |
7 |Asj | 20 |

there is a forign key define :

ALTER TABLE emp ADD (
CONSTRAINT rop_fk
FOREIGN KEY (deptno)
REFERENCES dept (deptno));

Now I want to change the deptno from 10 to 20 and 20 to 10 in dept table . So the ouput will be like

deptno| dname|dseq|
20 | CS | 1
10 | SC | 2

when i am deleting that row I am getting error "ORA-02292: integrity constraint (ROP_FK) violated - child record found".IF i am trying to update i am getting unique key contractin failure.

(Even I cannot update the rest of the column values in dept table as it is also having foreign key dependency) i need to put a sql script in production so that it will interchange.

View 10 Replies View Related

PL/SQL :: How Many Foreign Key Can Be Created On Table

May 3, 2013

in a table how many foreign keys can we create.

View 10 Replies View Related

Session Hang Due To Foreign Key?

Jun 21, 2012

Version 10.2.0.4

CREATE TABLE T1 (I INT PRIMARY KEY);

CREATE TABLE T2 (I INT PRIMARY KEY, J INT);

ALTER TABLE T2 ADD CONSTRAINT T2_FK FOREIGN KEY (J) REFERENCES T1(I);Session 1

INSERT INTO T1 VALUES (1);Session 2

INSERT INTO T2 VALUES (1,1);Session 2 hangs. Why ?

View 9 Replies View Related

SQL & PL/SQL :: Defining Foreign Keys

Oct 7, 2010

In my sql developer I have created a table. Created primary keys. Using alter table I have also created the foreign key. Now I need to add one more foreign key to the same table. Do I have to drop the existing foreign key and then create the 2 new foreign keys using the alter table condition or can I retain the existing FK and just use alter table to add another FK.

View 9 Replies View Related

SQL & PL/SQL :: Primary Key / Foreign Key Joins

Sep 29, 2010

I am working on a new project in OBIEE. I am asked to do the data modeling in the database using oracle sql developer. I have to create the joins based on the requirements. I have the tables created already. But the primary keys for few tables are not defined for few tables. PK-FK joins are also not done properly.

My questions are
(1) If I have to define the primary key for the existing tables can I do that using the alter table command or should I create the table all over again and then define it?
(2) If I have to make the changes in the existing PK-FK joins how do i go about doing that?

View 11 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved