473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

normalizing two related tables

Consider the following...
- A customer table with a customer number column as the primary key, and
also a column called SHORTNAME.
- An account table with an account number column as the primary key, a
customer number column as a foreign key (referencing the customer table, of
course), and a column called SHORTNAME.

Obviously the account table is not properly normalized.

Now consider the fact that there are many existing programs/queries that
read the SHORTNAME column from the account table.

Is there some way that we can 're-define' the SHORTNAME column to somehow do
an implicit join with the SHORTNAME column on the customer table, rather
than using data actually stored in the account table?

Obviously I am trying to avoid having to change all of the programs/queries
to explicitly do this join.

DB2/LUW v9.1

Be gentle. I am still very much a RDBMS newbie!

Frank

Jun 27 '07 #1
17 1861
Frank Swarbrick wrote:
Consider the following...
- A customer table with a customer number column as the primary key, and
also a column called SHORTNAME.
- An account table with an account number column as the primary key, a
customer number column as a foreign key (referencing the customer table, of
course), and a column called SHORTNAME.

Obviously the account table is not properly normalized.

Now consider the fact that there are many existing programs/queries that
read the SHORTNAME column from the account table.

Is there some way that we can 're-define' the SHORTNAME column to somehow do
an implicit join with the SHORTNAME column on the customer table, rather
than using data actually stored in the account table?

Obviously I am trying to avoid having to change all of the programs/queries
to explicitly do this join.

DB2/LUW v9.1

Be gentle. I am still very much a RDBMS newbie!
Two random thoughts

1) rename account to something else, say X and then create a view over it:

create view account as
select X.account_number, customer.SHORTNAME, X....
from X, customer
where ...

You might need to create instead of triggers on the account view

2) create a superkey in customer as:

alter table customer add constraint AK_CUSTOMER
UNIQUE ( customer_number, SHORTNAME )

then move the f.k in account so that it points to this new key in customer:

alter table account drop constraint ...
alter table account add constraint ...
foreign key ( customer_number, SHORTNAME )
references customer (customer_number, SHORTNAME)
on ...

Still not normalized but the f.k will prevent any anomalies. Of course
this means some hazzle for the use cases that updates SHORTNAME in
either table.

You might need to fix some data in account table before you can add the
new f.k

As mentioned, these are just random thoughts that at best will give you
some ideas.
HTH
/Lennart

Frank
Jun 27 '07 #2
On Wed, 27 Jun 2007 21:56:03 +0200, Lennart
<er******************@gmail.comwrote:
>Frank Swarbrick wrote:
>Consider the following...
- A customer table with a customer number column as the primary key, and
also a column called SHORTNAME.
- An account table with an account number column as the primary key, a
customer number column as a foreign key (referencing the customer table, of
course), and a column called SHORTNAME.

Obviously the account table is not properly normalized.

Now consider the fact that there are many existing programs/queries that
read the SHORTNAME column from the account table.

Is there some way that we can 're-define' the SHORTNAME column to somehow do
an implicit join with the SHORTNAME column on the customer table, rather
than using data actually stored in the account table?

Obviously I am trying to avoid having to change all of the programs/queries
to explicitly do this join.

DB2/LUW v9.1

Be gentle. I am still very much a RDBMS newbie!

Two random thoughts

1) rename account to something else, say X and then create a view over it:

create view account as
select X.account_number, customer.SHORTNAME, X....
from X, customer
where ...

You might need to create instead of triggers on the account view
2) create a superkey in customer as:

alter table customer add constraint AK_CUSTOMER
UNIQUE ( customer_number, SHORTNAME )

then move the f.k in account so that it points to this new key in customer:

alter table account drop constraint ...
alter table account add constraint ...
foreign key ( customer_number, SHORTNAME )
references customer (customer_number, SHORTNAME)
on ...

Still not normalized but the f.k will prevent any anomalies. Of course
this means some hazzle for the use cases that updates SHORTNAME in
either table.

You might need to fix some data in account table before you can add the
new f.k

As mentioned, these are just random thoughts that at best will give you
some ideas.
HTH
/Lennart

>Frank
I concur with both. I would think that the first is the appropriate
solution. I'd just comment on the second that the UNQIUE is not
required. Since the composite FK will keep it in sync, the uniqueness
of shortname is irrelevant.

B.
Jun 28 '07 #3
Brian Tkatch wrote:
[...]
>
I concur with both. I would think that the first is the appropriate
solution. I'd just comment on the second that the UNQIUE is not
required. Since the composite FK will keep it in sync, the uniqueness
of shortname is irrelevant.

B.
Are you suggesting that one can declare a composite f.k. against
something that is not declared as primary key or unique in the parent table?
/Lennart
Jun 28 '07 #4
On Thu, 28 Jun 2007 16:02:29 +0200, Lennart
<er******************@gmail.comwrote:
>Brian Tkatch wrote:
[...]
>>
I concur with both. I would think that the first is the appropriate
solution. I'd just comment on the second that the UNQIUE is not
required. Since the composite FK will keep it in sync, the uniqueness
of shortname is irrelevant.

B.

Are you suggesting that one can declare a composite f.k. against
something that is not declared as primary key or unique in the parent table?
/Lennart
Ugh, you're right, i forgot that ridiculous DB2 limitation. I ran into
that once before myself.

Thanx for the correction.

B.
Jun 28 '07 #5
Brian Tkatch wrote:
>Are you suggesting that one can declare a composite f.k. against
something that is not declared as primary key or unique in the parent table?
Ugh, you're right, i forgot that ridiculous DB2 limitation. I ran into
that once before myself.
Brian,

Trying to reverse engineer the requirement here:
a) Allow creation of FK to any parent without enforcing uniqueness of
the parent
b) Automatically derive a unique constraint on the parent when defining RI?
c) Something else

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Jun 28 '07 #6
Brian Tkatch wrote:
On Thu, 28 Jun 2007 16:02:29 +0200, Lennart
<er******************@gmail.comwrote:
[...]
>>Are you suggesting that one can declare a composite f.k. against
something that is not declared as primary key or unique in the parent table?
/Lennart

Ugh, you're right, i forgot that ridiculous DB2 limitation. I ran into
that once before myself.
I dont think the limitation lies within DB2. Eventhough it should be
possible for the DBMS to derive the uniqness of p.k + another attribute,
I'm not aware of any sql dbms that does this (and I would be surprised
if the standard allows it). I also think that it could cause some
confusion if implemented that way

/Lennart

Jun 28 '07 #7
On Thu, 28 Jun 2007 11:58:50 -0400, Serge Rielau <sr*****@ca.ibm.com>
wrote:
>Brian Tkatch wrote:
>>Are you suggesting that one can declare a composite f.k. against
something that is not declared as primary key or unique in the parent table?
Ugh, you're right, i forgot that ridiculous DB2 limitation. I ran into
that once before myself.
Brian,

Trying to reverse engineer the requirement here:
a) Allow creation of FK to any parent without enforcing uniqueness of
the parent
b) Automatically derive a unique constraint on the parent when defining RI?
c) Something else

Cheers
Serge
If it's good enough for the parent now, why change it just because the
child wants to use it as a composite FK?

I don't understand why it has to be UNIQUE at all.

B.
Jun 28 '07 #8
On Thu, 28 Jun 2007 18:35:35 +0200, Lennart
<er******************@gmail.comwrote:
>Brian Tkatch wrote:
>On Thu, 28 Jun 2007 16:02:29 +0200, Lennart
<er******************@gmail.comwrote:
[...]
>>>Are you suggesting that one can declare a composite f.k. against
something that is not declared as primary key or unique in the parent table?
/Lennart

Ugh, you're right, i forgot that ridiculous DB2 limitation. I ran into
that once before myself.

I dont think the limitation lies within DB2. Eventhough it should be
possible for the DBMS to derive the uniqness of p.k + another attribute,
I'm not aware of any sql dbms that does this (and I would be surprised
if the standard allows it). I also think that it could cause some
confusion if implemented that way

/Lennart
Even without the PK, i don't understand why a parent must be UNIQUE.
IIRC, one build of DB2 doesn't even require that. And, how else could
we have a many-to-many relationship?

In my case, i wanted to use RI where the parent included NULLs. And
since NULLs are kept on the INDEX, it could not be UNIQUE. I had to
add a new TABLE with just one COLUMN in it and FK to it from both
TABLEs.

B.
Jun 28 '07 #9
Brian Tkatch wrote:
[...]
Even without the PK, i don't understand why a parent must be UNIQUE.
I have a hard time imagining what the consequences would be otherwise.
For example, what would:

foreign key (x)
references T (x)
on delete cascade

mean if T.x were not unique?
IIRC, one build of DB2 doesn't even require that.
With all due respect, I strongly doubt it. If you can provide the name
of that version I will reconcider ;-)
And, how else could
we have a many-to-many relationship?
I dont follow, what does many-to-many have to do with unique keys?
/Lennart

[...]
Jun 29 '07 #10
On Fri, 29 Jun 2007 09:18:46 +0200, Lennart
<er******************@gmail.comwrote:
>Brian Tkatch wrote:
[...]
>Even without the PK, i don't understand why a parent must be UNIQUE.

I have a hard time imagining what the consequences would be otherwise.
For example, what would:

foreign key (x)
references T (x)
on delete cascade

mean if T.x were not unique?
If all instances of the value are gone.
>IIRC, one build of DB2 doesn't even require that.

With all due respect, I strongly doubt it. If you can provide the name
of that version I will reconcider ;-)
I thought is was os/390, i don't know though. It was mentioned in
passing when i first stumbled on this limitation.
>And, how else could
we have a many-to-many relationship?

I dont follow, what does many-to-many have to do with unique keys?
A many-to-many requires an FK. If one side has to be UNIQUE, it can't
be "many".

B.
>

/Lennart

[...]
Jun 29 '07 #11
Brian Tkatch wrote:
On Fri, 29 Jun 2007 09:18:46 +0200, Lennart
<er******************@gmail.comwrote:
>>Brian Tkatch wrote:
[...]
>>Even without the PK, i don't understand why a parent must be UNIQUE.

I have a hard time imagining what the consequences would be otherwise.
For example, what would:

foreign key (x)
references T (x)
on delete cascade

mean if T.x were not unique?

If all instances of the value are gone.
Well, I cant even imagine the anoalies one can derive from such a
system. IMO it's like opening Pandora's Box.
>>IIRC, one build of DB2 doesn't even require that.

With all due respect, I strongly doubt it. If you can provide the name
of that version I will reconcider ;-)

I thought is was os/390, i don't know though. It was mentioned in
passing when i first stumbled on this limitation.
I never worked with os/390, and if what you describe is true, I'm glad I
didnt. But, yes it is a limitation, a limitation that protects your data
from beeing corrupted.
>>And, how else could
we have a many-to-many relationship?

I dont follow, what does many-to-many have to do with unique keys?

A many-to-many requires an FK. If one side has to be UNIQUE, it can't
be "many".
A key identifies something, no? What prevents me from doing:

create table vehicle_type (
vehicle_type_id int not null primary key,
vehicle_type_short_name char(5) not null,
vehicle_type_description varchar(20)
);

insert into vehicle_type values (1,'mc','Motorcycle'), (2,'mc','Monster
Truck');

-- No, I dont care about vin numbers and iso whatever at the moment
create table vehicle (
vehicle_id int not null primary key,
vehicle_type_short_name char(5) not null,
foreign key (vehicle_type_short_name)
references vehicle_type
);

insert into vehicle values (1,'mc');

-- Now I'm the proud owner of both an mc and a truck, since

select v.id, vt.vehicle_type_description
from vehicle_type vt, vehicle v
where v.vehicle_type_short_name = vt.vehicle_type_short_name;

I see little gain and an awfull lot of trouble, dont you agree?
/Lennart
Jun 29 '07 #12
Lennart wrote:
>>>IIRC, one build of DB2 doesn't even require that.

With all due respect, I strongly doubt it. If you can provide the name
of that version I will reconcider ;-)

I thought is was os/390, i don't know though. It was mentioned in
passing when i first stumbled on this limitation.

I never worked with os/390, and if what you describe is true, I'm glad I
didnt. But, yes it is a limitation, a limitation that protects your data
from beeing corrupted.
It's not true. DB2 for z/OS requires a unique constraint being referenced,
too. (Primary keys are just unique constraints.) You will get
SQLCODE -538.
>>>And, how else could
we have a many-to-many relationship?

I dont follow, what does many-to-many have to do with unique keys?

A many-to-many requires an FK. If one side has to be UNIQUE, it can't
be "many".
Many-to-many relationships have to be modeled with an additional table.

CREATE TABLE a ( a INT NOT NULL PRIMARY KEY )
CREATE TABLE b ( b INT NOT NULL PRIMARY KEY )
CREATE TABLE r (
a INT NOT NULL,
b INT NOT NULL,
PRIMARY KEY (a, b),
FOREIGN KEY (a) REFERENCES a(a),
FOREIGN KEY (b) REFERENCES b(b) )

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Jul 2 '07 #13
On Fri, 29 Jun 2007 21:10:19 +0200, Lennart
<er******************@gmail.comwrote:
>Brian Tkatch wrote:
>On Fri, 29 Jun 2007 09:18:46 +0200, Lennart
<er******************@gmail.comwrote:
>>>Brian Tkatch wrote:
[...]
Even without the PK, i don't understand why a parent must be UNIQUE.

I have a hard time imagining what the consequences would be otherwise.
For example, what would:

foreign key (x)
references T (x)
on delete cascade

mean if T.x were not unique?

If all instances of the value are gone.

Well, I cant even imagine the anoalies one can derive from such a
system. IMO it's like opening Pandora's Box.
>>>IIRC, one build of DB2 doesn't even require that.

With all due respect, I strongly doubt it. If you can provide the name
of that version I will reconcider ;-)

I thought is was os/390, i don't know though. It was mentioned in
passing when i first stumbled on this limitation.

I never worked with os/390, and if what you describe is true, I'm glad I
didnt. But, yes it is a limitation, a limitation that protects your data
from beeing corrupted.
>>>And, how else could
we have a many-to-many relationship?
I dont follow, what does many-to-many have to do with unique keys?

A many-to-many requires an FK. If one side has to be UNIQUE, it can't
be "many".

A key identifies something, no? What prevents me from doing:

create table vehicle_type (
vehicle_type_id int not null primary key,
vehicle_type_short_name char(5) not null,
vehicle_type_description varchar(20)
);

insert into vehicle_type values (1,'mc','Motorcycle'), (2,'mc','Monster
Truck');

-- No, I dont care about vin numbers and iso whatever at the moment
create table vehicle (
vehicle_id int not null primary key,
vehicle_type_short_name char(5) not null,
foreign key (vehicle_type_short_name)
references vehicle_type
);

insert into vehicle values (1,'mc');

-- Now I'm the proud owner of both an mc and a truck, since

select v.id, vt.vehicle_type_description
from vehicle_type vt, vehicle v
where v.vehicle_type_short_name = vt.vehicle_type_short_name;

I see little gain and an awfull lot of trouble, dont you agree?
/Lennart
Why is that trouble? Get a better DBA! :)

B.
Jul 2 '07 #14
On Mon, 02 Jul 2007 13:02:51 +0200, Knut Stolze <st****@de.ibm.com>
wrote:
>Lennart wrote:
>>>>IIRC, one build of DB2 doesn't even require that.

With all due respect, I strongly doubt it. If you can provide the name
of that version I will reconcider ;-)

I thought is was os/390, i don't know though. It was mentioned in
passing when i first stumbled on this limitation.

I never worked with os/390, and if what you describe is true, I'm glad I
didnt. But, yes it is a limitation, a limitation that protects your data
from beeing corrupted.

It's not true. DB2 for z/OS requires a unique constraint being referenced,
too. (Primary keys are just unique constraints.) You will get
SQLCODE -538.
Thanx for the report. Is there a platform where DB2 allows it? I seem
to remember it being mentioned.

B.
>>>>And, how else could
we have a many-to-many relationship?

I dont follow, what does many-to-many have to do with unique keys?

A many-to-many requires an FK. If one side has to be UNIQUE, it can't
be "many".

Many-to-many relationships have to be modeled with an additional table.

CREATE TABLE a ( a INT NOT NULL PRIMARY KEY )
CREATE TABLE b ( b INT NOT NULL PRIMARY KEY )
CREATE TABLE r (
a INT NOT NULL,
b INT NOT NULL,
PRIMARY KEY (a, b),
FOREIGN KEY (a) REFERENCES a(a),
FOREIGN KEY (b) REFERENCES b(b) )
Jul 2 '07 #15
Brian Tkatch <N/Awrote:
On Mon, 02 Jul 2007 13:02:51 +0200, Knut Stolze <st****@de.ibm.com>
wrote:
>>Lennart wrote:
>>>>>IIRC, one build of DB2 doesn't even require that.
>
>With all due respect, I strongly doubt it. If you can provide the name
>of that version I will reconcider ;-)

I thought is was os/390, i don't know though. It was mentioned in
passing when i first stumbled on this limitation.

I never worked with os/390, and if what you describe is true, I'm glad I
didnt. But, yes it is a limitation, a limitation that protects your data
from beeing corrupted.

It's not true. DB2 for z/OS requires a unique constraint being
referenced,
too. (Primary keys are just unique constraints.) You will get
SQLCODE -538.

Thanx for the report. Is there a platform where DB2 allows it? I seem
to remember it being mentioned.
If there is one, it could only be AS/400. But I doubt it because there is a
lot of effort going into ensuring that the 3 flavors of DB2 support the
same SQL syntax and semantics as much as possible.

--
Knut Stolze
DB2 z/OS Utilities Development
IBM Germany
Jul 2 '07 #16
Knut Stolze wrote:
If there is one, it could only be AS/400. But I doubt it because there is a
lot of effort going into ensuring that the 3 flavors of DB2 support the
same SQL syntax and semantics as much as possible.
http://publib.boulder.ibm.com/infoce...zmsthctabl.htm

looks all regular to me..Brian, did you ever work with DB2 for vm/vse
perhaps?
Matter of fact I'd gladly accept any product that does this....

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Jul 2 '07 #17
On Mon, 02 Jul 2007 08:59:20 -0400, Serge Rielau <sr*****@ca.ibm.com>
wrote:
>Knut Stolze wrote:
>If there is one, it could only be AS/400. But I doubt it because there is a
lot of effort going into ensuring that the 3 flavors of DB2 support the
same SQL syntax and semantics as much as possible.
http://publib.boulder.ibm.com/infoce...zmsthctabl.htm

looks all regular to me..Brian, did you ever work with DB2 for vm/vse
perhaps?
Nope.
>Matter of fact I'd gladly accept any product that does this....

Cheers
Serge
I must be mistaken then. I thought last time i asked about this
someone mentioned where it was the case.

But if the two of you say no, that's i'm pretty sure it ain't so.

B.
Jul 2 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Evan Escently | last post by:
Hi, I've laid out a _very_ simple database that tracks my artwork the table 'works' looks like: +---------+----------+------------+------------+-------------+ | work_id | title | media ...
3
by: Jason | last post by:
I am trying to filter records in a primary form based on records in related tables. The data in the related tables is being displayed in the primary form through subforms. To be more specific, I...
3
by: Megan | last post by:
hello everybody- i'm normalizing a database i inherited. i'm breaking up a huge table named case into several smaller tables. i am creating several many to many relationships between the new...
5
by: Kevin | last post by:
I'm almost done working on my relationships and the database groundwork - I'm stuck on one part. I have the relationships and tables as shown here:...
3
by: Ryan Stewart | last post by:
I'm getting XHTML input that can be in a number of formats, and I'm trying to get it into a consistent format for later use. "Consistent" in this case means everything in the root/body is in either...
8
by: Richard Hollenbeck | last post by:
I have a recipe database that I've been building but I haven't yet put any of the ingredients in because of a little problem of normalization. If I build a table of ingredients, all the recipes...
5
by: imani_technology_spam | last post by:
I re-designed a predecessor's database so that it is more properly normalized. Now, I must migrate the data from the legacy system into the new one. The problem is that one of the tables is a...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
1
by: Neekos | last post by:
Hey guys, So after some of you (FishVal, Nico, NeoPa) pointed out previously that my tables are not normalized, i've decided to go back to the drawing board, but im not really sure where to start....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.