0% found this document useful (0 votes)
15 views38 pages

SQL Basics: Commands and Constraints

The document provides an overview of SQL, including its definition, commands, data types, and integrity constraints. It explains how to create and modify tables, use various SQL commands such as INSERT, UPDATE, DELETE, and the importance of constraints like primary keys and foreign keys. Additionally, it includes practical experiments demonstrating SQL operations, including creating tables, inserting records, and using triggers.

Uploaded by

rajatnyamati1206
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views38 pages

SQL Basics: Commands and Constraints

The document provides an overview of SQL, including its definition, commands, data types, and integrity constraints. It explains how to create and modify tables, use various SQL commands such as INSERT, UPDATE, DELETE, and the importance of constraints like primary keys and foreign keys. Additionally, it includes practical experiments demonstrating SQL operations, including creating tables, inserting records, and using triggers.

Uploaded by

rajatnyamati1206
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Database Basic Concepts

Introduction to SQL

SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel –
(Structured English Query Language)”. It is a query language used for accessing and modifying
information in the database. IBM first developed SQL in 1970s. Also it is an ANSI/ISO standard.
It has become a Standard Universal Language used by most of the relational database
management systems (RDBMS). Some of the RDBMS systems are: Oracle, Microsoft, SQL
server, Sybase etc. Most of these have provided their own implementation thus enhancing its
feature and making it a powerful tool. Few of the SQL commands used in SQL programming are
SELECT Statement, UPDATE Statement, INSERT INTO Statement, DELETE Statement,
WHERE Clause, ORDER BY Clause, GROUP BY Clause, ORDER Clause, Joins, Views,
GROUP Functions, Indexes etc.

SQL Commands

SQL commands are instructions used to communicate with the database to perform specific task
that work with data. SQL commands can be used not only for searching the database but also to
perform various other functions like, for example, you can create tables, add data to tables, or
modify data, drop the table, set permissions for users.

CREATE TABLE Statement

The CREATE TABLE Statement is used to create tables to store data. Integrity Constraints like
primary key, unique key and foreign key can be defined for the columns while creating the table.
The integrity constraints can be defined at column level or table level. The implementation and
the syntax of the CREATE Statements differs for different RDBMS.

The Syntax for the CREATE TABLE Statement is:

CREATE TABLE tablename (column_name1 datatype constraint, column_name2 datatype, ...

column_nameN datatype);

SQL Data Types:

[Link](size) =Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.
2.Varchar2(size)=Variable-length character string. Max size is specified in parenthesis.

[Link](size) or int =Number value with a max number of column digits specified in
parenthesis.

[Link] =Date value in „dd-mon-yy‟. Eg., ‟07-jul-2004‟

[Link](size,d) or real =Number value with a maximum number of digits of "size" total, with a

maximum number of "d" digits to the right of the decimal.

SQL Integrity Constraints:

Integrity Constraints are used to apply business rules for the database tables. The constraints

available in SQL are Foreign Key, Primary key, Not Null, Unique, Check.

Constraints can be defined in two ways:

1. The constraints can be specified immediately after the column definition. This is called
column-level definition.

2. The constraints can be specified after all the columns are defined. This is called table- level
definition.

1) Primary key:

This constraint defines a column or combination of columns which uniquely identifies each row
in the table.

Syntax to define a Primary key at column level:

Column_namedatatype [CONSTRAINT constraint_name] PRIMARY KEY

Syntax to define a Primary key at table level:

[CONSTRAINT constraint_name] PRIMARY KEY (column_name1,

column_name2...)
2) Foreign key or Referential Integrity:

This constraint identifies any column referencing the PRIMARY KEY in another table. It
establishes a relationship between two columns in the same table or between different tables. For
a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table
which it is referring. One or more columns can be defined as foreign key.

Syntax to define a Foreign key at column level:

[CONSTRAINT constraint_name] REFERENCES referenced_table_name(column_name)

3) Not Null Constraint:

This constraint ensures all rows in the table contain a definite value for the column which is
specified as not null. Which means a null value is not allowed.

Syntax to define a Not Null constraint:

[CONSTRAINT constraint name] NOT NULL

4) Unique Key:

This constraint ensures that a column or a group of columns in each row have a distinct value. A
column(s) can have a null value but the values cannot be duplicated.

Syntax to define a Unique key at column level:

[CONSTRAINT constraint_name] UNIQUE

Syntax to define a Unique key at table level:

[CONSTRAINT constraint_name] UNIQUE(column_name)

5) Check Constraint:

This constraint defines a business rule on a column. All the rows must satisfy this rule. The
constraint can be applied for a single column or a group of columns.
Syntax to define a Check constraint:

[CONSTRAINT constraint_name] CHECK (condition)

ALTER TABLE Statement

The SQL ALTER TABLE command is used to modify the definition structure) of a table by
modifying the definition of its columns. The ALTER command is used to perform the following
functions.

1) Add, drop, modify table columns

2) Add and drop constraints

3) Enable and Disable constraints

The HAVING clause

The HAVING clause can be used to restrict the display of grouped rows. The result of the
grouped query is passed on to the HAVING clause for output filtration.

The INSERT INTO Statement

The INSERT INTO statement is used to insert a new row in a table.

The UPDATE Statement

The UPDATE statement is used to update existing records in a table.

The DELETE Statement

The DELETE statement is used to delete rows in a table.

Commit command

Commit command is used to permanently save any transaaction into database


Rollback command

This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.

Savepoint command

savepoint command is used to temporarily save a transaction so that you can rollback to that
point

whenever necessary.
Experiment 1
Create a table called Employee & execute the following.
Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL, COMMISSION)
1. Create a user and grant all permissions to the user.
2. Insert the any three records in the employee table contains attributes
EMPNO,ENAME JOB, MANAGER_NO, SAL, COMMISSION and use rollback.
Check the result.
3. Add primary key constraint and not null constraint to the employee table.
4. Insert null values to the employee table and verify the result.
Solution
Step 1:Login into mysql using the command:
$ mysql -u root -p
mysql> CREATE DATABASE COMPANY;
mysql> USE COMPANY;

Step 2: Creating the Employee Table


mysql> CREATE TABLE [Link] (
-> EMPNO INT,
-> ENAME VARCHAR(255),
-> JOB VARCHAR(255),
-> MANAGER_NO INT,
-> SAL DECIMAL(10, 2),
-> COMMISSION DECIMAL(10, 2)
-> );
mysql> SHOW TABLES;
Step 3:Create a User and Grant Permissions
CREATE USER IF NOT EXISTS 'demouser'@'localhost' IDENTIFIED BY 'deepti1';

GRANT ALL PRIVILEGES ON [Link] TO 'demouser'@'localhost';


Step 4:Command to login with a new user account is shown below.
$ mysql -u demouser -p
Enter password:
Step 5:Change the current database to COMPANY database using USE command. Now we will
illustrate how to insert records and also the COMMIT and ROLLBACK facilities.
-- Change the current database to COMPANY
mysql> USE COMPANY;
mysql> SELECT * FROM Employee;
-- START A TRANSACTION
mysql> START TRANSACTION;
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION)VALUES (1, 'Kavana Shetty', 'Manager', NULL, 5000.00, 1000.00);
-- COMMIT DATABASE, db CONTENTS ARE WRITTEN TO THE DISK
mysql> COMMIT;
-- DISPLAY TABLE CONTENTS
mysql> SELECT * FROM Employee;
-- START ANOTHER TRANSACTION
mysql> START TRANSACTION;

-- INSERT MORE RECORDS


mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION)VALUES (2, 'Ram Charan', 'Developer', 1, 4000.00, NULL);
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION)VALUES (3, 'Honey Singh', 'Salesperson', 2, 3000.00, 500.00);

mysql> SELECT * FROM Employee;


mysql> DELETE FROM Employee where ENAME = 'Kavana Shetty';
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM Employee;


2 rows in set (0.00 sec)
-- ROLLBACK 2 INSERTS AND 1 DELETE OPERATIONS
mysql> ROLLBACK;
Query OK, 0 rows affected (0.06 sec)

mysql> SELECT * FROM Employee;


+-------+---------------+---------+------------+---------+------------+
| EMPNO | ENAME | JOB | MANAGER_NO | SAL | COMMISSION |
+-------+---------------+---------+------------+---------+------------+
| 1 | Kavana Shetty | Manager | NULL | 5000.00 | 1000.00 |
+-------+---------------+---------+------------+---------+------------+
1 row in set (0.00 sec)

Step 6:Add Primary Key Constraint


-- Add Primary Key Constraint
mysql> ALTER TABLE Employee
-> ADD CONSTRAINT pk_employee PRIMARY KEY (EMPNO);
-- verify primary key constraint
mysql> DESC Employee;
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (1, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);
Note:
Since the EMPNO field is the primary key it cannot have duplicate values, hence we see that the
insert operation fails when provided with a duplicate value.

Step 7:Add Not Null Constraint


-- Add Not Null Constraints
mysql> ALTER TABLE Employee
-> MODIFY ENAME VARCHAR(255) NOT NULL,
-> MODIFY JOB VARCHAR(255) NOT NULL,
-> MODIFY SAL DECIMAL(10, 2) NOT NULL;
Query OK, 0 rows affected (1.08 sec)

mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,


COMMISSION)VALUES (4, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);
Query OK, 1 row affected (0.16 sec)

mysql>
mysql> SELECT * FROM Employee;

mysql> INSERT INTO Employee (ENAME, JOB, MANAGER_NO, SAL, COMMISSION)


-> VALUES (NULL, 'Tester', NULL, 3500.00, NULL);
ERROR 1048 (23000): Column 'ENAME' cannot be null
OUTPUT
EXPERIMENT 2
Create a table called Employee that contain attributes EMPNO,ENAME,JOB, MGR,SAL &
execute the following.
1. Add a column commission with domain to the Employeetable.
2. Insert any five records into the table.
3. Update the column details of job
4. Rename the column of the Employee table using the alter command.
5. Delete the employee whose Empno is 105.

Step 1:Creating the Employee Table

CREATE DATABASE COMPANY02;


USE COMPANY02;
CREATE TABLE Employee (
-> EMPNO INT,
-> ENAME VARCHAR(255),
-> JOB VARCHAR(255),
-> MGR INT,
-> SAL DECIMAL(10, 2)
-> );
SHOW TABLES;
DESC Employee;
Step 2:Adding a Column (Commission) to the Employee Table
ALTER TABLE Employee ADD COLUMN COMMISSION DECIMAL(10, 2);

Step 3:Inserting 5 Records into the Employee 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);
mysql> SELECT * FROM Employee;

Step 4:Updating Column Details (JOB) in the Employee Table


UPDATE Employee
-> SET JOB = 'Senior Developer'
-> WHERE EMPNO = 102;

Select * from employee;


Step 5:Renaming a Column in the Employee Table
ALTER TABLE Employee
-> CHANGE COLUMN MGR MANAGER_ID INT;

Step 6:Deleting a Specific Employee (EMPNO = 105) from the Employee Table
DELETE FROM Employee
WHERE EMPNO = 105;
EXPERIMENT 3
Queries using aggregate functions(COUNT,AVG,MIN,MAX,SUM),Group by,Orderby.
Employee(E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employeetable
3. Find the Maximum age from employee table.
4. Find the Minimum age from employeetable.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.

Step 1:Creating the Employee Table


CREATE DATABASE COMPANY03;
USE COMPANY03;
CREATE TABLE Employee (
-> E_id INT PRIMARY KEY,
-> E_name VARCHAR(255),
-> Age INT,
-> Salary DECIMAL(10, 2)
-> );
DESC Employee;
Step 2:Populating the Employee Table with 12 Records
INSERT INTO Employee (E_id, E_name, Age, Salary)
-> VALUES
-> (1, 'Samarth', 30, 50000.00),
-> (2, 'Ramesh Kumar', 25, 45000.00),
-> (3, 'Seema Banu', 35, 60000.00),
-> (4, 'Dennis Anil', 28, 52000.00),
-> (5, 'Rehman Khan', 32, 58000.00),
-> (6, 'Pavan Gowda', 40, 70000.00),
-> (7, 'Shruthi Bhat', 27, 48000.00),
-> (8, 'Sandesh Yadav', 29, 51000.00),
-> (9, 'Vikram Acharya', 33, 62000.00),
-> (10, 'Praveen Bellad', 26, 46000.00),
-> (11, 'Sophia Mary', 31, 55000.00),
-> (12, 'Darshan Desai', 34, 63000.00);

Step 3:Count Number of Employee Names


SELECT COUNT(E_name) AS TotalEmployees FROM Employee;
Step 4:Find the Maximum Age
SELECT MAX(Age) AS MaxAge FROM Employee;

Step 5:Find the Minimum Age


SELECT MIN(Age) AS MinAge FROM Employee;

Step 6:Find Salaries of Employees in Ascending Order.


SELECT E_name, Salary
FROM Employee
ORDER BY Salary ASC;
Step 7:Find Grouped Salaries of Employees
SELECT Salary, COUNT(*) AS EmployeeCount
FROM Employee
GROUP BY Salary;
Experiment 4
Create a row level trigger for the customers table that would fire for INSERT or UPDATE or
DELETE operations performed on the CUSTOMERS table. This trigger will display the salary
difference between the old & new Salary.
CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)

Step 1:Create the CUSTOMERS Table


CREATE DATABASE COMPANY04;
USE COMPANY04;
CREATE TABLE CUSTOMERS (
-> ID INT PRIMARY KEY AUTO_INCREMENT,
-> NAME VARCHAR(255),
-> AGE INT,
-> ADDRESS VARCHAR(255),
-> SALARY DECIMAL(10, 2)
-> );

Step 2:Create Trigger for INSERT Operation


-- INSERT TRIGGER
DELIMITER //

CREATE TRIGGER after_insert_salary_difference


AFTER INSERT ON CUSTOMERS
FOR EACH ROW
BEGIN
​ SET @my_sal_diff = CONCAT('salary inserted is ', [Link]);
END;//

DELIMITER ;

Step 3:Create Trigger for UPDATE Operation


-- UPDATE TRIGGER
DELIMITER //

CREATE TRIGGER after_update_salary_difference


AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL(10, 2);
DECLARE new_salary DECIMAL(10, 2);

SET old_salary = [Link];


SET new_salary = [Link];
​ SET @my_sal_diff = CONCAT('salary difference after update is ', [Link] -
[Link]);
END;//
DELIMITER ;

Step 4:Create Trigger for DELETE Operation


-- DELETE TRIGGER
DELIMITER //

CREATE TRIGGER after_delete_salary_difference


AFTER DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
​ SET @my_sal_diff = CONCAT('salary deleted is ', [Link]);
END;//

DELIMITER ;
Step 5:Testing the Trigger:
Once the triggers are created, you can perform INSERT, UPDATE, or DELETE operations on
the CUSTOMERS table to observe the salary difference messages generated by the triggers.

INSERT INTO CUSTOMERS (NAME, AGE, ADDRESS, SALARY)


-> VALUES ('Shankara', 35, '123 Main St', 50000.00);
SELECT @my_sal_diff AS SAL_DIFF;
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.
EXPERIMENT 5
Create a cursor for the Employee table & extract the values from the table. Declare the
variables,Open the cursor & extract the values from the cursor. Close the cursor.
CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)

Step 1:Creating the Employee Table and insert few records


CREATE DATABASE COMPANY05;
USE COMPANY05;
CREATE TABLE Employee (
E_id INT,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);
INSERT INTO Employee (E_id, E_name, Age, Salary)
VALUES
(1, 'Samarth', 30, 50000.00),
(2, 'Ramesh Kumar', 25, 45000.00),
(3, 'Seema Banu', 35, 62000.00),
(4, 'Dennis Anil', 28, 52000.00),
(5, 'Rehman Khan', 32, 58000.00);
Step 2:Create a Stored Procedure with Cursor
To create a cursor for the Employee table, extract values using the cursor, and then close the
cursor in MySQL, you’ll need to use stored procedures that support cursor operations.

DELIMITER //

CREATE PROCEDURE fetch_employee_data()


BEGIN
-- Declare variables to store cursor values
DECLARE emp_id INT;
DECLARE emp_name VARCHAR(255);
DECLARE emp_age INT;
DECLARE emp_salary DECIMAL(10, 2);

-- Declare a cursor for the Employee table


DECLARE emp_cursor CURSOR FOR
SELECT E_id, E_name, Age, Salary
FROM Employee;

-- Declare a continue handler for the cursor


DECLARE CONTINUE HANDLER FOR NOT FOUND
SET @finished = 1;

-- Open the cursor


OPEN emp_cursor;

-- Initialize a variable to control cursor loop


SET @finished = 0;

-- Loop through the cursor results


cursor_loop: LOOP
-- Fetch the next row from the cursor into variables
FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;

-- Check if no more rows to fetch


IF @finished = 1 THEN
LEAVE cursor_loop;
END IF;

-- Output or process each row (for demonstration, print the values)


SELECT CONCAT('Employee ID: ', emp_id, ', Name: ', emp_name, ', Age: ', emp_age, ',
Salary: ', emp_salary) AS Employee_Info;
END LOOP;

-- Close the cursor


CLOSE emp_cursor;
END//
DELIMITER ;

In this stored procedure (fetch_employee_data):


We declare variables (emp_id, emp_name, emp_age, emp_salary) to store values retrieved from
the cursor.
A cursor (emp_cursor) is declared to select E_id, E_name, Age, and Salary from the Employee
table.
We declare a continue handler (CONTINUE HANDLER) for NOT FOUND condition to handle
the end of cursor data.
The cursor is opened (OPEN emp_cursor), and a loop (cursor_loop) is used to fetch each row
from the cursor.
We fetch values into the variables and process them within the loop (for demonstration, we print
the values using a SELECT statement).
The loop continues until all rows are fetched (@finished = 1).
Finally, the cursor is closed (CLOSE emp_cursor).
Step 3:Execute the Stored Procedure
Once the stored procedure fetch_employee_data is created, you can execute it to fetch and
process data from the Employee table:

CALL fetch_employee_data();

The stored procedure fetch_employee_data declares variables (emp_id, emp_name, emp_age,


emp_salary) to store values retrieved from the cursor.
A cursor (emp_cursor) is declared for the Employee table to select E_id, E_name, Age, and
Salary.
The cursor is opened (OPEN emp_cursor), and the FETCH statement retrieves the first row from
the cursor into the declared variables.
A WHILE loop processes each row fetched by the cursor (SQLSTATE() = '00000' checks for
successful fetching).
Within the loop, you can perform operations or output the values of each row.
The CLOSE statement closes the cursor after processing all rows.
This example demonstrates how to create and use a cursor in MySQL to extract values from the
Employee table row by row. Adjust the cursor query and processing logic based on your table
structure and desired operations.
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 exists in the second table then that data should be skipped.

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.

Step 1:Create the Tables


CREATE DATABASE ROLLCALL;
USE ROLLCALL;
-- Create N_RollCall table
CREATE TABLE N_RollCall (
student_id INT PRIMARY KEY,
student_name VARCHAR(255),
birth_date DATE
);

-- Create O_RollCall table with common data


CREATE TABLE O_RollCall (
student_id INT PRIMARY KEY,
student_name VARCHAR(255),
birth_date DATE
);
Step 2:Add Sample Records to both tables
Let’s insert some sample data into the O_RollCall table:
mysql> -- Insert common data into O_RollCall
mysql> INSERT INTO O_RollCall (student_id, student_name, birth_date)
-> VALUES
-> (1, 'Shivanna', '1995-08-15'),
-> (3, 'Cheluva', '1990-12-10');

Let’s insert some sample data into the N_RollCall table, including records that are common with
O_RollCall:
mysql> -- Insert sample records into N_RollCall
mysql> INSERT INTO N_RollCall (student_id, student_name, birth_date)
-> VALUES
-> (1, 'Shivanna', '1995-08-15'), -- Common record with O_RollCall
-> (2, 'Bhadramma', '1998-03-22'),
-> (3, 'Cheluva', '1990-12-10'), -- Common record with O_RollCall
-> (4, 'Devendra', '2000-05-18'),
-> (5, 'Eshwar', '1997-09-03');
Step 3:Define the Stored Procedure
Next, let’s define the merge_rollcall_data stored procedure to merge records from N_RollCall
into O_RollCall, skipping existing records:
DELIMITER //

CREATE PROCEDURE merge_rollcall_data()


BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE n_id INT;
DECLARE n_name VARCHAR(255);
DECLARE n_birth_date DATE;

-- Declare cursor for N_RollCall table


DECLARE n_cursor CURSOR FOR
SELECT student_id, student_name, birth_date
FROM N_RollCall;
-- Declare handler for cursor
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET done = TRUE;

-- Open the cursor


OPEN n_cursor;

-- Start looping through cursor results


cursor_loop: LOOP
-- Fetch data from cursor into variables
FETCH n_cursor INTO n_id, n_name, n_birth_date;

-- Check if no more rows to fetch


IF done THEN
LEAVE cursor_loop;
END IF;

-- Check if the data already exists in O_RollCall


IF NOT EXISTS (
SELECT 1
FROM O_RollCall
WHERE student_id = n_id
) THEN
-- Insert the record into O_RollCall
INSERT INTO O_RollCall (student_id, student_name, birth_date)
VALUES (n_id, n_name, n_birth_date);
END IF;
END LOOP;

-- Close the cursor


CLOSE n_cursor;
END//

DELIMITER ;

The stored procedure merge_rollcall_data uses a cursor (n_cursor) to iterate through the records
of the N_RollCall table.
Inside the cursor loop (cursor_loop), each record (n_id, n_name, n_date) from N_RollCall is
fetched and checked against the O_RollCall table.
If the record does not already exist in O_RollCall (checked using NOT EXISTS), it is inserted
into O_RollCall.
The cursor loop continues until all records from N_RollCall have been processed.
The cursor is then closed (CLOSE n_cursor).

Step 4:Execute the Stored Procedure


Finally, execute the merge_rollcall_data stored procedure to merge records from N_RollCall into
O_RollCall while skipping existing records:

mysql> CALL merge_rollcall_data();

Step 5:Verify Records in O_RollCall


After executing the procedure, verify the records in the O_RollCall table to confirm that new
records from N_RollCall have been inserted, while existing common records have been skipped:
SELECT * FROM O_RollCall;
Experiment 7
Install an Open Source NoSQL Database MongoDB & perform basic CRUD(Create, Read,
Update & Delete) operations. Execute MongoDB basic Queries using CRUD operations.

Solution:Installing Open Source NoSQL Database MongoDB.


Step 1:Perform basic CRUD(Create, Read, Update & Delete) operations.

You might also like