Suggestion for better relational DB Schema [duplicate] - mysql

This question already has answers here:
Database design for a survey [closed]
(11 answers)
Closed 3 years ago.
I am here to design a relational database schema that models polling and users. Each
category can have one or many questions. Each user can participate
only once at each category, and can poll (or not) exactly once on
each question. Each poll is yes, no, or abstain (no vote).
I have designed my schema with four tables:
users, [userId, IP]
category, [catId, catTitle]
question, [queId, queTitle]
polls [pollId, queId, userId, answer]
is better or
users, [userId, IP]
category, [catId, catTitle]
question, [queId, queTitle]
polls [pollId, catId, userId]
pollAnswers [pollAndId, queId, pollId, answer]
I would like to know which one is better and why?
as per me since i dun have any extra information about polls i am directly cross joining users and questions with answer.
I also do need to find how many users were abstain for a.) all category question b.) particular category c.) particular question
I have my view as for choice one schema :
select U1.*, Q1.*, P2.*, C1.*
from
( users U1,
questions Q1 )
Left outer Join polls P2 on
Q1.queId = P2.queId AND U1.userId = P2.userId
Left Outer Join category C1 on
Q1.catId = C1.catId
I am trying to worried using above query with cross join between users and question will loose my performance or not?
If second schema is better can u suggest options for my results?

users: rename to user so all entities are singular (or rename the singulars to plural)
category: OK
question: You need a foreign key from question to category
pollAnswers: Rename to pollAnswer, and make it a reference table with three rows: Yes, No, Abstain
polls: Rename to poll, and make this a transaction of a user polling a question, with foreign keys to user, question, and pollAnswer.
(Options: Either validate on insert that the user has no existing polls for questions in the category, or denormalize PollAnswers with the question's foreign key to category and put a unique constraint on user/question. I don't personally like this option because it presents issues if you change the category of a question.)
Then a query for a particular category could look like this:
select *
from category c
LEFT OUTER JOIN question q ON q.catID = c.catID
LEFT OUTER JOIN poll p ON p.queId = q.queId
LEFT OUTER JOIN user u ON u.userId = p.UserID
WHERE c.catID = 'blah blah'

Related

MySQL Query Inner Join Inverse?

How can we show the 'inverse' of the Inner Join. For example, I have a list of actual transactions of customers that went thru the payment processor, in this case 'Paypal' but they never clicked the 'Back to Merchant' tab so that we can process their userid and password.
This script shows ALL the people that are in the customer list and their associated place in the users database:
SELECT
`Transactions List`.`Customer Email`,
users.Email,
`Transactions List`.`Transaction ID`,
users.`Name`,
users.Phone
FROM
`Transactions List`
INNER JOIN users ON `Transactions List`.`Customer Email` = users.Email
What I'm seeking to do is show the INVERSE of that. i.e. all the people who LOST their way. They DO appear in the TRANSACTIONS LIST table but do NOT appear in the USERS table.
Anyone have an idea how to convert this MYSQL Query into the Inverse so we can quickly identify which customers did not get user accounts?
There's an existing post "Inner join inverse Php MySQL" here that wasn't answered that asks a similar question. Perhaps the person asking the question was not clear enough: Inner join inverse Php mysql
also
What is the difference between “INNER JOIN” and “OUTER JOIN”?
What is the difference between "INNER JOIN" and "OUTER JOIN"?
but neither of these methods actually do what I want the script to do.
What I'm seeking to do is show [...] all the people who [...] appear in the TRANSACTIONS LIST table but do NOT appear in the USERS table.
You could use not exists:
select t.*
from transactions_list t
where not exists (
select 1 from users u where t.customer_email = u.email
)
Another way to phrase this is to use an anti-left join (this is more in the spirit of your question, that relates to joins):
select t.*
from transactions_list t
left join users u on t.customer_email = u.email
where u.email is null
This means: try to join each transaction with a user, and filter on those that did not match.
select t.*
from `Transactions List` t
left join users u on t.`Customer Email` = u.email
where u.email is null
Given the above syntax and the name of the table in the database as specified above this is the correct answer. Thank you to GMB for answering the question. For other readers, keep in mind that if your database tables include spaces in their names or field names then you must use the scare quotes to identify your table or field names. This is commonly used when importing tables into MySQL from 3rd party tools.

I'm trying to let a user who has been assigned something be able to view it with PHP and MYSQL

I've tried searching an answer for this and I can't realy find the one that fits what I'm looking for.
I'm making a small web application that allows an admin to create course material and a test, that a user will be assigned.
What I am trying to figure out, is how a user, who has been assigned a course, can access that course by clicking a button.
I have created one table which stores the user information, with a user ID being a primary key. I have a course table, which stores all materials and the course's test, with a course id being that table's primary key.
I made a third table which is an assigned_courses table, it contains two foreign keys. One referencing the user id from the user table, and one referencing the course id on the course table.
I've inserted a few images to show what you mean.
I can't quite figure out the Sql syntax to pull course materials and test based on the assigned_course table.
All feedback appreciated. This is the two foreign keys that reference the user id (id) and the course id(u_id)
Not sure what column names you have so I used * instead. You can use JOIN (AKA INNER JOIN) to link the tables together. You join on the primary keys of the 2 tables.
I have created one table which stores the user information, with a
user ID being a primary key. I have a course table, which stores all
materials and the course's test, with a course id being that table's
primary key.
I used id for your user table and course_id for the course table. Change as needed.
Find all courses for a given user.
SELECT a.*
FROM course a
JOIN assigned_course b ON a.course_id = b.id
WHERE b.u_id = :userId
;
Find all users for a given course.
SELECT a.*
FROM user a
JOIN assigned_course b ON a.id = b.u_id
WHERE b.id = :courseId
;
EDIT #1 - Subquery without JOIN
Thanks for your feedback, as I stated above, I was hoping to avoid a
join.
I highly recommend you learn joins. They are essential when you need to query more than 1 table. In this case, you can use a sub-query to find matching courses for a user but you won't be able to access that user data at the same time; that is the power of a join. I'll include a final example underneath showing how to get courses and user data at the same time.
Find all courses for a given user (using sub-query)
SELECT a.*
FROM course a
WHERE course_id IN (
SELECT id
FROM assigned_course b
WHERE u_id = :userId)
;
Final example showing courses related to users (many-to-many)
SELECT * -- You now have access to a, b, and c data
FROM course a
JOIN assigned_course b ON a.course_id = b.id -- joined by PKs
JOIN user c ON c.id = b.u_id -- joined by PKs
WHERE c.id = :userId
;
It sounds to me like you want to use SQL Join syntax which goes as follows:
select column1, column2, column3 etc...
from table1
join course on table1.id = course.id
join assigned_course on table1.id = assigned_course.id
replace the column1,2,3 etc with the actual column names, and replace the foreign_key_name with the actual name of the foreign key in the sub-table and the same for the user_primary_key
you can find some other examples of mysql joins here
as far as how to implement them into your html or php file, that is a very different question and would require knowing alot more about how your code is setup in the form
a place that is pretty user friendly is that might help with pulling the data is here just make sure you are aware of using prepared statements for your code

Single SQL query on many to many relationship

I have a simple database with few tables (and some sample columns):
game (gm_id , game_name , company , desc)
plateform_master (p_id , plateform_name)
plateform_details (pd_id, gm_id, p_id, release_date)
Is there a way to create single SQL query which will return all game details with multiple plateform
Your question is not very clear. To which posts and categories are you referring yourself?
Here an example based on what I understood: Select all records from plateform_master and, for each of them, get its details from plateform_details. You can uncomment the where part in order to get one master record with its details. ("--" means comment in SQL)
select
pm.p_id
pd.*
from plateform_master as pm
inner join plateform_details as pd on pm.p_id = pd.p_id
-- where pm.p_id = 123
Sorry, my bad, it should be left join, not inner join!
With your changed requirements try this (not tested): Select all games and, for each one, get it's plateform_details and plateform_master correspondent values:
select
gm.gm_id,
gm.game_name,
gm.company,
pd.release_date,
pm.plateform_name
from game as gm
left join plateform_details as pd on pd.gm_id = gm.gm_id
left join plateform_master as pm on (pm.p_id = pd.p_id AND pd.gm_id = gm.gm_id)
-- WHERE CONDITIONS
;
Thanks for voting. I also wanted to tell you, that you have no many-to-many relationships here. You have just one-to-many. Many-to-many means the use of a middle table between two other ones.

Joining activity table with the source tables in MYSQL

I have a MYSQL 'activity' table where all activity by a user gets logged. Let's say that a user's activities can be either to ask a question or answer a question. So if a user asks a question, their question gets put into the 'question' table and also the activity gets put into the 'activity' table. Same thing if user provides an answer, except that the answer goes into the 'answers' table and the 'activity' table.
The question table's fields are:
q_id question
The answers table's fields are:
a_id q_id answer
The activity table's fields are:
activity_id q_id user_id action_type
(There are more columns then shown above, but these illustrate the point.)
What I want to be able to do is show a user's recent actions on the site, and actually show what the actions were.
My current MYSQL query simply joins the activity table with the question table as follows (using PDO):
"SELECT *
FROM activity
LEFT JOIN question ON activity.q_id = question.q_id
WHERE activity.user_id = :user_id
ORDER BY activity.a_id desc"
This works okay, and can always give me the question for display in the activities list. However, if the activity that is returned is an answer, I would like to be able to show the answer as well. I tried just using a triple join as follows:
"SELECT *
FROM activity
LEFT JOIN question ON activity.q_id = question.q_id
LEFT JOIN answers ON activity.q_id = answers.q_id
WHERE activity.user_id = :user_id
ORDER BY activity.a_id desc"
But this just gives me a list of rows that is twice as long as the first query, and does not display the answers when the action_type was an answer.
I figure this should be possible, but the more I think about it I believe that maybe I should scrap the activity table and just perform the query directly on the question and answers tables with a UNION? Any opinions on this decision, and any thoughts on the queries?
You could use an outer join, which would return everything in the activity table, even if there isn't a corresponding row in the answer table:
SELECT *
FROM activity
LEFT JOIN question ON activity.q_id = question.q_id
LEFT OUTER JOIN answers ON activity.q_id = answers.q_id
WHERE activity.user_id = :user_id
ORDER BY activity.a_id desc
I'm assuming that for every q_id value in the activity table, there's always going to be one corresponding row in the question table.
To fix the problem with the duplicate answers, I'd add an a_id field to the activity table. For the question activity, you'd set it to NULL, and for each answer you'd set the a_id field to the appropriate a_id value to identify which answer goes with the activity. You'd then adjust the SQL above as follows:
SELECT *
FROM activity
LEFT JOIN question ON
activity.q_id = question.q_id
LEFT OUTER JOIN answers ON
activity.q_id = answers.q_id AND
activity.a_id = answers.a_id
WHERE activity.user_id = :user_id
ORDER BY activity.a_id desc

Mysql fetching data from multiple tables

I am having three tables
user(id,fname,lname)
picture(picid,pic_path,userid)
friends(friend_of,friends_id,status)
I want to fetch id,fname,lname,pic_path for all friends_id of friends_of=1
picture path may be null
Thankx in advance.
What you're looking for is called a join. In particular, you're looking for a JOIN and a LEFT JOIN.
SELECT
user.id, user.fname, user.lname, picture.pic_path
FROM
friends
JOIN user
ON user.id = friends_id
LEFT JOIN picture
ON userid = user.id
WHERE
friends.friend_of = 1
This will only work though if there's a maximum of 1 entry in picture.
Though this answers your immediate question, I suggest you become very familiar with joins. They're one of the fundamental operations of a relational database. (Note that a join essentially is a mathematical relation)
Try this
SELECT u.*,p.*,f.*
FROM USER u
LEFT JOIN picture p ON p.user_id = id
INNER JOIN friends f ON f.friends_of = u.id
WHERE f.friends_id = 1
For querys like that you need to understand and employ the relations between your entities. Then you work in two steps: selection and projection and contrary to what SQL-syntax may imply the part before the FROM keyword is the projection.
First we compile data. Clearly we need the data from all three tables and we need it once. So at first we construct all possible combinations, by joining all three tables. In SQL this is done in the FROM part, i.e.
FROM friends f, picture p, user u
f, p and u are aliases which serve the purpose of saving us the efford of typing the full table names in the following.
Now we have all possible combinations. Let's select the ones we want:
I suppose every picture in your DB belongs to a user which is stored in your DB as well. So my assumption is that you only want pictures associated to a user. Hence we have a first restrictions on all the combinations we made before! The restriction derived from the (probable) meaning of the data stored in you database and stated as:
u.id = p.userid
(Notice: by applying this restriction to combination above we "select" only certain combinations.)
Then you already stated another restrictions as a request "friends_of=1" to associate this constraint on the combination we write:
f.friends_of=1
Then we combine your request "friends_of=1" with the other data by:
f.friend_of = u.id
This constraint selects only those users who are a friend of somebody. Now we can combine these constraints. As we want all constraints to be satisfied we AND them in a WHERE statement:
WHERE u.id = p.userid AND f.friend_of = u.id AND f.friends_of=1
The ordering does not affect meaning (in this case. But let's rethink those constraints:
u.id = p.userid : we want information about the user and the pictures associated with that user
f.friend_of = u.id : we are looking for a users who are friends of somebody
f.friends_of=1 : we are looking for friends of a particular somebody
Now we project the data stored in our DB to what we want. We want all the user data and picture paths. In SQL:
SELECT u.*,p.pic_path
Now we put everything together.
SELECT u.*,p.pic_path FROM friends f, picture p, user u WHERE u.id = p.userid AND f.friend_of = u.id AND f.friends_of=1
To allow for friends that don't have a picture associated with them (note: that's very different to pic_path being NULL) you need an outer join, which means you also want combinations with empty sets. That's where my MySQL is not so good but I'd guess you'd generate all combinations you want (and many more) with:
FROM friends f JOIN user u LEFT JOIN picture p ON u.id = p.userid
and
SELECT u.*,p.pic_path FROM friends f JOIN user u LEFT JOIN picture p WHERE f.friend_of = u.id AND f.friends_of=1
Notice, that the constraint that may be violated has been made explicit by moving it from the general selection to the generation of the data combinations as a rule on how to create combinations. And yes, it's a shortcut instead of following through the idea of selection and projection.