Enrollment No.: 0103IT181121 Date: 03-02-2020 Experiment No.- 4 Aim: To write a query with a functions of Data Manupulation Language (DML) commands such as insert, select, update, delete in RDBMS. 1. About DML DML is short name of Data Manipulation Language which deals with data manipulation and includes most common SQL statements such SELECT, INSERT, UPDATE, DELETE, etc., and it is used to store, modify, retrieve, delete and update data in a database. SELECT - retrieve data from a database INSERT - insert data into a table UPDATE - updates existing data within a table DELETE - Delete all records from a database table 2. How to insert data in a mysql database table. Mysql insert statement is used to insert record(s) or row(s) into a table. The insertion of records or rows in the table can be done in two ways, insert a single row at a time, and insert multiple rows at a time. 2.1 Insert a single row at a time. Syntax: Insert into table_Name Values(Column1_data,........Columnn_data); Example: Insert single row in table 'Student'. mysql> Desc Student; + -------------+----------------+------+-----+----------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+----------------+------+-----+----------+-------+ | Roll_No | varchar(12) | NO | PRI | NULL | | | S_Name | char(16) | NO | | NULL | | | S_Age | int(3) | NO | | NULL | | | S_Address | varchar(25) | NO | | NULL | | +-------------+----------------+------+-----+----------+-------+ 4 rows in set (0.00 sec) mysql> Insert into Student values('0103IT181097','Amit Kumar','19','M.P. Nagar, Bhopal'); Query OK, 1 row affected (0.05 sec) 2.2.1 How to display data in the table 'Student'. Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 mysql> Select * from Student; +-------------- ----+---------------+--------+------------------------+ | Roll_No | S_Name | S_Age | S_Address | +-------------- ----+---------------+--------+------------------------+ | 0103IT181097 | Amit Kumar | 19 | M.P. Nagar, Bhopal | +------------------+---------------+--------+------------------------+ 1 row in set ( 0.00 sec) 2.2 Insert a multiple row at a time. Syntax: Insert into table_Name Values(Column1_data,........Columnn_data), (Column1_data,........Columnn_data), (Column1_data,........Columnn_data); Example: Insert multiple rows in table 'Student'. mysql> Insert into Student values('0103IT181098','Ram Kumar','19','M.P. Nagar, Bhopal'), ('0103IT181099','Ashok Kumar','20','New Market, Bhopal'); Que ry OK, 2 rows affected (0.03 sec) Records: 2 Duplicates: 0 Warnings: 0 2.2.2 How to display dat a in the table 'Student'. mysql> Select * from Student; +------------------+-----------------+---------+--------------------------+ | Roll_No | S_Name | S_Age | S_Address | +----------- -------+-----------------+---------+--------------------------+ | 0103IT181097 | Amit Kumar | 19 | M.P. Nagar, Bhopal | | 01 03IT181098 | Ram Kumar | 19 | M.P. Nagar, Bhopal | | 0103IT181099 | Ashok Kumar | 20 | New Market, Bhopal | +-------------- ----+-----------------+---------+--------------------------+ 3 rows in set (0.00 sec) 3. How to retrive or extra ct data in mysql database table. The select statement is used to form queries for extracting information out of the database. 3.1 How to retrive or extract all tuples or row in mysql database table. Syntax: Select * from table_name; 3.1.2 How to retrive or extract all tuples or row in the table 'Student'. mysql> select * from Student; +-----------------+-----------------+---------+------------------------+ | Roll_No | S_Name | S_Age | S_Address | Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 +------------------+----------------+---------+--------------------------+ | 0103IT181097 | Amit Kumar | 19 | M.P. Nagar, Bhopal | | 0103IT181098 | Ram Kumar | 19 | M.P. Nagar, Bhopal | | 0103IT181099 | Ashok Kumar | 20 |New Market, Bhopal | +------------------+-----------------+---------+-------------------------+ 3 rows in set (0.00 sec) 3.2 How to retrive or extract full or some tuple/rows based on condition (Where Clause) in mysql database table. Syntax: Select * from table_name Where Condition =' '; or Select column_1, ....column_n from table_name Where Condition =' '; 3.2.1 How to retrive or extract full/ particular tuples or rows based on condition in table 'Student'. mysql> Select * from Student where S_Address='M.P. Nagar, Bhopal'; +-------------- ----+----------------+--------+------------------------+ | Roll_No | S_Name | S_Age | S_Address | +------------ ------+----------------+--------+------------------------+ | 0103IT181097 | Amit K umar | 19 | M.P. Nagar, Bhopal | | 01 03IT181098 | Ram Kumar | 19 | M.P. Nagar, Bhopal | +-------- ----------+----------------+-------+-------------------------+ 2 rows in set (0.01 sec) OR mysql> Select Roll_No from Student where S_Name='Amit Kumar'; +------------- -----+ | Roll_No | +-------------- ----+ | 0103IT181097 | +-------------- ----+ 1 row in set (0.00 sec) OR mysql> Select S_Name from Student where S_Age=19; +-------- -------+ | S_Name | +------------ ---+ Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 | Amit Kumar | | Ram Kumar | +----------- ----+ 2 rows in s et (0.00 sec) 3.3.2 How to retrive or extract distinct tuples or rows in table. To minimize the output, retrieve each unique output record just once by adding the keyword Distinct. Syntax: Select distinct column_name1,...column_namen from table_name; Example: mysql> Select distinct S_age from student; +--------+ | S_age | +--------+ | 18 | | 19 | | 20 | +--------+ 3 rows in set (0.00 sec) 3.3.3 How to retrive or extract total tuples or rows in table. mysql> Select Count(*) from Student; +------- ----+ | Count(*) | +---------- -+ | 3 | +---------- -+ 1 row in set (0.00 sec) 3.3.4 How to retrive or extract maximum age of the Student from the table student. mysql> Select max(S_Age) from Student; +------------ ---+ | max(S_Age) | +------------ ---+ | 20 | +------------ ---+ 1 row in set (0.00 sec) 3.3.5 How to retrive or extract minimum age of the Student from the table student. mysql> Select min(S_Age) from Student; +- --------------+ | min(S_Age) | +---- -----------+ Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 | 19 | +---------------+ 1 row in set (0.00 sec) 3.3.6 How to retrive or extract average age of the Student from the table student. mysql> Select avg(S_Age) from Student; +-------- ------+ | avg(S_Age) | +---------- ----+ | 19.333 3 | +--------------+ 1 row in set (0.00 sec) 4. How to update data in tuples of the table UPDATE statement is use to update existing data in a table. you can also use the UPDATE statement to change column values of a single row, a group of rows, or all rows in a table. Syntax: Update tbale_name Set column_name1=........... column_namen= Where= 'condition'; Example: Update the address of student whose Roll_No is '0103IT181099' in Student table. mysql> Update Student Set S_Address='Indrapuri, Bhopal' Where Roll_No='0103IT181099'; Query OK, 0 rows affected (0.05 sec) mysql> Select * from Student; +-------------- ----+-----------------+---------+-----------------------+ | Roll_No | S_Name | S_Age | S_Address | +---------- --------+-----------------+---------+-----------------------+ | 0103IT181097 | Amit Kumar | 19 | M.P. Nagar, Bhopal | | 0103IT181098 | Ram Kumar | 19 | M.P. Nagar, Bhopal | | 0103IT181099 | Ashok Kumar | 20 | Indrapuri, Bhopal | +--- ---------------+----------------+---------+------------------------+ 3 rows in set (0.00 sec) OR Example: Update the age of the Student to 25 whose name is 'Amit Kumar' in Student table. mysql> Update Student Set S_Age='25' Where S_Name='Amit Kumar'; Query OK , 1 row affected (0.04 sec) mysql> Select * from Student; +------------ ------+----------------+--------+------------------------+ | Roll_No | S_Name | S_Age | S_Address | Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 +----------- -------+-----------------+--------+------------------------+ | 0103IT181097 | Amit Kumar | 25 | M.P. Nagar, Bhopal | | 0103IT181098 | Ram Kumar | 19 | M.P. Nagar, Bhopal | | 0103IT181099 | Ashok Kumar | 20 | Indrapuri, Bhopal | +----- ------------+------------------+-------+-------------------------+ 3 rows in set (0.00 sec) 5. How to delete tuple s in the table To delete data from a table, you use the MySQL DELETE statement. Syntax: Delete From table_name Where= 'condition'; mysql> Delete from Student where S_Name='Ram Kumar'; Query OK, 1 row affected (0.05 sec) mysql> Select * from Student; +-------------- ----+-----------------+---------+------------------------+ | Roll_No | S_Name | S_Age | S_Address | +-------------- ----+-----------------+---------+------------------------+ | 0103IT181097 | Amit Kumar | 25 | M.P. Nagar, Bhopal | | 0103IT181099 | As hok Kumar | 20 | Indrapuri, Bhopal | +------------ ------+-----------------+---------+------------------------+ 2 rows in s et (0.00 sec) Date of Submission: 10-02-2020 Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 DML Exercise 1. Insert the following records into the table Location: LOCATION_ID REGIONAL_GROUP 122 Bhopal 123 Indore 124 Gwalior 167 Vidisha Solution: Insert into Location Values -> (122,'Bhopal'), -> (123,'Indore'), -> (124, 'Gwalior'), -> (167, 'Vidisha'); Query OK, 4 rows affected (0.12 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> Select * from Location; +--------------------+---------------------------+ | LOCATION_ID | REGIONAL_GROUP | +--------------------+---------------------------+ | 122 | Bhopal | | 123 | Indore | | 124 | Gwalior | | 167 | Vidisha | +-------------------+----------------------------+ 4 rows in set (0.10 sec) 2. Insert the following records into DEPARTMENT table: Department_ID Name Location_ID 10 Accounting 122 20 Research 124 30 Sales 123 40 Perations 167 Solution: Insert into Department Values -> (10,'Accounting',122), -> (20,'Research',1242), -> (30,'Sales',123), Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 -> (40,'Perations',167); Query OK, 4 rows affected (0.20 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> Select * from DEPARTMENT; +-------------------+-------------+---------------+ | Department_ID | Name | Location_ID | +------------------+--------------+---------------+ | 10 | Accounting | 122 | | 20 | Research | 1242 | | 30 | Sales | 123 | | 40 | Perations | 167 | +------------------+---------------+--------------+ 4 rows in set (0.00 sec) 3. Insert the following records into Job table: Job_ID FUNCTION 667 Clerk 668 Staff 669 Analyst 670 Salesperson 671 Manager 672 President Solution: Insert into Job Values -> (667,'Clerk'), -> (668,'Staff'), -> (669,'Analyst'), -> (670,'Salesperson'), -> (671,'Manager'), -> (672,'President'); Query OK, 6 rows affected (0.09 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> Select * from Job; +--------+-------------+ | Job_ID | Functions | +--------+-------------+ | 667 | Clerk | | 668 | Staff | | 669 | Analyst | Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 | 670 | Salesperson | | 671 | Manager | | 672 | President | +--------+-------------+ 6 rows in set (0.00 sec) 4. Insert the following records into Employee table Emp _Id Last_ Name First_ Name Middle _ Name Job _Id, Manage _Id Hire_Date Salar y Com Dept_ID 7369 Smith John O 667 7902 1984-12-17 800 NULL 20 7499 Allen Kevin J 670 7698 1985-02-20 1600 300 30 7505 Doyle Jean K 671 7839 1985-04-04 2850 NULL 30 7506 Dennis Lynn S 671 7839 1985-05-15 2750 NULL 30 7507 Baker Leslie D 671 7839 1985-06-16 2200 NULL 40 7521 Wark Cynthi a D 670 7698 1985-02-22 1250 500 30 Solution: Insert into Employee Values -> (7369,'Smith','John','O',667,7902,'1984-12-17',800,NULL,20), -> (7499,'Allen','Kevin','J',670,7698,'1985-02-20',1600,300,30), -> (7505,'Doyle','Jean','K',671,7839,'1985-04-04',2850,NULL,30), -> (7506, 'Dennis','Lynn', 'S',671,7839, '1985-05-15',2750,NULL,30), -> (7507, 'Baker','Leslie', 'D',671,7839, '1985-06-16',2200,NULL,40), -> (7521, 'Wark','Cynthia', 'D',670,7698, '1985-02-22',1250,500,30); Query OK, 6 rows affected (0.14 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> Select * from Employee; +--------+------------+--------------+----------------+-------+------------+------------+-------+-----+--------+ |Emp_Id|Last_Name|First_Name|Middle_Name|Job_Id|Manage_Id|Hire_Date|Salary|Com|Dept_ID| +--------+------------+--------------+----------------+-------+------------+------------+-------+-----+--------+ | 7369 | Smith | John | O | 667 | 7902 |1984-12-17 | 800 |NULL | 20 | | 7499 | Allen | Kevin | J | 670 | 7698 |1985-02-20 | 1600 | 300 | 30 | | 7505 | Doyle | Jean | K | 671 | 7839 |1985-04-04 | 2850 |NULL | 30 | | 7506 | Dennis | Lynn | S | 671 | 7839 |1985-05-15 | 2750 |NULL | 30 | | 7507 | Baker | Leslie | D | 671 | 7839 |1985-06-16 | 2200 |NULL | 40 | | 7521 | Wark | Cynthia | D | 670 | 7698 |1985-02-22 | 1250 | 500 | 30 | +-------+------------+--------------+----------------+-------+-----------+--------------+------+-------+-------+ 6 rows in set (0.00 sec) Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 Perform the following queries on the tables given above: 4.1 Find the employee name whose middle name is “D”. Solution: Select First_Name, Middle_Name, Last_Name from Employee Where Middle_Name='D'; +--------------+------------------+--------------+ | First_Name | Middle_Name | Last_Name | +--------------+------------------+--------------+ | Leslie | D | Baker | | Cynthia | D | Wark | +--------------+------------------+--------------+ 2 rows in set (0.00 sec) 4.2 Find the department name whose department id is 20. Solution: Select Name from Department Where Dept_ID=20; +-----------+ | Name | +-----------+ | Research | +-----------+ 1 row in set (0.00 sec) 4.3 Find the name of employee whose salary is 1250. Solution: Select First_Name, Middle_Name, Last_Name from Employee Where Salary=1250; +--------------+------------------+--------------+ | First_Name | Middle_Name | Last_Name | +--------------+------------------+--------------+ | Cynthia | D | Wark | +--------------+-----------------+---------------+ 1 row in set (0.00 sec) 4.4 Find out the location id of location Bhopal. Solution: Select Location_ID from Location Where REGIONAL_GROUP='Bhopal'; +--------------------+ | Location_ID | +-------------------+ | 122 | +-------------------+ 1 row in set (0.00 sec) 4.5 Update the salary of employee to 5000 of whose id is 7521. Solution : Update Employee Set Salary=5000 Where Emp_Id=7521; Query OK, 1 row affected (0.11 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> Select * from Employee; Name of Student: Vaishnavi Khatri Class Roll No:120 Enrollment No.: 0103IT181121 Date: 03-02-2020 +--------+-------------+-------------+----------------+-------+-------------+-------------+-------+----+-------+ |Emp_Id|Last_Name|First_Name|Middle_Name|Job_Id|Manage_Id|Hire_Date|Salary|Com|Dept_ID| +--------+------------+--------------+----------------+-------+-------------+-------------+-------+---+--------+ | 7369 | Smith | John | O | 667 | 7902 | 1984-12-17 |800 |NULL| 20 | | 7499 | Allen | Kevin | J | 670 | 7698 | 1985-02-20 |1600 | 300 | 30 | | 7505 | Doyle | Jean | K | 671 | 7839 | 1985-04-04 |2850 |NULL| 30 | | 7506 | Dennis | Lynn | S | 671 |7839 | 1985-05-15 |2750 |NULL| 30 | | 7507 | Baker | Leslie | D | 671 | 7839 | 1985-06-16 |2200 |NULL| 40 | | 7521 | Wark | Cynthia | D | 670 |7698 | 1985-02-22 |5000 | 500 | 30 | +-------+------------+--------------+----------------+--------+------------+---------------+-----+-------+-----+ 6 rows in set (0.00 sec) 4.6 Update the commission of employee to 700 whose job id is 671. Solution: Update Employee Set Com=700 Where Job_Id=671; Query OK, 3 rows affected (0.11 sec) Rows matched: 3 Changed: 3 Warnings: 0 mysql> Select * from Employee; +--------+-------------+-------------+----------------+-------+-------------+-----------+-------+----+---------+ |Emp_Id|Last_Name|First_Name|Middle_Name|Job_Id|Manage_Id|Hire_Date|Salary|Com|Dept_ID| +--------+------------+--------------+----------------+-------+-------------+-----------+-------+----+---------+ | 7369 | Smith | John | O | 667 | 7902 |1984-12-17| 800 |NULL| 20 | | 7499 | Allen | Kevin | J | 670 | 7698 |1985-02-20| 1600 | 300 | 30 | | 7505 | Doyle | Jean | K | 671 | 7839 |1985-04-04| 2850 | 700 | 30 | | 7506 | Dennis | Lynn | S | 671 | 7839 |1985-05-15| 2750 | 700 | 30 | | 7507 | Baker | Leslie | D | 671 | 7839 |1985-06-16| 2200 | 700 | 40 | | 7521 | Wark | Cynthia | D | 670 | 7698 |1985-02-22| 5000 | 500 | 30 | +-------+------------+--------------+----------------+-------+------------+-------------+-------+------+-------+ 6 rows in set (0.00 sec) Date of Submission: 10-02-2020 Name of Student: Vaishnavi Khatri Class Roll No:120