Oracle與MySQL的SQL語句區別
1.表
1.1建立表(同)
create table tableName(
cloumnName1 int,
cloumnName2 int
)
1.2刪除表(異)
MySQL:
drop table if exists tableName
Oracle:
drop table tableName
注:Oracle沒有if exists關鍵字,也沒用類似if exists的SQL語法。
2.列
2.1 新增列(異)
MySQL:
A. alter table tableName add columnName1 int;
B. alter table tableName add columnName1 int, add column columnName2 int;
Oracle:
A. alter table tableName add columnName1 int;
B. alter table tableName add (columnName1 int);
C. alter table tableName add (columnName1 int, columnName2 int);
注:對於A,只有新增單列的時候才可使用,對於新增多列時需要使用C,不能像MySQL那樣重複使用add關鍵字。
2.2 刪除列(異)
MySQL:
A. alter table tableName drop column columnName1
B. alter table tableName drop column columnName1, drop column columnName2
注:其中關鍵字column可有可無。
Oracle:
A. alter table tableName drop column columnName2
B. alter table tableName drop column (columnName1)
C. alter table tableName drop column (columnName1,columnName2)
注:對於A,只有刪除單列的時候才可使用,對於刪除多列時需要使用C,不能像MySQL那樣重複使用drop column關鍵字。
2.3 修改列名(異)
MySQL:
alter table tableName change column columnNameOld columnNameNew columnType;
Oracle:
alter table tableName rename column columnNameOld to columnNameNew;
2.3 修改列型別(異)
Oracle中,在列有資料的時候,無法修改列型別;沒有資料時可以。
MySQL中,無論列是否有資料都可以修改列型別。
但是當有資料是,直接修改列型別都可能對資料造成丟失等,所以一般需要結合具體的業務來對列資料做處理後,再修改列型別型別。所以修改列的型別並非使用SQL語句進行一步到位的修改,而是通過以下流程:
A. 新增臨時列
B. 將需要更改的列的值經過型別轉換的驗證後,賦值給臨時列
C. 刪除原有列
D. 將臨時列的列名修改為原有列列名
3.索引
在整個資料庫內,MySQL的索引可以同名,也就是說MySQL的索引是表級別的;但是Oracle索引不可以同名,也就是說Oracle的索引是資料庫級別的。
3.1建立索引(同)
create index indexName on tableName (columnName);
3.2刪除索引(異)
MySQL:
alter table tableName drop index indexName
Oracle:
drop index indexName
3.2查詢表的索引(異)
MySQL:
show index from tableName
Oracle:
oracle中表的索引資訊存在 user_indexes 和 user_ind_columns 兩張表裡面,
其中:
user_indexes 系統檢視存放是索引的名稱以及該索引是否是唯一索引等資訊,
user_ind_columns 統檢視存放的是索引名稱,對應的表和列等
select * from user_indexes;
select * from user_ind_columns ;
select index_name, table_name, column_name from user_ind_columns where table_name=' tableName '
3.空字串的問題
Oracle中空字串''就是null(也就是說,只有null,沒有空字元),而MySQL是區分null和''的。
這就要求一方面,以後在編寫程式碼的時候,儘量保證不會往資料庫插入空字串''這樣的值,要麼保持有資料,要麼保持為null。另外,對於MySQL中已經同時存在Null和''時,所有判斷是否為null或者''的地方改為判斷列的長度是否為0。