-- Create the original table CREATE TABLE employees1 ( employee_id INT PRIMARY KEY, name VARCHAR(100), position VARCHAR(100), salary DECIMAL(10, 2) ); -- Insert data into the original table INSERT INTO employees1 VALUES (1, 'MOHAN', 'DEVELOPER', 20000); INSERT INTO employees1 VALUES (2, 'KAVIN', 'TESTER', 30000); INSERT INTO employees1 VALUES (3, 'SELVA', 'HELPER', 45000); INSERT INTO employees1 VALUES (4, 'NITHI', 'SQL TECH', 30000); INSERT INTO employees1 VALUES (5, 'KASI', 'PLSQL TECH', 25000); -- Check data in the original table SELECT * FROM employees1; -- Create the history table CREATE TABLE employees_history ( history_id INT AUTO_INCREMENT PRIMARY KEY, -- Correct auto-increment with primary key employee_id INT, name VARCHAR(100), -- Correct data type for MySQL position VARCHAR(100), -- Correct data type for MySQL salary DECIMAL(10, 2), updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- Add the missing column to store the timestamp ); -- Check data in the history table SELECT * FROM employees_history; -- Set the delimiter for trigger creation in MySQL -- Create the trigger to log old values before update CREATE OR REPLACE TRIGGER before_employee5_update BEFORE UPDATE ON employees1 FOR EACH ROW BEGIN -- Insert the old values into the history table INSERT INTO employees_history (employee_id, name, position, salary, updated_at) VALUES (OLD.employee_id, OLD.name, OLD.position, OLD.salary, CURRENT_TIMESTAMP); -- Use CURRENT_TIMESTAMP instead of SYSDATE() END; / -- Reset the delimiter back to the default -- Perform an update operation to test the trigger UPDATE employees1 SET name = 'John Doe', position = 'Software Engineer', salary = 70000 WHERE employee_id = 1; -- Check data in the original and history tables after the update SELECT * FROM employees1; SELECT * FROM employees_history;
Write, Run & Share PL/SQL code online using OneCompiler's Oracle PL/SQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for Oracle PL/SQL running on latest version 23c (23.3.0.0). Getting started with the OneCompiler's Oracle PL/SQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'PL/SQL' and start writing code to learn and test online without worrying about tedious process of installation.
PL/SQL is procedural extension for SQL created by Oracle. It is by default embedded into the Oracle Database. PL/SQL program units are compiled and stored inside the Oracle Database which results in optimal execution times as the PL/SQL and SQL run within the same server process.
Following is the syntax structure for the PL/SQL code blocks
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
DECLARE
message varchar2(100):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
CREATE OR REPLACE FUNCTION
hello_user
(user_name IN VARCHAR2)
RETURN VARCHAR2
IS
BEGIN
RETURN 'Hello ' || user_name;
END hello_user;
/
BEGIN
dbms_output.put_line(hello_user('Peter'));
END;
/
BEGIN
DBMS_OUTPUT.put_line (1/0);
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('error is: ' || SQLERRM);
END;