Mysql - unit-II (DML, DCL) Unit2
Mysql - unit-II (DML, DCL) Unit2
COMMIT, ROLLBACK,
TCL Transaction Control Language
SAVEPOINT, SET TRANSACTION
SQL, which stands for Structured Query Language, is a powerful
language used for managing and manipulating relational databases.
• What is SQL Commands?
SQL commands are the fundamental building blocks for
communicating with a database management system (DBMS). These
commands perform various database operations, such as creating
tables, inserting data, querying information, and controlling access
and security. SQL commands can be categorized into different types,
each serving a specific purpose in the database management process.
DDL:-
• DDL, which stands for Data Definition Language, is a subset of
SQL (Structured Query Language) commands used to define and
modify the database structure. These commands are used to
create, alter, and delete database objects like tables, indexes,
and schemas.
The primary DDL commands in SQL include:
• CREATE: This command is used to create a new database
object. For example, creating a new table, a view, or a
database.
– Syntax for creating a table: CREATE TABLE table_name (column1
datatype, column2 datatype, ...);
Operator Description
• = Equal to.
• > Greater than.
• < Less than.
• >= Greater than equal to.
• <= Less than equal to.
• Equal to (=):-
• SELECT * FROM MATHS WHERE MARKS=50;
The AND operator takes priority over the OR operator in this expression. As a result,
the conditions department = 'Sales' AND salary > 50000 will be considered first,
followed by the OR condition with department = 'Marketing'.
The order in which logical operators in MySQL are evaluated in a SQL query is called
logical operator precedence in MySQL. The precedence of logical operators is: NOT >
AND > OR.
E.g:2
SELECT * FROM employees WHERE department = 'Sales' OR department = 'Marketing'
AND salary > 50000;
name age department salary
A 25 Marketing 60000
B 29 Sales 60000
C 35 Sales 45000
• MySQL analyses the AND condition first in this query, followed by the OR
condition because the OR condition has lesser precedence. As a result, only
employees in the Marketing department earning more than $50,000 per year or
all employees in the Sales department will be returned.
• E.g.3:-
• SELECT * FROM employees WHERE NOT department = 'Sales' OR salary > 50000;
• The NOT operator takes precedence over the OR operator. As a result, the
condition NOT department = 'Sales' is evaluated first. This condition returns all
employees who do not work in the Sales department. The OR condition is then
reviewed, and those employees earning more than $50,000 are returned.
Example 1:
Query:
SELECT * FROM employees WHERE (salary > 55000 OR department = 'IT') AND age < 40;
In this query, we are looking for employees who are under the age of 40 and either earn
over $55,000 per year or work in the IT department. By grouping them with parenthesis,
we ensure that the first two conditions are evaluated together before using the AND
operator for the third condition. As a result, the query is clearer and easier to interpret
with the parentheses.
Query:
• SELECT * FROM employees WHERE (department = 'IT' OR department =
'Sales' OR department = 'Marketing'
| Bitwise OR operator
Output:
Aman
Sushant
Kausiki
The above query is similar to saying WHERE Physics >= 75 AND Physics <= 95
Using NOT BETWEEN Operator:-
• We can reverse the working of between query in MySQL using
the NOT operator. We can combine the NOT operator with
the BETWEEN operator to exclude the records that lie in the provided range or
values. Let's use an example to understand the working of NOT BETWEEN.
Example:-
• SELECT Name FROM students WHERE Physics NOT BETWEEN 75 AND 95;
Output:-
• Saurabh
• Aditya
LIKE Operatores:-
• The SQL LIKE operator within a WHERE clause serves to search for a specific pattern
within a column.
• Two commonly utilized wildcards in association with the SQL LIKE operator are:
• The percent sign (%), which signifies zero, one, or multiple characters.
• The underscore sign (_), representing a single character.
Example:-
• SELECT *FROM studentsWHERE student_name LIKE 'A%';
• This query will return all rows from the students table where the student_name
column starts with "A", followed by zero or more characters.
Example 2:
• SELECT * FROM words WHERE word LIKE '_e_';
• This query retrieves rows from the "words" table where the "word" column
contains exactly three characters, with "e" positioned as the second character, and
any characters occupying the first and third positions.
Wildcard characters Description
The percent sign (%) in SQL serves to denote
% the presence of zero, one, or multiple
characters in a pattern.
The underscore (_) in SQL denotes the
_ existence of a single character or number
within a pattern.
Examples
Expression Description
LIKE ‘%a’
Find any values that contain “a" in end position
LIKE ‘%or%’
Find any values that have "def" in the between
LIKE ‘_r%’
R in the second position
LIKE ‘a_%’
Starts with ‘a’ and are at least 2 characters in length
LIKE ‘a__%’ Starts with ‘a’ and are at least 3 characters in length
LIKE ‘a%o’
Start with ‘a’ and ends with ‘o’
Like Operator in SQL with OR
• The SQL LIKE operator allows us to specify multiple string patterns for selecting rows
by utilizing the OR operators.
Syntax
• SELECT column1, column2, ... FROM table_name WHERE column1 LIKE pattern1 OR
column2 LIKE pattern2 OR ...;
ID Student_Name Marks
1 Sameer 95
2 Shyam 90
3 Pinki 88
4 Soni 80
5 Samar 67
SELECT *FROM student WHERE Student_Name LIKE 'S%r' OR name LIKE 'P%i';
ID Student_name Marks
1 Sameer 95
3 Pinki 88
5 Samar 67
• The SQL LIKE operator offers powerful capabilities for pattern matching within
column data, enabling flexible search functionalities in queries.
• With the percent sign (%) and underscore (_) as wildcards, SQL LIKE facilitates
searches for patterns with variable and fixed character positions, respectively.
SQL IN operator
• The SQL IN operator allows testing a list of values in the where clause. The values
are separated using a comma(,). It signifies multiple OR conditions in a SELECT,
INSERT, UPDATE or DELETE statement in the SQL Query.
Syntax
• SELECT column_name(s) FROM table_name WHERE column_name IN (value1,
value2, ...);
company_id company_name city state
1 Microsoft Bangalore Karnataka
2 Oracle Gurgaon Uttar Pradesh
3 Amazon Chennai Tamil Nadu
4 Adobe Pune Maharastra
5 PayPal Kolkata West Bengal
6 Codenation Patna Bihar