68
V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

Embed Size (px)

Citation preview

Page 1: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0 OE NIK 2013 1

DBMAN6

DDLConstraintsDMLTransactionsExamples

Page 2: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

SELECTDisplayed order of suffixes

1. INTO2. FROM3. WHERE4. GROUP BY5. HAVING6. UNION/MINUS7. INTERSECT8. ORDER BY

2OE NIK 2013

Page 3: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0 OE NIK 2013 3

DBMAN6

DDLConstraintsDMLTransactionsExamples

Page 4: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

CREATE TABLE …

CREATE TABLE {name} AS {subquery};

CREATE TABLE {user/db}.{TABLENAME} ({COLUMN DEFINITIONS}{TABLE CONSTRAINTS}

);

COLUMN DEFINITION: {NAME} {TYPE} [{COLUMN CONSTRAINTS}] [DEFAULT

{DEFAULT VALUE}]

4OE NIK 2013

Page 5: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Data Types(Oracle)• These types are used for all columns and function

parameters• Data types:

– char(N) – varchar(N) – varchar2(N) [255byte / 4K]– blob (vs. clob, ~2-4GB)– numeric(N[, M]) / number(N[, M]) / int, float– date / timestamp (time – not available in Oracle

instead usable: interval) alter session set NLS_DATE_FORMAT = 'YYYY-

MM-DD';

5OE NIK 2013

Page 6: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Data Types(MySQL)• char(N) – varchar(N) [rowsize: 65K]• blob (vs. text: tinytext, text, mediumtext, longtext,

max. 4GB)• tinyint (sbyte), smallint (integer), int, bigint (long),

float, double• date / time / datetime / timestamp• Others (year, enum, set, binary, bit, decimal,

numeric, bool – we will not use those)

6OE NIK 2013

Page 7: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Most simple usage

CREATE TABLE TestTable (Field1 VARCHAR(50),Field2 DATE,Field3 NUMERIC(10,2),Field4 Int,Field5 Float

);• If the table exists, it must be deleted before (no

CREATE OR REPLACE TABLE)!• Oracle:

SELECT * FROM user_catalog WHERE UPPER(table_name)='TESTTABLE';

• MySQL: SHOW tables like '%TESTTABLE%';7OE NIK 2013

Page 8: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

MySQL table types

• Unique (maybe the best?) approach (Oracle: Tablespace + disk)

• Possibility to use various storage modes for every table: according to the needs of the various tasks

• MyIsam vs InnoDB• CSV, Merge/MRG_MyIsam (union of tables), • Federated (remote tables), Memory, Archive, Blackhole• ENGINE=‘xxx’ after the CREATE TABLE• We usually do not use those, default=InnoDB

8OE NIK 2013

Page 9: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

DROP TABLE / RENAME• DROP TABLE {tablename};• MySQL: [IF EXISTS]

• RENAME {tablename} TO {newname};

9OE NIK 2013

Page 10: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

ALTER TABLE• ALTER TABLE {tablename} ADD {column definition};

• Oracle: ALTER TABLE {tablename} MODIFY {column name} {column definition without name};

• Oracle: ALTER TABLE {tablename} RENAME COLUMN {column name} TO {newname};

• MySQL: ALTER TABLE {tablename} CHANGE {column name} {full column definition};

• ALTER TABLE {tablename} DROP COLUMN {column name};

10OE NIK 2013

Page 11: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0 OE NIK 2013 11

DBMAN6

DDLConstraintsDMLTransactionsExamples

Page 12: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Column constraints

• NOT NULL – Field cannot have NULL inside• UNIQUE – Field cannot have repeated values

• PRIMARY KEY – Unique + NOT NULL (+ indexed) – this can be applied only to one field as a column constraint!

• REFERENCES othertable(otherfield) – Define foreign key. The other field must be a primary key in the other table

• REFERENCES othertable(otherfield) ON DELETE/UPDATE CASCADE – the delete/update commands have consequences

• MySQL: auto_increment (others: sequence, identity...)12OE NIK 2013

Page 13: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Column constraints

• CREATE TABLE test1 (test1_id NUMERIC(3,0) PRIMARY KEY,test_nev VARCHAR(30) NOT NULL UNIQUE,test_datum TIMESTAMP NOT NULL);

SQL> desc test1; Name Null? Type ------------------- -------- ------------------ TEST1_ID NOT NULL NUMBER(3) TEST_NEV NOT NULL VARCHAR2(30) TEST_DATUM NOT NULL TIMESTAMP(6)

13OE NIK 2013

Page 14: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Column constraints

• CREATE TABLE test2 (test2_id NUMERIC(3,0) PRIMARY KEY,test2_test1 NUMERIC(3, 0)

REFERENCES test1(test1_id) ON DELETE CASCADE,

test2_data INT NOT NULL);

SQL> desc test2 Name Null? Type ---------------- -------- ------------------- TEST2_ID NOT NULL NUMBER(3) TEST2_TEST1 NUMBER(3) TEST2_DATA NOT NULL NUMBER(38)

14OE NIK 2013

Page 15: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Table constraints

• After the field list

• This is the only way we can define complex keys:– PRIMARY KEY (field1, field2)– FOREIGN KEY (field1, field2) REFERENCES

othertable (fieldX, fieldY)

15OE NIK 2013

Page 16: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Defining simple keys• CREATE TABLE test3 (

test3_id NUMERIC(3,0),test3_test1 NUMERIC(3, 0),test3_data INT NOT NULL,PRIMARY KEY (test3_id),FOREIGN KEY (test3_test1)

REFERENCES test1(test1_id));

16OE NIK 2013

Page 17: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Defining complex keys

• CREATE TABLE test4 (first_pkey INT,second_pkey INT,data INT,primary key (first_pkey, second_pkey));

17OE NIK 2013

Page 18: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Defining complex keys

• CREATE TABLE test5 (kulcs INT PRIMARY KEY,first_fkey INT,second_fkey INT,data FLOAT,FOREIGN KEY (first_fkey, second_fkey)REFERENCES test4

(first_pkey, second_pkey));

18OE NIK 2013

Page 19: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

CHECK constraint• CHECK (condition): define an always-true condition• CREATE TABLE test6 (

key int primary key,data int,CONSTRAINT check_data

CHECK ((data>=500) AND (data<=999)));• insert into test6 values (1, 300);• Query OK, 1 row affected (0.06 sec) [In MySQL]• It must be true for all records (forbid insert/update)• MySQL: „The CHECK clause is parsed but ignored by all

storage engines.”, even in 5.7 !!!

19OE NIK 2013

Page 20: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

CHECK constraint

• It defines an always-true condition (can't add CHECK (sal<2000) )

• "A CLERK's salary must be maximum 2000 USD" CHECK ( (sal<=2000 AND upper(job)='CLERK') OR upper(job)<>'CLERK')

• NOW() / Sysdate, dynamic functions, subqueries are not allowed!

• Alternative (also suggested by MySQL devs): use triggers: sub program that is executed in connection with some SQL operation (INSERT, UPDATE, DELETE)

20OE NIK 2013

Page 21: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Constraints• Every constraint can (and should) be named:

CONSTRAINT [NAME], otherwise automatic name• E.g.:

CONSTRAINT test3_prim_key PRIMARY KEY (test3_id)CONSTRAINT test3_for_key FOREIGN KEY (test3_test1) REFERENCES test1(test1_id)

21OE NIK 2013

Page 22: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Constraints• Oracle:

– ALTER TABLE {tablename} DISABLE CONSTRAINT {constraint name}

– ALTER TABLE {tablename} ENABLE CONSTRAINT {constraint name}

– ALTER TABLE {tablename} ADD {constraint};– ALTER TABLE {tablename} DROP CONSTRAINT

{constraint name};• MySQL:

– ALTER TABLE {tablename} ADD {constraint};– ALTER TBALE {tablename} DISABLE KEYS / ENABLE

KEYS;

22OE NIK 2013

Page 23: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0 OE NIK 2013 23

DBMAN6

DDLConstraintsDMLTransactionsExamples

Page 24: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

INSERT INTO• INSERT INTO {table} ({list of columns}) VALUES ({list of

values});

• The column list can be left out, in this case every column's new value must be specified (in the same order as the table was created)

• Otherwise: the skipped columns will have their default values (specified at the CREATE TABLE)

• INSERT INTO test1 VALUES (5, 'BILL' , NOW());• INSERT INTO test1 (test1_id, test1_nev, test1_datum)

VALUES (6, 'JOE' , NOW());

24OE NIK 2013

Page 25: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

UPDATE/DELETE• UPDATE {tablename} SET {field1}={value1}[,

{fieldN}={valueN}, … …] [WHERE {expression}]

• DELETE FROM {tablename} [WHERE {expression}]

• It is advised to execute the same WHERE beforehand using a SELECT command, to test the filter!

• MySQL: REPLACE INTO … / TRUNCATE TABLEOracle: MERGE INTO …

25OE NIK 2013

Page 26: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0 OE NIK 2013 26

DBMAN6

DDLConstraintsDMLTransactionsExamples

Page 27: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Transactions• Transaction management: during the execution of

multiple DML operations, we can choose to actually accept every modification (COMMIT) or we can cancel (ROLLBACK) everything and go back to the last commit or we can define specific points (SAVEPOINT) to be used as the rollback target

• „A transaction is a group of operations that are atomic, consistent, isolated, and durable” (ACID)

• Oracle: "client tells us when to commit", "sqlplus always commits upon exit"

• MySQL: select @@autocommit; / START TRANSACTION

27OE NIK 2013

Page 28: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Transaction management• A simple feature that requires some extra CPU/memory

resources and A LOT of well-planned design and programming not so simple feature :)

• Intermediate point: SAVEPOINT {name};• Cancel the transaction: ROLLBACK [TO SAVEPOINT

{name}]• DDL: not reversible, automatic commit!

• MySQL: only in InnoDB is ACID compliant, since 5.0 ... InnoDB is only default since 5.5 (Thanks to the Oracle acquisition, because Oracle owned InnoDB)

• Since 5.5 = Only since 2010 !!!28OE NIK 2013

Page 29: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

MySQL = TOY DATABASE?• 3.2x (1997-2004): „Works and fast” (1992: Oracle 7:

„integrity, stored procedures, triggers”)• 4.1 (2004): Subqueries, prepared statements• 5.0 (2005): Views, procedures, triggers (Not on views!

Views without subqueries! One trigger / action / timing!)• 5.5 (2010): InnoDB default (integrity, transactions!), almost

all subqueries are accepted in views• Sun (2008), Oracle (2010) "Oracle was holding back

MySQL Server test cases, […] Oracle is attempting to kill the product. […] Oracle is no longer synchronizing their changes with the public source repositories. […] breach of the agreement that Oracle entered into with the EU as a condition of their acquisition of Sun."

29OE NIK 2013

Page 30: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Future of MySQL• MariaDB???• In April 2013, Wikipedia announced that it had completed the

migration of the English and German Wikipedias as well as Wikidata to MariaDB 5.5

• Google has also said they will be switching to MariaDB• Linux distributions Red Hat, Fedora, OpenSUSE, Slackware and

Arch Linux have announced that they are switching to MariaDB by default, while Ubuntu has stated that it will be sticking with MySQL. Other Linux distributions are offering MariaDB as an option.

• 5.6 GA: February 2013 : Optimizer, NoSQL/memcached• 5.7.5 DEV: Sept 2014: multiple triggers (Oracle: 1992!)• 6.0 alpha: killed in May 2009

30OE NIK 2013

Page 31: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0 OE NIK 2013 31

DBMAN6

DDLConstraintsDMLTransactionsExamples

Page 32: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Example #1

Increase the salaries with 1000 USD for those who don't have any commission and who have more than one person in their job and who work in Dallas or Chicago.

drop table if exists emp1;create table emp1 as select * from emp;

32OE NIK 2013

Page 33: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Let's start…• No commission, Dallas or Chicago:

UPDATE emp1 SET sal=sal+1000 WHERE ((comm is null) or (comm = 0)) AND

(upper(loc) in ('DALLAS', 'CHICAGO'))

… … … loc? JOIN!

33OE NIK 2013

Page 34: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Join in UPDATE?• With the SELECT command, the join is simple: an extra

condition in the WHERE• Here, we only have to modify those records, where the

joined location is correct – but originally, we can put only one table in the UPDATE statement

• Thus: the join must be put into the WHERE as a sub-query

correlated sub-query (typical approach in the Oracle world)

34OE NIK 2013

Page 35: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Approach #1• Let's try to define the location for ONE worker:• select upper(loc) from emp1, dept where

emp1.deptno=dept.deptno and emp1.empno={ID for the wanted worker}

• This should go into the WHERE suffix of the UPDATE command

35OE NIK 2013

Page 36: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

SolutionUPDATE emp1 SET sal=sal+1000WHERE ((comm is null) or (comm = 0)) AND ((select upper(loc) from emp1, dept where

emp1.deptno=dept.deptno and emp1.empno={ID for the wanted worker}) IN ('CHICAGO', 'DALLAS'))

Problem: two instances of emp1 table???ID for the worker???

36OE NIK 2013

Page 37: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

SolutionUPDATE emp1 a SET sal=sal+1000WHERE ((comm is null) or (comm = 0)) AND ((select upper(loc) from emp1 b, dept where

b.deptno=dept.deptno and b.empno=a.empno) IN ('CHICAGO', 'DALLAS'))

Problem: two instances Aliases emp1 a, emp1 bID of the worker a.empno

37OE NIK 2013

Page 38: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Solution

More than one person in the job

• Same principle: approach from the SELECT:select count(*) from emp1 where job={some job};

• Same problems: use alias, use external_query.job

38OE NIK 2013

Page 39: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Solution

UPDATE emp1 a SET SAL=SAL+1000WHERE (select count(*) from emp1 b where

b.job=a.job)>1;

• Now: Combine the previous conditions, so the UPDATE will have a WHERE that contains three conditions

39OE NIK 2013

Page 40: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Solution• The two sub-queries are independents from each other,

so they can have the same table-alias "emp1 b"• It is STRONGLY SUGGESTED to execute a SELECT before

executing the actual UPDATE/DELETE command, so that we can see the affected rows beforehand

SELECT * FROM EMP1 A WHERE((COMM=0) OR (COMM IS NULL)) AND((select upper(loc) from emp1 b, dept where b.deptno=dept.deptno and b.empno=a.empno) IN ('CHICAGO', 'DALLAS')) AND(select count(*) from emp1 b where b.job=a.job)>1;

40OE NIK 2013

Page 41: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Solution #1

UPDATE emp1 a SET sal=sal+1000 WHERE((COMM=0) OR (COMM IS NULL)) AND((select upper(loc) from emp1 b, dept where b.deptno=dept.deptno and b.empno=a.empno) IN ('CHICAGO', 'DALLAS')) AND(select count(*) from emp1 b where b.job=a.job)>1;

41OE NIK 2013

Page 42: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Solution #1 – Not working in MySQL …• ERROR 1093 (HY000): You can't specify target table 'a' for

update in FROM clause [STILL IN 5.7! F..... MYSQL!]• „Correlated Subquery”, this works in Oracle, but it is

slow – and this type of correlated subqueries cannot even be optimized…

• Reason: for every record, we execute a DIFFERENT sub-query to verify if the location and the job are correct

• Possibility to optimize: let's try to create sub-queries that remain the SAME, so that the query optimizer can cache them somehow (in addition, in this case we might be able to use views too…)

42OE NIK 2013

Page 43: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

APPROACH #2• Use lists!• Collect the people who work in the good locations• Collect the jobs where there are enough people OR

Collect the people who have good jobs lists that contain ID fields are always preferred,

especially in Oracle (key-preserved joined views)• Then increase the salaries for those who are in BOTH

LISTS

43OE NIK 2013

Page 44: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Location-condition

• SELECT empno FROM emp1, deptWHERE emp1.deptno=dept.deptno AND

dept.loc IN ('DALLAS', 'CHICAGO');

• Result: a list of those employee IDs who work in Dallas or Chicago

CREATE OR REPLACE VIEW data1 ASSELECT empno FROM emp1, deptWHERE emp1.deptno=dept.deptno AND

dept.loc IN ('DALLAS', 'CHICAGO');

44OE NIK 2013

Page 45: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Job-condition• SELECT job FROM emp1 GROUP BY job

HAVING count(*)>1; Result: a list of those jobs where there are more than

one workers (NOT key-preserved! BAD!)

• SELECT empno FROM emp1 WHERE job IN (SELECT job FROM emp1 GROUP BY job HAVING count(*)>1)

Result: a list of those workers who have good jobsBetter solution (the reason will be explained later):• SELECT empno FROM emp1, (SELECT job FROM emp1

GROUP BY job HAVING count(*)>1) sub WHERE emp1.job=sub.job;

45OE NIK 2013

Page 46: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Job-conditionBetter solution:• SELECT empno FROM emp1, (SELECT job FROM emp1

GROUP BY job HAVING count(*)>1) sub WHERE emp1.job=sub.job;

• CREATE OR REPLACE VIEW data2 ASSELECT job FROM emp1 GROUP BY job HAVING count(*)>1;

• CREATE OR REPLACE VIEW data3 ASSELECT empno FROM emp1, data2 WHERE emp1.job=data2.job;

• (In Oracle, it is possible to merge the two views into one CREATE VIEW)

46OE NIK 2013

Page 47: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Solution #2

UPDATE emp1 a SET sal=sal+1000 WHERE ((COMM=0) OR (COMM IS NULL))

AND empno IN (SELECT empno from data1)

AND empno IN (SELECT empno from data3);

47OE NIK 2013

Page 48: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Solution #2 – not working in MySQL …

• ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause

• This works in Oracle/MSSQL… This is still slow (the subqueries are the same for every row, but by default they are still executed for every row)

• (thought, this is at least optimizable: materialized/indexed view (Oracle/MSSQL) – or CREATE TEMPORARY TABLE)

• or: http://dev.mysql.com/doc/refman/5.0/en/rewriting-subqueries.html

48OE NIK 2013

Page 49: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

APPROACH #3

Correlated subqueries are always slow, because they are executed more than once. AVOID them! Rewrite all the IN subqueries!

SELECT * FROM emp1 WHERE empno IN (…) SELECT emp1.* FROM emp1, (…) sub WHERE

emp1.empno=sub.empno

Same result, but the sub-query is only executed ONCE – decorrelated subquery! Best performance!

49OE NIK 2013

Page 50: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Decorrelated select

SELECT * FROM emp1, (SELECT empno FROM emp1, dept WHERE

emp1.deptno= dept.deptno AND dept.loc IN ('DALLAS', 'CHICAGO') ) sub1,

(SELECT empno FROM emp1, (SELECT job FROM emp1 GROUP BY job HAVING count(*)>1) sub WHERE emp1.job=sub.job) sub2WHERE((COMM=0) OR (COMM IS NULL)) AND

emp1.empno=sub1.empno ANDemp1.empno=sub2.empno;

50OE NIK 2013

Page 51: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Decorrelated select

SELECT * FROM emp1, data1, data3WHERE ((COMM=0) OR (COMM IS NULL)) AND

emp1.empno=data1.empno ANDemp1.empno=data3.empno;

51OE NIK 2013

Page 52: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Decorrelated DML?Avoiding subqueries in the WHERE is usually the fastest

way to execute an SQL command! Can't use MANUAL JOIN in other commands, only in

SELECTIn UPDATE/DELETE, we should avoid correlated subqueries

too (MySQL: they don't work. Oracle: they are slow)Use lists that contain Primary Key fields!

UPDATE emp1 SET xxx WHERE empno IN (…) UPDATE emp1 INNER JOIN (…) sub ON

(emp1.empno=sub.empno) SET xxx

52OE NIK 2013

Page 53: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

APPROACH #3 – this one is FAST (MySQL)UPDATE emp1 INNER JOIN (SELECT empno FROM emp1, dept WHERE

emp1.deptno= dept.deptno AND dept.loc IN ('DALLAS', 'CHICAGO') ) sub1 ON (emp1.empno=sub1.empno)

INNER JOIN (SELECT empno FROM emp1, (SELECT job FROM emp1 GROUP BY job HAVING count(*)>1) sub WHERE emp1.job=sub.job) sub2ON (emp1.empno=sub2.empno)

SET sal=sal+1000WHERE ((COMM=0) OR (COMM IS NULL));

53OE NIK 2013

Page 54: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Let's combine this with views…UPDATE emp1 INNER JOIN data1 ON (emp1.empno=data1.empno)INNER JOIN data3 ON (emp1.empno=data3.empno)SET sal=sal+1000WHERE ((COMM=0) OR (COMM IS NULL));

54OE NIK 2013

Page 55: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

How does it work in Oracle?Almost the same (updateable inline view) :

UPDATE ( SELECT * FROM emp1 INNER JOIN data1 ON (emp1.empno=data1.empno) INNER JOIN data3 ON (emp1.empno=data3.empno))SET sal=sal+1000WHERE ((COMM=0) OR (COMM IS NULL));

Important restriction: key-preserved table/view!!!

55OE NIK 2013

Page 56: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Example #2

Take the two people with the lowest salaries in every departments. Increase their salaries with the salary rate of the department: which is 15% of the difference between the biggest and the smallest salary in a department.

56OE NIK 2013

Page 57: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Necessary data• The difference between the minimum and the maximum

salary in the departments Data1 This will be later used in the SET part of the big UPDATE

• The two people with the lowest salaries in the departments Data2 This will be later used in the WHERE of the big UPDATE

57OE NIK 2013

Page 58: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Data1

• CREATE OR REPLACE VIEW data1 AS SELECTmin(sal) as Minimum,max(sal) as Maximum,max(sal)-min(sal) as Delta,0.15*(max(sal)-min(sal)) as DeltaPct,deptno FROM emp GROUP BY deptno;

• select * from data1;

58OE NIK 2013

Page 59: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Data2 alternative (rownum, Oracle)

• Global order:CREATE OR REPLACE VIEW deptsal AS(SELECT rownum as dsal, al.* FROM (select * from emp order by deptno,sal) al);

• From every department, we need the first two– Advanced function: ROW_NUMBER() OVER (ORDER

BY sal PARTITION BY deptno)– Now:

1 + {number} - {minimum_number_of_department}

59OE NIK 2013

Page 60: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Data2 alternative (rownum, Oracle)

• Order within the departments:

CREATE OR REPLACE VIEW deptsal2 AS (SELECT a.*,(1+a.dsal-

(select min(dsal) from deptsal bwhere b.deptno=a.deptno)

) AS dsal2FROM deptsal a

);

60OE NIK 2013

Page 61: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Data2 alternative (rownum, MySQL)

drop table if exists deptsal;

set @num = 0;create table deptsal as

SELECT empno, deptno, sal, @num := (@num + 1) as dsal

FROM emp ORDER BY deptno, sal;

select * from deptsal;

61OE NIK 2013

Page 62: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Data2 alternative (rownum, MySQL)

drop table if exists deptsal2;

create table deptsal2 AS SELECT a.*,

( 1+a.dsal-(select min(dsal) from deptsal bwhere b.deptno=a.deptno) ) AS dsal2

FROM deptsal a;

select * from deptsal2;62OE NIK 2013

Page 63: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Data2 solution (rownum, both)

• CREATE OR REPLACE VIEW data2 ASSELECT empno FROM deptsal2 WHERE dsal2<=2;

• SELECT ename, sal, emp.deptno, deltapct FROM emp, data1, data2WHERE emp.deptno=data1.deptno AND

emp.empno=data2.empno;

63OE NIK 2013

Page 64: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

UPDATE• UPDATE emp1

SET sal=sal+(select deltapct from data1 where data1.deptno=emp1.deptno)WHERE empno IN (select * from data2);

• In Oracle, it’s a correlated subquery, so it works• In MySQL, we had to use CREATE TABLE to create data2.

Because of this, data2 is not a descendant view of emp1 it works in MySQL too

BUT IT IS SLOW!

64OE NIK 2013

Page 65: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Decorrelated update (MySQL)

• UPDATE emp1INNER JOIN data2 ON (emp1.empno=data2.empno)SET sal=sal+(select deltapct from data1 where data1.deptno=emp1.deptno);

• UPDATE emp1INNER JOIN data1 ON (emp1.deptno=data1.deptno)INNER JOIN data2 ON (emp1.empno=data2.empno)SET sal=sal+deltapct;

OE NIK 2013 65

Page 66: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0

Decorrelated update (Oracle)• Updateable inline view

• UPDATE (

SELECT * FROM emp1INNER JOIN data1 ON (emp1.deptno=data1.deptno)INNER JOIN data2 ON (emp1.empno=data2.empno)

) SET sal=sal+deltapct;

Important restriction: key-preserved table/view!!!

OE NIK 2013 66

Page 67: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

V 1.0 67OE NIK 2013

Page 68: V 1.0 OE NIK 2013 1 DBMAN 6 DDL Constraints DML Transactions Examples

68OE NIK 2013