Presentation is loading. Please wait.

Presentation is loading. Please wait.

MySQL DML Commands By Prof. B.A.Khivsara

Similar presentations


Presentation on theme: "MySQL DML Commands By Prof. B.A.Khivsara"— Presentation transcript:

1 MySQL DML Commands By Prof. B.A.Khivsara
Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.

2 Design at least 10 SQL queries for suitable database application using SQL DML statements
Insert, Select, Update, Delete with operators, functions and set operator.

3 Insert - INSERT tuples into a relation
Where every attribute in the table is given a value INSERT INTO tablename VALUES (attr1_value, attr2_value, ... attrn_value) Where every attribute in the table is has not a value then it considered as NULL. INSERT INTO tablename (attrx, ... , attry) VALUES (attrx_value, ... , attry_value) Example: Insert into Stud values (1,’Pooja’, ‘Pune’) Insert into Stud (rollno,name) values (2, ‘Amit’) The output of "Stud" table after inserting above 2 rows rollno Name Address 1 Pooja Pune 2 Amit

4 Delete –DELETE tuples from relation
Syntax DELETE FROM tablename <WHERE condition> It removes zero, one or many tuples; The WHERE clause is optional; if it is left out all tuples are removed, but the table still exists; it is empty. Example: 1> DELETE FROM Stud WHERE Address = 'Pune'; ( Only delete roes that satisfy the condition) 2> DELETE FROM Stud; (Delete all rows)

5 Update- UPDATE of one or more selected tuples
Syntax UPDATE tablename SET attrx = new_value, <WHERE condition>; Example: 1> Update Stud Set Name= ‘Shruti’ where rollno=1 (Only name field of row having rollno as 1 will be chaged as ‘Shruti’) 2> Update Stud Set Name= ‘Shruti’ (name field of All row will be changed as ‘Shruti’)

6 Select- SELECT statement retrieves data from table
Syntax SELECT attribute_list FROM table_list <WHERE condition>; The attributes are those you want to see in the result, the tables are those required for the query, the condition is a boolean expression that specifies which tuples are to be retrieved by the query. Example: Select * from Stud; Select * from Stud where rno=1; Select Name, Address from Stud; Select Name, Address from Stud where rno=1 Select Name from Stud where address=‘Nashik’;

7 Order By- Retrieval with ordering Ascending/ Descending
The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword. Syntax SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC Example Select * from Stud order by rno desc; Select * from Stud order by rno ; Select * from Stud order by Name;

8 LIKE Operator- search for a specified pattern in a column
Syntax SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern SQL Wildcards  SQL wildcards can substitute for one or more characters when searching for data in a database. SQL wildcards must be used with the LIKE operator. Wildcard Description % A substitute for zero or more characters _ A substitute for exactly one character

9 LIKE Operator- Example
"Persons" table P_Id LastName Address 1 Heena Pune 2 Savita 3 Sarika Bombay We use the following SELECT statement: SELECT LastName from Person where LastName like ‘S%’ ; The result-set will look like this: LastName Savita Sarika

10 DISTINCT Operator Syntax Example
The DISTINCT keyword can be used to return only distinct (different) values. Syntax SELECT DISTINCT column_name(s) FROM table_name Example SELECT distinct(Address) from Person The result-set will look like this The "Persons" table: P_Id Name Address 1 Hansen Pune 2 Svendson 3 Pettersen Bombay Address Pune Bombay

11 IN Operator- The IN operator allows you to specify multiple values in a WHERE clause.
Syntax SELECT column_name(s) FROM table_name WHERE column_name IN (value1,value2,...) Example SELECT * FROM Persons WHERE Address IN ('Pune','Bombay') The result-set will look like this The "Persons" table: P_Id Name Address 1 Hansen Pune 2 Svendson Nashik 3 Pettersen Bombay P_Id Name Address 1 Hansen Pune 3 Pettersen Bombay

12 NULL Operator- Syntax Example
To test if a value is NULL you cannot use =, because every NULL is considered distinct from every other one. Must use "IS NULL" or "IS NOT NULL“ Syntax SELECT Attr_List FROM table_name WHERE Attr_name IS NULL; Example SELECT * FROM Stud WHERE Address is null; SELECT * FROM Stud WHERE Address is not null;

13 Aggregate Functions Aggregate /Group functions are built-in SQL functions that operate on groups of rows and return one value for the entire group. The Functions are as below: MIN returns the smallest value in a given column MAX returns the largest value in a given column SUM returns the sum of the numeric values in a given column AVG returns the average value of a given column COUNT returns the total number of values in a given column COUNT(*) returns the number of rows in a table

14 Count()- Syntax COUNT(column_name)-The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: Syntax SELECT COUNT(column_name) FROM table_name COUNT(*)-The COUNT(*) function returns the number of records in a table: SELECT COUNT(*) FROM table_name

15 Count()- Example Consider an employee_tbl table as shown below
SELECT COUNT(*) FROM employee_tbl ; SELECT COUNT(*) FROM employee_tbl WHERE name="Zara"; Output Count(*) 7 Output Count(*) 2

16 Avg(),Min(),Max(),Sum()
Syntax SELECT AVG(column_name) FROM table_name Example SELECT AVG(Marks) FROM Stud SELECT Min(column_name) FROM table_name SELECT Min(Marks) FROM Stud Syntax of all other functions are similar.

17 GROUP BY & Having Clause
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. This GROUP BY clause follows the WHERE clause in a SELECT statement Having is similar to where, to give condition, but it can only work with group by Syntax SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ;

18 GROUP BY & Having Clause- Example
Products Table id name type price 123451 Park's Great Hits Music 19 123452 Silly Puddy Toy 5 123453 Playstation 89 123454 Men's T-Shirt Clothing 32 123455 Blouse 34 123456 Electronica 2002 3 SELECT type, MIN(price) FROM products GROUP BY type SELECT type, Min(price) FROM products GROUP BY type having Min(price)>4 Type Min(Prize) Music 3 Toy 5 Clothing 32 Type Min(Prize) Toy 5 Clothing 32 Out Put

19 Set Operator: UNION,UNION ALL
UNION operator displays the combined result from all the compounded SELECT queries, after removing all duplicates and in sorted order (ascending by default), without ignoring the NULL values. UNION ALL gives the result set without removing duplication and sorting the data.

20 Set Operator- Example Union and Union all
ENo Ename Addr 101 Jenny Mumbai 102 Akash Pune 103 Sona 104 Minal Nashik 105 Raksh Jalgoan Emp Table Proj Table PrNo Addr 10 Mumbai 20 Pune 30 Addr Jalgoan Mumbai Nashik Pune Addr Jalgoan Mumbai Nashik Pune SELECT City FROM Emp UNION All SELECT City FROM Proj ORDER BY City; SELECT City FROM Emp UNION SELECT City FROM Proj ORDER BY City; O/P O/P

21 Assignment Create Employee table, Project table and add rows shown below Eid EName Address Salary Commision 1 Amit Pune 35000 5000 2 Sneha 25000 3 Savita Nasik 28000 2000 4 Pooja Mumbai 19000 5 Sagar 3000 PrNo Addr 10 Mumbai 20 Pune 30 Jalgoan Find different locations from where employees belong to? What is maximum and minimum salary? Display the content of employee table according to the ascending order of salary amount. Find the name of employee who lived in Nasik or Pune city. Find the name of employees who does not get commission. Change the city of Amit to Nashik. Find the information of employees whose name starts with ‘A’. Find the count of staff from Mumbai. Find the count of staff from each city Find the address from where employees are belonging as well as where projects are going on. Find city wise minimum salary. Find city wise maximum salary having maximum salary greater than 26000 Delete the employee who is having salary greater than 30,000.

22 References https://www.tutorialspoint.com/mysql/


Download ppt "MySQL DML Commands By Prof. B.A.Khivsara"

Similar presentations


Ads by Google