Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've got a question about storing information in a mysql database.
If i have user information, should i have separate tables to store each type of information? Or should I use 1 single table?
Let's say I have user email, username, password, first name, last name, address, gender etc...
Should I have 1 table to store email, username, and password, but another table for firstname, lastname, address and gender? Perhaps a separation from user info and account info? What do you think?
I'm not sure from a query/performance standpoint if there would be any difference by splitting up. Also, a JOIN query using the assoc. index should be able to link both tables up by User ID or some other auto increment value. Not sure what to do here!
Thanks!
Unless you have a very good reason, use proper database normalization techniques and have one table per entity type.
You're talking about a user here, so unless user and person are fundamentally different you should store that in one table.
If one user can have multiple logins, which is hopefully not the case as it tends to confuse people, then you may want to separate that out one-to-many. Otherwise, use the simplest thing that could possibly work.
As always, follow the advice in a guide book like PHP The Right Way paying particular attention to the part about password security.
Using a
development framework like Laravel is an even better plan as that has a security model built-in that you can use.
Depends on the functionality of your application / functionality.
But, you should normalise your datamodel.
Have a look at Codd, http://en.wikipedia.org/wiki/Database_normalization
How many records do we talk about? Even with a few thousand, its not something to worry for. Once you get into 100K + then joins (and indexes) are getting important. And then you want to make sure your datamodel is normalised.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a small side project on which guests can vote on some sort of posts with either agree or disagree, then I want to save the IP that voted in the database. I was thinking of saving an array of IP's that voted in the 'posts' table, however that might be insufficient I guess. Would a separate table like the following be recommended?;
post_votes:
post_id, IP
Doesn't matter if the user voted agree or not, as long as the vote is counted they won't be able to vote again. What's the best approach for this? Thanks in advance.
Yes, the best way is to use a seperate table with identifiers (e.g. post_id) to identify the posts.
Never save more than one information per field - saving the information in an array in the same table is cruel.
However, do NOT store the IP address 'as is' in the database as this is illegal in many countries and can cause a lot of trouble!
Instead use a hashing function like hash_hmac (http://php.net/manual/de/function.hash-hmac.php), hash the IP address and save the hash instead..
Because hashes are one-way functions, this will ensure anonymity for the user but still give you the opportunity to prevent users from voting several times.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm building a website that contains articles, these articles won't be shown until the user is logged in, I'm planning to add two buttons, the first one is called "follow this article", the second is meant to make the article as a favourite. My question is how can I structure that in my database?
Should I just add a table that contains id_article, id_user, followed(1 or 0) and favourite (1 or 0), or is there another solution that would let me economize more memory space?
NOTE: My website will have lots of articles that will be visited by a lot of users.
Make two separate relations: articles_followed (id, id_article, id_user) and articles_favourite (id, id_article, id_user). This will give you enough flexibility in:
adding new attributes, specific for each relation,
sharding your database (at some point): it'd be easier to move one relation from another since they are not depending on each other,
concurrent update: you can use separate locks on these two, in addition, you'll have fewer issues with transaction visibility,
you don't need to keep is_followed or is_favourite flag: if there's an entry, then it means that article is followed (once unfollowed, simply remove the line)
Think about your domain classes structure -- which option would be more convenient to code? I bet, in case of two separate relations, it would be clearer.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've a large users table on my database with 80k users which cause mostly login problems.
I have a solution! Don't know whether it's a good one or not!
I want to create other users tables for new users and save last IP of them in it.
So in most cases of login by knowing Ip range I can redirect users to the right table in login. If it's not the right table then it will check other tables.
So I think it will increase speed!
Is it a good solution!? I will be appreciated if anyone come up with a better solution!
Table Structure
Is it a good solution!?
No, it is not a good solution. It is, in fact, a terrible solution.
From your table definition, we can guess that your users give their email addresses to log in.
Try creating an index on that column.
ALTER TABLE ci_users ADD INDEX index_email (email);
Once you do that, queries of the form
SELECT something FROM ci_users WHERE email = 'user-supplied-value'
and
SELECT something FROM ci_users WHERE email LIKE 'user-supplied-value%'
will start to be very fast, almost as if by magic. That's DBMS technology at work.
You should read about indexing. http://use-the-index-luke.com/ is a good source.
If you will have some users supplying a phone number, and others providing a email address, you need two different indexes, one for each.
A typical table can have as many indexes as you need. With a table that size, you can create them easily without disrupting production. But don't create indexes until you know you need them.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am creating a website where users can easily calculate the calories they eat and see the repartition in term of fat, carbo, etc.
I want the users to be able to retrieve data from previous days.
I then need to store the data sent by my users everyday (basically, they input how much of each food they have eaten everyday and I am making the calculation then store the results).
The question if the following: what would be the best way to store the data? I have to store the data for each user for each day. I can't think of a simple solution (I think creating a new table for each new day would not be great, would it?).
I'm using PHP and MySQL for now.
Thanks for the help!
It seems that you are a step ahead of your self with the daily breakdown question.
First, you need to decide what you need to store, e.g. fields and normalise the way they are stored.
For example, you would have the following tables:
Users:
Id
..
EatItems:
UserId
ProductId
Calories
Fat
DateTime
Once you have these tables up and running, you can build reporting layer on top of that to breakdown consumption by user / date or anything else you might be interested in.
You could have a table that holds the input/calculated data/date which relates to a user/account.
When the user views previous day's, select the data that relates to that user.
I wouldn't create a table for each day. One table would suffice.
However, I would suggest attempting something and posting the code for specific issues you have if you run into before posting here.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Wish you a peaceful and healthy new year. **
I am working on a survey database design for mysql/php/wordpress for estimated 10,000,000 users. Each user will eventually answer about 5,000 questions over a course of several years. These questions are answered mainly as scale of : AGREE, NEUTRAL, DISAGREE, DONT KNOW as multiple choice answers. There is no right or wrong answer. A user would be able to attempt the questions again in the future. Also, at each attempt his/her answer_record gets updated with new data. Would the following database design be reasonable from database performance and data normalization perspective? Thank you in advance.
TABLE_USER:
user_id
username
user_email
[other user specific fields]
TABLE_QUESTION:
question_id
question_text
question_image
question_category1 [A question may exist in more than 1 category]
question_category2
question_category3
TABLE_ANSWER:
answer_id
user_id
question_id
answer_agree
answer_neutral
answer_disagree
answer_dontknow
answered_datetime
answer_number_of_attempts
Sincerely,
Harrison.
Part of proper db design means stepping back and making sure that if you added one more thing, you wouldn't have to redesign the tables, and also that discrete types of information are separated out. If several columns are doing the same thing (but recording different answers) you should split them out into another table and have a single link table.
Also, do you really need to say Table in the table name? of course it's a table, what else would it be?
TABLE_USER is fine
for TABLE_QUESTION you should drop the category columns, instead make a new table
TABLE_CATEGORIES
with information on the different categories
and have another table
CATEGORIES_PER_QUESTION
question_id
category_id
that allows a question to have any number of categories, you can look which ones each question has by querying categories_per_question
TABLE_ANSWER should be split into two tables,
RESPONSE
response_id
user_id
question_id
answer_id
datetime_responded
and ANSWERS
answer_id
answer_name
Where answer name is AGREE, NEUTRAL, DISAGREE, DONT KNOW or any other sort of answer you might provide.
If you wanted to be fancy, you could even have another join table between ANSWERS and TABLE_QUESTION, indicating what answers will be available per question.
To know the number of attempts, and other information I dropped, you can query the DB so it doesn't need a column for itself.
I realize you want help with DB design, but even if the design is perfect, this will fail to scale reasonable if your system is not planned properly for scaling (BIG).
A proper designed API can scale endlessly.
With those numbers it will be cheaper overtime to have this external and build an API for it so you can scale properly. Building something directly into WordPress will require you to scale to quickly in all directions just for running PHP, HTTP and MySQL.
If you build an API in between WordPress and your survey database you can then scale MySQL and build any number of systems in between, Memcache, search engines, etc...
This will give you better separation between your systems allowing for more efficient scaling.
Scaling each only when it needs it.
So I would plan your system/infrastructure at this point also.