Should 2 different tables share the same like/vote table - mysql

I'm in a situation where I'm in a bit of doubt of whenever I should give a shared 'like' table or split it up into two different tables.
The application will show both videos/images in a 'wall' like manner, like Facebook and many other platforms do.
If the user has already liked an image or video the like button will be colored to show that the content has already been liked.
Video table
id title desc url src
------------------------------------
1 soms thing wwww .png
Image table
id title desc src
------------------------------
1 soms thing .jpg
Options 1 two tables
Vote table video
id video_fk user_fk voted
-----------------------------------------
1 1 1 2
Vote table image
id image_fk user_fk voted
-----------------------------------------
1 1 1 2
Options 2 1 table
Vote table shared (type_fk is identifying which type it is, 1 for image 2 for video)
id content_fk user_fk type_fk voted
-------------------------------------------------
1 1 1 2 2
The problem with 2 tables is when I have to lookup whenever or not a user has liked the content (when scrolling through the wall with many results) I will have to store the results in a multidimensional array with the id and an identifier to decide which content type it is (image or video).
What would you be the better way, or even a third way?

You should keep as little tables as possible, because then you reduce amount of requests and complexity of your code.
I would even go as far as to put both videos and images into the same table called 'content', seeing how almost all of your columns are the same.

Related

database schema design with order

I am pretty new to database schema design. So this may sounds trivial to experts
I am designing a app that store photo for each trip I created.
So, I have one table storing trips and one table storing some photos belonging to each trips.
So, things seems easy for designing this table . And below are my drafted schema
Trips
ID
title
created_at
Photos
ID
title
created_at
image_location
trip_id
Here is the problem:
If I would like to show the order of photos, I can make use of the attribute of created_at in Photos.
But what if one day I would like to rearrange the order of images or I inadvertently assign the photos with wrong order at the first time I add it into the trip.
What I come up with is that I try to make an column called order_no in Photos, so when there is new photos added, its order_no would be increased by one and assigned to it.
Photos
ID
title
created_at
trip_id
order_no
However, the problem is that I will have to update every order_no to keep the sequence order consistent when there is a new rearrangement
For example , I would like to rearrange the position of photo4 , the original order is like below
1 photo1
2 photo2
3 photo3
4 [photo4]
1 [photo4]
2 photo1 (order_no need to be changed from 1 to 2)
3 photo2 (order_no need to be changed from 2 to 3)
4 photo3 (order_no need to be changed from 3 to 4)
Is there a consistent way to implement this ?
Thanks

Link in three tables in php redbeans

I have three tables
USER
user_id,name
EVENT
event_id, name,desc
PARTICIPATION
id, type
1 yes
2 no
3 maybe
I want to handle relationship among these in table user_event_participation
id , user_id,event_id,participation_id
1 1 1 3
How can I do in redbeans
I am afraid that i don't get the problem fully.
But the way it seems to me is linking.
http://www.redbeanphp.com/link_beans
If you want full example please ask. ( I'm sorry, but I simply don't have much time right now)

Database design for chatroom application

EDIT
People think this question is too broad so I'm narrowing things down a bit here:
I have a table with dual primary keys: roomID and userID.
Here is some example data:
roomID | userID
1 | 5
1 | 9
1 | 10
1 | 12
2 | 5
2 | 9
2 | 10
3 | 5
3 | 17
Given a list of users: 5,9,10, how can I return the roomID containing ONLY those users? So in this case it should return 2.
I hope this can be done within 1 SQL query.
Any help would be appreicated.
ORIGINAL QUESTION
I am making a chat room application and need to design a database backend for it. All the rooms are created on the fly and are destroyed when the last user exits the room. Users are able to add other users to any room they are in.
Currently this is my design:
I have a chatroom table with two columns. The two columns are both primary keys for the table (so a row is considered duplicate only when both columns are the same). The first column is the room ID. The second column is a user ID. The idea I have here is with the same room ID, there can be many users in this room (so rows with same room ID but different user ID). When I need to create a new room, I simply select MAX(room ID) + 1 and create a new room with this ID and add the users into it.
Given a list of users IDs (such as 1,5,31,12), I need to find out if a room is already created for them. In other words, I need to determine if there are rows all with the same room ID having users IDs 1,5,31,12. However, if a room is created with users 1,5,31,12,6 (one or more extra users), this should not count as room already created. I will then need to create a new room for them and add the users to that. Same goes for less users than the list.
Right now I'm having trouble forming the query to determine if I need to create a new room or not, and if not, retrieve the room ID of the existing room.
Any help would be appreciated.
Also, I think this design is quite cumbersome, you are welcome to suggest a better database design.
P.S. the database I'm using is MySQL
I think yoy can add 1 more col to the chatroom table, name num_member, this is number of member in room( or better have room(room_id, number_member) table). To make it simple first, I assume you have num_member in chatroom. This query might work:
Select * From chatroom where user_id IN ($userIdList) Group by room_id HAVING count(*) = chatroom.num_member
Hope this help

Media Gallery MySql Database Design

I am putting together a database which will hold information on various media items of different media types. Each media item could then be added to a media collection which could then be used to create media sliders, galleries etc.
Here is an example db
media_types
-----------
id code title
1 images Images
2 videos Videos
3 files Files
gallery_items
-------------
id gallery_id mediaTypeId mediaId
1 1 1 2
2 1 3 1
3 1 2 3
images
------
id title filename
1 X-Wing xwing.png
2 Tie Fighter tie.png
3 A-Wing awing.jpg
videos
------
id title filename
1 Hoth iceplanet.wmv
2 Bespin bespin.ogg
3 Tatooine desert.mpeg
files
-----
id title filename
1 Death Star Plans bothanx.pdf
2 Millenium Falcon Schematics yt1300.pdf
3 Rebel Alliance manifesto.doc
I would like to be able to select all items in a particular gallery with one MySql statement but at present, I can only work out how to perform it in two steps:
1) Select all items from the gallery_items table with a particular gallery id
2) Select the item details from the relevant media type table by analysing the mediaTypeId
In the example I give, the media type tables are very simple, sharing the same columns but in reality they will have different amounts of columns and different column names. A basic join statement on mediaId won't work as each media type table could share the same id.
I had thought about using a unique mediaId across all media types but this would enforce an absolute relationship between the gallery_items table and the media types tables which I would like to avoid (not all items in the media types tables will end up in a media collection). Perhaps the solution would be to create another table, media_items, which created a unique media id for each each which would then be used in each media type table? I am however weary of unnecessary table proliferation.
Any suggestions on how to best approach this design would be most appreciated.
IIWM: I would have one table of 'media types' like you have it. Then another table of media with a unique ID, media type, filename, etc. Last have a table with gallery info - unique id, media id and any other gallery info.
To make it more useful you could have a table of 'changes' that track datetime of assignments to/from a gallery for each piece of media, date it was received, sold, etc. This would track the 'lifespan' of each piece in your collection.

What table structure would best fit this scenario?

I am developing an evaluation system for different programs that needs a lot of flexibility. Each program will have different things to track, so I need to store what data points they want to track, and the corresponding data for the person being evaluated on the particular data point. I am guessing several tables are appropriate. Here is a general outline:
Table: accounts
- unique ID assigned to each account. We'll call this 'aid'
Table: users
- each user with unique ID.
Table: evaluation
- each program will enter in the metrics they want to track into this table (i.e attendance)
- column 'aid' will correspond to 'aid' in account table
Table: evaluation_data
- data (i.e attendance) entered into this database
- column 'aid' will correspond to 'aid' in account table
- column 'uid' will correspond to 'uid' in user table
The input form for evaluation_data will be generated from what's in the evaluation table.
This is the only logical way I can think of doing this. Some of these tables will be growing quite large over time. Is this the most optimal way of doing this?
I'm a little confused about how accounts, users and programs all relate to each other and whether or not account and program are the same thing and that you used the terms interchangeably. I'm going to use different terms which are just easier for me to understand.
Say you have a website that allows freelancers to keep track of different projects and they can create their own data to track. (Hope you see the similarity)
Tables...
freelancers
id title etc
projects
id freelancer_id title description etc
data_options
id freelancer_id title
You can even add other columns like data_type and give options like URL, email, text, date, etc which can be used for validation or to help format the input form.
example data:
1 5 Status
2 5 Budget
3 5 Customer
4 99 Job Type
5 99 Deadline
6 102 Price
7 102 Status
8 102 Due By
This display 3 different freelancers tracking data, freelancers with the id's 5, 99, and 102. Deadline and Due By are essentially the same but freelancers can call these whatever they want.
data_values
id project_id option_id option_value
a column freelancer_id as you would be able to to a join and get the freelancer_id from either the project_id or the option_id
example data:
1000 1 2 $250
1001 1 1 Completed
1002 1 3 Martha Hayes
This is only showing information freelancer with the id 5 has input because option_id's 1-3 belong to that user.