Oracle 11g vs. MySQL 5.6

advertisement
Oracle 11g
vs.
MySQL 5.6
Ingemar Jansson Haverstad
[email protected]
Keystep AS
16.04.2013
2
Skrevet av Michael McLaughlin
I hovedsak for utviklere
Kan brukes til å migrere begge veier
ISBN 978-0-07-176885-6
SQL
3
ANSI SQL-89
ANSI SQL-92 (JOINs)
Oracle: PL/SQL
MySQL 5.0: SQL/PSM* (ANSI SQL-2003)
PSM: Persistent Stored Module
SQL er SQL...
4
Finnes kun 16 nøkkelord i SQL
Data Definition Language (DDL)
ALTER, DROP, RENAME, CREATE og COMMENT
Data Control Language (DML)
SELECT, INSERT, UPDATE, DELETE og MERGE
Transaction Control Language (TCL)
SAVEPOINT, ROLLBACK og COMMIT
Database og skjema
Oracle:
Skjema er synonymt med bruker.
Skjema er et privat område knyttet til en bruker.
Skjemaer
MySQL:
sys
oe
hr
Databaser
Database tilsvarer Oracle skjema.
Database er ikke knyttet til en bruker.
mysql
information_schema
oe
hr
5
Klientprogrammer
Oracle:
SQL*Plus
SQL*Developer
MySQL:
MySQL Monitor
MySQL Workbench
6
Klientprogrammer
Oracle: SQL*Plus
[email protected]:~$ export ORACLE_SID=XE
[email protected]:~$ sqlplus ingemar/password
SQL*Plus: Release 11.2.0.2.0 Sat Sep 8 20:23:39 2012
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 ­ 64bit
SQL> MySQL: MySQL Monitor
[email protected]:~$ mysql ­u ingemar ­p
Enter password: ##########
Welcome to the MySQL monitor. Commands end with ; or \g.
Server version: 5.6.10 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL>
7
Oracle: SQL*Developer
8
MySQL: MySQL Workbench
9
MySQL: MySQL Workbench
10
Listener
Oracle: Port 1521
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = xps)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.oraklet.no)
)
)
MySQL: Port 3306
localhost
● IP, subdomain eller domain
● %
●
[[email protected] Keystep]$ mysql ­u ingemar ­p ­h xps oe
MySQL> 11
ACID transaksjoner
ACID: atomicity, consistency, isolation, durability
Atomic
Betyr at fullfører eller feiler og retter delendringer.
Consistent
Forandrer seg på samme vise enten det skjer serielt eller i
parallel.
Isolated
Betyr at delendringer ikke vises for andre brukere eller
prosesser.
Durable
Skrives til disk og lagres permanent når transaksjonen er
avsluttet.
12
MySQL engines
InnoDB er standard nå.
MyISAM var standard tidligere.
mysql> show engines;
+­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| Engine | Support | Comment | +­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| FEDERATED | NO | Federated MySQL engine | | MRG_MYISAM | YES | Collection of MyISAM tables | | MEMORY | YES | Hash based, stored in memory | | BLACKHOLE | YES | /dev/null storage engine | | MyISAM | YES | MyISAM storage engine | | CSV | YES | CSV storage engine | | ARCHIVE | YES | Archive storage engine | | InnoDB | DEFAULT | ACID | | PERFORMANCE_SCHEMA | YES | Performance Schema | +­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
13
Installere MySQL server
14
Installasjon på Fedora Linux:
[[email protected] Nedlastinger]$ sudo yum install mysql­server
Loaded plugins: langpacks, presto, refresh­packagekit
Resolving Dependencies
­­> Running transaction check
­­­> Package mysql­server.x86_64 0:5.5.27­1.fc17 will be installed
­­> Processing Dependency: perl­DBI for package: mysql­server­5.5.27­1.fc17.x86_64
­­> Processing Dependency: perl­DBD­MySQL for package: mysql­server­5.5.27­1.fc17.x86_64
­­> Processing Dependency: perl(DBI) for package: mysql­server­5.5.27­1.fc17.x86_64
­­> Running transaction check
Total download size: 13 M
Installed size: 56 M
Is this ok [y/N]: y
Installed:
mysql­server.x86_64 0:5.5.27­1.fc17 Complete!
SQL*Plus
Lokalt:
[email protected]:~$ export ORACLE_SID=XE
[email protected]:~$ sqlplus system/passord
SQL*Plus: Release 11.1.0.7.0 ­ Wed Feb 4 15:37:55 2009
Koblet til: Oracle Database 11g Enterprise Edition 11.1.0.7.0 ­ Production
With the Partitioning, OLAP and Real Application Testing options
SQL> Fra en klient:
[email protected]:~$ sqlplus system/[email protected]
SQL*Plus: Release 11.1.0.7.0 ­ Wed Feb 4 15:40:02 2009
Koblet til: Oracle Database 11g Enterprise Edition 11.1.0.7.0 ­ Production
With the Partitioning, OLAP and Real Application Testing options
SQL>
15
Noen forskjeller...
Oracle:
SQL*Plus formattering.
● SQL*Plus har støtte for avansert SQL aggregering.
● Har eksterne prosedyrer.
● SYS bruker eier meta-dataene.
●
MySQL:
Listener prosessen er en del av database serveren.
● Kan lese og skrive XML-filer.
● root bruker eier meta-datene.
●
16
Klientprogrammer
Oracle: SQL*Plus
[email protected]:~$ export ORACLE_SID=XE
[email protected]:~$ sqlplus ingemar
[email protected]:~$ sqlplus [email protected]
[email protected]:~$ sqlplus [email protected]:1521/XE
MySQL: MySQL Monitor
[email protected]:~$ mysql ­u ingemar ­p
[email protected]:~$ mysql ­­user ingemar –password
[email protected]:~$ mysql ­u ingemar ­p ­P 3306 ­h xps
[email protected]:~$ mysql ­­user ingemar ­­password ­­host xps
17
SQL prompt
Oracle: Legg inn standardverdier i glogin.sql
[email protected]:~$ cd $ORACLE_HOME/sqlplus/admin
/oracle/product/11.2.0.3/db/sqlplus/admin
[email protected]:~$ vi glogin.sql SET SQLPROMPT '_USER"@"_CONNECT_IDENTIFIER SQL> '
[email protected]:~$ sqlplus petter/[email protected]
[email protected] SQL>
MySQL: Flere muligheter
[email protected]:~$ export MYSQL_PS1="(\[email protected]\h) [\d]> "
[email protected]:~$ mysql ­u ingemar ­p ­h xps hr
([email protected]) [hr]>
18
SQL Variabler
19
Oracle: 75 variabler
SQL> SHOW ALL;
appinfo is OFF and set to "SQL*Plus"
arraysize 15
autocommit OFF
autoprint OFF
...
USER is "STUDENT"
verify ON
MySQL: 327 variabler
MySQL> SHOW VARIABLES;
+­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| Variable_name | Value |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
| autocommit | ON |
| ...
Opprette en bruker: Oracle
Opprett en bruker
[email protected] SQL> CREATE USER student IDENTIFIED BY student
DEFAULT TABLESPACE users QUOTA 50M ON users
TEMPORARY TABLESPACE temp;
User created.
Tildel rettigheter
[email protected] SQL> GRANT create session, create table,
create view TO student;
Grant succeeded.
20
Opprette en bruker: MySQL
Opprett en bruker
([email protected]) [(none)]> CREATE USER 'student'@'%' IDENTIFIED BY 'student';
Query OK, 0 rows affected (0.00 sec)
Tildele rettigheter
([email protected]) [(none)]> GRANT ALL ON hr.* TO 'student'@'%';
Query OK, 0 rows affected (0.00 sec)
[email protected]:~$ mysql ­u student ­p ­h xps
([email protected]) [(none)]> USE hr
Database changed
([email protected]) [hr]> [email protected]:~$ mysql ­u student ­p ­h xps hr
([email protected]) [hr]> 21
Brukere: Oracle vs. MySQL
Oracle: sqlnet.ora
tcp.validnode_checking = yes
tcp.invited_nodes = (192.168.0.91)
tcp.validnode_checking = yes
tcp.excluded_nodes = (192.168.0.129)
MySQL:
([email protected]) [(none)]> CREATE USER 'student'@'192.168.0.91'
IDENTIFIED BY 'student';
([email protected]) [(none)]> GRANT ALL ON studentdb.* TO 'student'@'192.168.0.91';
([email protected]) [(none)]> CREATE USER 'student'@'*.mydomain.no'
IDENTIFIED BY 'student';
([email protected]) [(none)]> GRANT ALL ON studentdb.* TO 'student'@'*.mydomain.no';
22
Oracle: SQL*Plus miljø
Dynamisk miljøparametere i SQL*Plus
[email protected] SQL> SHOW ALL;
appinfo is OFF and set to "SQL*Plus"
arraysize 15
autocommit OFF
...
tab ON
termout ON
timing OFF
underline "­" (hex 2d)
USER is "STUDENT"
verify ON
wrap : lines will be wrapped
xmloptimizationcheck OFF
[email protected] SQL> SHOW feedback
FEEDBACK ON for 6 or more rows
[email protected] SQL> SET feedback OFF;
[email protected] SQL> SET autocommit ON;
23
Oracle: SQL*Plus miljø
Standard miljøparametere i SQL*Plus
[email protected] SQL> DEFINE
DEFINE _DATE = "10­SEP­12" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "XE" (CHAR)
DEFINE _USER = "STUDENT" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1102000200" (CHAR)
DEFINE _EDITOR = "ed" (CHAR)
DEFINE _O_VERSION = "Oracle Database 11g Express Edition Release 11.2.0.2.0 ­ 64bit Production" (CHAR)
DEFINE _O_RELEASE = "1102000200" (CHAR)
[email protected] SQL> DEFINE _editor=vi
[email protected] SQL> DEFINE x=" Her er en tekst!"
[email protected] SQL> SELECT '&&x' AS Tekst FROM dual;
Tekst
­­­­­­­­­­­­­­­­­
Her er en tekst!
24
Oracle: SQL*Plus hjelp
25
Hjelp i SQL*Plus
[email protected] SQL> HELP
HELP
­­­­
Accesses this command line help system. Enter HELP INDEX or ? INDEX
for a list of topics.
You can view SQL*Plus resources at
http://www.oracle.com/technology/tech/sql_plus/
and the Oracle Database Library at
http://www.oracle.com/technology/documentation/
HELP|? [topic]
Oracle: SQL*Plus hjelp
26
Hjelp i SQL*Plus
[email protected] SQL> HELP index
Enter Help [topic] for help.
@ COPY PAUSE SHUTDOWN
@@ DEFINE PRINT SPOOL
/ DEL PROMPT SQLPLUS
ACCEPT DESCRIBE QUIT START
APPEND DISCONNECT RECOVER STARTUP
ARCHIVE LOG EDIT REMARK STORE
ATTRIBUTE EXECUTE REPFOOTER TIMING
BREAK EXIT REPHEADER TTITLE
BTITLE GET RESERVED WORDS (SQL) UNDEFINE
CHANGE HELP RESERVED WORDS (PL/SQL) VARIABLE
CLEAR HOST RUN WHENEVER ...
[email protected] SQL> HELP store
Saves attributes of the current SQL*Plus environment in a script.
STORE {SET} file_name[.ext] [CRE[ATE] | REP[LACE] | APP[END]]
MySQL: Monitor hjelp
27
Hjelp i MYSQL Monitor
([email protected]) [hr]> help;
For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
...
MySQL: Monitor hjelp
28
Hjelp i MYSQL Monitor server kommandoer
([email protected]) [hr]> HELP contents
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
Account Management
Administration
Compound Statements
Data Definition
Data Manipulation
Data Types
Functions
Functions and Modifiers for Use with GROUP BY
Geographic Features
Help Metadata
Language Structure
Plugins
Table Maintenance
...
Skrive SQL setninger
Oracle:
[email protected] SQL> SELECT 'Hello World!' FROM dual;
'HELLOWORLD!
­­­­­­­­­­­­
Hello World!
MySQL:
([email protected]) [oe]> SELECT 'Hello World!';
+­­­­­­­­­­­­­­+
| Hello World! |
+­­­­­­­­­­­­­­+
| Hello World! |
+­­­­­­­­­­­­­­+
([email protected]) [oe]> SELECT "Hello World!" FROM dual;
+­­­­­­­­­­­­­­+
| Hello World! |
+­­­­­­­­­­­­­­+
| Hello World! |
+­­­­­­­­­­­­­­+
29
Lese data i MySQL
30
([email protected]) [hr]> SELECT * FROM EMPLOYEES;
+­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­­­+
| EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL | PHONE_NUMBER |...
+­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­­­+
| 100 | Steven | King | SKING | 515.123.4567 |...
+­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­+­­­­­­­+­­­­­­­­­­­­­­+
MySQL: kan benytte \G for å vise kolonner.
([email protected]) [hr]> SELECT * FROM EMPLOYEES\G
*************************** 1. row ***************************
EMPLOYEE_ID: 100
FIRST_NAME: Steven
LAST_NAME: King
EMAIL: SKING
PHONE_NUMBER: 515.123.4567
HIRE_DATE: 2017­06­20
JOB_ID: AD_PRES
SALARY: 24000
COMMISSION_PCT: NULL
MANAGER_ID: NULL
DEPARTMENT_ID: 90
MySQL monitor status
([email protected]) [hr]> STATUS
­­­­­­­­­­­­­­
mysql Ver 14.14 Distrib 5.6.10, for Linux (x86_64)
Connection id:
33
Current database: ougn
Current user:
[email protected]
SSL:
Not in use
Current pager:
stdout
Using outfile:
''
Using delimiter:
;
Server version:
5.6.10 MySQL Community Server (GPL)
Protocol version: 10
Connection:
Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket:
/var/lib/mysql/mysql.sock
Uptime:
7 days 6 hours 54 min 58 sec
Threads: 2 Questions: 211 Slow queries: 0 Opens: 81 Flush tables: 1 Open tables: 68 Queries per second avg: 0.000
­­­­­­­­­­­­­­
31
Kjøre SQL programmer
Oracle:
[email protected] SQL> @create_data.sql
[email protected] SQL> @/home/oracle/bin/create_data.sql
[email protected] SQL> START create_data.sql
[email protected] SQL> ?/rdbms/admin/somescript.sql
MySQL:
([email protected]) [oe]> source create_data.sql
([email protected]) [oe]> source /home/oracle/bin/create_data.sql 32
Håndtere sessions variabler
33
Oracle kan benytte «anonyme» PL/SQL prosedyrer.
[email protected] SQL> SET SERVEROUTPUT ON SIZE UNLIMITED;
[email protected] SQL> BEGIN
dbms_output.put_line('Hello World!');
END;
/
Hello World!
PL/SQL procedure successfully completed.
[email protected] SQL> VARIABLE whom VARCHAR2(20);
BEGIN
:whom := 'Ola';
END;
/
[email protected] SQL> SELECT 'Play it again. ' || :whom || '!' FROM dual;
'PLAYITAGAIN.'||:WHOM||'!'
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Play it again. Ola!
Håndtere sessions variabler
«FROM dual» ikke nødvendig i MySQL.
([email protected]) [hr]> SELECT 1 INTO @myint;
([email protected]) [hr]> SELECT 'Hello World' INTO @mystring;
([email protected]) [hr]> SELECT @myint, @mystring;
+­­­­­­­­+­­­­­­­­­­­­­­+
| @myint | @mystring |
+­­­­­­­­+­­­­­­­­­­­­­­+
| 1 | Hello World! |
+­­­­­­­­+­­­­­­­­­­­­­­+
([email protected]) [hr]> SET @myint := 1;
([email protected]) [hr]> SET @mystring := 'Hello World!';
([email protected]) [hr]> SELECT @myint, @mystring FROM dual;
+­­­­­­­­+­­­­­­­­­­­+
| @myint | @mystring |
+­­­­­­­­+­­­­­­­­­­­+
| NULL | Ola |
+­­­­­­­­+­­­­­­­­­­­+
34
Oracle PL/SQL program
35
Oracle funksjon kan benyttes i en SELECT.
[email protected] SQL> CREATE or REPLACE FUNCTION hello_function
RETURN VARCHAR2
IS
BEGIN
RETURN 'Hello World!';
END hello_function;
/
[email protected] SQL> SELECT hello_function FROM dual;
HELLO_FUNCTION
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
Hello World!
Oracle prosedyrer bruker EXECUTE.
[email protected] SQL> SET SERVEROUTPUT ON
[email protected] SQL> EXECUTE hello_prosedure();
...
MySQL SQL/PSM program
MySQL har ikke «anonyme pakker».
([email protected]) [hr]> DELIMITER $$
([email protected]) [hr]> CREATE FUNCTION hello_function() RETURNS VARCHAR(12)
BEGIN
RETURN "Hello World!";
END;
$$
Query OK, 0 rows affected (0.01 sec)
([email protected]) [hr]> DELIMITER ;
([email protected]) [hr]> SELECT hello_function();
+­­­­­­­­­­­­­­­­­­+
| hello_function() |
+­­­­­­­­­­­­­­­­­­+
| Hello World! |
+­­­­­­­­­­­­­­­­­­+
36
SQL loggfiler
Oracle:
[email protected] SQL> SET ECHO ON
[email protected] SQL> SPOOL /home/oracle/log/Create_data
[email protected] SQL> SPOOL /home/oracle/log/Create_data APPEND
[email protected] SQL> SPOOL OFF MySQL:
([email protected]) [oe]> TEE /home/MySQL/log/Create_data.txt
([email protected]) [oe]> NOTEE
37
SQL «escape»
Oracle:
[email protected] SQL> HOST ls ­ltr
...
[email protected] SQL> !ls ­ltr
...
MySQL:
([email protected]) [oe]> SYSTEM ls ­ltr
38
Rettigheter
Oracle
MySQL
ALTER ANY CLUSTER...
ALTER ANY SEQUENCE...
ALTER
ALTER
ALTER ANY PROCEDURE...
ALTER ROUTINE
CREATE ANY DATABASE...
CREATE
CREATE ANY PROCEDURE
CREATE ROUTINE
CREATE USER
CREATE USER
DELETE ANY TABLE...
DELETE
DROP ANY TABLE...
DROP
EXECUTE ANY PROCEDURE
EXECUTE
SELECT ANY TABLE
SELECT
SELECT ANY TABLE
SUPER
CREATE SESSION...
USAGE
39
Eksempel: rettigheter
Oracle:
[email protected] SQL> GRANT CREATE ANY TABLE TO student;
Grant succeeded.
[email protected] SQL> GRANT ALL ON lib.sample TO student;
Grant succeeded.
MySQL:
([email protected]) [(none)]> GRANT CREATE ON hr.* TO [email protected];
Query OK, 0 rows affected (0.00 sec)
([email protected]) [(none)]> GRANT CREATE ON hr.* TO [email protected]'%';
Query OK, 0 rows affected (0.00 sec)
([email protected]) [(none)]> GRANT ALL ON lib.* TO [email protected]'%';
Query OK, 0 rows affected (0.00 sec)
40
Eksempel: metadata
Oracle:
[email protected] SQL> SELECT utp.owner, utp.grantee, utp.privilege
FROM user_tab_privs utp
WHERE utp.table_name = 'SAMPLE';
OWNER GRANTEE PRIVILEGE
­­­­­­ ­­­­­­­­ ­­­­­­­­­­­­­­­­­­
LIB STUDENT ALTER
LIB STUDENT DEBUG
LIB STUDENT DELETE
LIB STUDENT FLASHBACK
LIB STUDENT INDEX
LIB STUDENT INSERT
LIB STUDENT ON COMMIT REFRESH
LIB STUDENT QUERY REWRITE
LIB STUDENT REFERENCES
LIB STUDENT SELECT
LIB STUDENT UPDATE
11 rows selected.
41
Eksempel: metadata
MySQL:
([email protected]) [lib]> SELECT tp.table_schema,
tp.table_name,
tp.privilege_type,
tp.grantee
FROM information_schema.table_privileges tp
WHERE tp.table_schema = 'lib'
AND tp.table_name = 'sample'
AND tp.grantee REGEXP '.*student.*';
+­­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­+
| table_schema | table_name | privilege_type | grantee |
+­­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­+
| lib | sample | ALTER | 'student'@'%' |
| lib | sample | CREATE | 'student'@'%' |
| lib | sample | CREATE VIEW | 'student'@'%' |
| lib | sample | DELETE | 'student'@'%' |
...
| lib | sample | TRIGGER | 'student'@'%' |
| lib | sample | UPDATE | 'student'@'%' |
+­­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­+
12 rows in set (0.00 sec)
42
Eksempel: metadata
MySQL:
([email protected]) [lib]> SELECT ...
WHERE tp.table_schema = 'lib'
AND tp.table_name = 'sample'
AND tp.grantee REGEXP '.*student.*';
+­­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­+
| table_schema | table_name | privilege_type | grantee |
+­­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­+
| lib | sample | ALTER | 'student'@'%' |
| lib | sample | CREATE | 'student'@'%' |
| lib | sample | CREATE VIEW | 'student'@'%' |
| lib | sample | DELETE | 'student'@'%' |
| lib | sample | DROP | 'student'@'%' |
| lib | sample | INDEX | 'student'@'%' |
| lib | sample | INSERT | 'student'@'%' |
| lib | sample | REFERENCES | 'student'@'%' |
| lib | sample | SELECT | 'student'@'%' |
| lib | sample | SHOW VIEW | 'student'@'%' |
| lib | sample | TRIGGER | 'student'@'%' |
| lib | sample | UPDATE | 'student'@'%' |
+­­­­­­­­­­­­­­+­­­­­­­­­­­­+­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­+
43
Eksempel: Tabell
44
ORACLE:
SQL> CREATE TABLE member
(member_id NUMBER CONSTRAINT pk_member PRIMARY KEY,
member_type NUMBER,
account_no VARCHAR2(10) CONSTRAINT nn_member_1 NOT NULL,
created_by NUMBER CONSTRAINT nn_member_2 NOT NULL,
creation_date DATE CONSTRAINT nn_member_3 NOT NULL,
CONSTRAINT fk_member_1
FOREIGN KEY(created_by)
REFERENCES system_user(system_user_id));
Table created.
SQL> CREATE SEQUENCE member_id_seq;
Sequence created.
SQL> CREATE TRIGGER member_id_trigger
...;
Trigger created.
Eksempel: Tabell
MySQL:
MySQL > CREATE TABLE member
(member_id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
member_type INT UNSIGNED,
account_no CHAR(10) NOT NULL,
created_by INT UNSIGNED NOT NULL,
creation_date DATE NOT NULL,
CONSTRAINT fk_member_1 FOREIGN KEY(created_by)
REFERENCES system_user(system_user_id));
Query OK, 0 rows affected (0.01 sec)
45
Eksempel: DESCRIBE
46
Oracle:
SQL> DESCRIBE member;
Name
Null? Type
­­­­­­­­­­­­­­­­­­­­ ­­­­­­­­ ­­­­­­­­­­­­­­­­­­­­­­­­­­­­
MEMBER_ID
NOT NULL NUMBER
MEMBER_TYPE
NUMBER
ACCOUNT_NO NOT NULL VARCHAR2(10)
CREATED_BY
NOT NULL NUMBER
CREATION_DATE
NOT NULL DATE
MySQL:
MySQL> DESCRIBE member;
+­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­­­­­­­­+
| Field | Type | Null | Key | Extra | +­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­­­­­­­­+
| member_id | int(10) unsigned | NO | PRI | auto_increment |
| member_type | int(10) unsigned | YES | | | | account_no | char(10) | NO | | | | created_by | int(10) unsigned | NO | MUL | | | creation_date | date | NO | | | +­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­­­­­­­­+
Eksempel: Tabell DDL
47
Oracle:
SQL> SET LONG 300000
SQL> SELECT dbms_metadata.get_ddl('TABLE','MEMBER') FROM dual;
CREATE TABLE "STUDENT"."MEMBER" (
"MEMBER_ID" NUMBER,
"MEMBER_TYPE" NUMBER,
"ACCOUNT_NO" VARCHAR2(10) CONSTRAINT "NN_MEMBER_1" NOT NULL ENABLE,
"CREATED_BY" NUMBER CONSTRAINT "NN_MEMBER_2" NOT NULL ENABLE,
"CREATION_DATE" DATE CONSTRAINT "NN_MEMBER_3" NOT NULL ENABLE,
CONSTRAINT "PK_MEMBER" PRIMARY KEY ("MEMBER_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(...)
TABLESPACE "SYSTEM" ENABLE,
CONSTRAINT "FK_MEMBER_1" FOREIGN KEY ("CREATED_BY")
REFERENCES "STUDENT"."SYSTEM_USER" ("SYSTEM_USER_ID") ENABLE )
SEGMENT CREATION IMMEDIATE ...
TABLESPACE "USERS"
Eksempel: Tabell DDL
48
MySQL:
MySQL> SHOW CREATE TABLE member;
+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| Table | Create Table |
+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| member | CREATE TABLE `member` ( |
| `member_id` int(10) unsigned NOT NULL AUTO_INCREMENT, |
| `account_number` char(10) NOT NULL, |
| `creation_date` date DEFAULT NULL, |
| PRIMARY KEY (`member_id`) |
|) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
XXX???
49
Oracle:
[email protected] SQL> CONNECT student/password
Connected.
[email protected] SQL> CREATE TABLE hr.sample(sample_id NUMBER);
Table created.
MySQL:
[email protected]:~$ mysql ­u student ­p ­h xps hr
([email protected]) [hr]> CREATE TABLE oe.sample(sample_id INT UNSIGNED);
Query OK, 0 rows affected (0.00 sec)
Datatyper
ANSI Data type
BLOB
CHAR(n)
DATE
DECIMAL(p,s)
DOUBLE PRECISION
FLOAT
INT
INTEGER
NUMERIC(p,s)
REAL
SMALLINT
TIMESTAMP
VARCHAR(n)
50
Oracle
Størrelse
BLOB
CHAR(n)
DATE
NUMBER(p,s)
FLOAT(126)
FLOAT(126)
NUMBER(38)
NUMBER(38)
NUMBER(p,s)
FLOAT(63)
NUMBER(38)
TIMESTAMP
VARCHAR2(n)
8-32 TB
2000 bytes
1/100 sekund
1 x 1038
1 x 1026
1 x 1026
1 x 1038
1 x 1038
1 x 1038
1 x 1063
1 x 1038
1/100 sekund
4000 bytes
MySQL: Update trigger
MySQL> DELIMITER $$
MySQL> CREATE TRIGGER sample_trigger
BEFORE INSERT ON sample
BEGIN
SET new.created = current_timestamp(); END;
$$
Query OK, 0 rows affected (0.01 sec)
MySQL> DELIMITER ;
MySQL> INSERT INTO sample VALUES(NULL, NULL, NULL);
Query OK, 1 row affected (0.01 sec)
MySQL> SELECT * FROM sample;
+­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+
| sample_id | created | updated |
+­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+
| 1 | 2012­09­17 15:15:00 | 2012­09­17 15:15:00 |
+­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­+
51
Oracle: CHECK regel
Oracle:
SQL> CREATE TABLE club_member
(club_member_id NUMBER PRIMARY KEY,
first_name VARCHAR2(30),
last_name VARCHAR2(30),
Gender CHAR(1) DEFAULT 'F' CHECK (gender IN ('M', 'F')));
Table created.
52
MYSQL: ENUM regel
MySQL: ENUM kan ha 65535 verdier.
MySQL> CREATE TABLE indexed_list
(var1 ENUM('Female','Male'),
var2 ENUM('Female','Male') DEFAULT 'Male',
var3 ENUM('Female','Male') NOT NULL);
MySQL> INSERT INTO indexed_list VALUES(NULL, NULL, 'Male');
Query OK, 1 row affected (0.00 sec)
MySQL> INSERT INTO indexed_list VALUES(NULL, 'Female', 'Male');
Query OK, 1 row affected (0.00 sec)
MySQL> INSERT INTO indexed_list VALUES('Female', NULL, 'Male');
Query OK, 1 row affected (0.00 sec)
MySQL> SELECT * FROM indexed_list;
+­­­­­­­­+­­­­­­­­+­­­­­­+
| var1 | var2 | var3 |
+­­­­­­­­+­­­­­­­­+­­­­­­+
| NULL | NULL | Male |
| NULL | Female | Male |
| Female | NULL | Male |
+­­­­­­­­+­­­­­­­­+­­­­­­+
53
MYSQL: SET regel
MySQL: SET kan ha opptil 64 verdier.
MySQL> CREATE TABLE inclusive_set
(var1 SET('Ham','Pepperoni','Pineapple','Salami'));
MySQL> INSERT INTO inclusive_set VALUES('Ham');
Query OK, 1 row affected (0.00 sec)
MySQL> INSERT INTO inclusive_set VALUES('Chicken');
Query OK, 1 row affected, 1 warning (0.01 sec)
MySQL> SELECT * FROM inclusive_set;
+­­­­­­+
| var1 |
+­­­­­­+
| Ham |
| |
+­­­­­­+
54
Datatyper INT/INTEGER
55
MySQL
Undertype
Bytes
Verdier
TINYINT
TINYINT
SMALLINT
SMALLINT
MEDIUMINT
MEDIUMINT
INT
SIGNED
UNSIGNED
SIGNED
UNSIGNED
SIGNED
UNSIGNED
SIGNED
1
1
2
2
3
3
4
INT
BIGINT
BIGINT
UNSIGNED
SIGNED
UNSIGNED
4
8
8
-128..127
0..255
-32.768..32.767
0..65.535
-8.388.608..8.388.607
0..16.777.215
-2.147.483.648..
2.147.483.648
0..4.294.967.295
-9.2 x 10-18..9.2 x 1018
0..1.8 x 1019
Oracle: Håndtering av tall
56
Oracle: NUMBER mest benyttet.
SQL> CREATE TABLE sample_numbers
(num1 NUMBER(12,2) CONSTRAINT nn_sample1 NOT NULL,
num2 NUMBER,
num3 FLOAT(12),
num4 NUMBER(38,0) CHECK (num4 >= 0)); ­­ UNSIGNED INT
Table created.
SQL> INSERT INTO sample_numbers VALUES(24.62, 24, 24.62, 24);
SQL> INSERT INTO sample_numbers (num1, num4) VALUES (­1, ­1);
INSERT INTO sample_numbers (num1, num4) VALUES (­1, ­1)
*
ERROR at line 1:
ORA­02290: check constraint (STUDENT.SYS_C0010364) violated
Oracle: Håndtering av tall
Metadata: Bruk ALL_, DBA_ og USERS_ views.
SQL> SELECT utc.column_id, utc.column_name, utc.data_type, utc.data_length, utc.data_precision
FROM user_tab_columns utc
WHERE utc.table_name = 'SAMPLE_NUMBERS';
Column
ID # Column Name Data Type Data Length Data Precision
­­­­­­ ­­­­­­­­­­­ ­­­­­­­­­­ ­­­­­­­­­­­ ­­­­­­­­­­­­­­
1 NUM1 NUMBER 22 12
2 NUM2 NUMBER 22
3 NUM3 FLOAT 22 12
4 NUM4 NUMBER 22 38
57
MySQL: Håndtering av tall
MySQL: Bruk DECIMAL, de andre er alias.
SQL> CREATE TABLE number_table
(num1 DEC(65,2),
num2 DECIMAL(65,2),
num3 NUMERIC(65,2),
num4 FIXED(65,2));
Query OK, 0 rows affected (0.01 sec)
MySQL> DESCRIBE number_table;
+­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­+­­­­­­­+
| Field | Type | Null | Key | Default | Extra |
+­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­+­­­­­­­+
| num1 | decimal(65,2) | YES | | NULL | |
| num2 | decimal(65,2) | YES | | NULL | |
| num3 | decimal(65,2) | YES | | NULL | |
| num4 | decimal(65,2) | YES | | NULL | |
+­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­+­­­­­­­+
4 rows in set (0.00 sec)
58
MySQL: Håndtering av tall
59
Metadata: columns, table_constraints,
key_column_usage.
MySQL> USE information_schema;
MySQL> SELECT c.ordinal_position, c.table_name, c.column_name,
c.data_type,
IFNULL(c.character_maximum_length,c.numeric_precision)
AS Size
FROM columns c
WHERE c.table_name = 'number_table';
+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­+­­­­­­­­­­­­­+­­­­­­­­­­­+­­­­­­+
| ordinal_position | table_name | column_name | data_type | Size |
+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­+­­­­­­­­­­­­­+­­­­­­­­­­­+­­­­­­+
| 1 | number_table | num1 | decimal | 65 |
| 2 | number_table | num2 | decimal | 65 |
| 3 | number_table | num3 | decimal | 65 |
| 4 | number_table | num4 | decimal | 65 |
+­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­+­­­­­­­­­­­­­+­­­­­­­­­­­+­­­­­­+
MySQL: Unsigned integers
60
MySQL:
SQL> CREATE TABLE integer_sample
(positive_spectrum INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
full_spectrum INT NOT NULL,
fill_full_spectrum INT ZEROFILL,
fill_pos_spectrum INT UNSIGNED ZEROFILL);
Query OK, 0 rows affected (0.01 sec)
MySQL> DESCRIBE integer_sample;
+­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+...
| Field | Type | Null | Key |...
+­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+...
| positive_spectrum | int(10) unsigned | NO | PRI |...
| full_spectrum | int(11) | NO | |...
| fill_full_spectrum | int(10) unsigned zerofill | YES | |...
| fill_pos_spectrum | int(10) unsigned zerofill | YES | |...
+­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+...
4 rows in set (0.00 sec)
MySQL: Unsigned integers
61
MySQL:
SQL> CREATE TABLE integer_sample
(positive_spectrum INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
full_spectrum INT NOT NULL,
fill_full_spectrum INT ZEROFILL,
fill_pos_spectrum INT UNSIGNED ZEROFILL);
Query OK, 0 rows affected (0.01 sec)
MySQL> DESCRIBE integer_sample;
+­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+...
| Field | Type | Null | Key |...
+­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+...
| positive_spectrum | int(10) unsigned | NO | PRI |...
| full_spectrum | int(11) | NO | |...
| fill_full_spectrum | int(10) unsigned zerofill | YES | |...
| fill_pos_spectrum | int(10) unsigned zerofill | YES | |...
+­­­­­­­­­­­­­­­­­­­­+­­­­­­­­­­­­­­­­­­­­­­­­­­­+­­­­­­+­­­­­+...
4 rows in set (0.00 sec)
MySQL: Håndtering av tekst
MySQL: VARCHAR mest benyttet.
SQL> CREATE TABLE variable_width
(var1 VARCHAR(10),
var2 VARCHAR(10) BINARY,
var3 CHARACTER VARYING(10),
var4 NVARCHAR(10),
var5 NATIONAL CHARACTER(10),
var6 VARBINARY(10));
MySQL> DESCRIBE variable_width;
+­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­+­­­­­­­+
| Field | Type | Null | Key | Default | Extra |
+­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­+­­­­­­­+
| var1 | varchar(10) | YES | | NULL | |
| var2 | varchar(10) | YES | | NULL | |
| var3 | varchar(10) | YES | | NULL | |
| var4 | varchar(10) | YES | | NULL | |
| var5 | char(10) | YES | | NULL | |
| var6 | varbinary(10) | YES | | NULL | |
+­­­­­­­+­­­­­­­­­­­­­­­+­­­­­­+­­­­­+­­­­­­­­­+­­­­­­­+
62
MySQL: DATE/TIME
MySQL
Format
DATE
DATETIME
TIMESTAMP
TIME
YEAR
'YYYY-MM-DD'
'YYYY-MM-DD HH24:MI:SS'
'YYYY-MM-DD HH24:MI:SS'
'HH24:MI.SS'
'YYYY'
MySQL> SELECT current_date();
+­­­­­­­­­­­­­­­­+
| current_date() |
+­­­­­­­­­­­­­­­­+
| 2012­09­17 |
+­­­­­­­­­­­­­­­­+
MySQL> SELECT current_timestamp();
+­­­­­­­­­­­­­­­­­­­­­+
| current_timestamp() |
+­­­­­­­­­­­­­­­­­­­­­+
| 2012­09­17 13:39:52 |
+­­­­­­­­­­­­­­­­­­­­­+
63
MySQL:
DATETIME og TIMESTAMP
64
MySQL: Kan kun ha EN TIMESTAMP kolonne i en
tabell.
SYSDATE som i Oracle må simuleres.
MySQL> CREATE TABLE sample
(sample_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
created DATETIME, updated TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
Oracle: Partisjonering
65
Oracle: RANGE, LIST, HASH og SAMMENSATT.
Eksempel: LIST partisjonering:
SQL> CREATE TABLE franchise
(franchise_id NUMBER CONSTRAINT pk_franchise PRIMARY KEY,
franchise_name VARCHAR2(20), city VARCHAR2(20),
state VARCHAR2(20))
PARTITION BY LIST(state)
(PARTITION offshore VALUES('Alaska','Hawaii'),
PARTITION west VALUES('California','Oregon','Washington'),
PARTITION desert VALUES('Arizon','New Mexico'),
PARTITION rockies VALUES('Colorado','Idaho','Montana','Wyoming')
...);
CREATE TABLE franchise
*
ERROR at line 1:
ORA­00439: feature not enabled: Partitioning
MySQL: Partisjonering
66
MySQL supporterer RANGE, LIST, HASH og KEY.
Eksempel: RANGE partisjonering:
MySQL> CREATE TABLE ordering
(ordering_id INT UNSIGNED AUTO_INCREMENT,
item_id INT UNSIGNED, rental_amount DECIMAL(15,2),
rental_date DATE,
index idx(ordering_id))
PARTITION BY RANGE(item_id)
(PARTITION jan2012 VALUES LESS THAN(10000),
PARTITION feb2012 VALUES LESS THAN(20000),
PARTITION mar2012 VALUES LESS THAN(30000));
MySQL: Partisjonering
67
MySQL RANGE slik du er vant til i Oracle.
MySQL> CREATE TABLE report_status (
report_id INT NOT NULL,
report_status VARCHAR(20) NOT NULL,
report_updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)
PARTITION BY RANGE ( UNIX_TIMESTAMP(report_updated) ) (
PARTITION jan2012 VALUES LESS THAN ( UNIX_TIMESTAMP('2012­02­01 00:00:00') ),
PARTITION feb2012 VALUES LESS THAN ( UNIX_TIMESTAMP('2012­03­01 00:00:00') ),
PARTITION mar2012 VALUES LESS THAN ( UNIX_TIMESTAMP('2012­04­01 00:00:00') ),
...
PARTITION div2012 VALUES LESS THAN (MAXVALUE));
Query OK, 0 rows affected (0.02 sec)
SELECT: Ulike funksjoner
CONCAT
Oracle:
SQL> SELECT i.title AS title, 'MPAA: ' || i.rating AS rating FROM item i;
TITLE RATING
­­­­­­­­­­­­­­­ ­­­­­­­­­­­­­­­­
Star Wars I MPAA: PG
Star Wars II MPAA: PG
MySQL:
MySQL> SELECT i.title AS title, CONCAT('MPAA: ', i.rating) AS rating FROM item i;
+­­­­­­­­­­­­­­+­­­­­­­­­­+
| title | rating |
+­­­­­­­­­­­­­­+­­­­­­­­­­+
| Star Wars I | MPAA: PG |
| Star Wars II | MPAA: PG |
+­­­­­­­­­­­­­­+­­­­­­­­­­+
68
SELECT: Ulike funksjoner
LIMIT
Oracle:
SQL> SELECT i.title AS title FROM item i WHERE rownum <= 2;
TITLE
­­­­­­­­­­­­­­­
Star Wars I
Star Wars II
MySQL: LIMIT offset, n
MySQL> SELECT i.title AS title FROM item i LIMIT 2;
+­­­­­­­­­­­­­­+
| title |
+­­­­­­­­­­­­­­+
| Star Wars I |
| Star Wars II |
+­­­­­­­­­­­­­­+
69
SELECT: Ulike funksjoner
Regulære uttrykk
Oracle:
SQL> SELECT i.title AS title FROM item i WHERE REGEXP_LIKE(i.title, '^Star Wars.+');
TITLE
­­­­­­­­­­­­­­­
Star Wars I
Star Wars II
MySQL:
SQL> SELECT i.title AS title FROM item i WHERE i.title REGEXP '^Star Wars.+';
+­­­­­­­­­­­­­­+
| title |
+­­­­­­­­­­­­­­+
| Star Wars I |
| Star Wars II |
+­­­­­­­­­­­­­­+
70
INSERT
Oracle
SQL> DESCRIBE rental
SQL> INSERT INTO rental
Name Null? Type VALUES
­­­­­­­­­­­­­­­­­ ­­­­­­­­ ­­­­­­
(rental_seq.nextval,
RENTAL_ID NOT NULL NUMBER 1009,
CUSTOMER_ID NOT NULL NUMBER SYSDATE,
CHECK_OUT_DATE NOT NULL DATE TRUNC(SYSDATE + 5),
RETURN_DATE DATE 1,
CREATED_BY NOT NULL NUMBER SYSDATE,
CREATION_DATE NOT NULL DATE 1,
LAST_UPDATED_BY NOT NULL NUMBER SYSDATE);
LAST_UPDATE_DATE NOT NULL DATE
71
INSERT
72
Oracle
MySQL
SQL> INSERT INTO rental
VALUES
(rental_seq.NEXTVAL,
1009,
SYSDATE,
TRUNC(SYSDATE + 5),
1,
SYSDATE,
1,
SYSDATE);
MySQL> INSERT INTO rental
VALUES
(NULL,
1009,
NOW(),
ADDDATE(UTC_DATE(),INTERVAL 5 DAY),
1,
NOW(),
1,
NOW());
INSERT
MySQL: AUTO_INCREMENT
MySQL> SHOW TABLE STATUS LIKE 'rental'\G
*************************** 1. row ***************************
Name: rental
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 2
Avg_row_length: 8192
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 5242880
Auto_increment: 2
Create_time: 2012­09­19 08:22:28
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
73
INSERT logikk
Oracle:
74
MySQL:
MySQL> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
SQL> INSERT INTO parent VALUES
(parent_seq.NEXTVAL, 'One Parent');
1 row created.
MySQL> INSERT INTO parent
VALUES
(NULL, 'One Parent');
Query OK, 1 row affected (0.00 sec)
SQL> INSERT INTO child VALUES
(child_seq.NEXTVAL, parent_seq.CURRVAL, 'One Child');
1 row created.
MySQL> INSERT INTO child VALUES
(NULL, last_insert_id(), 'One Child');
Query OK, 1 row affected (0.00 sec)
SQL> COMMIT;
MySQL> COMMIT;
Query OK, 0 rows affected (0.01 sec)
Commit complete.
UPDATE
ORACLE:
SET
SQL> UPDATE item
SET (item_title, item_rating) =
(SELECT item_title, item_rating
FROM import_item
WHERE item_barcode = 'B004A8ZWUG')
WHERE item_title = 'Pirates of the Caribbean 4';
MySQL:
LIMIT, IGNORE, ORDER BY og runtime priority
SQL> UPDATE teeshirt
SET teeshirt_size = DEFAULT
ORDER BY teeshirt_id DESC
LIMIT 2;
75
UPDATE – flere tabeller
76
MySQL:
SQL> UPDATE rental_item ri
SET ri.rental_item_price = (SELECT p.amount
FROM price p CROSS JOIN rental r
WHERE p.item_id = ri.item_id
AND p.price_type = ri_rental_item_type
AND r.rental_id = ri.rental_id
AND r.check_out_date BETWEEN p.start_date AND IFNULL(p.end_date, UTC_DATE());
MySQL: (ikke relevant for Oracle)
SQL> UPDATE rental_item ri
JOIN rental r ON ri.rental_id
JOIN price p ON ri.item_id = p.item_id
AND ri.rental_item_type = p.price_type
AND r.check_out_date BETWEEN p.start_date AND IFNULL(p.end_date, UTC_DATE())
SET ri.rental_item_price = p.amount;
DELETE – ANSI SQL-89/92
77
MySQL ANSI SQL-89:
SQL> DELETE rental_item FROM rental_item, price p, rental r
WHERE p.item_id = rental_item.item_id
AND p.price_type = rental_item.rental_item_type
AND r.rental_id = rental_item.rental_id
AND r.check_out_date BETWEEN p_start_date
AND IFNULL(p.end_date, UTC_DATE());
MySQL: ANSI SQL-92:
SQL> DELETE ri, p, r FROM rental_item AS ri INNER JOIN price AS p
ON ri.item_id = p.item AND ri.rental_item_type = p.price_type
INNER JOIN rental AS r ON ri.rental_id = r.rental_id
WHERE r.check_out_date BETWEEN p_start_date
AND IFNULL(p.end_date, UTC_DATE());
Spørring, føye sammen data
ORACLE:
||-tegn (pipe)
SQL> SELECT i.item_title AS title,
'MPAA: ' || i.item_rating AS rating
FROM item i
WHERE i.item_rating = 'PG'
AND i.item_title = 'Star Wars%;
MySQL:
CONCAT-funksjon
SQL> SELECT i.item_title AS title,
CONCAT('MPAA: ', i.item_rating) AS rating
FROM item i
WHERE i.item_rating = 'PG'
AND i.item_title = 'Star Wars%;
CONCAT('Tekst ', CONCAT('Mere tekst ', 'Tekst igjen.'))
78
Spørring, formatering
79
ORACLE:
TO_CHAR-funksjon
SQL> SELECT t.transaction_date,
TO_CHAR(t.transaction_amount, '90.00') AS price,
TO_CHAR(t.transaction_amount * .25, '90.00') AS tax
FROM transaction t
WHERE t.transaction_date = '10­JAN­2013';
MySQL:
FORMAT-funksjon
SQL> SELECT t.transaction_date,
FORMAT(t.transaction_amount, 2) AS price,
FORMAT(t.transaction_amount * .25, 2) AS tax
FROM transaction t
WHERE t.transaction_date = '10­JAN­2013';
Regulære utrykk
ORACLE:
REGEXP_LIKE-funksjon
SQL> SELECT i.item_title AS title,
'MPAA: ' || i.item_rating AS rating
FROM item i
WHERE i.item_rating = 'PG'
AND REGEXP_LIKE(i.item_title, '^Star Wars.+');
MySQL:
REGEXP-funksjon
SQL> SELECT i.item_title AS title,
CONCAT('MPAA: ', i.item_rating) AS rating
FROM item i
WHERE i.item_rating = 'PG'
AND i.item_title REGEXP '^Star Wars.+';
80
Inline view
ORACLE:
SQL> SELECT c.first_name || ' ' || c.last_name AS person,
inline.street_address,
inline.city,
inline.state_province
FROM contact c INNER JOIN
(SELECT a.contact_id,
sa.street_address,
a.city,
a.state_province
FROM address a INNER JOIN street_address sa
ON a.address_id = sa.address_id) inline
ON inline.contact_id = c.contact_id;
81
Inline view
MySQL:
SQL> SELECT CONCAT(c.first_name, ' ', c.last_name AS person,
inline.street_address,
inline.city,
inline.state_province
FROM contact c INNER JOIN
(SELECT a.contact_id,
sa.street_address,
a.city,
a.state_province
FROM address a INNER JOIN street_address sa
ON a.address_id = sa.address_id) inline
ON inline.contact_id = c.contact_id;
82
Inline view med WITH
ORACLE:
ANSI SQL-1999
SQL> WITH inline AS
(SELECT a.contact_id,
sa.street_address,
a.city,
a.state_province
FROM address a INNER JOIN street_address sa
ON a.address_id = sa.address_id)
SELECT c.first_name || ' ' || c.last_name AS person,
inline.street_address,
inline.city,
inline.state_province
FROM contact c INNER JOIN inline inline
ON inline.contact_id = c.contact_id;
83
SQL utvalg
84
ORACLE:
DECODE-funksjon
SQL> SELECT DECODE('One', 'One', 'Same') AS "Test 1",
DECODE('One', 'Two', 'Same') AS "Test 2",
DECODE('One', 'One', 'Same','Not same') AS "Test 2",
DECODE('One', 'Two', 'Same','Not same') AS "Test 2"
FROM dual;
Test 1 Test 2 Test 2
Test 2
­­­­­­­­­­ ­­­­­­­­­­ ­­­­­­­­­­ ­­­­­­­­­­
Same
NULL Same Not same
●
Ikke portabelt.
SQL utvalg
ORACLE:
DECODE eksempel
SQL> WITH inline AS
(SELECT 'Single' AS marital_status FROM dual
UNION ALL
SELECT 'Single' AS marital_status FROM dual
UNION ALL
SELECT 'Married' AS marital_status FROM dual)
SELECT COUNT(DECODE(inline.marital_status,'Single',1)) AS Single
FROM inline;
SINGLE
­­­­­­­­­­
2
85
SQL utvalg
86
MySQL:
IF
SQL> SELECT IF('Star Wars VII' IN (SELECT item_title FROM title) , 'In Stock', 'Out of Stock') AS "Stock";
+­­­­­­­­­­­­­­­­­­­­­­+
| Stock |
+­­­­­­­­­­­­­­­­­­­­­­+
| Out og stock |
+­­­­­­­­­­­­­­­­­­­­­­+
●
Ikke portabelt.
SQL utvalg - ANSI
ORACLE:
CASE eksempel
SQL> SELECT CASE
WHEN 'Star Wars VII' IN (SELECT item_title FROM item)
THEN 'In­stock'
ELSE 'Out­of­stock'
END AS yes_no_answer
FROM dual;
YES_NO_ANSWER
­­­­­­­­­­­­­
In­stock
87
SQL utvalg - ANSI
88
MySQL:
CASE
[email protected] mySQL> SELECT ­> CASE
­> WHEN 'Star Wars VII' IN (SELECT item_title FROM item)
­> THEN 'In­stock'
­> ELSE 'Out­of­stock'
­> END AS yes_no_answer
­> FROM dual;
+­­­­­­­­­­­­­­­+
| yes_no_answer |
+­­­­­­­­­­­­­­­+
| In­stock |
+­­­­­­­­­­­­­­­+
1 row in set (0,00 sec)
SQL utvalg - Dato
89
ORACLE:
DATE funksjon
[email protected] SQL> SELECT SYSDATE ­ 1 AS Yesterday,
SYSDATE AS Today,
SYSDATE + 1 AS Tomorrow FROM dual;
YESTERDAY TODAY TOMORROW
­­­­­­­­­­ ­­­­­­­­­­ ­­­­­­­­­­
14/04/2013 15/04/2013 16/04/2013
SQL> ALTER SESSION SET nls_date_format='DD/MM/YYYY HH24:MI:SS';
[email protected] SQL> SELECT SYSDATE ­ 1 AS Yesterday,
SYSDATE AS Today,
SYSDATE + 1 AS Tomorrow FROM dual;
YESTERDAY TODAY
TOMORROW
­­­­­­­­­­­­­­­­­­­ ­­­­­­­­­­­­­­­­­­­ ­­­­­­­­­­­­­­­­­­­
14/04/2013 07:22:12 15/04/2013 07:22:12 16/04/2013 07:22:12
SQL utvalg - Dato
MySQL:
DATE funksjon
mysql> SELECT DATE_ADD(UTC_DATE(), INTERVAL 6 DAY);
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| DATE_ADD(UTC_DATE(), INTERVAL 6 DAY) |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| 2013­04­21 |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
mysql> SELECT DATE_ADD(UTC_DATE(), INTERVAL 1 WEEK);
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| DATE_ADD(UTC_DATE(), INTERVAL 1 WEEK) |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| 2013­04­22 |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
mysql> SELECT DATEDIFF('2013­01­01','2013­04­01');
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| DATEDIFF('2013­01­01','2013­04­01') |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| ­90 |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
90
SQL utvalg – Dato format
MySQL:
mysql> SELECT DATE_FORMAT(UTC_DATE(), '%D %M %Y');
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| DATE_FORMAT(UTC_DATE(), '%D %M %Y') |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
| 15th April 2013 |
+­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­+
MySQL følger samme system som i Linux og Unix:
%b – Måned i kortform
%m – Måned, 1-12
%M – Måned
%h – Timer, 1-12
%H – Timer, 0-23
%y – År med to tegn
%Y – År med fire tegn
91
SQL Inner Join
92
ORACLE:
ANSI-89
[email protected] SQL> SELECT COUNT(*) FROM member m, contact c
WHERE m.member_id = c.member_id;
COUNT(*)
­­­­­­­­­­
123677
ANSI-92
[email protected] SQL> SELECT COUNT(*) FROM member m INNER JOIN contact c USING (member_id);
COUNT(*)
­­­­­­­­­­
123677
SQL Inner Join
93
MySQL:
ANSI-89
[email protected] SQL> SELECT COUNT(*) FROM member m, contact c
WHERE m.member_id = c.member_id;
+­­­­­­­­­­+
| COUNT(*) |
+­­­­­­­­­­+
| 123677 |
+­­­­­­­­­­+
ANSI-92
[email protected] SQL> SELECT COUNT(*) FROM member m INNER JOIN contact c USING (member_id);
+­­­­­­­­­­+
| COUNT(*) |
+­­­­­­­­­­+
| 123677 |
+­­­­­­­­­­+
SQL Outer Join
ORACLE:
[email protected] SQL> SELECT *
FROM food f;
ID FOOD Y ANIMAL
­­­ ­­­­­­­­­­­­­­­­ ­ ­­­­­­­­­­­­­­­­
1 apple Y human
2 oranges Y human
3 apple N dog
4 oranges N dog
5 apple N cat
6 oranges N cat
[email protected] SQL> SELECT *
FROM animals a;
NAME WEIGHT
­­­­­­­­­­­­­­­­ ­­­­­­­­­­
cat 12
dog 25
94
SQL Outer Join
ORACLE:
[email protected] SQL> SELECT f.*, a.weight
FROM food f, animals a
WHERE f.animal = a.name;
ID FOOD Y ANIMAL WEIGHT
­­­ ­­­­­­­­­­­­­­­­ ­ ­­­­­­­­­­­­­­­­ ­­­­­­­­­­
3 apple N dog 25
4 oranges N dog 25
5 apple N cat 12
6 oranges N cat 12
[email protected] SQL> SELECT f.*, a.weight
FROM food f, animals a
WHERE f.animal = a.name(+);
ID FOOD Y ANIMAL WEIGHT
­­­ ­­­­­­­­­­­­­­­­ ­ ­­­­­­­­­­­­­­­­ ­­­­­­­­­­
...
3 apple N dog 25
2 oranges Y human
1 apple Y human
95
SQL Outer Join
ORACLE:
ANSI-92
[email protected] SQL> SELECT f.*, a.weight
FROM food f
LEFT JOIN animals a ON (f.animal=a.name);
ID FOOD Y ANIMAL WEIGHT
­­­ ­­­­­­­­­­­­­­­­ ­ ­­­­­­­­­­­­­­­­ ­­­­­­­­­­
6 oranges N cat 12
5 apple N cat 12
4 oranges N dog 25
3 apple N dog 25
2 oranges Y human
1 apple Y human
[email protected] SQL> SELECT f.*, a.weight
FROM food f
LEFT OUTER JOIN animals a ON (f.animal=a.name);
96
SQL Outer Join
MySQL:
ANSI-92
[email protected] SQL> SELECT f.*, a.weight
FROM food f
LEFT JOIN animals a ON (f.animal=a.name);
+­­­­+­­­­­­­­­+­­­­­­­+­­­­­­­­+­­­­­­­­+
| id | food | yummy | animal | weight |
+­­­­+­­­­­­­­­+­­­­­­­+­­­­­­­­+­­­­­­­­+
| 5 | apple | N | cat | 12 |
| 6 | oranges | N | cat | 12 |
| 3 | apple | N | dog | 25 |
| 4 | oranges | N | dog | 25 |
| 1 | apple | Y | human | NULL |
| 2 | oranges | Y | human | NULL |
+­­­­+­­­­­­­­­+­­­­­­­+­­­­­­­­+­­­­­­­­+
[email protected] SQL> SELECT f.*, a.weight
FROM food f
LEFT OUTER JOIN animals a ON (f.animal=a.name);
97
Download
Related flashcards

Google

20 cards

Databases

28 cards

Yahoo!

50 cards

Create Flashcards