Two foreign keys referencing to one primary key - what relation? - mysql

I have problem related to my database. I have user table, which I don't think I need to explain but then i got game table. After each game it will save record of which user was winner and which was loser. What type of relation should I use? I was thinking about many to many. User can have many games and also ech game can have same user winning multiple game as well as losing many games.
Am I right?

A foreign key is a column or a set of columns in one table that references the primary key columns in another table. The primary key is defined as a column (or set of columns) where each value is unique and identifies a single row of the tabl

Related

MYSQL UPDATES, DELETES, INSERTS, CREATE clauses

I'm trying to create a database schema for Game achievements which should include these:
Users:
username,
avatar image file location,
achievements their received in each game
Each achievement has
achievement name
achievement level
achievement type
a game that achievement was part of. Each achievement is specific per game. You can’t have the same achievement across different games
Each game has a title and game type.
It also must have clear relationship definitions (crow’s feet for 1 to many), primary key and foreign keys should be marked.
I've started it but I'm not sure if I did it right and I'm not really sure which of the information types are supposed to be primary keys and which supposed to be foreign keys
The image shows what I have so far and where I'm stuck (stuck on the keys)
So this is what I've gotten now after some comments had explained a bit of this for me
But I'm still not really sure if I did these foreign keys correctly
Primary keys must be unique value, You should give appropriate primary keys for each, as:
Users: user_id,
games: game_id,
achievements: achievement_id,
Then you can specify the foreign keys accordingly, like in achievements, user_id, and game_id will be foreign keys.
Any key that you want to make foreign key then their should be a table where that key present as a primary key.
in your case Users table
username primary key
avtar_image
achievment_name (foreign key) reference to Achievements table (achievment_name)
if you want game also in user table then add a new column in User table
game_title (foreign key) reference to Game table(game_title)

Foreign Key Multi Table Possibility

I have seen similar posts and have not found a definitive answer.
I have a series of tables that store data about certain events. Each of these tables have the same structure. Each of these tables has a foreign key constraint for an id showing what item the data is related to.
Each of these tables id structure is also the same CHAR(24). The tables these ids come from must remain separate because they are all completely different.
I am interested in combining all of my data tables into one with a foreign key being constrained to one of 3 tables. So, a row in my data table will have to have an id that is present in one of the three tables. Additionally this foreign key will need the possibility of ON DELETE settings. Is this possible? And to that, is this poor design?
Items A
- id
- ...
Items B
- id
- ...
Items C
- id
- ...
Data
- id FK
No. What you're describing is sometimes called polymorphic-associations but it should be a clue that it's not good design because you can't make a foreign key constraint for it. That is, the FOREIGN KEY syntax only allows you to reference one table, not three.
The only way you could make a real foreign key constraint that performs ON DELETE actions is to make three separate foreign keys:
Data
- idA FK
- idB FK
- idC FK
For a given row in Data, presumably only one of these three foreign keys would be non-NULL. The other two would be NULL. Ensuring this could be done in a trigger or CHECK constraint, or else you would just have to implement it in application logic (i.e. don't insert a row with more than one of these columns non-NULL).
Polymorphic associations, that is storing a single column that may reference one of three different tables, is not a valid relational design.
You can see past answers I've written about polymorphic associations: https://stackoverflow.com/search?q=%5Bpolymorphic-associations%5D+user%3A20860

In MySQL, if has many-to-many table, should i create auto-incr column as PK or use multi-column unique as PK? and Why?

for exmaple, has course relationship table, student id and course id is multi-unique, if i create this relationship table, should i use auto-incr column as PK, or use student id and course id as multi-PK ?
Some people add auto-increment column as PK to just every table.
But I believe it is good to have a multi-column-PK in the case where the table is a relationship table between two or more tables.
On the other hand, it is more effort to delete a multi-column-PK table entry, because you need to give all columns in the multi-column-PK.
Also, check whether your technology stack (programming language) has problems with multi-column-PK.
This is something of a matter of opinion, but I put a synthetic primary key (auto-incremented id) in almost every table I create, including association/junction tables.
Why? Here are some reasons:
If I need to delete or update rows, then the primary key simplifies the process and reduces the change for error.
The primary key captures the insertion order of the rows.
If the row needs to be referred to by another table, then you can refer to it by a primary key.
In some databases, the primary key is used to cluster the data (that is, sort the data on the data pages). An auto-incremented primary key ensures that data goes "at the end". A natural primary key can result in fragmented data.
As an example of the third point, you might have an attendance table that records -- by day -- whether a student attended a class s/he is enrolled in. This could refer to the enrollment table.

Primary / Foreign Key example

I am trying to figure out relationships between my tables and the notion of how it all inter-relates is not clear.
Lets say I have a Person table (first name, last name, etc) where the Primary key is social security number.
I also have a Fireman table that has just 2 columns -- date when joined the fire company, and a unique fireman number. The 3rd column would be a link back to the Person table.
Initially, I made SSN in the Fireman table to be a foreign key linking to SSN in the Person table. But doesnt it imply that every time I create a new Fireman, I can re-use the same SSN over and over? The foreign key constraint would not be violated -- so its "all good" -- but its not what I want. Does it make sense? I dont want to allow for different unique Fireman numbers all sharing the same SSN number. So how do I set this up -- whats Primary, whats foreign, whats one to one and whats one to many.
A key can be primary and foreign all at the same time in a one-to-one relationship.
You can scrap your primary key from the Fireman table, and just use SSN as the unique key. Since it's one-to-one, there's no need for a separate identifier.
Alternatively, you could create a unique index on the column SSN in your fireman table. That would prevent duplicate entry.
Note that data validation, relationships and indexes are all separate things. You can use indexes and relationships to validate data, but you can also use different concepts, like constraints.

Weak entity on mysql with no primary key

I have a question about transfering a weak entity from an e-r model to sql tables.
Specifically i am using mysql.
Lets say i have the table hotels in which i have primary key id for each hotel, named id.
Then i want to create a table for rooms. A room is considered weak entity because it cannot be identified without refering to a hotel and because it cannot exist without a hotel.
So lets say that rooms contain columns with data and two other columns.
First column is a foreign key to hotel id and a second column which is a room number.
Note than many rooms can have the same number but no room in the same hotel can have the same number. So this is a way to identify a specific room.
How can i put this into a table?
I cannot declare the number as unique because its not unique, i cannot declare it as a primary key either for the same reason. So am i creating a table without primary key?
Also, the foreign key "hotel_id" is a unique key since it is a primary key in hotels table.
I suppose i won't having problems using many rooms with the same foreign key.
To sum up.
My question is how to create the table rooms, is there any way to automatically identify each room except the obvious (identifying the room by selecting a room with specific number and hotel_id)?.
Thank you in advance.
The primary key in the Rooms table is composed of two columns (HotelID, RoomNumber). That combination must be unique for your data model to operate correctly.
You create a table rooms with an id column as primary key. This table has also the columns hotel_id (FK to hotels) and room_number. You define an unique index over (hotel_id, room_number).
That's all ;)