Download Comparing Oracle and SQL Server What is a database?

Document related concepts

Commitment ordering wikipedia , lookup

DBase wikipedia , lookup

Relational algebra wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Serializability wikipedia , lookup

Database wikipedia , lookup

Concurrency control wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Tandem Computers wikipedia , lookup

Microsoft Access wikipedia , lookup

Ingres (database) wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Team Foundation Server wikipedia , lookup

Btrieve wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Null (SQL) wikipedia , lookup

Relational model wikipedia , lookup

Oracle Database wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
<Insert Picture Here>
Comparing SQL Server
and Oracle
Comparing Oracle and SQL Server
Similarities
•
•
•
•
•
•
•
Similar Schema Objects (tables, views)
Similar Datatypes
Referential Integrity
Check Constraints / Rules
Transaction Support
Triggers and Stored Subprograms
SQL Access to System Catalogs
Comparing Data Types
SQL Server
Oracle
INTEGER
NUMBER(10)
SMALLINT
NUMBER(6)
TINYINT
NUMBER(3)
DECIMAL(p,[q])
NUMBER(p,[q])
NUMERIC(p,[q])
NUMBER(p,[q])
REAL
FLOAT
FLOAT[(p)]
FLOAT[(p)]
BIT
NUMBER(1)
CHAR(n)
CHAR(n)
VARCHAR(n)
VARCHAR2(n)
NCHAR(n)
CHAR(n*2)
NVARCHAR(n)
VARCHAR2(n*2)
Comparing Data Types
SQL Server
Oracle
TEXT
IMAGE
BINARY(n)
VARBINARY(n)
DATETIME
SMALLDATETIME
MONEY
SMALLMONEY
TIMESTAMP
SYSNAME
CLOB
BLOB
RAW(n), BLOB
RAW(n), BLOB
DATE (or TIMESTAMP)
DATE
NUMBER(19,4)
NUMBER(10,4)
NUMBER
VARCHAR2(30)
Comparing Oracle and SQL Server
Organization
Main Differences:
• Organization
• Terminology
• Connection Models
• Transactional and Isolation Models
• Temporary Tables
• Application programming
• Stored Subprograms
• Utilities (Bulk Loading)
Comparing Oracle and SQL Server
What is a database?
Oracle instance
(Database plus processes)
Oracle database:
•
Collection of
schemas
•
Stored in
tablespaces
•
Central schema:
SYSTEM
Memory
Processes
SYSTEM
Schema 1
Schema 2
Schema 3
= SQL Server server
SQL Server
database
= Oracle schema
Memory
Processes
Master,
model,msdb,
tempdb
Database 1
Database 2
Database 3
Comparing Storage Structures
• Oracle
•
•
•
•
•
Database
Tablespace
Segment
Extent
Block
• SQL Server
•
•
•
•
Database
Filegroup
Extent (64 KB fixed)
Page (8 KB fixed)
SQL Server Storage Structures
• Fundamental storage unit: Page (8 KB fixed)
• Basic unit to allocate space to tables and indexes: Extent
(64 KB fixed)
Database
OS file:
Primary data file
Secondary data file
Log file
Filegroup
Oracle Storage Structures
• Fundamental storage unit: Block
• A logical block consists of one or more OS blocks.
• The size of a logical block is defined by an initialization
parameter.
Logical
Physical
Tablespace
Data file
Segment
Extent
Block
OS block
Comparing Oracle and SQL Server
Terminology
Main Differences:
• Organization
• Terminology
• Connection Models
• Transactional and Isolation Models
• Temporary Tables
• Application programming
• Stored Subprograms
• Utilities (Bulk Loading)
Differences in Terminology
•
•
•
•
•
•
Oracle spfile(auto managed binary) = SQL Server sysconfig
Oracle v$, USER_TABLES = SQL Server sp_ stored procedures,
sysxxx tables
Oracle has schemas/tablespaces = SQL Server
databases/devices
Oracle has redo buffer cache, redo logs for archiving = SQL
Server transaction log
Oracle has UNDO space for read consistency = no equivalent in
SQL Server* (SS2K5)
Oracle SQL*PLUS (/) = SQL Server ISQL (go)
Connecting to the Database
• With multiple databases in SQL Server, you use the
following command to switch databases:
SQL> Use hr
• With only one database in Oracle, you issue one of the
following commands to switch schemas:
SQL> CONNECT hr/hr;
• OR
SQL> ALTER SESSION SET CURRENT_SCHEMA=HR;
Comparing Schema Objects
• Oracle schema objects not available in SQL Server:
• Database link
• Profile
• Materialized view
• Sequence (SQL Server: Serial data type)
• Synonym
• SQL Server rule, integrity, and default are implemented as
constraints of Oracle tables.
Naming Database Objects
• Names must be from 1 to 30 bytes long with these exceptions:
• Names of databases are limited to 8 bytes.
• Names of database links can be as long as 128 bytes.
• Nonquoted names cannot be Oracle-reserved words.
• Nonquoted names must begin with an alphabetic character from
your database character set.
Naming Database Objects
• Nonquoted names can contain only:
• Alphanumeric characters from your database character set
• The underscore (_)
• Dollar sign ($)
• Pound sign (#)
• No two objects can have the same name within the same
namespace.
• MS Tip: OMWB assists with resolving naming conflicts.
Comparing Oracle and SQL Server
Connection Models
Main Differences:
• Organization
• Terminology
• Connection Models
• Transactional and Isolation Models
• Temporary Tables
• Application programming
• Stored Subprograms
• Utilities (Bulk Loading)
Differences in Connection Models
• The Oracle server is “connection-based”. It offers:
•
•
•
•
•
Multiple active result-sets per connection
Only one connection needed
Multiple sessions per connection
Multiple transactions per session
Distributed database access via database links
• SQL Server is “stream-based”. It offers:
• One active result-set per connection
• Typically several connections used
Handling Result Sets
•
SQL Server automatically put resultsets in stream
• Returns data in Tablular Data Stream format (TDS)
• Multiple resultsets possible
•
Oracle provides cursor variables
•
•
•
•
•
Client receives cursor variable
Cursor Variable is a handle to server side memory resident cursor
Client fetches as much of data as desired
Multiple Cursor Variables easily accommodated
Can pass Cursor Variable to other clients or servers
Comparing Oracle and SQL Server
Transaction & Isolation Models
Main Differences:
• Organization
• Terminology
• Connection Models
• Transactional and Isolation Models
• Temporary Tables
• Application programming
• Stored Subprograms
• Utilities (Bulk Loading)
Comparing Transactional Models
Oracle
SQL Server
‘Readers’ never block ‘writers’
Read locks provide consistency, but may
block ‘writers’.
‘Read’ transactions are always
consistent.
’Dirty reads’, i.e. reads of uncommitted
data allowed to bypass locks
True row-level locks
N/A
Locks never escalate
Locks escalate as numbers
increase
Locking on with the block
Locking in memory
Transactional Models
Transaction Handling
•
Oracle supports always full Isolation Model
• Only committed data visible to other users
• Allows repeatable reads
•
SQL Server allows several modes
• SET TRANSACTION ISOLATION LEVEL … for
each transaction
• Uses BROWSE mode (timestamp) to detect update
conflicts (optimistic locking)
Transactional Models
Transaction Handling
•
•
Oracle has implicit Transactions
•
•
•
All SQL statements are transaction controlled
No BEGIN TRANSACTION - a new transaction begins at the end of the previous one
Transaction ends at COMMIT or ROLLBACK
•
Nested Transactions could be defined via SAVEPOINT
SQL Server programmers use explicit Transactions
•
•
•
Programmers may explicitly use BEGIN, END TRANSACTION and control
COMMIT/ROLLBACK manually.
If not in an explicit transaction, each statement auto-commits after execution
Nested Transactions do not commit but may rollback
Comparing the Transaction Models
• Key differences between the transaction models:
SQL Server
Oracle
Explicit
Implicit
End a
Uses auto-commit mode
Requires the COMMIT
transaction.
by default
statement by default
Begin a
transaction.
Beginning a Transaction
SQL Server begins transactions explicitly:
SQL>
2
3
4
BEGIN TRANSACTION
INSERT INTO regions VALUES (5, ‘Southeast Asia’)
INSERT INTO countries VALUES (‘VN’, ‘Vietnam’, 5)
COMMIT TRANSACTION
The Oracle database begins transactions implicitly:
SQL> INSERT INTO regions VALUES (5, Southeast Asia’);
2
3
INSERT INTO countries VALUES (‘VN’,‘Vietnam’, 5);
COMMIT;
Ending a Transaction
SQL Server always commits statements if outside an
explicit transaction:
SQL> INSERT INTO regions
2
VALUES (6, ‘South America’)
3 INSERT INTO countries
4
VALUES (‘PE’, ‘Peru’, 6)
Transaction #1
Transaction #2
With Oracle you always need to COMMIT or
ROLLBACK
SQL> INSERT INTO regions
2
3
4
5
6
VALUES (6, ‘South America’);
COMMIT;
INSERT INTO countries
VALUES (‘PE’, ‘Peru’, 6)
COMMIT;
Transaction #1
Transaction #2
Comparing Isolation Levels
• Isolation levels supported by SQL Server and Oracle:
Isolation Level
SQL Server
Oracle
Read uncommitted
Read committed
Repeatable read
Serializable
* Read Only Transactions only
Default
Default
*
Comparing Oracle and SQL Server
Temporary Tables
Main Differences:
• Organization
• Terminology
• Connection Models
• Transactional and Isolation Models
• Temporary Tables
• Application programming
• Stored Subprograms
• Utilities (Bulk Loading)
Temporary Tables
• SQL Server:
• Local temporary tables, names beginning with #
• Global temporary tables, names beginning with ##
• Not compatible with Oracle’s naming conventions
• Options in Oracle:
• Temporary ANSI-style (global temporary) tables
• Multitable joins (optimized internally)
• Materialized views
• PL/SQL tables
Temporary Tables
CREATE GLOBAL TEMPORARY TABLE emp_temp(
eno NUMBER, ename VARCHAR2(20), sal NUMBER)
ON COMMIT DELETE ROWS;
INSERT INTO emp_temp VALUES( 101,’Inga’,1000);
SELECT count(*) FROM emp_temp;
•
Data exists only for the duration of a transaction or session
•
Data only visible within a single transaction or session
•
No redo generated, only undo
•
Data segments created in a user’s temporary tablespace
Comparing Oracle and SQL Server
Programming
Main Differences:
• Organization
• Terminology
• Connection Models
• Transactional and Isolation Models
• Temporary Tables
• Application programming
• Stored Subprograms
• Utilities (Bulk Loading)
Migrate a table with an IDENTITY
column
• Oracle doesn't support the IDENTITY attribute. If you
want an auto-incrementing column in Oracle, then
create a sequence and use that sequence in a trigger
associated to the table
Migrate a table with an IDENTITY
column
• SQL Server version
• Create the Table
SQL> CREATE TABLE Friend (
2
3
4•
FriendID INT IDENTITY PRIMARY KEY NOT NULL,
Name VARCHAR(50),
PhoneNo
Insert
RowVARCHAR(15)DEFAULT ‘Unknown Phone’)
SQL> INSERT INTO Friend (Name, PhoneNO)
2
VALUES (‘Mike’,’123-456-7890’);
Migrate a table with an IDENTITY
column
• Oracle version
• Create the Table
SQL> CREATE TABLE Friend (
2
3
4•
FriendID NUMBER PRIMARY KEY NOT NULL,
Name VARCHAR(50),
PhoneNo
Insert
RowVARCHAR(15)DEFAULT ‘Unknown Phone’)
SQL> INSERT INTO Friend (Name, PhoneNO)
2
VALUES (‘Mike’,’123-456-7890’);
Migrate a table with an IDENTITY
column
• Oracle version cont.
• Create the Sequence
SQL> CREATE SEQUENCE SEQ;
• Create the Trigger
SQL> CREATE OR REPLACE TRIGGER FRIEND_AUTO_NUMBER
2
3
4
5
6
BEFORE INSERT ON Friend
FOR EACH ROW
BEGIN
SELECT SEQ.NEXTVAL INTO :NEW.FriendID FROM DUAL;
END;
Null Handling Semantics
• SQL Server interprets the empty string as a
single blank space.
SQL> SELECT customer_id, date_of_birth
2 FROM customers
3 WHERE cust_email = ‘’
• Oracle interprets the empty string as NULL value.
Rewrite to use a single blank space and not the
empty string.
SQL> SELECT customer_id, date_of_birth
2 FROM customers
3 WHERE cust_email = ‘ ’
SQL Comparison
Object Name Changes
• The way to reference a table or view in a SQL statement is
different:
• SQL Server
• database_name.owner_name.table_name
• Oracle
• user_schema.table_name
• Example accessing other schema:
SQL Server
Oracle
SELECT * FROM oe.oe_dba.orders
SELECT * FROM oe.orders
Displaying Information about an
object
• In Oracle use the SQL*Plus DESCRIBE command to
display the structure of a table.
Microsoft SQL Server
Oracle
SP_HELP table_name
DESCRIBE table_name
SELECT Statement: FROM Clause
In SQL Server, FROM clause is optional.
SQL> SELECT getdate()
In Oracle, FROM clause is required.
SQL> SELECT sysdate FROM dual;
SELECT Statement: SELECT INTO
Clause
In SQL Server, SELECT INTO clause is used.
SQL> SELECT cust_first_name, cust_last_name
2 INTO contacts
3 FROM customers
In Oracle, if the table exists, rewrite using the INSERT
INTO clause.
SQL> INSERT INTO contacts
2 SELECT cust_first_name, cust_last_name
3 FROM customers;
SELECT Statement: SELECT INTO
Clause cont.
In SQL Server, SELECT INTO clause is used.
SQL> SELECT cust_first_name, cust_last_name
2 INTO contacts
3 FROM customers
In Oracle, if the table does not exist, rewrite using the
Create table as select clause.
SQL> CREATE contacts AS
2 SELECT cust_first_name, cust_last_name
3
FROM customers
SELECT Statement: Column Alias
In SQL Server, example using column alias:
SQL> SELECT email = cust_email
2 FROM customers
In Oracle, column alias is placed after the column
name
SQL> SELECT cust_email email
2 FROM customers;
SELECT Statement: TOP n Clause
In SQL Server, TOP clause is gives you the top n rows
retrieved in the result set.
SQL> SELECT TOP 5 empname, total_com FROM emp
ORDER BY total_com
In Oracle, you must do a subselect and use
ROWNUM
SQL> SELECT * FROM
(SELECT empname, total_com FROM emp
ORDER BY total_com )WHERE
ROWNUM < 6
INSERT Statement
• In SQL Server, the INTO clause is optional.
SQL> INSERT regions
2
VALUES (202, ‘Southeast’)
• In Oracle, the INTO clause is required.
SQL> INSERT INTO regions
2
VALUES (202, ‘Southeast’);
UPDATE statement
• SQL Server example:
SQL> UPDATE inventories
2 SET quantity_on_hand = 0
3 FROM inventories i, product_information p
4 WHERE p.product_id = p.product_id
• Rewrite
Oracle:
5 and inproduct_status=‘planned’
SQL>
2
3
4
5
UPDATE inventories
SET quantity_on_hand = 0
WHERE product_id IN (SELECT product_id
FROM product_information
WHERE product_status = ‘planned’);
DELETE statement
• SQL Server:
SQL>
2
3
4
DELETE FROM inventories
FROM inventories i, product_information p
WHERE i.product_id = p.product_id
AND supplier_id = 102066
• Rewrite in Oracle:
SQL>
2
3
4
DELETE FROM inventories
WHERE product_id IN (SELECT product_id
FROM product_information
WHERE supplier_id = 102066);
Operators
• Examples of operator differences:
SQL Server
Oracle
WHERE qty !< 100
WHERE qty >= 100
WHERE status = NULL
WHERE status IS NULL
String
SELECT fname + ‘ ‘ +
SELECT fname || ‘ ‘
Concatenation
lname AS name
|| lname AS name
Value
Comparison
Null Value
Comparison
and Literals
Built-In Functions
• Both SQL Server and Oracle have proprietary built-in
functions:
SQL Server
Oracle
Character
char()
chr()
Null Test
isnull(qty, 0)
nvl(qty,0)
Conditional Test CASE cust_type
WHEN 1 THEN ‘Gold’
DECODE (cust_type,
1,’Gold’,
WHEN 2 THEN ‘Silver’
2,’Silver’,
WHEN 3 THEN ‘Bronze’
3, ‘Bronze’)
END
Oracle also has CASE
Data Type Conversion
• SQL Server uses the CONVERT function to convert
data types.
SQL> SELECT CONVERT(char, GETDATE())
• Replace with the appropriate Oracle equivalent
function:
SQL> SELECT TO_CHAR(sysdate)
2
FROM dual;
Comparing Oracle and SQL Server
Stored procedures
Main Differences:
• Organization
• Terminology
• Connection Models
• Transactional and Isolation Models
• Temporary Tables
• Application programming
• Stored Subprograms
• Utilities (Bulk Loading)
Stored Modules
• Oracle Stored Modules are coded in Java or PL/SQL
• They can either be PROCEDURES or FUNCTIONS
• They can be contained in PACKAGES
Stored Modules
• Most SQL Server Stored Modules are coded in
Transact SQL.
• They can act as either procedures or functions.
• A set of modules can be coded under the same name
using a numeric suffix to identify different modules.
Issues in Transact SQL Conversion
•
•
•
•
•
•
•
Transaction Handling and Control
Stored Sub Programs returning Resultsets
Error Handling
SQL Syntax
Specific Built-in Functions
Dynamic SQL
Packaged Modules
Business Logic Transact-SQL
T-SQL issues - Error Handling
•
SQL Server / Sybase propagate errors back to client via global variable
• @@Error needs to be checked regulary
•
Oracle has Exception Handling in PL/SQL to deal with errors
• Throw/Catch exception model
• SQL statements are embedded in a PL/SQL block with optional
EXCEPTION section
• Use EXCEPTION to trap exactly the error conditions you wish
• Un-handled exceptions propagate to higher levels
Comparing Oracle and SQL Server
Triggers
SQL Server
Oracle
Types of
INSERT, UPDATE, and
INSERT, UPDATE,
triggers
DELETE statements
DELETE statements
Occurrence
AFTER and INSTEAD OF
BEFORE, AFTER and
trigger
INSTEAD OF triggers
Occurrence
Statement level
Row or statement level
Reference to
Uses DELETED and
Accessed via :OLD and
previous and
INSERTED temporary tables :NEW
new values
Comparing Oracle and SQL Server
Utilities
Main Differences:
• Organization
• Terminology
• Connection Models
• Transactional and Isolation Models
• Temporary Tables
• Application programming
• Stored Subprograms
• Utilities (Bulk Loading)
Differences in Utilities
•
•
•
•
•
•
Oracle has scott/tiger schema = SQL Server PUBS database
Oracle has System/manager = SQL Server SA/
Oracle Oracle Call Interface = SQL Server DB-Library/CT-Library
Oracle SQL*Loader = SQL Server BCP
Oracle Warehouse Builder = SQL Server Data Transformation
Services (DTS)
Oracle Advanced Queuing (AQ) = MSMQ
QUESTIONS
ANSWERS