Documente Academic
Documente Profesional
Documente Cultură
Revision
Constraints: Foreign Key Constraint
SubQueries
Transactions
Locking
Views
Temporary tables
MySQL variables
MySQL Programming
Revision
Indexes
Normal index
On single column.
Values can duplicate.
CREATE INDEX idx_name ON tbl_name(col_name [ASC|DESC]);
Composite index
Unique index
To show indexes:
To drop indexes:
DML is slower.
Index creation slower.
Index need space of disk.
Primary Key
Constraint on column.
It is equivalent to combination of NOT NULL and UNIQUE constraint.
However, only one Primary Key for the table.
Constraint
Types:
NOT NULL
UNIQUE
CHECK
PRIMARY KEY
FOREIGN KEY
FOREIGN Key
When tables are related (parent-child), PRIMARY key of parent table is associated with a
column in child table.
To enforce verifying this column should contain a value corresponding to Primary key of
parent table, Foreign key constraint is applied.
CREATE TABLE dept (deptno INT PRIMARY KEY, dname VARCHAR(20));
INSERT INTO dept VALUES (10, 'DEV');
INSERT INTO dept VALUES (20, 'QA');
INSERT INTO dept VALUES (30, 'OPS');
INSERT INTO dept VALUES (40, 'ACC');
Foreign key checks ensures data integrity, but make DML slower.
For data porting/backup, this slowdown the operations.
Here Foreign keys can be disabled temporarily.
In MySQL this can be using system variable foreign_key_checks.
SHOW VARIABLES;
SELECT @@foreign_key_checks;
SET @@foreign_key_checks=0;
SET @@foreign_key_checks=1;
Parent rows cannot be deleted (if there are corresponding child rows).
ON DELETE CASCADE and ON UPDATE CASCADE both features can be used together.
SubQueries
Query within query. (SELECT query within SELECT query).
Single row sub-query
When sub-query returns single row.
-- Find all emps whose sal is more that avg sal of emps.
SELECT AVG(sal) FROM emp;
SELECT @avgsal;
SELECT * FROM emp WHERE sal > (SELECT AVG(sal) FROM emp);
SELECT DISTINCT sal FROM emp ORDER BY sal DESC LIMIT 2,1;
SET @sal3 = (SELECT DISTINCT sal FROM emp ORDER BY sal DESC LIMIT 2,1);
SELECT @sal3;
SELECT * FROM emp WHERE sal = (SELECT DISTINCT sal FROM emp ORDER BY sal DESC
LIMIT 2,1);
Multi-row SubQuery
When sub-query returns more than one row.
To compare values returned from Multi row subquery we can use ALL or ANY keyword.
ALL operator
value is compared with all the values returned from subquery. When all are
matched the result is processed further.
it is logical AND.
ANY operator
value is compared with the values returned from subquery. When any one is
matched the result is processed further.
it is logical OR.
-- Display emps whose sal is more than sal of all salesman.
SELECT MAX(sal) FROM emp WHERE job='SALESMAN';
SELECT * FROM emp WHERE sal > (SELECT MAX(sal) FROM emp WHERE job='SALESMAN');
SELECT * FROM emp WHERE sal > ALL(SELECT sal FROM emp WHERE job='SALESMAN');
SELECT * FROM emp WHERE job != 'SALESMAN' AND sal > (SELECT MIN(sal) FROM emp
WHERE job='SALESMAN');
SELECT * FROM emp WHERE job != 'SALESMAN' AND sal > ANY(SELECT sal FROM emp
WHERE job='SALESMAN');
Correlated SubQueries
By default (if not optimized by database), for each row of outer query, inner query is
executed. This makes subqueries slower.
To make subqueries faster, we can write subqueries so that inner query produces lesser
number of rows.
This is done by referencing outer row from inner query. Such query is called as
"Correlated subquery".
-- Display depts in which emps are there.
SELECT * FROM dept WHERE deptno IN (SELECT deptno FROM emp);
EXPLAIN FORMAT=JSON
SELECT * FROM dept WHERE deptno IN (SELECT deptno FROM emp);
EXPLAIN FORMAT=JSON
SELECT * FROM dept d
WHERE deptno IN (SELECT deptno FROM emp e WHERE e.deptno = d.deptno);
EXPLAIN FORMAT=JSON
SELECT * FROM dept d
WHERE (SELECT COUNT(deptno) FROM emp e WHERE e.deptno = d.deptno) > 0;
EXISTS operator check whether subquery returns one or more rows. NOT EXISTS
operator check whether subquery returns 0 rows.
DELETE FROM emp WHERE sal < (SELECT AVG(sal) FROM emp); -- err in MySQL
DELETE FROM dept WHERE deptno NOT IN (SELECT deptno FROM emp); -- okay
Query performance
Usually subqueries are slower than joins (when no optimization is done).
But using some of the optimization switches, few subqueries tend to execute faster
than joins.
Query optimization switches in MySQL are accessible using @@optimizer_switch.
One of the switch is materialization. When it is on and if applicable, result of subquery
is cached into a temporary table. Due to this there is no need of executing subquery
again and again (for each row of outer query).
Another switch is block_nested_loop, which converts subquery into a nested loop (like
join). This will also improve the performance.
EXPLAIN FORMAT=JSON
SELECT * FROM dept WHERE deptno IN (SELECT deptno FROM emp);
SELECT @@optimizer_switch;
SET @@optimizer_switch='materialization=off'
SET @@optimizer_switch='block_nested_loop=off'
Derived Tables
Transactions
Transaction is set of DML queries that is executed as a single unit. if any query from tx
fails, rest of queries are rollbacked (discarded).
Example
START TRANSACTION;
UPDATE acc SET balance=balance-6000 WHERE accid=1;
UPDATE acc SET balance=balance+6000 WHERE accid=2;
SELECT * FROM acc;
COMMIT;
-- or
ROLLBACK;
Example
CREATE TABLE dept (deptno INT PRIMARY KEY, dname VARCHAR(20));
INSERT INTO dept VALUES (10, 'DEV');
INSERT INTO dept VALUES (20, 'QA');
INSERT INTO dept VALUES (30, 'OPS');
INSERT INTO dept VALUES (40, 'ACC');
SELECT * FROM dept;
START TRANSACTION;
DELETE FROM dept WHERE deptno=30;
SELECT * FROM dept;
ROLLBACK;
SELECT * FROM dept;
START TRANSACTION;
DELETE FROM dept WHERE deptno=40;
SELECT * FROM dept;
COMMIT;
SELECT * FROM dept;
Note that, when COMMIT or ROLLBACK is done, current tx is completed. For further
changes start a new tx.
START TRANSACTION;
SELECT * FROM dept;
DELETE FROM dept WHERE deptno=30;
SELECT * FROM dept;
Tx is not applicable for DDL queries. If DDL query is executed within a tx, that tx is
auto-committed.
Savepoint
Savepoint is state of database stored temporarily within transaction.
To perform multiple operations within a tx safely, it is recommended to create
savepoints.
We can rollback to a savepoint, which ensure all work done till that savepoint is intact.
However in this case, tx is not yet completed.
When tx is completed (COMMIT or ROLLBACK) all savepoints saved so far will be
discarded.
Note that, COMMIT TO savepoint is not supported.
START TRANSACTION;
dml1;
dml2;
SAVEPOINT sa1;
dml3;
dml4;
SAVEPOINT sa2;
dml5;
dml6;
ROLLBACK TO sa2;
dml7;
dml8;
COMMIT;
Tx characteristics
RDBMS tx are ACID.
Atomic: Set of queries are executed as single unit. They cannot be committed
partially.
Consistent: At the end of tx, all users will see the same data.
Isolated: All tx execute independent of each other.
Durable: At the end of tx, final state of data is saved on disk (durable).