Delete and Insert vs Update rows on table [closed] - php

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 5 years ago.
Improve this question
I have a web form in when the user can select many available items (idFamilia) for a single Provider (idProveedor) that looks like this
The values are saved in this table
-----------------------------
| idProveedor | idFamilia |
-----------------------------
| 5 | 1 |
-----------------------------
| 5 | 2 |
-----------------------------
| 6 | 2 |
-----------------------------
After a Provider is created, it can be edited and the values selected for idFamilia can be changed. So I have three different scenarios of what could happen:
The numbers of rows remain the same and I would only need to edit the values of idFamilia.
Some items were deleted from the selection and I would need to search the values on the table and delete those rows.
New items are added and I would need to insert those additional values.
Right now I am thinking about just deleting all the rows with the idProveedor that is being edited and just insert the new selection.
Is this a good practice? Will this affect the performance on the long run?
Could you recommend a tutorial or example to do it the other way?

I always go the way to delete and re-create them.
While this adds some performance penalty, it keeps the code simple to understand and easy to maintain. Unless you're Uber, Google, Facebook, Amazon, or other world scale app, the benefits you'd get from optimizing this are much smaller than the cost it incurs: in development, debugging and maintenance time.

Related

Best SIMPLE database structure for my web app with resources, categories, and tags [closed]

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 have been teaching myself to code for about 6 months now and am building my 1st web app for practice.
I'm aiming to build my own very simple cms to store my resources in the database and serve them back up on the page.
I will have resources -
each resource will be assigned to only 1 of 3 categories -
each resource can have multiple tags.
Ultimately the user can navigate to each category page and on that page filter the resources by tag.
I am struggling with how to design the DB.
Without a doubt I will have a resources table.
Should I store the category assignment directly in this table (since each resource can only have 1) or is it better to create a relationship with a category table for this? I've done a lot of research on tag schema's and some seem a bit too complicated for me at this beginner stage. What is the simplest way to store multiple tags for a resource. 2 tables? How would this be accomplished?
I am using PHP and mySQL.
For the category, since you have only one per resource, store on the same table. This is a one to one relationship.
For tagging, you might need two additional tables because this is a many to many relationship (each resource can have multiple tags and each tag can be assigned to multiple resources). One table is to store the tag name and its ID. Another table is to store the relations. This one has two columns, one for the tag ID and other for the resource ID.
Quick example:
id | resource | category
---+----------+----------
1 | rice | 24
2 | apple | 42
id | tag
---+-------
1 | fruit
2 | cereal
3 | food
id_t | id_p
-----+------
2 | 1
1 | 2
3 | 1
3 | 2
Then you can query the tables and JOIN them to get the desired results.

MySQL query to get one of each Item [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working with php and MySQL, I am making a messaging app, the app has communicates with a remote database, in my database is my messaging table, the table has 9 tables of which includes the "Subject table", I need help, I want to make a query that retrieves only one subject of each type, lets say I have 10 messages with the subject "Man", 12 of the "Dog", I want it to get only one man and one dog below is a graphic representation of my Messaging Table.
| message_id | subject | username |
|:-----------|------------:|:------------:|
| 1 | cyber | Chrome |
| 2 | Hyper | Ciare |
| 4 | Cyber | Gorger |
You can use SQL's DISTINCT:
SELECT DISTINCT subject FROM table
This will fetch each subject only one time, even if it appers more than once.

How to store daily hits for items using MySQL and PHP? [closed]

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 7 years ago.
Improve this question
In my MySQL database, I'm trying way of creating a table to track daily views on 100 items. Each day would insert a new row for each item. So the table columns would look like this:
| ItemID | ItemName | Hits |
Is there a way to store daily hits for items?
What you could do is have a table with a structure like:
| item_hits |
|---------------|
| itemID | date |
Then your SQL query could look like
SELECT count(*) FROM item_hits WHERE date = '2015/04/15' and itemID = 7;
You wouldn't need to save the itemName in the table, because you should be able to get the itemName by joining it to your items table.

How would a database structure for a quiz look like? [closed]

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 9 years ago.
Improve this question
Here's how my current quiz database structure looks like:
For the sake of testing and simplicity, I've put up only 3 questions.
This is how my table (quiz) looks like:
| id | score | q1_answered | q2_answered | q3_answered |
--------------------------------------------------------
| 1 | 210 | 0 | 1 | 0 |
| 9 | 380 | 1 | 0 | 1 |
| 5 | 210 | 1 | 1 | 1 |
--------------------------------------------------------
Each question column has numbers from 0 to 1, 0 indicating is hasn't been answered and 1 indicating it has been answered correctly.
I was wondering if any of you have another idea of creating a better structure or better, efficient.
Is there also an efficient way to add a timestamp for a user who has solved a specific question?
Say for example: If a user answers question 1, the time of answer will be recorded and will be displayed.
This structure could work but I always thought it might not be efficient if I were to add say 50 more questions.
Any help would be appreciated.
I would create two tables based on the table you have:
scores
sID score testID
1 210 1
9 380 1
// etc etc
quests
sID testID question answer anstime
1 1 1 0 1231237128961
1 1 2 1 1231237128964
1 1 3 0 1231237128968
9 1 1 1 1231237128961
9 1 2 0 1231237128968
Basically, you are normalizing your data in a way that you can easily add more questions without the need to modify the table in any way, you still have a nice clean table where you keep scores that is easy to join
You can also then do much more interesting queries, like How many people answered question 3 at a certain time, aggregate the data nicely and of course, still join it back to the scores if you want to display all the results for a particular ID.
I have also added two columns in the tables called testID. This way, you are able to track not only multiple users for one test, but multiple users across multiple test. you will be able to see whether a student is improving over the course of tests or progressively getting worse as the subject carries on.
Edit: To copy data from your structure will be a bit annoying, but this should get you started on the path at least:
insert into quests (sID, question, answer)
select sID, testID, q1_answered from yourTableName
You can insert the data into the new structure by using a select statement on your original tables like I showed you above.
In a nutshell, you define one table for the questions and one for the answers given by users:
Questions:
QuestionID, QuestionName, ...
Answers:
AnswerID, UserID, QuestionID, AnswerGiven, AnswerCorrect, DateAnswered
This way if you add a new question, you don't have to change the table structure.
I wouldn't worry about efficiency with 50 questions. Just have some fun programming. Try things out. If you have a million questions, or hundreds of thousands of questions you might start to think about efficiency. Computers are really fast.

Form show select box after choice option on select [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I intend that after selecting an option from a list, a new list appears according to the option selected.
But the second select list, sends a query to the database, depending on the choice list in the first select!
example:
table bd
| A | 1 |
| A | 2 |
| B | 3 |
| B | 4 |
first select
A or B
second select
if select A
1
2
if select B
3
4
A simple search for a filtered dropdown list brought me to the following code on GitHub which does a decent job explaining how to program one of these lists on your own. You can find it here.
Hope this helps.

Categories