Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Stored Proc?...Trigger?...Other?

 

Hi all,

 

I have a project that I'm working on that involves a SQL database with multiple tables that are all linked to a general Users table. The Users table includes an Identity field as the primary key. The other tables use this field as an identifier for the user that the record is associated with.

 

What method should I use so that if one of our apps inserts a record into a table, the "user information" is added to the Users table first, and then the Identity field is copied and combined with the rest of the information, and inserted into the original table? I'm vaguely familiar with stored procedures and triggers, but I have never used either one of them as of yet. Any suggestions/examples/tutorials would be greatly appreciated.

 

Thanks in advance,

 

Chris

Edited by dakota97
if(computer.speed == "slow")
    {  
       hamster.feed();  
    }
if(computer.speed == "really slow")
    {  
        hamster.kill();
        BuyNewHamster();
    }

  • 2 weeks later...
Posted
IMO' date=' a user shouldn't be adding information to a child table unless the relevant record is created in the parent table first.[/quote']Actually you can use an "instead of" trigger on the child table to populate the parent, then update the triggering table in same trigger. There is no problem with this and sometimes is the most doirect and efficient approach.

 

"instead of" triggers are not called recursively so you can update/insert/delete the triggering table without getting into an infinite loop.

 

For this situation I would use a uniqueidentifier for the key. . . i really don't like indentity fields.

 

in the trigger, generate a guid via newID. populate parent with guid, use guid in children.

 

on a tangent. . .

 

watch out for this novice schema design pattern mistake -

 

create table Parent(

id int identity(1,1) not null primary key,

uniqueSearchData nvarchar(255) not null unique)

go

create table child(

fk int references parent(id),

dateTimeOfTransaction datetime,

someSearchData nvarchar(255) not null,

primary key (fk, dateTimeOfTransaction)

)

go

create index ix_childlookup on child(someSearchData)

 

Over time, this can lead to some horrendous performance.

 

why? create the above schema and generate the resultant script.

see the clustering of the indexes?

 

by default, the sql engine clusters the primary keys, but you never search on the primary keys in a situtation like this. Also, the foreign keys are never retrieved in a sequential order. You want to cluster the searching fields.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...