I'm planning to log referral clicks in mysql by the country of the IP.
Let's say my table is named referral_clicks and has the columns id, referral_id, country.
I have 2 approaches in mind now:
Create another column clicks which is set to +1 for every country / referral_id. This means that I would have to check first if the row for the specific referral_id and country already exists and if not, create it.
Insert a new row for every request. My concern here is, that the table might geht messy and too big, as might get very much referral requests.
What would be the best approach now for something like that, or is there evern a better approach?
Use INSERT ... ON DUPLICATE KEY UPDATE.
I suggest you create a table with the following columns.
id (autoincrement)
referral_id
country
clickdate
clicks
I suggest you create a unique index of (referral_id,country,clickdate).
Then, I suggest you use the following SQL each time you want to log a click:
INSERT INTO referral_clicks (referral_id, country, clickdate, clicks)
VALUES ('whatever', 'whatcountry', CURDATE(), 1)
ON DUPLICATE KEY UPDATE clicks=click+1
This will start a new row for each referral id, for each country, for each date. If the row already exists it will increment clicks for you.
Related
I have a table (People) with columns (id, name, year). Is there any way I can get all the ids ( SELECT id FROM people ) and use them for creating new rows in other table (people_id, additional_info). I mean that for every id in table People I want to add new row in other table containing the id and some additional information. Maybe something like for-loop is needed here ?
Well, usually you have information on a person and write one record with this information. It makes litte sense to write empty records for all persons. Moreover, when it's just one record per person, why not simply add an information column to the people table?
Anyway, you can create mock records thus:
insert into people_info (people_id, additional_info)
select id, null from people;
Insert into targetTable (id, additional_info)
select id,'additionalinfo' from PEOPLE
No for loop is needed, Just use the above query.
You can use INSERT ... SELECT syntax for MySQL to insert the rows into people_info table (documentation here).
e.g.
insert into people_info(...)
select (...) from people; <and posibly other tables>
I just was wondering if is it possible to associate an user to the data inserted in a table.
For example:
I have a table Customers with columns customerID, name and address. Is there any method in MySql to know what user has inserted each row in the table or must I add a column userID in the table Customer to add the user in my SQL Insert statement?.
If I have only one table, it is not a proble to add the userID column. But when i have multiple tables, maybe it becomes a messy task.
Thanx in advance.
If you are trying to find out the user_id caused the insert then NO, there is no other way than you storing it explicitly likewise you already have thought of.
If there is multiple tables for which you want to store the same information; then you can probably have a separate table where you can have the user_id column and can use a AFTER INSERT TRIGGER to insert the user id in this table.
No, no such functionality is provided by MySQL. You'll have to add a column for user_id in your table(s) and insert the user id yourself.
When someone inserts some information into a database, this table has two columns id_user_created (user that created that information) and date_created (date that was created). This kind information is a good practice to have in a table or is better to save in a logger table?
EDIT
Example: I have a table 'score' with this columns:
id_score, id_class, id_subject, id_teacher, id_student, score,
id_type_score, id_category_score, id_status, id_user_created, date_created
I like to use a separate table. That way, you can keep track of multiple changes to the record. I also specify whether it was record creation or record modification, and depending on the circumstances, the old value and the new value of each field.
My question might be better understood if I provide a (simplified) example of the scenario. I have two tables, Event and Course. The Event table contains EventID, CourseID, and EventDate. The Course table contains CourseID, and CourseName.
I would like to add a new Event. I understand that the query normally would be as follows:
INSERT INTO event
(EventID,CourseID,EventDate) VALUES ( NULL,'2','2011-03-01');
But instead I would like to update Event by providing the CourseName instead of CourseID. How would I go about doing this?
Edit: Apparently there isn't a way to do this with just a MYSQL query. I'm trying to do this in PHP and attempting to allow a user to update the Event record. Obviously it would be convenient for the user to add a new event based on the name of the course, preferably with a dropdown box.
Thanks
INSERT INTO Event ( EventID, CourseID, EventDate )
SELECT NULL, C.CourseID, '2011-03-01'
FROM Course C
WHERE C.CourseName = 'Course Name Goes Here';
This one one thing that you could try. Though you may want to include the date in your query or some other way to make sure you are uniquely identifying the record you are looking for.
Another thing that you could think about is storing the event id, when you bring up the reference to the event you would like the user to update. Then you would ensure that you are only updating the record that you intend to.
update Event set ....
where courseID=(
select courseID from Course
where CourseName like ''
)
I have a webapp that currently stores all of a user's searches into a search_log table. I now want to create another table called results_log that stores all the results we supply to the user. The search_log table contains a primary key called id_search and the results log table has the foreign key id_search, and one other field id_result. The id_searched field is an auto_incrementing field in both tables.
In my web app I would do the inserts in this sequential order:
insert into search_log table
insert into result_log table
I am worried this may cause a race condition. If user A and user B both finish the webapp and reach this part of the code at about the same time, is it possible that the order would go:
User A -> Insert into search_log
User B -> Insert into search_log
User B -> Insert into result_log
User A -> Insert into result_log
Since both tables are auto_incrementing on the id_search field, I'm worried User A and User B will have their data swapped. I also thought about querying for the id_search, but it seems like a even worse solution.
My question is:
-Is there a way to fix this race condition?
-Would one solution be inserting into two tables with one SQL query? Is this possible?
If those tables are related, then you should include the auto increment ID with when inserting. After inserting into search_log, get the last insert ID, no lookup needed. Then include that in the result_log search as another field.
Never rely on auto increment IDs being the same in different tables.