Advanced SQL: Chrisandra S. Gaston Jeremy P. Palada Argel J. Ramos
Advanced SQL: Chrisandra S. Gaston Jeremy P. Palada Argel J. Ramos
SQL
Chrisandra S. Gaston
Jeremy P. Palada
Argel J. Ramos
Logical, Arithmetic,
Comparison and Bit Operators
OPERATORS are the building blocks of complex queries.
Logical operators allow you to relate numbers of
conditions in various ways.
Arithmetic operators allow you to perform basic
mathematical operations in your queries.
Comparison operators allow you to compare values, and
LOGICAL OPERATORS
Operator
AND, &&
Syntax
Descriptio
c1 AND c2, c1 && c2 Only true if both
conditions c1
and c2 are true
OR, ||
!, NOT
c1 OR c2, c1 || c2
! c1, NOT c1
True if either c1
or c2 is true.
True if c1 is false,
false if c1 is tru
LOGICAL OPERATORS
ARITHMETIC OPERATORS
Operator
+
Syntax
Description
a+b
a-b
Subtracts b from a,
a*b
a/b
Divides a by b, QUOTIENT
DIFFERENCE
a%b
a modulus b, REMAINDER
ARITHMETIC OPERATORS
Operator
=
!=
>
<
COMPARISON
OPERATORS
Name
Description
Equals
True if both a & b are equ
Not equal
True if a is not equa
Greater than True if a is greater than
Less than True if a is less than b
Name
IS NULL
COMPARISON
OPERATORS
IS NOT NULL
value
BETWEEN
Description
True if a contains a NULL value
True if a matches b
COMPARISON
OPERATORS
Name
NOT LIKE
IN
NOT IN
anything
RLIKE
exp.
NOT RLIKE
Description
True if a does not match b
True if a is equal to anything
in the list
True if a is not equal to
in the list
True if a matches b in regular
EQUALS
NOT EQUALS
Greater than
Less than
<=>
IS NULL
IS NOT NULL
BETWEEN
NOT BETWEEN
LIKE
NOT LIKE
IN
NOT IN
RLIKE
NOT RLIKE
Character
%
characters
-
Description
Any number of
One character
BIT OPERATORS
Operator
Syntax
Description
&
a&b
|
a|b
Bitwise AND
Bitwise OR
<<
by b
a << b
Left shift of a
>>
a by b
a >> b
Right shift of
ADVANCED JOINS
INNER JOINS
Syntax to be used:
NOTE:
REMOVING RECORDS
(Delete and Truncate)
DELETE
TRUNCATE
REMOVING RECORDS
(Delete and Truncate)
Performing transactions
with BEGIN &COMMIT
For InnoDB
tables
TRANSACTION
involves wrapping
the
queries together
BEGIN to indicate the start of the
transaction
COMMIT to indicate the end
ROLLBACK to reverse the incomplete
part of the transaction
Performing transactions
with BEGIN &COMMIT
Performing transactions
with BEGIN &COMMIT
Consistent Reads
Consistent Reads
AUTOMATIC COMMITS
TABLE LOCKINGS
Two kinds of table locks:
READ LOCKS
only reads may be performed on the
table,
writes are locked
WRITE LOCKS
no reads and or writes may be performed
on the table for the duration of the lock.
SYNTAX TO BE USED TO LOCK A TABLE:
LOCK TABLE tablename {READ | WRITE}
SYNTAX TO BE USED TO LOCK A TABLE:
UNLOCK TABLES;
WARNING!!!
The LOCK TABLES statement
is not transaction safe. It will
commit all active transactions
before it attempts to lock the
tables.
TRANSACTION LEVELS
READ UNCOMMITED This level
allows transactions to read the
uncommitted data from other
transactions (dirty read)
READ COMMITTED This level
does not allow dirty reads.
REPEATABLE READ This
level does not allow nonrepeatable reads
SERIALIZE- This level does
not allow phantom reads
END OF
PRESENTATION