I made a mysql database for my project. I need to know if what I did is correct or not.
I made a master table to store all my movie details and I built some other tables to store the views and ratings. Am I doing wrong by using different tables to store views and ratings, although I can use the master table to store views and rating in each post.
If I add the rating and views fields to the master table itself, will it affect my database? What's the optimal solution?
I am using Myisam as lots of select queries are needed instead of insert.
If you are just using the fields for incremental value, you can use the master table.
But if you wish to save extra parameters to a vote or view, you should use a secundary table.
e.g. You want to save which user voted which value on which date. Than you should add a table 'votes' with the fields: voteId, userId, rating, date.
Afterwards you can use the AVG function to calculate the avarage vote.
Related
I have a table tblResponses which records responses received for each project in my database. Multiple responses per project, each with a date.
Another table tblActivity stores each activity on a project. Multiple activities per project.
I want to update each record in the Activity table with the date of the MOST RECENT response received for that project. If I use a GROUP BY query on tblResponses to get the Max(ResponseDate) grouped by projectID, I cannot then use this in an update query on tblActivity, as it makes the query not updateable.
At the moment I am having to populate a temporary table from the output of the GROUP BY query, and then use this in the Update query to update tblActivity. Not ideal as leads to database bloat etc, poor performance etc.
Is there any way to do this WITHOUT populating a temporary table? I understand why a Group By query cannot be updateable itself, but don't see why it cannot be used to provide the Update To values for updating another table.
(And yes, I know it shouldn't be necessary to store the result physically in a separate table when it could be calculated, but for various lengthy reasons, that isn't an option here.)
Many thanks for any help!
Jim
I have a database called Pricelist. In it there are several tables eg. Store1, store2, etc. Each has a list of the products. Most products exist in each store but some only exits in one or two stores. I want to have something that I can run within SQL (stored procedure?) That will find the lowest price of a product by checking the price from each store and then when it finds the lowest price, it will get that price, along with the UPC,Description, and other columns and put all that information in a final table called BestPrices. Then it do it all again for each product in every store. So when it's done. every product from all the stores should be listed once in the BestPrices table with the lowest price and the additional information from the other columns I chose. Is this possible? I know it's a lot. Thanks in advance.
You can create temporary/buffer table to collect data from tables store, then you can query from that table to get the lowest price.
If all you want is display the best prices, then an SQL View should do the trick. A View is enough to
find the lowest price of a product by checking the price from each
store ... along with other columns and ... do it all again for each
product in every store.
However, an SQL View is rendered at runtime and hence, you do not have the values stored in a table, per se. That is, the best prices are calculated as and when you invoke the view
If you want to have the values stored for future viewing, then you shall need a Stored Procedure which shall insert the values into a physical table.
In the case of using a procedure, you should take care as to what should happen to the existing data when you insert into the table.
Also, your post doesn't include details regarding the actual table structure or the common identifier for the product across all the tables. Those details can greatly influence the design and performance of the View/Procedure.
I have a database with two tables. The first one contains the user_name, user_password, user_email. The second one contains the user_name, user_age, user_description.
When a person finds the user he needs by the user_name, the script looks through the database using the user_name, to give out the information about certain user.
But if the person changes his user_name via preferences, the value changes only in the first table.
Question:
1) Is there a way to make the user_name in the second table change automatically? (To connect them some how)
I am using MySQL (phpMyAdmin).
This is just a simple example. In "real world" I am trying to manage more serious applications that have more tables. Is there an easier way than to create a separate php query for each table?
You could always create an AFTER UPDATE MySQL trigger targeting single rows for this. See the manual. It's probably not easier than using separate PHP queries for the tables, though. You don't need to spell them all out, just map up what needs to be synchronized when, and abstract your code.
However I'd recommend that you use a unique ID field for the user and only store the username in one of the tables -- and refer to the user with the ID under the hood of your code, and in both tables. Not a good idea to use something changeable as a unique identifier in your database design.
I have statistics for every user in our system. These statistics need to be updated frequently.
The statistics looks something like this:
Campaign : Performance
Campaign : Performance
Each campaign has it's own data stored in another table in MYSQL. The performance metric needs to be updated for each user and each campaign frequently. There are over 1000 campaigns that need to be updated for over 5000 users.
At first I considered simply making a huge VARCHAR column which just stored the data as "campaignId-Performance,campaignId2-Performance,..."
When querying the DB these campaigns will be searched for by their COUNTRY first and then ordered in descending order by the highest performing. The COUNTRY data is stored in the campaigns table. In my new table I was considering making a column for every COUNTRY and splitting the campaigns with their respective performances up like that. I'm unsure whether that would be more efficient or not.
With this poor description I'm hoping someone can suggest an optimal structure. Thanks!
I would think you would want to store this performance in a table with 3 columns: campaignID,userID,performance. This would give you the most flexibility to then query the information however you might need it. Would easily aggregate performance by campaign, or by user. and with a quick join to the campaign table would allow you to aggregate by country as well.
So, your building a MySQL database that contains 1000 campaigns and 5000 users? Do the campaigns contain the performances of each country? If so, I would suggest making a column named Country followed by Performance Column. Also, it would be helpful to know what your trying to view the information in.
I have 200 users each user will eventually have a "reviewINFO" table with certain data.
Each user will have a review every 3 to 4 months
So for every review, it creates a new row inside the "reviewINFO" table.
This is where i'm stuck. I'm not sure if I need to serialize a table inside each row or not.
Example:
-> links
"USER1reviewINFO"-row1->USER1table1
-row2->USER1table2
-row3->USER1table3
-row4->USER1table4
-row5->USER1table5
"USER2reviewINFO"-row1->USER2table1
-row2->USER2table2
-row3->USER2table3
-row4->USER2table4
-row5->USER2table5
using this method it will make a couple of thousand rows within two years. And I think its harder to manage.
"Userxtablex" is a table with dynamic rows of children names,ages,boolean
What i'm think of doing is serialize each USERxtable into its corresponding row.
Please help as I would not like to make this complicate or inefficient
Generally, you should never have to serialize data of this nature into a table row to accomplish what your goal is (which I am assuming is an implicit link between a user and a review)
What you need to do is key the reviews by a user_id such that all the reviews are packaged in one table, and relate numerically back to the users table.
Assuming you have an AUTO_INCREMENT primary key in the user table, all you would need is a user_id field in the reviews table that represents what user the review relates to. There is no need for a separate structure for each user, if that's what you are suggesting. Reviews can have date fields as well, so you can perform queries for a specific year or window of time.
You can then use a JOIN query to select out your data set relating to a particular user or review, and apply the usual WHERE clause to determine what result set you want to fetch.