Home All Groups Group Topic Archive Search About

Update Col2 based on Col1(IDENTITY) ??

Author
8 Jul 2005 7:53 PM
Arthur Dent
Hello all, i have a table CAT with an IDENTITY column "ROWID".
There is a second column "CATID" which i need to do essentially the
following:
<pseduocode>
    TRIGGER AFTER INSERT ON CAT
        UPDATE CAT SET CATID = 'CAT'+CONVERT(VARCHAR,ROWID)

but this doesnt work, because the IDENTITIES havent been set yet.
So i figured ill try just making CATID a computed column with formula of:
    'CAT'+CONVERT(VARCHAR,[ROWID])

Only problem is, i need to use this column in a foreign key, which isnt
possible on a
computed column. So how can i do the trigger code above? I dont want to use
the @@IDENTITY
or any of those, because of the case where the trigger might be firing for
multiple rows.

Thanks in advance >
- Arthur Dent.

Author
8 Jul 2005 8:11 PM
Aaron Bertrand [SQL Server MVP]
>    TRIGGER AFTER INSERT ON CAT
>        UPDATE CAT SET CATID = 'CAT'+CONVERT(VARCHAR,ROWID)

WHY DO YOU NEED TO STORE THIS?

Create a VIEW.

SELECT ROWID, CATID = 'CAT'+CONVERT(VARCHAR(12), ROWID) FROM CAT

> Only problem is, i need to use this column in a foreign key, which isnt
> possible on a
> computed column.

Why is CAT5 or CAT162 a better usage of the foreign key than 5 or 162?  Why
is this computed value based on a constant and a surrogate used as a primary
key in any table?

A
Author
8 Jul 2005 8:20 PM
Arthur Dent
I wanted to use this on this table. Then i had a PROD table which did the
same thing, but prepended 'PRD' instead of 'CAT'.
These then are both used as foreign keys into a single table which stores
ATTRIBUTES for both Categories and Products.
This would be keyed on an ITEMID which would be such as: PRD1, CAT4, PRD23,
CAT62, so it needs unique keys
between the PRD and CAT tables, which, if they each just PK on an identity
column, they wont have. Unless there is some way
to have two tables SHARE an identity column's incements? so when TblA added
a row, the IDENT in TblB would also get bumped up?


Show quote
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:OFytqk$gFHA.2840@tk2msftngp13.phx.gbl...
>>    TRIGGER AFTER INSERT ON CAT
>>        UPDATE CAT SET CATID = 'CAT'+CONVERT(VARCHAR,ROWID)
>
> WHY DO YOU NEED TO STORE THIS?
>
> Create a VIEW.
>
> SELECT ROWID, CATID = 'CAT'+CONVERT(VARCHAR(12), ROWID) FROM CAT
>
>> Only problem is, i need to use this column in a foreign key, which isnt
>> possible on a
>> computed column.
>
> Why is CAT5 or CAT162 a better usage of the foreign key than 5 or 162?
> Why is this computed value based on a constant and a surrogate used as a
> primary key in any table?
>
> A
>
Author
8 Jul 2005 8:25 PM
Aaron Bertrand [SQL Server MVP]
> This would be keyed on an ITEMID which would be such as: PRD1, CAT4,
> PRD23, CAT62, so it needs unique keys
> between the PRD and CAT tables, which, if they each just PK on an identity
> column, they wont have.

Why not have ProductID and CategoryID, each of them a foreign key to
Products and Categories, where one of them has to be valid and one of them
has to be NULL?

Or, you could design things a bit better and store product details and
category details in their own table?

This scheme seems very messy and will be impossible to program around.  Ugh.

> Unless there is some way
> to have two tables SHARE an identity column's incements?

Well, you could simulate Oracle's SEQUENCE table, but again, ugh.
Author
8 Jul 2005 8:34 PM
Arthur Dent
Well, i was just trying to keep the db a little cleaner, there are at least
3 or four tables which need to cover both products and categories, with the
same structure in each mode, so i was trying to keep the db to 3 or 4
tables, instead of 6 or 8 tables, it would be less to maintain, and more
consistent sql statements.

But, if it cant be done, well, duplication itll be i guess.

Show quote
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:%23332Qs$gFHA.3608@TK2MSFTNGP12.phx.gbl...
>> This would be keyed on an ITEMID which would be such as: PRD1, CAT4,
>> PRD23, CAT62, so it needs unique keys
>> between the PRD and CAT tables, which, if they each just PK on an
>> identity column, they wont have.
>
> Why not have ProductID and CategoryID, each of them a foreign key to
> Products and Categories, where one of them has to be valid and one of them
> has to be NULL?
>
> Or, you could design things a bit better and store product details and
> category details in their own table?
>
> This scheme seems very messy and will be impossible to program around.
> Ugh.
>
>> Unless there is some way
>> to have two tables SHARE an identity column's incements?
>
> Well, you could simulate Oracle's SEQUENCE table, but again, ugh.
>
Author
8 Jul 2005 8:44 PM
Aaron Bertrand [SQL Server MVP]
> Well, i was just trying to keep the db a little cleaner, there are at
> least 3 or four tables which need to cover both products and categories,

Why?

> same structure in each mode, so i was trying to keep the db to 3 or 4
> tables, instead of 6 or 8 tables, it would be less to maintain, and more
> consistent sql statements.

If you have different types of entities, you should have different tables.
If they're identical as you suggest, why would they be harder to maintain?
Author
9 Jul 2005 5:08 AM
Louis Davidson
So your product table has the exact same attributes as your category table?
Then you probably aren't making full use of the database then.  The more
tables the better if one table could represent multiple things and you have
to decide at usage time what kind of thing you are representing.  Plus SQL
works better the smaller things are and with better indexes.  An index with
a couple of values is far worse generally that multiple tables partitioned
on the couple of values.

--
----------------------------------------------------------------------------
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP


Show quote
"Arthur Dent" <hitchhikersguideto-n***@yahoo.com> wrote in message
news:Okn9hx$gFHA.3300@TK2MSFTNGP15.phx.gbl...
> Well, i was just trying to keep the db a little cleaner, there are at
> least 3 or four tables which need to cover both products and categories,
> with the same structure in each mode, so i was trying to keep the db to 3
> or 4 tables, instead of 6 or 8 tables, it would be less to maintain, and
> more consistent sql statements.
>
> But, if it cant be done, well, duplication itll be i guess.
>
> "Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in
> message news:%23332Qs$gFHA.3608@TK2MSFTNGP12.phx.gbl...
>>> This would be keyed on an ITEMID which would be such as: PRD1, CAT4,
>>> PRD23, CAT62, so it needs unique keys
>>> between the PRD and CAT tables, which, if they each just PK on an
>>> identity column, they wont have.
>>
>> Why not have ProductID and CategoryID, each of them a foreign key to
>> Products and Categories, where one of them has to be valid and one of
>> them has to be NULL?
>>
>> Or, you could design things a bit better and store product details and
>> category details in their own table?
>>
>> This scheme seems very messy and will be impossible to program around.
>> Ugh.
>>
>>> Unless there is some way
>>> to have two tables SHARE an identity column's incements?
>>
>> Well, you could simulate Oracle's SEQUENCE table, but again, ugh.
>>
>
>

AddThis Social Bookmark Button