Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Dec 27, 2024
1 parent 51eba29 commit 32afdf9
Show file tree
Hide file tree
Showing 3 changed files with 606 additions and 151 deletions.
179 changes: 179 additions & 0 deletions docs/oracle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# Oracle Database Conversion Guide

## Table of Contents
1. [Introduction](#introduction)
2. [Data Type Mappings](#data-type-mappings)
3. [Syntax Differences](#syntax-differences)
4. [Common Issues](#common-issues)
5. [Examples](#examples)

## Introduction
This guide provides detailed information about converting Oracle database schemas to and from other database systems using SQLMapper.

## Data Type Mappings

### Oracle to MySQL
| Oracle Type | MySQL Type | Notes |
|------------|------------|-------|
| NUMBER(p,s) | DECIMAL(p,s) | For exact numeric values |
| NUMBER | BIGINT | When no precision specified |
| VARCHAR2 | VARCHAR | - |
| CLOB | LONGTEXT | - |
| BLOB | LONGBLOB | - |
| DATE | DATETIME | - |
| TIMESTAMP | TIMESTAMP | - |

### Oracle to PostgreSQL
| Oracle Type | PostgreSQL Type | Notes |
|------------|-----------------|-------|
| NUMBER(p,s) | NUMERIC(p,s) | - |
| VARCHAR2 | VARCHAR | - |
| CLOB | TEXT | - |
| BLOB | BYTEA | - |
| DATE | TIMESTAMP | - |
| TIMESTAMP | TIMESTAMP | - |

## Syntax Differences

### Sequences
Oracle:
```sql
CREATE SEQUENCE my_sequence
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
```

MySQL equivalent:
```sql
CREATE TABLE my_sequence (
id BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
```

PostgreSQL equivalent:
```sql
CREATE SEQUENCE my_sequence
START 1
INCREMENT 1
NO CYCLE;
```

### Stored Procedures
Oracle:
```sql
CREATE OR REPLACE PROCEDURE update_employee(
p_emp_id IN NUMBER,
p_salary IN NUMBER
)
IS
BEGIN
UPDATE employees
SET salary = p_salary
WHERE employee_id = p_emp_id;
END;
/
```

MySQL equivalent:
```sql
DELIMITER //
CREATE PROCEDURE update_employee(
IN p_emp_id INT,
IN p_salary DECIMAL(10,2)
)
BEGIN
UPDATE employees
SET salary = p_salary
WHERE employee_id = p_emp_id;
END //
DELIMITER ;
```

## Common Issues

### 1. Date Format Differences
Oracle's default date format differs from other databases. Use explicit format strings:
```sql
-- Oracle
TO_DATE('2023-12-27', 'YYYY-MM-DD')

-- MySQL
STR_TO_DATE('2023-12-27', '%Y-%m-%d')

-- PostgreSQL
TO_DATE('2023-12-27', 'YYYY-MM-DD')
```

### 2. Sequence Usage
When converting sequences, be aware that:
- MySQL doesn't support sequences natively
- PostgreSQL sequences require explicit nextval() calls
- Oracle sequences can be used in DEFAULT values

### 3. NULL Handling
Oracle treats empty strings as NULL, while other databases distinguish between empty strings and NULL values.

## Examples

### Converting Table with Identity Column
```sql
-- Original Oracle Table
CREATE TABLE employees (
emp_id NUMBER GENERATED ALWAYS AS IDENTITY,
name VARCHAR2(100),
salary NUMBER(10,2),
hire_date DATE,
CONSTRAINT pk_emp PRIMARY KEY (emp_id)
);

-- MySQL Conversion
CREATE TABLE employees (
emp_id BIGINT AUTO_INCREMENT,
name VARCHAR(100),
salary DECIMAL(10,2),
hire_date DATETIME,
PRIMARY KEY (emp_id)
);

-- PostgreSQL Conversion
CREATE TABLE employees (
emp_id SERIAL,
name VARCHAR(100),
salary NUMERIC(10,2),
hire_date TIMESTAMP,
PRIMARY KEY (emp_id)
);
```

### Converting Complex Types
```sql
-- Oracle Table with Complex Types
CREATE TABLE documents (
doc_id NUMBER,
content CLOB,
metadata VARCHAR2(4000),
binary_data BLOB,
CONSTRAINT pk_doc PRIMARY KEY (doc_id)
);

-- MySQL Conversion
CREATE TABLE documents (
doc_id BIGINT,
content LONGTEXT,
metadata JSON,
binary_data LONGBLOB,
PRIMARY KEY (doc_id)
);

-- PostgreSQL Conversion
CREATE TABLE documents (
doc_id BIGINT,
content TEXT,
metadata JSONB,
binary_data BYTEA,
PRIMARY KEY (doc_id)
);
```
200 changes: 200 additions & 0 deletions docs/sqlserver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# SQL Server Database Conversion Guide

## Table of Contents
1. [Introduction](#introduction)
2. [Data Type Mappings](#data-type-mappings)
3. [Syntax Differences](#syntax-differences)
4. [Common Issues](#common-issues)
5. [Examples](#examples)

## Introduction
This guide provides detailed information about converting SQL Server database schemas to and from other database systems using SQLMapper.

## Data Type Mappings

### SQL Server to MySQL
| SQL Server Type | MySQL Type | Notes |
|----------------|------------|-------|
| BIGINT | BIGINT | - |
| INT | INT | - |
| SMALLINT | SMALLINT | - |
| TINYINT | TINYINT | Range differences |
| DECIMAL(p,s) | DECIMAL(p,s) | - |
| VARCHAR(n) | VARCHAR(n) | - |
| NVARCHAR(n) | VARCHAR(n) | UTF-8 encoding |
| TEXT | LONGTEXT | - |
| DATETIME2 | DATETIME | Precision differences |
| UNIQUEIDENTIFIER | CHAR(36) | - |

### SQL Server to PostgreSQL
| SQL Server Type | PostgreSQL Type | Notes |
|----------------|-----------------|-------|
| BIGINT | BIGINT | - |
| INT | INTEGER | - |
| SMALLINT | SMALLINT | - |
| DECIMAL(p,s) | NUMERIC(p,s) | - |
| VARCHAR(n) | VARCHAR(n) | - |
| NVARCHAR(n) | VARCHAR(n) | - |
| TEXT | TEXT | - |
| DATETIME2 | TIMESTAMP | - |
| UNIQUEIDENTIFIER | UUID | - |

## Syntax Differences

### Identity Columns
SQL Server:
```sql
CREATE TABLE users (
id INT IDENTITY(1,1) PRIMARY KEY,
username VARCHAR(50)
);
```

MySQL equivalent:
```sql
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50)
);
```

PostgreSQL equivalent:
```sql
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50)
);
```

### Stored Procedures
SQL Server:
```sql
CREATE PROCEDURE UpdateEmployee
@EmpID INT,
@Salary DECIMAL(10,2)
AS
BEGIN
UPDATE Employees
SET Salary = @Salary
WHERE EmployeeID = @EmpID;
END;
```

MySQL equivalent:
```sql
DELIMITER //
CREATE PROCEDURE UpdateEmployee(
IN p_EmpID INT,
IN p_Salary DECIMAL(10,2)
)
BEGIN
UPDATE Employees
SET Salary = p_Salary
WHERE EmployeeID = p_EmpID;
END //
DELIMITER ;
```

## Common Issues

### 1. Collation Differences
SQL Server uses different collation naming conventions:
```sql
-- SQL Server
COLLATE SQL_Latin1_General_CP1_CI_AS

-- MySQL
COLLATE utf8mb4_general_ci

-- PostgreSQL
COLLATE "en_US.utf8"
```

### 2. DateTime Handling
```sql
-- SQL Server
GETDATE()
DATEADD(day, 1, GETDATE())

-- MySQL
NOW()
DATE_ADD(NOW(), INTERVAL 1 DAY)

-- PostgreSQL
CURRENT_TIMESTAMP
CURRENT_TIMESTAMP + INTERVAL '1 day'
```

### 3. String Concatenation
```sql
-- SQL Server
SELECT FirstName + ' ' + LastName

-- MySQL
SELECT CONCAT(FirstName, ' ', LastName)

-- PostgreSQL
SELECT FirstName || ' ' || LastName
```

## Examples

### Converting Table with Computed Columns
```sql
-- Original SQL Server Table
CREATE TABLE orders (
order_id INT IDENTITY(1,1),
quantity INT,
unit_price DECIMAL(10,2),
total_price AS (quantity * unit_price),
CONSTRAINT pk_orders PRIMARY KEY (order_id)
);

-- MySQL Conversion
CREATE TABLE orders (
order_id INT AUTO_INCREMENT,
quantity INT,
unit_price DECIMAL(10,2),
total_price DECIMAL(10,2) GENERATED ALWAYS AS (quantity * unit_price) STORED,
PRIMARY KEY (order_id)
);

-- PostgreSQL Conversion
CREATE TABLE orders (
order_id SERIAL,
quantity INT,
unit_price NUMERIC(10,2),
total_price NUMERIC(10,2) GENERATED ALWAYS AS (quantity * unit_price) STORED,
PRIMARY KEY (order_id)
);
```

### Converting Table with Custom Types
```sql
-- SQL Server Table with Custom Types
CREATE TABLE customer_data (
id INT IDENTITY(1,1),
customer_code UNIQUEIDENTIFIER DEFAULT NEWID(),
status VARCHAR(20),
metadata NVARCHAR(MAX),
CONSTRAINT pk_customer PRIMARY KEY (id)
);

-- MySQL Conversion
CREATE TABLE customer_data (
id INT AUTO_INCREMENT,
customer_code CHAR(36) DEFAULT (UUID()),
status VARCHAR(20),
metadata JSON,
PRIMARY KEY (id)
);

-- PostgreSQL Conversion
CREATE TABLE customer_data (
id SERIAL,
customer_code UUID DEFAULT gen_random_uuid(),
status VARCHAR(20),
metadata JSONB,
PRIMARY KEY (id)
);
```
Loading

0 comments on commit 32afdf9

Please sign in to comment.