Hello! I was given those tables(no data) and I am supossed to write sql code for the following. But I looks like I have a syntax error or there is something I have wrote wrong. (see images) (1) Create a trigger “insert_inventory” on table “Inventory”. The trigger is fired after a row is inserted in table “Inventory”. After a row is inserted in table “inventory”, the “itemid”, the insertion time, and the action is inserted in table “Inventory_history”. The action is set to ‘add an item’. The oldprice is set to null. Test your trigger by inserting a row into Inventory and displaying the contents of Inventory_history. (3) Create a trigger “change_price” on table “Inventory”. The trigger is fired before a change is made to the “Inventory” table. Before the price of an item is changed, the “itemid”, the item’s old price, the action, and the time of change are inserted into the table “Inventory_history”. The action is set to “price change”. Test your trigger by updating a row in Inventory and displaying the contents of Inventory_history.
Hello! I was given those tables(no data) and I am supossed to write sql code for the following. But I looks like I have a syntax error or there is something I have wrote wrong. (see images)
(1) Create a trigger “insert_inventory” on table “Inventory”. The trigger is fired after a
row is inserted in table “Inventory”. After a row is inserted in table “inventory”, the
“itemid”, the insertion time, and the action is inserted in table “Inventory_history”.
The action is set to ‘add an item’. The oldprice is set to null.
Test your trigger by inserting a row into Inventory and displaying the contents of
Inventory_history.
(3) Create a trigger “change_price” on table “Inventory”. The trigger is fired before a
change is made to the “Inventory” table. Before the price of an item is changed, the
“itemid”, the item’s old price, the action, and the time of change are inserted into
the table “Inventory_history”. The action is set to “price change”.
Test your trigger by updating a row in Inventory and displaying the contents of
Inventory_history.
Here, We have to create two SQL Triggers. Requirements for those are as following:
(1) Create a trigger “insert_inventory” on table “Inventory”. The trigger is fired after a
row is inserted in table “Inventory”. After a row is inserted in table “inventory”, the
“itemid”, the insertion time, and the action is inserted in table “Inventory_history”.
The action is set to ‘add an item’. The oldprice is set to null.
Test your trigger by inserting a row into Inventory and displaying the contents of
Inventory_history.
(3) Create a trigger “change_price” on table “Inventory”. The trigger is fired before a
change is made to the “Inventory” table. Before the price of an item is changed, the
“itemid”, the item’s old price, the action, and the time of change are inserted into
the table “Inventory_history”. The action is set to “price change”.
Test your trigger by updating a row in Inventory and displaying the contents of
Inventory_history.
Given table schema:
CREATE TABLE Inventory (
itemid VARCHAR(20) PRIMARY KEY,
name VARCHAR(30),
price DECIMAL(6,2),
quantity INT
);
CREATE TABLE Transaction (
transid INT AUTO_INCREMENT PRIMARY KEY,
itemid VARCHAR(20),
quantity INT,
time DATETIME,
FOREIGN KEY (itemid) REFERENCES Inventory(itemid)
);
CREATE TABLE Inventory_history (
id INT AUTO_INCREMENT PRIMARY KEY,
itemid VARCHAR(20),
action VARCHAR(20),
oldprice DECIMAL(6,2),
time DATETIME,
FOREIGN KEY (itemid) REFERENCES Inventory(itemid)
);
What is a SQL Trigger?
A SQL trigger is a special type of stored procedure that is automatically executed in response to certain database events, such as the insertion, deletion, or modification of data in a table. Triggers are attached to a specific table and are defined to execute in response to a specific database event.
Step by step
Solved in 2 steps