The difference between --sql server and Oracle :
--DBMS database management system
--1. The data types are different.
--sql server data type: int,smallint,char,varchar,nchar,nvarchar,ntext,datetime,smalldatetime,money,decima,
--float,bit... --oracle data type: number(p,s) ,char,varchar2,Date,LOB --note : insert into table_name values('1','Zhangsan','male', date' 2012-3-5');---add date before inserting string date Conversion type -- 2. The function to obtain the current system time is different. --sql server:getdate() --oracle:sysdate --For example: function to set date format : to_char(sysdate,'yyy-mm-dd'); --3. There is no default constraint in oracle- -Add default constraint to sql server: alter table talbe_name add DF_table_name default('male') for sex; --oracle add default value: alter table table_name modify(sex default('male'
--sql server connection: use " + " connection, for example: print 'aaaa'+@name; --oracle connection: use " || " connection, for example: dbms_output.put_line('aaa'||name); ---name is a variable--5.oracle does not have an identity auto-growth column, but uses a sequence to achieve growth --sql server auto-growth: identity (1,1) can be used directly in the primary key column of the table to achieve growth --oracle Use sequence automatic growth: create sequence se_id start with 1 increment by 1 --Use sequence to achieve automatic growth: se_id.nextval --6 . The syntax of the conditional statement if...else...is different --in sql server: if condition begin ... …… end else begin
…………
end
--oracle中:
if 条件1 then
…………;
elsif 条件2 then
…………;
else
…………;
end if;
--7.case语句的语法不同
--sql server中:
--select ....case.....(else)....end....语句
select stuno '学号',case
when grade>=90 and grade<=100 then '★★★★'
when grade>=80 and grade<90 then '★★★'
when grade>=70 and grade<80 then '★★'
when grade>=60 and grade<70 then '★'
else '差'
end as '等级' from score
go
--oracle中:
declare
nums number:=&nos;--&nos表示提示传入值
begin
case nums
when 100 then
dbms_output.put_line('满分也,不错');
when 90 then
dbms_output.put_line('90分页很不错了');
end case;
end;
--8.触发器创建语法不同
--sql server中:
--首先判断触发器是否已经存在
if exists (select * from sys.sysobjects where name='tr_delete')
--如果存在先删除
drop trigger tr_delete
go
--创建触发器
create trigger tr_delete
on bookInfo
instead of delete
as
--定义变量
declare @bookid int
select @bookid=Bookid from deleted---deleted执行删除语句( delete from BookInfo where BookId=1),自动生成的deleted表
--删除与该图书的相关记录(先删除从表再删除主表)
delete from borrowinfo where bookid=@bookid
delete from backinfo where bookid=@bookid
delete from BookInfo where BookId=@bookid
--判断
if @@error<>0
begin
print '删除失败'
rollback transaction
end
else
begin
print '删除成功'
end
go
delete from BookInfo where BookId=1
--oracle中:
--创建触发器
create or replace trigger tri_test
before insert or update or delete
on table_name
[for each row]---如果要使用 :new /:old 就必须使用行触发器
declare
nums varchar2(20);
begin
select 'F'||lpad('aa',5,0) into nums from dual;
end;
--9.oracle中的存储过程
--sql server中存储过程:
--判断存储过程是否已经存在
if exists(select * from sys.sysobjects where name='proc_name')
--如果存在先删除
drop proc proc_name
go
--创建存储过程语句
create proc/procedure proc_name
@参数名1 数据类型 [out/output],
@参数名2 数据类型 [out/output]
as
…………
go
--调用存储过程
--如果有输出参数,则需定义变量(假设@参数2为输出参数)
declare @变量名 数据类型
exec proc_name @参数名1='aaa',@参数名2=@变量名 out
---oracle中带游标及循环的存储过程
create or replace procedure proc_selCurrent
(
names varchar2
)
as
cursor cursor_sel
is
select DepositSum,cardType,name,state from CurrentAccount where name like '%'||names||'%';
dd number;
cc number;
nn varchar2(20);
sta number;
begin
open cursor_sel;
loop
fetch cursor_sel into dd,cc,nn,sta;
dbms_output.put_line('存款金额:'||dd||'姓名:'||nn);
exit when cursor_sel%notfound;
end loop;
close cursor_sel;
end;
--调用存储过程
begin
proc_selCurrent('a');
end;
--10.创建用户的方式不同
--sql server中
--1、创建登陆账号:sa-----123456
create Login 登陆名称 with password='登陆密码'
--修改登陆账户:
alter Login 登陆名称 with name='新登录名称' and password='新登录密码'
--禁用/启用登陆账号
alter Login 登录名称 disable(禁用)/enable(启用)
--删除登陆账号
drop Login 登录名称
--2、创建用户:
create user 用户名 for/from Login 登陆名称
--修改用户名
alter user 用户名 with name='新用户名'
--删除用户名
drop user 用户名
---Authorization limit
grant select/update/delete/insert on table name to user name ---In oracle: ---Create user syntax: create user user name identified by password default tablespace users temporary tablespace temp quota 10M on users - -Modify password: alter user username identified by new password --Grant permission: grant create session to username --delete user drop user username cascade;