oracle與sqlserver的十大區別

轉載於:http://blog.csdn.net/lingxyd_0/article/details/8781727#comments

--sql server 與  oracle的區別:

      --DBMS 數據庫管理系統
--1.數據類型不同。
      --sql server 的數據類型:int ,smallint ,char,varchar,nchar,nvarchar,ntext,datetime,smalldatetime,money,decima,
      --float,bit……
      
      
      --oracle 的數據類型:number(p,s),char,varchar2,Date,LOB
               --注意:insert into table_name values('1','張三','男',date'2012-3-5');---插入字符串日期前加date轉換類型
      
--2.獲得當前系統時間的函數不同。
      --sql server :getdate()
      
      --oracle:sysdate
            --例如:設定日期格式的函數:to_char(sysdate,'yyy-mm-dd');
--3.在oracle中沒有默認約束的說法
      --sql server 中添加默認約束:alter table talbe_name add DF_table_name default('男') for sex;
      
      --oracle 中添加默認值:alter table table_name modify(sex default('男'));


--4.連接變量和字符串的方式不一樣
      --sql server 中連接:使用“+”連接,例如:print 'aaaa'+@name;
      
      --oracle  中連接:使用“||”連接,例如:dbms_output.put_line('aaa'||name);---name爲變量
 
--5.oracle沒有identity自動增長列,而是使用序列實現增長
      --sql server 自動增長:在表的主鍵列中可直接使用identity(1,1)實現增長
      
      --oracle 使用序列自動增長:
                                 create sequence se_id 
                                 start with 1
                                 increment by 1
      --使用序列實現自動增長:se_id.nextval
--6.條件語句if……else……的語法不同
      --sql server中:
            if 條件
            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 用戶名
            
            ---授權限
            grant select/update/delete/insert on 表名 to 用戶名
              
            
        ---oracle中:
        
            ---創建用戶語法:
                  create user 用戶名
                  identified by 密碼
                  default tablespace users
                  temporary tablespace temp
                  quota 10M on users
                  

                  --修改密碼:
                  alter user 用戶名 identified by 新密碼
                  
                  --授予權限:
                  grant create session to 用戶名
                  
                  --刪除用戶
                  drop user 用戶名 cascade;
                  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章