I'm trying to create a rather complicated relationship between the tweets and its author. In my system, I have 3 sources which can be related to a tweet (teams, players and persons). The issue is that instead of having a teamId, personId and teamId in each tweet and only one of them is not null. I've come up with below solution with a relational table called created. However the issue is that I'm not sure it is possible to create a foreign key from the sourceId to all 3 ids in Teams, Players and Persons tables? What can I do in order to create the right table structure for this type of system?
To explain the Created table further it contains free columns:
id = primary key and auto_increment
sourceType = enum can be (persons, teams or Players). By doing this i know whether it is a person, team or player
sourceId = is the id of the particular person, team or player.
Players
id
name
image
twitter_user
teamId
Teams
id
name
twitter_user
Persons
id
name
image
twitter_user
teamId
Tweet
id
created
text
createdId
Created
id
sourcetype
sourceId
Related
I have 2 tables called teams and players and I want to copy the field points from the table teams to the table players using the foreign key team in the table players which is the primary key id_team in the table teams. I need to have each player with the points of your own team. I'm trying
INSERT INTO players (points_team) SELECT points FROM teams WHERE
and here I don't know how to write the correct where sentence
where id_team = (whatever you the fk/pk is named of the other table);
this will essential connect the fk to the pk of both the tables.
I'm very new to Databases and looking forward to develop an application with some advanced functionality.
Here I'm going to have a table with users just like the following one.
So, in the sports column, value will be the sports the user plays. They will be some ids from the records of sports table.
So my problem is how to store those multiple values in the same row same column ?
Thanks
PS: Please note that I'm a beginner...
The proper way to do this would be to maintain three different tables.
User: id, name, age
Sport: id, name
UserSport: user_id, sport_id
The UserSport table references the primary key (id) of both tables (User and Sport) and contains a separate entry for each sport the user participates in.
The primary key of UserSport should then be a composite key of both user_id and sport_id. This allows you to have multiple rows for each user and multiple rows for each sport, but a unique combination of both the user and sport.
I have MySQL database with two tables, Users and Games. Basically each user has a unique ID and a set of games that they are playing and each game table has an ID and HasStarted flag (when HasStarted = false players can join the game). The table was set up so the users list of games a string of ID numbers separated by semi-colons (EX: 1;3;5; means they are part of game 1, 3, and 5). Create examples at bottom
What I would like to do is fetch all the games that are joinable AND that they are not already a part of.
I tried using
SELECT * FROM games INNER JOIN users ON games.GID = users.gamelist
WHERE games.status = 0;
but that only matches the games based on the first game in their list. What I'm going to do for now is just fetch all the joinable games into an array and then get the player's list of games and drop those but I was wondering if there was a way to fetch them all in one query. Does anyone know if I can do something like this?
CREATE TABLE users
(UID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(UID),
games TINYTEXT)
CREATE TABLE games
(GID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(GID),
has_started BIT)
You REALLY should alter your schema to have a users_to_games table to normalize your data. The setup could look like this:
users
------
uid - primary key
[other fields specific to a single user]
games
------
gid - primary key
has_started
[other fields specific to a single game]
users_to_games
--------------
uid - first field in compound primary key
gid - second field in compound primary key
There would be one record in users_to_games for every game a particular user is involved in.
Now in order to get your list of games that have not started but user has not joined you could do this:
SELECT gid, [any other fields from game table you want for display]
FROM games
WHERE is_started = 0
AND gid NOT IN (SELECT DISTINCT gid FROM users_to_games WHERE uid = ?)
I have two tables. "users" and "movies". Users table consists of "id"(Auto increment), "name" and "password" columns. There are 2 usernames stored right now. In movies table there are 'title' and 'year' columns. The PHP script allows each user to watch and add new movies to their list. How do I link or make the parent-child relationship or whatever is needed to make it happen in MySQL? Oh, and I also use Adminer. Right now when I log in one user I still see the same movies that I've added with the other user.
If you are stuck with using just two tables as stated in a comment, you have to redesign the Movies table to include a column UserID which identifies which user created that entry. Then you can filter the data so that a user only sees information about the movies they added to the list.
This isn't a good design — the answer by Jeremy Smyth suggesting an extra table to relate movies to users is much more sensible, but you've indicated that isn't allowed. The reason it isn't a good design is that you're going to end up with lots of rows indicating that the same movie was released in the same year, each row entered by a different user, so there is unnecessary repetition. There's also more chance for error; you'll get entries for 'Gone With The Wind' 1938, and 'Gone With The Wind' 1939, and 'Gone With The Wind' 1940 when there should only be one year (1939, as it happens).
Can you please be more specific about what I have to do ...
In the two-tables-only system, you would create the Movies table like this:
CREATE TABLE Movies
(
Title VARCHAR(32) NOT NULL,
Year INTEGER NOT NULL,
UserID INTEGER NOT NULL REFERENCES Users(ID),
PRIMARY KEY(Title, Year, UserID)
);
When you insert a record into this table, you record the ID of the user who did the insertion, so you can query who created which movie records.
If you are actually going to reference this table from elsewhere in the database, you might well add an ID column here, but if there are more tables, then you'd drop the UserID column from this table and create a relationship table:
CREATE TABLE Movies
(
ID INTEGER AUTOINCREMENT PRIMARY KEY,
Title VARCHAR(32) NOT NULL,
Year INTEGER NOT NULL,
UNIQUE(Title, Year)
);
CREATE TABLE Users_Movies
(
MovieID INTEGER NOT NULL REFERENCES Movies(ID),
UserID INTEGER NOT NULL REFERENCES Users(ID),
PRIMARY KEY(MovieID, UserID)
);
Now you can have one record for 'Gone With The Wind' 1939, which might have ID number 207, and twenty different people might list MovieID 207 as one of their movies with 20 simple records in the Users_Movies table.
You will need to create a "many-to-many" relationship between your two tables.
To do this:
First, create an ID column in the Movies table to uniquely identify each one
Then, create another table called user_movies (or "watched" or something useful), that contains the user ID, the movie ID, and any other information you wish to add such as date watched or rating (number of "stars") etc.
Then, whenever a user watches a movie, add a record to the user_movies table to mark the fact that they've done it.
It should be many-to-many, because each user can watch several movies, but each movie can be watched by several users. A "parent-child" relationship isn't appropriate in this case, being a one-to-many relationship.
I'm designing my first big database and want to check, whether I'm OK in providing tables relationships.
I'm designing a webapp where
users can play games in teams
each game has it's categories
users create their team for the game and choose their team category
each game has it's own subset of categories enabled
in fact categories slightly differ from game to game. For example, there are slowpoke categories in game1 and game2, but they differ in alowed time.
How do I hope to store this:
there are tables users, teams, games,
categories, each with it's adequate data fields and INT primary_keys
there is user-team M-N relationship table
there is game-category M-N relationship table, with alowed time column
there are category_id and game_id foreign key columns at team table
My common queries:
user -> all his/her teams, in which they were games, categories
game -> all it's categories
game -> all teams paticipated, with category, with users
game -> all users participated
team -> it's game, category
My suggestions to me:
Make a real-category id creating a separate INT primary index in category-game relation table and use it in team table
Create separate table with team_id category_id game_id and set all them as a concatenated primary index
Just set game_id in teams table as INDEX
The question is if this thing is OK and what suggestions to accept?
Some thoughts:
Use UNSIGNED INT for primary keys rather than INT.
You must have some TEAM -> GAME mapping, right?
Given that, it seems like you can easily process your "common queries".
What problems were your "suggestions" trying to solve?