在sql数据库中的多个表中插入一个条目

时间:2022-09-25 23:55:28

I'm creating a game in actionscript 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.

我的第一个表包含标题 - ID,电子邮件,用户名,密码。

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

我的第二个表包含标题 - 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.

在我的游戏中,当创建新用户时,它会在第一个表中创建一个条目,ID会自动递增。

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.

我的问题是 - 当我添加到我的第一个表时,是否有自动在我的第二个表中使用其默认值和相同的ID创建一个条目?我已经阅读了关于连接的内容,但我读过的所有内容都只是讨论了在多个表上查找数据。

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.

此外,我的表结构是正确的,因为可以使用JOIN关键字从两个表中查找条目来使用ID值。

3 个解决方案

#1


1  

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

在table1上插入后,为每个行开始插入到table2值(new.id,“lvl2score的值”,“lvl3score的值”);创建或替换触发器trigger_name结束

Something like this.

像这样的东西。

#2


1  

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.

或者你的意思是这应该为每个用户存储多个分数?在这种情况下,您不应为用户插入默认记录。相反,score.ID字段应该引用user.ID并允许重复。

#3


0  

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:

我建议你使用触发器,为了更灵活,在“用户”和“级别”之间创建许多关系,所以你最终会得到3个表:

  • user
  • level
  • user_level (this will contain the foreign keys: user_id, level_id)
  • user_level(这将包含外键:user_id,level_id)

#1


1  

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

在table1上插入后,为每个行开始插入到table2值(new.id,“lvl2score的值”,“lvl3score的值”);创建或替换触发器trigger_name结束

Something like this.

像这样的东西。

#2


1  

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.

或者你的意思是这应该为每个用户存储多个分数?在这种情况下,您不应为用户插入默认记录。相反,score.ID字段应该引用user.ID并允许重复。

#3


0  

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:

我建议你使用触发器,为了更灵活,在“用户”和“级别”之间创建许多关系,所以你最终会得到3个表:

  • user
  • level
  • user_level (this will contain the foreign keys: user_id, level_id)
  • user_level(这将包含外键:user_id,level_id)