Good day to all!
I have 1 table (users) that I wish to use to update the uid field in another table (recipes). So, when a user creates a recipe, the uid should be inserted into the recipes table.
This is the trigger I created, followed by the error message:
create trigger users_recipes
after insert on recipes
for each row
update recipes
set uid = users.uid
where users.uid=recipes.uid
Error in INSERT into recipes (type, type2, rname, category, country, recipe, allergy, diet, path, pathvideo, posted) VALUES ('','','insert uid','','United States','blah
1/2 more blah','a:1:{i:0;s:3:"Nut";}','Omnivorous','','','2014-11-04') == ----->Can't update table 'recipes' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
If anyone could help me understand what I did wrong, I would greatly appreciate it.
Thanks so much in advance!
Related
So, I have a table A that each time a user sends an image, a record is created storing the time it was uploaded, the username of the user and the image number out of all the images uploaded over time.
I need to make a second table B that will store the amount of images uploaded per user and the user name. I need this table B to be updated when a new entry is generated in A.
I found that a trigger function can be created, nevertheless I'm having a rough time finding an example that will suit my needs.
Does anyone know a way of doin what I want?
Just update b table with a select count of total inserted records on a from current user NEW.userid (userid is your column name or whatever name you have there, and NEW is a fixed mySql reference for the current values to be inserted):
CREATE TRIGGER img_sum AFTER INSERT ON a
FOR EACH ROW SET b.total = (SELECT COUNT(*) FROM a WHERE a.userid=NEW.userid)
WHERE b.userid = NEW.userid;
From what you have described i don't think you need a second table. You can just count the number of time a user name has occurred, and you will get the number of images that user has uploaded.
You can get the count doing something like that
SELECT COUNT(DISTINCT username) FROM table_name;
If you still need to create 2 tables, you might want to take a look at procedures and how they work.
Let's say we have 3 tables for this case:
- users(id, username, email ....),
- user_images(id, userId, image_num, date_uploaded)
- user_images_count(id, user_name, images_count)
The user_images_count is initially empty. We have to fill it up by such query:
INSERT into user_images_count(user_name, images_count)
SELECT (select username from users where ui.userId = id) as username, count(userId) as counter FROM `user_images` ui group by ui.userId;
Then, we must immediately create the trigger that will process every INSERT operation into user_images table.
CREATE TRIGGER `count_user_images` AFTER INSERT ON `user_images`
FOR EACH ROW begin
declare u_name tinytext default "";
set u_name = (select username from users where id = NEW.userId limit 1);
if(u_name != "") then
update user_images_count set images_count = images_count + 1 where user_name = u_name;
end if;
end
This two queries (user_images_count fulfillment and trigger creation must be performed in one transaction, one by one).
I've created similar triggers on my local databases. They work pretty good. )))
I have database table kladilnica with 37 values, I want to make the users can send tickets each other, so I have created another table called ticket but that table have 38 values including receiverName so i can easy check who is the receiver and who can see the ticket. I'm going with w3schools example of copying table info and pasting into another table ex. ($sql = "INSERT INTO ticket (senderName, receiverName, Date) SELECT Username, '', Date FROM kladilnica WHERE ticket_id='".$ticketID."'";) this example works well but I have a minor modifications in my example. In the SELECT part i have nothing to select from the kladilnica to get the receiverName and display it into ticket what should I do to get and write the receiverName into ticket table?
$sql = "INSERT INTO ticket (senderName, receiverName, Date) SELECT Username, '".$receiverName."', Date FROM kladilnica WHERE ticket_id='".$ticketID."'";
(and add security for SQL injection of course...)
Okay so im new to databases, and have created a site with a users table, and i also hace a list table, where suers can insert list items, however when they log in everyones list is appearing, how can i link the user table to the lists table, is it creating the same field in each one and using a foreign key? Sorry I am very new to this. Appreciate any help
I think you can just use user_id on both tables to fix this. Let me give an example:
Table A (user_id, username, password)
Table B (list_item_id, user_id , any_other_attribute)
When you design your tables like this a simple sql call will do what you need like:
SELECT 'list_item_id','any_other_attribute' FROM Table B Where user_id=$user_id
Where $user_id is the user_id of the one's who loginned your system.
Also by your question, i suggest you to read about these : 'sessions' , 'sql queries' , 'generating sql query results' on your choice of programming language.
It calls MANY MANY relationnship. There mus be 1 table with fields user_id and field_id that will join this 2 tables
I want to insert data partially from select statement..
For example..
I have one table named movie..here's the fields :
-id_movie
-tittle
-studio
-time
And another table named reservation.. here's the fields :
-id_reservation
-tittle
-studio
-time
-seat_1
-seat_2
So, I want to insert value of tittle, studio, and time into reservation table that getting from select statement from movie table. But I'm a little bit confuse about another field in reservation table (id_reservation, seat_1, seat_2).. They're actually don't exist in the first table (movie table)
so how to write the query?
any simple example would be very helpful
thank you
$query = "INSERT INTO reservation(tittle,studio,time)
SELECT tittle,studio,time FROM movie";
INSERT INTO reservation (
tittle,
studio,
time
)
SELECT tittle,
studio,
time
FROM movie;
But you better not have NOT NULL on the columns you leaving without values.
If you want mysql query then:
"INSERT INTO reservation(tittle,studio,time)
SELECT tittle,studio,time from movie";
First do a SELECT from "movie" to obtain the fields you need from that table.
You will have the fields you need stored in 3 variables, say $title, $studio, $time.
As per the table reservation, the field id_reservation should be the PK so set it to autoincrement. You don't have to insert that value as it does it automatically in every insert.
The fields seat_1 and seat_2 will depend on other facts than the data you get from the first table. You should obtain the free seats (I guess there is another table with the total amount of seats), and select 2 using some kind of logic (for example, that they are next to each other)
So in the end, the INSERT query for the table reservation should look like
"INSERT INTO reservation (title, studio, time, seat_1, seat_2) VALUES('$title', '$studio', '$time', $seat_1, $seat_2)");
Been trying to wrap my head around this one, but have been unsuccessful.
Basically I am trying to create a database containing only two tables.
The first table is the logins table, and contains columns:
User ID
Username
Password
First Name
Last Name
The second table is the vote table, and contains columns:
User ID
Vote 1
Vote 2
Vote 3
etc. etc.
Is there anyway that I can relate these tables with the User ID as a primary key, that can cascade update/delete, so that when I add an entry into the logins table, it auto creates an entry in the votes table with the same User ID, and default value for all the vote columns?
I could add the votes to the main table, but wanted to seperate them out as much as possible, as not everyone will be creating votes.
Many thanks
Eds
If you want to cascade updates, you'll need to use a transaction:
START TRANSACTION;
INSERT INTO logins (username, passhash, salt, `first name`, `last name`)
VALUES ('$username', SHA2(CONCAT('$salt','$password'),512), '$salt',
'$firstname', '$lastname');
SELECT #userid:= LAST_INSERT_ID();
INSERT INTO votes (userid) VALUES (#userid);
COMMIT;
Make sure you have default values set in the table definition of the votes table.
Note the use of salted hashed passwords.
You could specify an insert trigger on the logins table.
This will execute each time a row is inserted into the logins table. You can specify what you'd like the trigger to do. In this case you can create the trigger to insert a single row into your vote table using the id just created in the login table along with your default values.
So something like:
CREATE TRIGGER logins_default_vote AFTER INSERT ON logins
FOR EACH ROW
INSERT INTO vote (UserID,Vote1,Vote2,Vote3)
VALUES (NEW.UserId,'vote1 default','vote2 default','vote3 default');