473,405 Members | 2,187 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,405 software developers and data experts.

IDENTITY columns as foreign keys

So we're trying to decide if it's better to use IDENTITY columns or
sequences to create a surrogate key as the primary key for our tables. I
kind of like the identity column, because it's more 'tightly integrated' in
to the table. With a sequence you have to make sure that each application
that inserts records uses the same sequence. (Probably not likely that it
wouldn't, but...)

One thing where it seems like a SEQUENCE would be better is when you insert
a row in to a table, and then insert more than one row in to another table,
where the PK from the first table is an FK in to the second.

For example:

INSERT INTO test.accounts (account_id, appl_code, appl_id, branch_number)
VALUES (NEXT VALUE FOR test.accounts_seq, 'DDA', 1239876543, 543);

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
PREVIOUS VALUE FOR test.accounts_seq
, 123456789
, 'EN'
);

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
PREVIOUS VALUE FOR test.accounts_seq
, 987654321
, 'JP'
);

There seem to be various ways to handle this for an identity column. You
can use IDENTITY_VAL_LOCAL():

INSERT INTO test.accounts (appl_code, appl_id, branch_number)
VALUES ('DDA', 1239876543, 543);

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
IDENTITY_VAL_LOCAL()
, 123456789
, 'EN'
);

But that will not work, in this case, for the second insert in to
account_relationships, because IDENTITY_VAL_LOCAL() would return the
identity of the previous insert into account_relationships, rather than the
one in to accounts.

Of course you could retrieve IDENTITY_VAL_LOCAL() in to a host variable
after the insert in to accounts, but that's an extra trip to the database.

There's also this:
SELECT account_id
INTO :account-id
FROM FINAL TABLE (
INSERT INTO test.accounts (appl_code, appl_id, branch_number)
VALUES ('DDA', 1239876543, 543)
);

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
:account-id
, 123456789
, 'EN'
);

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
:account-id
, 987654321
, 'JP'
);

I'm not sure if this actually creates an extra trip to the database. In any
case, my client (DB2 for VSE) does not appear to be able to handle this type
of statement, so I can't use it.

Another thing that does work for my particular example is using a select
inside the values clause, ie:

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
(SELECT account_id
FROM test.accounts
WHERE appl_code = 'DDA'
AND appl_id = 1239876543)
, 123456789
, 'EN'
);

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
(SELECT account_id
FROM test.accounts
WHERE appl_code = 'DDA'
AND appl_id = 1239876543)
, 987654321
, 'JP'
);

This works in my case because of a UNIQUE (appl_code, appl_id) constraint.
I don't like the fact that it does the SELECT for each insert in to
account_relationships, but that does not appear to add too much overhead
since the predicate uses the index for the UNIQUE constraint.

Have I missed any options for how to do this when using an IDENTITY column?

Seems to me it would be nice to extend the IDENTITY_VAL_LOCAL() function to
allow an input parameter naming the table that you want to retrieve it from.
For instance:

INSERT INTO test.accounts (appl_code, appl_id, branch_number)
VALUES ('DDA', 1239876543, 543);

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
IDENTITY_VAL_LOCAL(test.accounts)
, 123456789
, 'EN'
);

INSERT INTO test.account_relationships (account_id, cif_number,
relationship_ind)
VALUES (
IDENTITY_VAL_LOCAL(test.accounts)
, 987654321
, 'JP'
);

But since that does not appear to be available...

Here's all of the relevant DDL for all of the above examples:

CREATE TABLE test.accounts (
account_id INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH
+1)
, CONSTRAINT accounts_pk PRIMARY KEY (account_id)
, appl_code CHAR(3) NOT NULL
, appl_id DEC(16) NOT NULL
, branch_number DEC(3) NOT NULL
, address_key DEC(3) NOT NULL DEFAULT 1
, category_code CHAR(1) NOT NULL DEFAULT 'N'
, open_date DATE NOT NULL DEFAULT
, closed_date DATE
, pmt_appl_code CHAR(3)
, pmt_appl_id DEC(16)
, last_mtce_date DATE
, last_update TIMESTAMP NOT NULL
GENERATED ALWAYS FOR EACH ROW ON UPDATE AS ROW
CHANGE TIMESTAMP
, CONSTRAINT account_unique UNIQUE (appl_code, appl_id)

)
@

CREATE TABLE test.account_relationships (
account_rel_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH
+1)
, CONSTRAINT account_rel_pk PRIMARY KEY (account_rel_id)
, account_id INTEGER NOT NULL
, CONSTRAINT account_rel_fk1 FOREIGN KEY (account_id) REFERENCES
test.accounts (account_id)
ON DELETE CASCADE ON UPDATE NO ACTION
ENFORCED
ENABLE QUERY OPTIMIZATION
, cif_number DECIMAL(9) NOT NULL --need to add foreign key...
, relationship_ind CHAR(4) NOT NULL
, added_date DATE NOT NULL DEFAULT
, last_update TIMESTAMP NOT NULL
GENERATED ALWAYS FOR EACH ROW ON UPDATE AS ROW
CHANGE TIMESTAMP
, CONSTRAINT account_rel_unique UNIQUE (cif_number, relationship_ind)
)
@

CREATE SEQUENCE test.accounts_seq AS INTEGER
START WITH +9999
@

Thanks,
Frank

Jun 27 '08 #1
0 2993

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

Similar topics

9
by: Rathtap | last post by:
I want to use the Identity field (increment 1,1) as a primary key and have a unique constraint on my other field which is of type char. I am worried that related data in other tables may lose...
112
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please,...
4
by: dayong | last post by:
i need to alter all foreign keys in my database and uncheck the "Enforce relationship for replication" check box. Using the EM, I extracted the code snippet below. unfortunately, when i run this...
24
by: Ilija_G | last post by:
Hi, Is there any replace for "Select @@identity" that could return "just inserted" GUID as a primary key? Has anyone tested what's faster, working with Guid or Autonumber ?
7
by: Sam | last post by:
Hi, I would like a piece of advice. I have 3 foreign keys in a table used as primary keys for this table. Is it useful in that case to have just one identity column that would be used as the...
2
by: Will | last post by:
I have a table, tblManinstructions with fields Code & InstructionID, one Code can have many InstructionID. I also have tblinstructions (fields instructionID & instruction). What I want to do is...
41
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB)...
1
by: Halfdood | last post by:
I am working on a data migration project using a pre-made SQL 2005 database. We need to migrate data to over 300 different talbes and the records number 20 million plus for some of the tables. We...
11
by: stegze | last post by:
Hi All, I have a problem with a DB2 server of my customer. It is a Debian Linux running DB2 Express-C. I have an IDENTITY field as PK in a table and I use this value as FK in another table. Two...
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
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.