創建數據庫
create database databaseName;
Oracle:無法使用create database,oracle沒有數據庫概念但有表空間;
一般使用:
drop database if exists databaseName;
create database databaseName default character set utf8 collate utf8_general_ci;
刪除數據庫
drop database if exists databaseName;
創建表
create table tableName(
id int(32) primary key,
name varchar(128) default null
);
一般使用:
drop table if exists tableName;
create table tableName(
id int(32) primary key not null auto_increment comment 'id,主鍵',
`code` varchar(128) unique not null comment '唯一碼',
`status` enum('-1','0','1') not null default '0' comment '狀態,-1未連接,0關,1開',
recorded_date datetime not null comment '錄入時間'
)engine=innodb default charset=utf8 comment='xx表';
Oracle:同mysql,但無法在建表語句內使用comment,需要在建表之後再添加。
create table ww_offic_doc_upload
(
id int not null primary key,
file_xml clob,
file_name varchar2(300),
file_title varchar2(300) not null,
file_number varchar2(300),
receive_number varchar2(300),
requestid varchar2(100) not null,
operate_name varchar2(100),
upload_user_id int not null,
upload_user_name varchar2(50),
upload_time varchar2(20) not null
);
comment on table ww_offic_doc_upload is '公文上傳(導入)信息記錄表。';
comment on column ww_offic_doc_upload.file_xml is 'xml文件內容';
comment on column ww_offic_doc_upload.file_name is '文件名';
comment on column ww_offic_doc_upload.file_title is '文件標題';
comment on column ww_offic_doc_upload.file_number is '文件編號';
comment on column ww_offic_doc_upload.receive_number is '迴文編號';
comment on column ww_offic_doc_upload.requestid is '流程requestid';
comment on column ww_offic_doc_upload.operate_name is '操作名稱';
comment on column ww_offic_doc_upload.upload_user_id is '上傳人id';
comment on column ww_offic_doc_upload.upload_user_name is '上傳人名稱';
comment on column ww_offic_doc_upload.upload_time is '上傳時間';
oracle exists函數
exists表示()內子查詢語句返回結果不爲空,效果等同於count()>0,可以配合not一起使用。
當父查詢數據量小於子查詢數據量時,使用exists的效率更高,反之使用in效率更高。
示例:
select * from t1 where exists(select 1 from t2 where t1.id=t2.id);
oracle判斷表是否存在
declare tb_count number;
begin
select count(1) into tb_count from user_tables where table_name = upper('tableName') ;
if num > 0 then execute immediate 'drop table tableName'; end if;
end;
oracle immediate
immediate是oracle用於動態執行字符串形式的sql,常用於sql編程。
傳參實例:
--參數:v_table ,v_id
execute immediate 'select * from '||v_table||' where id=:id' using v_id;
--參數v_id,v_name
str varchar2(200);
str:='select * from user_t where id=:1 and name=:2';
execute immediate str using v_id,v_name;
刪除表
drop table tableName;
Oracle:同mysql;
修改表名
alter table oldTbleName rename newTableName;
Oracle:同mysql;
添加列
alter table tableName add column columnName varchar(10);
Oracle:alter table tableName add (columnName varchar2(10) );
添加列到指定列之後(僅限mysql,oracle不支持)
alter table tableName add column columnName2 varchar(10) after columnName1;
刪除列
alter table tableName drop column columnName;
Oracle:alter table tableName drop (columnName);
修改列名
alter table tableName change column oldColumnName newColumnName varchar(10);
修改列類型(或修改列長度)
alter table tableName modify column columnName varchar(10);
或
alter table tableName modify columnName varchar(10);
Oracle:alter table tableName modify (column varchar2(10));
添加主鍵
alter table tableName add primary key(id);
刪除主鍵
alter table tableName drop primary key;
添加外鍵
alter table table1 add constraint foreignKeyName foreign key( table1_ColumnName) references table2(table2_PrimaryKeyColumnName);
刪除外鍵
alter table table1 drop foreign key foreignKeyName;
添加自增
auto_increment必須要求該列是主鍵(或別的鍵,詳細請看文章:MySQL添加/刪除主鍵、外鍵、唯一鍵、索引、自增),所以
如果該列不是主鍵:
alter table tableName change columnName columnName int(16) not null primary key auto_increment;
如果該列是主鍵:
alter table tableName change columnName columnName int(16) not null auto_increment;
刪除自增
alter table tableName change columnName columnName int(16);//刪除自增長
oracle自增
oracle不支持自增,但可以使用觸發器+序列實現自增效果,並且在使用時需特別注意,爲每個表單獨創建一個序列,不要多表共用一個序列,不然自增的id不是連續的。
create or replace trigger triggerName before insert on tableName
for each row begin select sequenceName.nextval into :new.id from dual; end;
示例:
create table ww_offic_doc_operlog
(
id int not null primary key,
operate_name varchar2(100),
upload_user_id int not null,
upload_user_name varchar2(50),
upload_time varchar2(20) not null
);
comment on table ww_offic_doc_operlog is '公文上傳(導入)歷史記錄表。';
create sequence ww_offic_doc_operlog_seq start with 1 increment by 1 cache 10;
create or replace trigger ww_offic_doc_operlog_tri before insert on ww_offic_doc_operlog
for each row
begin
select ww_offic_doc_operlog_seq.nextval into :new.id from dual;
end;