I need some help on how to set up a database for user accounts on a mobile device. I have 2 tables: login and settings.
login
id 1
username test
password pass
settings
id(same key from login) 1
setting1 0
setting2 0
setting3 0
I have a register page and I want to insert the username, password, and an auto increment primary ID into the login table and I want to insert that same login ID, setting1, setting2, setting3 into the settings table. Can anyone give an example query on how to insert the login ID into both tables at the same time? How do people in the field normally set up user account databases? I will be using web-sql, but you can answer using any sql driver or just plain charts/text.
With PostgreSQL 9.1 or later, you can chain data-modifying statements together in a writeable CTE
These two statements are executed at the same time – or a close as it gets to be precise.
Also, this construct uses the actual
idsaved inlogin, which may be different from what you handed in, if triggers or rules overrule the input. It can also be used for serial primary keys (which is a very common use case) to insert new rows in multiple tables without having to know the new id beforehand. So, no extra round-trip to the server.If your table
loginis defined:This would work: