1

I have the next table structure:

Users:

columns: user_id, ...


Users_Skins:

columns: user_id, ...

I want that if a users registers (Users is being populated), it will automatically add a row on Users_Skins with the user_id of the Auto Increment, to Users_Skins.

Should I search for the max user_id, and then insert a new row, or there is a better way (probably) ?

EDIT: I'm using SQL Server

Novak
  • 2,760
  • 9
  • 42
  • 63
  • Depends on your architecture. Triggers, code, managed classes, a service to do that. Does it have to be transparent for the application? I mean, come on, you are "a cook, I need good food, can you tell me what to do", instead of at least telling us what your requirements are. – TomTom Jan 06 '13 at 19:42
  • Which DBMS are you using? Oracle? PostgreSQL? –  Jan 06 '13 at 19:42
  • @a_horse_with_no_name SQL Server – Novak Jan 06 '13 at 19:43

1 Answers1

3

If you do not need to insert any additional data to User_Skins the trigger may be useful, something along those lines:

create trigger UsersInsert
on Users after insert as
begin
   insert into Users_Skins(user_id, ...)
   select i.user_id from inserted I         
end

Instead of trying to find maximum user_id and increment it I would rather use IDENTITY.

Sebastian K
  • 6,235
  • 1
  • 43
  • 67
  • This works perfectly fine for me. On `Users_Skins` I only use a default value for 2 other fields. Thank you very much. – Novak Jan 06 '13 at 19:53