开发者

Inserting an entry in multiple tables in an sql database

I'm creating a game in a开发者_开发问答ctionscript that requires the use of an external database to store user details and scores.

This database will contain multiple tables, currently there are two.

My first table contains the headers - ID, email, username, password.

My second table contains the headers - ID, lvl1Score, lvl2Score, lvl3Score.

In my game, when a new user is created it creates an entry in the first table with the ID auto-incrementing.

My question is - Is there anyway to automatically create an entry in my second table with its default values and the same ID when I add to my first table? I've read about joins, but everything i've read just talks about looking up data over multiple tables.

Also, is my table structure correct in the sence that the ID value can be used using the JOIN keywork to look up an entry from both tables.


I would suggest you to go for triggers.

create or replace trigger trigger_name after
insert on table1
for each row
begin
insert into table2 values(new.id,"value for lvl2score","value for lvl3score");
end

Something like this.


If the tables truly have a one-to-one relation, I would recommend that you simply make one table having all the fields.

Or did you mean this should store multiple scores for each individual user? In this case, you should not insert a default record for the user. Instead, the score.ID field should instead reference user.ID and allow duplicates.


I suggest you to use triggers and for more flexibility create a many-many relationship between "user" and "level", so you will end up with 3 tables:

  • user
  • level
  • user_level (this will contain the foreign keys: user_id, level_id)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜