MySQL Employee Table Management Guide
MySQL Employee Table Management Guide
Rohith H P 1
MySQL - Create Users
• We can create a new user account using the CREATE USER statement in
MySQL. To execute this statement, the current account must have the CREATE
USER privilege or the INSERT privilege for the MySQL system schema.
Open terminal , type sudo mysql –u root then type password : vcetputtur
Following is the syntax of the MySQL CREATE USER statement −
CREATE USER 'user_name'@'host_name’ IDENTIFIED BY 'password’;
Where,
• user_name is the name of the user you need to create.
• hostname specifies the host from which the user can connect.
• password is the user's password.
Rohith H P 2
In the following query, we are creating a user named ‘rohith’ who can only connect
from the 'localhost' host and sets their password as ‘vcetputtur'. Make sure that you
have logged in with a user with admin privileges (root)
CREATE USER ‘rohith’@’localhost’ IDENTIFIED BY ‘vcetputtur’ ;
The output will be displayed as –
Query OK, 0 rows affected(0.12 sec)
VERIFICATION – using following query we can verify the list of users
SELECT USER FROM [Link];
The table will be displayed like this
USER
[Link]
[Link]
-----------------
root
rohith
Rohith H P 3
Granting Privileges(permissions) in MySQL
• We can grant all privileges to the created user using the GRANT ALL statement. This
allows you to give specific permissions to users for actions like accessing databases,
tables, and performing operations, such as SELECT, INSERT, or DELETE, on them.
• Following is the syntax to grant all privileges in MySQL −
GRANT ALL PRIVILEGES ON database_name.* TO ‘username’@’localhost’ ;
EX:
o USE database_name;
database changed
Rohith H P 5
CREATE TABLE statement is used to create
tables in MYSQL database
CREATE TABLE table_name(
column-1 datatype,
column-2 datatype,
column-3 datatype,
.....
...
column-N datatype );
Rohith H P 6
Create a table called Employee
Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL, COMMISSION)
Rohith H P 7
To check structure of Table
• The MySQL’s DESCRIBE or DESC both are equivalent. The DESC is the short
form of DESCRIBE command and used to dipslay the information about a table
like column names and constraints on column name.
Mysql> desc table_name;
• Above query generates FIELD, TYPE, NULL, DEFAULT and EXTRA
Rohith H P 8
INSERT THREE RECORDS
We can add new rows to an existing table of MySQL using the INSERT statement.
INSERT INTO table_name (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Rohith H P 9
• Verify the inserted records (Retrive all information from the table)
SELECT * FROM table_name;
mysql> COMMIT;
Verify………….
Insert One(new) record…
Verify….
mysql> ROLLBACK;
Verify…
Rohith H P 10
3. Add primary key constraint and not null constraint to
the employee table
Step 1: Add NOT NULL Constraint
Rohith H P 11
4) Insert null values to the employee table and verify the result.
Rohith H P 12
DELETE and UPDATE
DELETE FROM table_name UPDATE table_name
WHERE condition; SET column1=value1,
column2=value2,
delete from employee where …………
ename=‘XXXX’; WHERE condition;
Rohith H P 13
Experiments -2
Create a table called Employee that contain attributes EMPNO, ENAME,
JOB, MGR, SAL and execute the following.
Rohith H P 14
Creating the Employee Table
mysql> CREATE DATABASE COMPANY02;
Query OK, 1 row affected (0.16 sec)
Rohith H P 16
(2). Insert any FIVE records into the table
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION) VALUES
( (101, 'Radha Bai', 'Manager', NULL, 5000.00, 1000.00),
(102, 'Krishna Kumar', 'Developer', 101, 4000.00, NULL),
(103, 'Abdul Sattar', 'Salesperson', 102, 3000.00, 500.00),
(104, 'Bob Johnson', 'Accountant', 101, 4500.00, NULL),
(105, 'Amartya Sen', 'HR Manager', 101, 4800.00, 800.00) );
Query OK, 5 rows affected (0.12 sec)
Records: 5 Duplicates: 0 Warnings: 0
(5). Syntax for delete operation delete from table_name where condition;
mysql> delete from Employee
-> where empno = 105;
Query OK, 1 row affected (0.14 sec)
Rohith H P 21
• Procedure :
➢ Create the database COMPANY03.
➢ Switch to created database using USE command.
Rohith H P 22
➢ Populating the employee table with 12 records.
➢ Find the maximum age of employee [display the maximum age among the
employees].
Rohith H P 24
➢ Find the minimum age of employee [display the minimum age among the employees].
➢ Find salaries of employees in ascending order [sorts the employees based on their salaries in
ascending order].
➢ Find grouped salaries of employees [groups employees by their salaries and counts the
number of employees for each salary]
Rohith H P 26
➢ Login to mysql as root
➢ sudo mysql –u root
➢ Enter password : vcetputtur
➢ create a database
➢ use database
➢ create customer table
➢ check the table description
➢ Insert 5 records to customer table
➢ display the extension of customer table ( select * from customer; )
➢ create trigger for insert
➢ execute the trigger
➢ Insert new record
➢ Execute the after insert trigger
➢ create update trigger
➢ Update one row in the table using update command
➢ Execute after update trigger
➢ Create delete trigger
➢ delete one row from customer table and execute the after delete trigger.
Rohith H P 27
$ sudo mysql –u root
Enter password : vcetputtur
Rohith H P 28
mysql> CREATE TABLE CUSTOMERS (
-> ID INT PRIMARY KEY,
-> NAME VARCHAR(25),
-> AGE INT,
-> ADDRESS VARCHAR(50),
-> SALARY DECIMAL(10, 2)
-> );
Query OK, 0 rows affected (0.49 sec)
Rohith H P 29
insert into CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)
values(1,’Avinash’,24,’Mangaluru’,salary);
Rohith H P 30
To achieve the desired functionality of capturing changes
on INSERT, UPDATE, or DELETE operations
and displaying the salary difference in MySQL, you’ll need to create
separate row-level triggers for each operation
(INSERT, UPDATE, DELETE).
These triggers will capture the OLD and NEW values of
the SALARY column and display the salary difference when an INSERT,
UPDATE, or DELETE operation occurs.
Rohith H P 31
Create trigger for INSERT operations
DELIMITER //
Query ok,…
//
Rohith H P 34
mysql> -- test INSERT TRIGGER
mysql> INSERT INTO CUSTOMERS (ID,NAME, AGE, ADDRESS, SALARY)
-> VALUES (6,'Shankara', 35, ‘Puttur', 50000.00);
Query OK, 1 row affected (0.14 sec)
mysql>
mysql> SELECT @my_sal_diff AS SAL_DIFF;
//
+---------------------------------+
| SAL_DIFF |
+----------------------------------+
| salary inserted is 50000.00 |
+----------------------------------+
1 row in set (0.00 sec)
Rohith H P 35
mysql> -- test UPDATE TRIGGER
mysql> UPDATE CUSTOMERS
-> SET SALARY = 55000.00
-> WHERE ID = 1;
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
mysql> SELECT @my_sal_diff AS SAL_DIFF;
//
+-------------------------------+
| SAL_DIFF |
+-------------------------------+
| salary deleted is 55000.00 |
+-------------------------------+
1 row in set (0.00 sec)
Rohith H P 37
Each operation (INSERT, UPDATE, DELETE) will trigger the respective trigger
(after_insert_salary_difference,
after_update_salary_difference,
after_delete_salary_difference), which will display the salary change or difference
associated with that operation.
By using separate triggers for each operation and utilizing
the OLD and NEW keywords appropriately within the trigger bodies, you can
effectively capture and handle changes to the SALARY column in
the CUSTOMERS table in MySQL. You can adjust the trigger logic and message
formatting as needed based on your specific requirements.
Rohith H P 38
CREATE TRIGGER display_salaary_changes
AFTER DELETE OR INSERT OR UPDATE ON customeers
FOR EACH ROW
WHEN ([Link] > 0)
DECLARE sal_diff number;
BEGIN
sal_diff = [Link] - [Link];
dbms_output.put_line('Old salary: ‘ ,[Link]);
dbms_output.put_line('New salary: ‘ ,[Link]);
dbms_output.put_line('Salary difference: ‘ , sal_diff);
END;
//
Rohith H P 39
Experiment-5
Create cursor for Employee table & extract the values from the table.
Declare the variables, Open the cursor & extract the values from the
cursor. Close the cursor.
Employee(E_id, E_name, Age, Salary)
Rohith H P 40
Procedure
1. Creating the Employee Table and insert few records
mysql> CREATE DATABASE COMPANY05;
Rohith H P 43
-- Check if no more rows to fetch
IF @finished = 1 THEN
LEAVE cursor_loop;
END IF;
// DELIMITER ;
Rohith H P 44
3. Execute stored procedure
mysql> CALL fetch_employee_data();
+-------------------------------------------------------------------------------+
| Employee_Info |
+-------------------------------------------------------------------------------+
| Employee ID: 1, Name: Samarth, Age: 30, Salary: 50000.00 |
+-------------------------------------------------------------------------------+
1 row in set (0.07 sec)
+---------------------------------------------------------------------------------------+
| Employee_Info |
+---------------------------------------------------------------------------------------+
| Employee ID: 2, Name: Ramesh Kumar, Age: 25, Salary: 45000.00 |
+---------------------------------------------------------------------------------------+
1 row in set (0.07 sec)
+------------------------------------------------------------------------------------+
| Employee_Info |
+------------------------------------------------------------------------------------+
| Employee ID: 3, Name: Seema Banu, Age: 35, Salary: 62000.00 |
+------------------------------------------------------------------------------------+
1 row in set (0.07 sec)
+-----------------------------------------------------------------------------------+
| Employee_Info |
+-----------------------------------------------------------------------------------+
| Employee ID: 4, Name: Dennis Anil, Age: 28, Salary: 52000.00 |
+-----------------------------------------------------------------------------------+
1 row in set (0.07 sec)
+--------------------------------------------------------------------------------------+
| Employee_Info |
+--------------------------------------------------------------------------------------+
| Employee ID: 5, Name: Rehman Khan, Age: 32, Salary: 58000.00 |
+--------------------------------------------------------------------------------------+
1 row in set (0.07 sec)
Rohith H P 45
A cursor is a select statement, defined in the declaration section in
mysql. Cursor is a database object that allow us to retrieve and
manipulate each row one at a time. It is nothing more than a row
pointer. It is always used alongside a SELECT command.
1. Declare the cursor and variables
DECLARE cursor_name CURSOR FOR
Select statement;
cursor_name: name of the cursor
select_statement: select query associated with the cursor
CALL fetch_employee_data();
Rohith H P 48
Experiment-6
Write a PL/SQL block of code using parameterized
Cursor, that will merge the data available in the newly
created table N_RollCall with the data available in the
table O_RollCall.
If the data in the first table already exist in the second
table then that data should be skipped.
Rohith H P 49
Solution :
To accomplish this task in MySQL, we can use a stored
procedure with a parameterized cursor to merge data
from one table (N_RollCall) into another table (O_RollCall)
while skipping existing data.
We’ll iterate through the records of N_RollCall and insert
them into O_RollCall only if they do not already exist.
Rohith H P 50
1. Create the tables
Let’s create two tables N_RollCall and O_Rollcall with similar structures.
• USE ROLLCALL;
Let’s insert some sample data into the N_RollCall table, including records that are common with O_RolCall
INSERT INTO N_RollCall VALUES (1, 'Shivam', '1995-08-15’); -- Common record with O_RollCall
INSERT INTO N_RollCall VALUES(2, ‘Bharath', '1998-03-22’);
INSERT INTO N_RollCall VALUES (3, ‘Chethana', '1990-12-10’); -- Common record with O_RollCall
INSERT INTO N_RollCall VALUES(4, 'Devendra', '2000-05-18’);
INSERT INTO N_RollCall VALUES(5, 'Eshwar', '1997-09-03');
Rohith H P 52
3. Define the stored procedure
Let’s define the merge_rollcall_data stored procedure to merge records from
N_RollCall into O_RollCall, skipping existing records.
DELIMITER //
Rohith H P 53
-- Declare handler for cursor
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET done = TRUE;
END
//DELIMITER ;
Rohith H P 55
Rohith H P 56
4. Execute the stored procedures
Execute the merge_rollcall_data stored procedure to merge records from N_RollCall into
O_RollCall while skipping existing records.
Rohith H P 57
+----------------+--------------------+----------------+
| student_id | student_name | birth_date |
+----------------+--------------------+----------------+
| 1 | Shivam | 1995-08-15 | <-- Common record, not duplicated
| 2 | Bharath | 1998-03-22 | <-- New record from N_RollCall
| 3 | Chethana | 1990-12-10 | <-- Common record, not duplicated
| 4 | Devendra | 2000-05-18 | <-- New record from N_RollCall
| 5 | Eshwar | 1997-09-03 | <-- New record from N_RollCall
+---------------+---------------------+-----------------+
5 rows in set
Rohith H P 58