Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

> desc table1; +-------+--------+------+-----+---------+----------------+ | Fiel

ID: 3920440 • Letter: #

Question

> desc table1;
+-------+--------+------+-----+---------+----------------+
| Field | Type   | Null | Key | Default | Extra          |
+-------+--------+------+-----+---------+----------------+
| id    | int(5) | NO   | PRI | NULL    | auto_increment |
| var   | int(5) | YES |     | NULL    |                |
+-------+--------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

> desc table2
    -> ;
+-------+--------+------+-----+---------+----------------+
| Field | Type   | Null | Key | Default | Extra          |
+-------+--------+------+-----+---------+----------------+
| id    | int(5) | NO   | PRI | NULL    | auto_increment |
| var1 | int(5) | YES |     | NULL    |                |
| var2 | int(5) | YES |     | NULL    |                |
+-------+--------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

> DELIMITER $$
> CREATE TRIGGER total BEFORE INSERT ON table2 FOR EACH ROW BEGIN set NEW.var2 = table1.var * new.var1; END$$
Query OK, 0 rows affected (0.11 sec)
> DELIMITER ;

]> INSERT INTO table2 (var1) VALUES('5');
ERROR 1109 (42S02): Unknown table 'table1' in field list

How come I cannot insert. I tried many different things, it still cannot insert.

Explanation / Answer

If you have any doubts, please give comment...

DELIMITER $$

CREATE TRIGGER total BEFORE INSERT ON table2

FOR EACH ROW SET NEW.var2 = (

SELECT var * NEW.var1

FROM table1

WHERE id = NEW.id

);

END$$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote