beginning sql: differences between sql server and oracle les kopari independent consultant a quick...

32
Beginning SQL: Beginning SQL: Differences Between Differences Between SQL Server and SQL Server and Oracle Oracle Les Kopari Les Kopari Independent Consultant Independent Consultant A Quick Intro for SQL Server Users

Upload: aldous-jordan

Post on 22-Dec-2015

237 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Beginning SQL:Beginning SQL:Differences Between Differences Between SQL Server and OracleSQL Server and Oracle

Les KopariLes Kopari

Independent ConsultantIndependent Consultant

A Quick Intro for SQL Server Users

Page 2: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

IntroductionIntroduction

If you're new to SQL or just new to Oracle SQL, perhaps coming from a Microsoft SQL Server environment, it may seem like the two versions should be very similar, and they are, to a certain degree, but they are also very different in some important and basic ways.

Page 3: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

AgendaAgenda

I. Quick Intro for SQL Server UsersI. Quick Intro for SQL Server Users

II. Some Detail: Joins, Subqueries, II. Some Detail: Joins, Subqueries, DeletesDeletes

III. Certain Conceptual DifferencesIII. Certain Conceptual Differences

IV. Powerful New FeaturesIV. Powerful New Features

V. Summary & ReferencesV. Summary & References

Page 4: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Don’t Use DatabasesDon’t Use Databases

SQL Server

use mydatabase

Oracle

connect mydatabase/mypassword

Page 5: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Use DualUse Dual

SQL Server

Oracle

select getdate();

select sysdate from dual;

Page 6: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Select IntoSelect Into

SQL Server

Oracle

select getdate() mycolumn into mytable;

insert into mytable (mycolumn)

values(sysdate);

Page 7: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

InsertsInserts

SQL Server

Oracle

Insert mytable values(‘more text’);

Insert into mytable values(‘more text’);

Page 8: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

UpdatesUpdates

SQL Server

update mytable set mycolumn=myothertable.mycolumn from mytable,myothertable where mytable.mycolumn like 'MY%'

and myothertable.myothercolumn='some text';

Page 9: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

UpdatesUpdates

Oracle

update mytableset mycolumn=(select a.mycolumn from myothertable a where myothertable.myothercolumn='some text';)

where mytable.mycolumn like 'MY%';

Page 10: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

DeletesDeletes

SQL Server

delete mytable where mycolumn like 'some%';

Oracle

delete from mytable

where mycolumn like 'some%';

Page 11: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

SoftwareSoftware

• isql

• osql: for queries developed in SQL Analyzer

• sqlplus

SQL Server

Oracle

Page 12: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

II. A Little More DetailII. A Little More Detail

• Outer JoinOuter Join

• Sub-Queries in Place of ColumnsSub-Queries in Place of Columns

• Deletes With a Second From ClauseDeletes With a Second From Clause

Page 13: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Outer JoinOuter Join

SQL ServerSQL Server

select d.deptname, e.ename from dept select d.deptname, e.ename from dept d, emp e where d.empno *= e.enum;d, emp e where d.empno *= e.enum;

OracleOracle

select d.deptname,e.ename from dept d, select d.deptname,e.ename from dept d, emp e where d.empno = e.enum (+);emp e where d.empno = e.enum (+);

Page 14: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

SubQueries in Place of SubQueries in Place of ColumnsColumns• SQL ServerSQL Server

select distinct year,

q1 = (select Amount amt FROM sales

where Quarter=1 AND year = s.year),

q2 = (SELECT Amount amt FROM sales

where Quarter=2 AND year = s.year),

q3 = (SELECT Amount amt FROM sales

where Quarter=3 AND year = s.year),

q4 = (SELECT Amount amt FROM sales

where Quarter=4 AND year = s.year)

from sales s;

Page 15: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

SubQueries in Place of SubQueries in Place of ColumnsColumns

OracleOracleSELECT year,

DECODE( quarter, 1, amount, 0 ) q1,

DECODE( quarter, 2, amount, 0 ) q2,

DECODE( quarter, 3, amount, 0 ) q3,

DECODE( quarter, 4, amount, 0 ) q4

FROM sales s;

Page 16: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Delete with Second From Delete with Second From ClauseClause

SQL ServerSQL Serverdelete

from products

from products, product_deletes

where products.a = product_deletes.a

and products.b = product_deletes.b

and product_deletes.c = 'd';

Page 17: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Delete with Second From Delete with Second From ClauseClause

Oracle

deletefrom productswhere ( a, b ) in( select a, b

from product_deleteswhere c = 'd' );

Page 18: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

III. More DepthIII. More Depth

• The Connect ConceptThe Connect Concept

• Other Conceptual DifferencesOther Conceptual Differences

• Data Type DifferencesData Type Differences

• Column AliasesColumn Aliases

• Sub-QueriesSub-Queries

Page 19: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

The Connect ConceptThe Connect Concept

SQL ServerSQL Server

Multiple databasesMultiple databases

OracleOracle

Single DatabaseSingle Database

Multiple tablespaces, schemas, usersMultiple tablespaces, schemas, users

Page 20: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Other Conceptual Other Conceptual DifferencesDifferences

SQL ServerDatabase owner, DBOGroup/RoleNon-unique indexT-SQL stored procedure {TriggerCompex ruleColumn identity property

OracleSchemaRoleIndexPL/SQL procedurePL/SQL functionBEFORE triggerAfter triggerSequence

Page 21: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Only in OracleOnly in Oracle

• ClustersClusters

• PackagesPackages

• Triggers for each rowTriggers for each row

• SynonymsSynonyms

• SnapshotsSnapshots

Page 22: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Data Type DifferencesData Type DifferencesSQL Server Oracle

INTEGER NUMBER(10)

SMALLINT NUMBER(6)

TINYINT NUMBER(3)

REAL FLOAT

FLOAT FLOAT

BIT NUMBER(1)

VARCHAR(n) VARCHAR2(n)

TEXT CLOB

IMAGE BLOB

BINARY(n) RAW(n) or BLOB

Page 23: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Data Type DifferencesData Type DifferencesSQL Server Oracle

VARBINARY RAW(n) or BLOB

DATETIME DATE

SMALL-DATETIMEDATE

MONEY NUMBER(19,4)

NCHAR(n) CHAR(n*2)

NVARCHAR(n) VARCHAR(n*2)

SMALLMONEY NUMBER(10,4)

TIMESTAMP NUMBER

SYSNAME VARCHAR2(30),

VARCHAR2(128)

Page 24: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

TimeTime

SQL ServerSQL Server

Datetime: 1/300th secondDatetime: 1/300th second

OracleOracle

Date: 1 secondDate: 1 second

Timestamp: 1/100 millionth secondTimestamp: 1/100 millionth second

Page 25: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Column AliasesColumn Aliases

SQL ServerSQL Server

select a=deptid, select a=deptid, b=deptname,c=empno from dept;b=deptname,c=empno from dept;

OracleOracle

select deptid a, deptname b, empno select deptid a, deptname b, empno c from dept;c from dept;

Page 26: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Sub-queries, againSub-queries, again

• SQL ServerSQL ServerSELECT ename, deptname

FROM emp, dept

WHERE emp.enum = 10

AND(SELECT security_code

FROM employee_security

WHERE empno = emp.enum) =(SELECT security_codeFROM security_master

WHERE sec_level = dept.sec_level);

Page 27: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Sub-queries, againSub-queries, again

OracleOracleSELECT empname, deptname

FROM emp, dept

WHERE emp.empno = 10

AND EXISTS (SELECT security_code

FROM employee_security es

WHERE es.empno = emp.empno

AND es.security_code =(SELECT security_codeFROM security_masterWHERE sec_level = dept.sec_level));

Page 28: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Powerful New FeaturesPowerful New Features

• Regular Expressions: Operators & Regular Expressions: Operators & FunctionsFunctions

• Operator: REGEXP_LIKEOperator: REGEXP_LIKE

• Functions: REGEXP_INSTRFunctions: REGEXP_INSTR

• REGEXP_SUBSTRREGEXP_SUBSTR

• REGEXP_REPLACEREGEXP_REPLACE

Page 29: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Regular ExpressionsRegular Expressions

Select zipSelect zip

from zipcodefrom zipcode

where regexp_like (zip, ‘[^[:digit:]]’);where regexp_like (zip, ‘[^[:digit:]]’);

Page 30: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

Regular ExpressionsRegular Expressions

SELECT REGEXP_INSTR('Joe Smith,

10045 Berry Lane, San Joseph, CA 91234-1234',

' [[:digit:]]{5}(-[[:digit:]]{4})?$')

AS starts_at

FROM dual

Page 31: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

SummarySummary

This discussion has been an attempt at a light and lively introduction to the Oracle database world for those familiar with the Microsoft SQL Server database products. Much more in-depth examples are available in the references shown that follow, from which many of the examples were drawn and for which we can thank the authors involved.

Welcome Aboard!

Page 32: Beginning SQL: Differences Between SQL Server and Oracle Les Kopari Independent Consultant A Quick Intro for SQL Server Users

ReferencesReferences• Oracle Migration Workbench Reference Guide for SQL

Server and Sybase Adaptive Server Migrations, Release 9.2.0 for Microsoft Windows 98/2000/NT and Microsoft Windows XP, Part Number B10254-01

• Oracle Technology Network, OTN:

http://otn.oracle.com/software/index.html

• Writing Better SQL Using Regular Expressions, By Alice Rischert

http://otn.oracle.com/oramag/webcolumns/2003/techarticles/rischert_regexp_pt1.html