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_s eq, 'DDA', 1239876543, 543);
INSERT INTO test.account_re lationships (account_id, cif_number,
relationship_in d)
VALUES (
PREVIOUS VALUE FOR test.accounts_s eq
, 123456789
, 'EN'
);
INSERT INTO test.account_re lationships (account_id, cif_number,
relationship_in d)
VALUES (
PREVIOUS VALUE FOR test.accounts_s eq
, 987654321
, 'JP'
);
There seem to be various ways to handle this for an identity column. You
can use IDENTITY_VAL_LO CAL():
INSERT INTO test.accounts (appl_code, appl_id, branch_number)
VALUES ('DDA', 1239876543, 543);
INSERT INTO test.account_re lationships (account_id, cif_number,
relationship_in d)
VALUES (
IDENTITY_VAL_LO CAL()
, 123456789
, 'EN'
);
But that will not work, in this case, for the second insert in to
account_relatio nships, because IDENTITY_VAL_LO CAL() would return the
identity of the previous insert into account_relatio nships, rather than the
one in to accounts.
Of course you could retrieve IDENTITY_VAL_LO CAL() 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_re lationships (account_id, cif_number,
relationship_in d)
VALUES (
:account-id
, 123456789
, 'EN'
);
INSERT INTO test.account_re lationships (account_id, cif_number,
relationship_in d)
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_re lationships (account_id, cif_number,
relationship_in d)
VALUES (
(SELECT account_id
FROM test.accounts
WHERE appl_code = 'DDA'
AND appl_id = 1239876543)
, 123456789
, 'EN'
);
INSERT INTO test.account_re lationships (account_id, cif_number,
relationship_in d)
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_relatio nships, 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_LO CAL() 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_re lationships (account_id, cif_number,
relationship_in d)
VALUES (
IDENTITY_VAL_LO CAL(test.accoun ts)
, 123456789
, 'EN'
);
INSERT INTO test.account_re lationships (account_id, cif_number,
relationship_in d)
VALUES (
IDENTITY_VAL_LO CAL(test.accoun ts)
, 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_re lationships (
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_in d 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_uni que UNIQUE (cif_number, relationship_in d)
)
@
CREATE SEQUENCE test.accounts_s eq AS INTEGER
START WITH +9999
@
Thanks,
Frank 0 3021 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 referntial
integrity if records in the ID table get messed up and need to be
re-entered.
Can you please advice on best way to do this. I definitely need a
numeric id field because it makes the joins and queries so much
faster.
|
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, share your experience in using IDENTITY as PK .
|
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 test
from query analyzer, then go back into the EM, the box is still
checked.
can anyone tell me what i am missing? any advice on unsetting this
attribute globally would be appreciated!
BEGIN TRANSACTION
|
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 ?
|
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 unique primary key, thus no need to have 3 primary keys
?
Advantages vs Drabacks ?
Regards
| |
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 create a table with the
fields Code and Instruction - a combination of all instructions from the
instruction IDs in tblManinstructions). E.g. Code 1234 currently has 4
fields in tblManinstructions, instructionIDs 28, 43 & 76. The new table I...
|
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)
SELECT ColumnA, ColumnB From OtherTable Where ColumnC > 15
--get entries just added
|
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 need to be able to insert values into multiple identity columns, so therefore SET IDENTITY_INSERT ON/OFF wont work.
We can't simply use the interface either as it means that a miss click or a moment of absent mindedness can cause a world of...
|
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 weeks ago the FK
values got corrupted or mixed up somehow. I also got some warnings
about system temporary table spaces so I created a 16K page size
system temporary table space to be sure. All seemed to be okay until
today. Something happened an...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |