I'd to create a database that records transactions between two users. A user can transfer points (think of it as money) to another user. user
table looks like:
| userID | name | email | balance |
| ------------- |---------------|------------------|------------|
| 101 | alpha | alpha@mail.com | 1000 |
| 102 | bravo | bravo@mail.com | 500 |
| 103 | charlie | charlie@mail.com | 2000 |
And the transaction
table should look like:
| transactionID | from_user | to_user | transfer_amount |
| ------------- |---------------|------------------|------------------|
| 1 | 101 | 103 | 100 |
| 2 | 102 | 101 | 150 |
| 3 | 102 | 103 | 200 |
I tried to draw a schema with Vertableo which doesn't seem have one to zero or many relationship option. But a user might not send or receive points at all, so the relationship should be one to zero or many. However, this schema on Vertableo throws the error reference name must be unique.
Could someone give hints to draw a schema or provide SQL code?
Best Answer
Example SQL code (create with MySQL Workbench):
This works. However if you want to query the 'transfer'-table and want to substitute the IDs for usernames it gets a bit more complicated. Heres an example query: