oracle和mssql中语句的区别

oracle和mssql中语句的区别

 

【转】http://hi.baidu.com/%B7%E7%B9%FD%CE%DE%BA%DB%C0%B6%C9%AB/blog/item/4eba4711c46fe68b6538dbaf.html

 

1.desc 表名 :显示数据表的结构
2.as 省略了as
比如 mssql中的select name as 姓名 在oracle中 改为 select name 姓名
3.增加了 any, some 的嵌套查询功能 两个参数作用一样 例子如下
select name form class.emp where number>any(select number form student.emp where sex='男')
他查询的是学号大于任何一个男生学号的女生名字
4. 增加了 all 的嵌套查询功能 例子如下
select name form class.emp where number>all(select number form student.emp where sex='男')
他查询的是学号大于所有男生学号的女生名字
5.出现了并操作,交操作,差操作, 例子如下
(select name for class.emp)union (select name for class.dept) 这是并操作意思就是两个查询的并集
(select name for class.emp)intersect (select name for class.dept) 这是交操作意思就是两个查询的交集
(select name for class.emp)minus (select name for class.dept) 这是交操作意思就是两个查询的差
6.数据删除 truncate table 表名 表删除语句 和mssql中大不一样了 告别了delete
现总结这么多 稍后继续

我今天又来了 继续写

在创建表的时候 应该在语句最后要 要注明保存到那个表空间 而sql没有这一项
语法如下:tableespace 表空间名 一半写两遍
同样在创建索引时候也要写下这个东西 不过写一遍 不过在创建试图的时候你可以不写轻松一下

在创建表的主键和 外键 还有约束的时候要
前面要加怎么一句 constarint “名称” 然后在写什么 check(...),primary key(...)
这主要是要和可视界面操作下编辑表的约束条件选项卡中的 名称 栏相对应要不是个空的

oracle里多了截断这个操作 截断炒作的sql语法如下
truncatetable 用户名 表名 drop|reuse storage
如果使用drop storage 语句 显示指明释放数据表和索引的空间 若使用 reuse storgae显示指明不是方数据表何索引表的空间
如果那个表又外键关系 必须先消除外键 然后才能截断 sql语法 截断trucate 对所有的表的操作都很快 delete操作要产生大量的回滚信息 所以 如果表很大 则删除速度较慢。trucate是 ddl语言(数据定义语言) 隐含了提交操作,因此不能回滚。在用truacte删除表时可以看加快全表的扫面速度 delete 不能截断操作不能删除完整性约束条件,而delete可以。

在aqlplus worksheet中创建用户
首先 开始创建图形界面中一般信息选项卡里的东西
crerte user "tempuser" profile "default"
identified by "tempuser" default tablespace "users"
account unlock
下面 创建图形界面中系统权限选项卡里的东西
cant create any table to "tempuser" with admin option
最后 创建对象权限中对象权限选项卡里的东西
crant “connect” to "tempuser" with anmin opion;
下一步 开始了 执行下列代码

begin
   dbms-resource-manager-privs.grant-switch-confumer-group(
        grantee-name => 'tempuser',
        consumer-group => 'default-consumer-group',
        grant-option => true); end;
最后的一步

begin
   dbms-resource-manager.set-intital-consumer-grourp(
      user =>'tempuser'
      consumer-group=>'default-consumer-group');end;
最后两步具体是什么东西等下回再说 上班时间不能多写

补一下linux下建立oracle 用户方法

linux下建立oracle 用户方法
创建 oracle 用户。 以 root 用户身份执行
# groupadd oinstall
# groupadd dba   
# mkdir -p /export/home/oracle /ocfs
# useradd -d /export/home/oracle -g oinstall -G dba -s /bin/ksh oracle
# chown oracle:dba /export/home/oracle /u01
# passwd oracle
New Password:
Re-enter new Password:
passwd: password successfully changed for oracle

创建 oracle 用户环境文件。
/export/home/oracle/.profile

然后添加环境变量   export 添加变量命令export [变量名称]=[变量设置值]

export PS1="`/bin/hostname -s`-> "
export EDITOR=vi
export ORACLE_SID=devdb1
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1
export ORA_CRS_HOME=$ORACLE_BASE/product/10.2.0/crs_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export PATH=$ORACLE_HOME/bin:$ORA_CRS_HOME/bin:/bin:
/usr/bin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin
umask 022umask [-S][权限掩码]

创建文件系统目录结构。以 oracle 用户身份执行
rac1-> mkdir p $ORACLE_BASE/admin
rac1-> mkdir p $ORACLE_HOME
rac1-> mkdir p $ORA_CRS_HOME
rac1-> mkdir -p /u01/oradata/devdb

今天是周末晚上到家了 听着歌 写一点关于备份存储方面的不同吧
先说下mssql 吧
看下面的命令先
backup database card
to disk = 'c:/card.bak'
restore filestonly  
form disk = 'c:/card.bak'
restore database card2
from disk = 'c:/card.bak'
with more 'card' to 'c:/card2.mdf'
      more 'card' to 'c:/card2.log'
这是masql的的备份方法 上面这个 改变了 还原的数据的库的名称和存放地方 可以用到数据太大 硬盘空间不够只能转存倒新的分区上面
另外说几个特殊的命令
RESTORE FILELISTONLY
返回由备份集内包含的数据库和日志文件列表组成的结果集。上面用到了
万一在还原时候 突然的原因关机了 停电了 该怎么办
要用倒restar 本人没有用过 只是听说过这个东西
RESTORE DATABASE MyNwind
   FROM MyNwind_1
   WITH RESTART
GO
另外还要说一下 有怎么一个情况 看下面的几个语句就说的是完整数据库备份后进行差异数据库备份
restore database mysql
   from mysql2
   with norecovery
restore database mysql
   from mysql2
   with flie = 2
norecory 当还原数据库备份和多个事务日志时,或在需要多个 RESTORE 语句时(例如在完整数据库备份后进行差异数据库备份),SQL Server 要求在除最后的 RESTORE 语句外的所有其它语句上使用 WITH NORECOVERY 选项。

好了 mssql 备份还原就说到这里了 下面该 oracle的 plsql了中的逻辑备份命令

数据导出备份:
1 将数据库TEST完全导出,用户名system 密码manager 导出到D:daochu.dmp中
   exp system/manager@TEST file=d:daochu.dmp full=y
2 将数据库中system用户与sys用户的表导出
   exp system/manager@TEST file=d:daochu.dmp ōwner=(system,sys)
3 将数据库中的表inner_notify、notify_staff_relat导出
    exp aichannel/aichannel@TESTDB2 file= d:datanewsmgnt.dmp tables=(inner_notify,notify_staff_relat)
4 将数据库中的表table1中的字段filed1以"00"打头的数据导出
   exp system/manager@TEST file=d:daochu.dmp tables=(table1) query=" where filed1 like '00%'"
数据的导入
1 将D:daochu.dmp 中的数据导入 TEST数据库中。
   imp system/manager@TEST file=d:daochu.dmp
   imp aichannel/aichannel@HUST full=y file=d:datanewsmgnt.dmp ignore=y
   上面可能有点问题,因为有的表已经存在,然后它就报错,对该表就不进行导入。
   在后面加上 ignore=y 就可以了。
2 将d:daochu.dmp中的表table1 导入
imp system/manager@TEST file=d:daochu.dmp tables=(table1)

基本上上面的导入导出够用了。不少情况要先是将表彻底删除,然后导入。


linux下使用 Oracle 恢复管理器 (RMAN) 备份和恢复 Oracle RAC 数据库的过程与单实例数据库的备份和恢复过程相同。
下面说个简单的备份恢复的例子 先看下步骤
1. 执行完整的数据库备份。
2. 在 test_d 表空间中创建 mytable 表。
3. 在 t1 时间,向 mytable 中插入第一个记录。
4. 在 t2 时间,向 mytable 中插入第二个记录。
5. 在 t3 时间,删除 mytable 表。
6. 将 test_d 表空间恢复到某个时间点。
7. 验证恢复结果。
执行完整的数据库备份。
rac1-> rman nocatalog target /

Recovery Manager: Release 10.2.0.1.0 - Production on Mon Nov 13 18:15:09 2006

Copyright (c) 1982, 2005, Oracle. All rights reserved.

connected to target database: DEVDB (DBID=511198553)
using target database control file instead of recovery catalog 蓝色的没有用

RMAN> configure controlfile autobackup on;

RMAN> backup database plus archivelog delete input;

在 test_d 表空间中创建 mytable 表。
19:01:56 SQL> connect system/oracle@devdb2
Connected.
19:02:01 SQL> create table mytable (col1 number) tablespace test_d;

Table created.
在 t1 时间,向 mytable 中插入第一个记录。
19:02:50 SQL> insert into mytable values (1);

1 row created.

19:02:59 SQL> commit;

Commit complete.
在 t2 时间,向 mytable 中插入第二个记录。
19:04:41 SQL> insert into mytable values (2);

1 row created.

19:04:46 SQL> commit;

Commit complete.
在 t3 时间,删除 mytable 表。
19:05:09 SQL> drop table mytable;

Table dropped.
将 test_d 表空间恢复到某个时间点。
为辅助数据库创建辅助目录。
rac1-> mkdir /u01/app/oracle/aux


RMAN> recover tablespace test_d
2> until time "to_date('13-NOV-2006 19:03:10','DD-MON-YYYY HH24:MI:SS')"
3> auxiliary destination '/u01/app/oracle/aux';

RMAN> backup tablespace test_d;

RMAN> sql 'alter tablespace test_d online'
验证恢复结果。
19:15:09 SQL> connect system/oracle@devdb2
Connected.
19:15:16 SQL> select * from mytable;

      COL1
----------
         1

 

前面扯太远了 后面拉回来 还是说 sql的不通 不说rman了

用了一个例子介绍plsql简单语法
set serveroutput on 允许服务器输出
declear             定义局部变量
maxrecords constant int:=100;
i int:=1;
begin 运行局部变量
for i in 1..maxrecords loop
insert into tempuser.testtable(recordnumber,currentdate)
values (i,sysdate); 插入数据
end loop;
dbms_output_line('问世间情为何物?一物降一物');
commit; 提交结果
end; 结束执行

下面是这个程序在mssql里 怎么写 用的时while循环 我觉得mssql里没有loop循环语句
declare
@maxrecords int @i int
@maxrecords = 100 @i =1
begin
while @i<@maxrecords
insert into testtable(recordnumber,currentdate) values (i,sysdate);
print '问世间情为何物?一物降一物';
select @i=@i+1;
end;

比较一下就可以看出来到底那里不一样了

plsql里面命令的结构为
delacre
    定义语句段
begin
    执行语句段
exception
    异常处理语句段
end
这就是plsql程序总体结构图

定义变量与mssql的不同
基本方法
变量名 类型标识符【notnull】:=值
例 age number(8):=26
多了定义复合数据类型变量的功能
1.多了%type 变量
declare
    mydate user。testtable.currentdate%type;
还有 %rowtype类型的变量可以识变量获得字段的数据类型,使用%rowtype可以识变量获得整个记录的数据类型。
变量名 数据表.列名%type
变量名 数据表%rowtype
declare
mytable testtbale%rowtype 包含了testtable 所有字段 只不过在输出时候可以选择输出那个
begin
shelect * into mytable
from temuuser.tedttbale
where recordnumber=88
dbms_output.put_line(mytable.currentdate);
end;
还有就是有了定义符合变量
格式
type 复合变量名 is record(
     变量 类型, 可以有好几个);
     变量名 复合变量名 这个变量名就好像java中类的对象一样而复合变量名就是类名可以这样理解 个人观点
begin
    select * into 变量名 from 表名 where 条件
   dbms_output.put_line(变量名.表中的值)
end

另外还可以定义一维数组
type 表类型 is table of 类型 index by binary_integer
表变量名 表类型
index by binary_integer子句代表以符号整数为索引,
这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”


Declare
type tabletype1 is table of varchar2(4) index by binary_integer;
type tabletype2 is table of tempuser.testtable.recordnumber%type index by
binary_integer;
table1 tabletype1;
table2 tabletype2;
begin
table1(1):='大学';
table1(2):='大专';
table2(1):=88;
table2(2):=55;
dbms_output.put_line(table1(1)||table2(1));
dbms_output.put_line(table1(2)||table2(2));
end;
一个标准的一维数组

定义多维表类型变量
定义了名为 tabletype1 的多维表类型,相当于多维数组,table1 是多维表类型变量,将数据表 tempuser.testtable 中
recordnumber为 60 的记录提取出来存放在 table1 中并显示。

type tabletype1 is table of testtable%rowtype index by binary_integer;
table1 tabletype1;
begin
select * into table1(60)
from tempuser.testtable
where recordnumber=60;
dbms_output.put_line(table1(60).recordnumber||table1(60).currentdate);
end;

在来看下面的这个程序
set serveroutput on
Declare
   result integer;
begin
result:=10+3*4-20+5**2;
dbms_output.put_line('运算结果是:'||to_char(result));
end;

|| 这个符号是连接语句
to_char(result) dbms_output.put_line函数输出只能是字符串,因此利用 to_char函数将数值型结果转换为字符型。
To_char:将其他类型数据转换为字符型。 To_date:将其他类型数据转换为日期型。 To_number:将其他类型数据转换为数值型。

再说下plsql中的控制语句组合有哪几种

1. if..then..end if条件控制
if   条件 then
   语句段;
end if;

2. if..then..else..end if条件控制
if   条件 then
语句段1;
else
语句段2;
end if;

3. if 嵌套条件控制
if   条件1 then
   if 条件2 then
     语句段1;
   else
     语句段2;
   end if;
else
   语句段3;
end if;

4.loop..exit..end loop 循环控制
loop
    循环语句段;
    if 条件语句 then
       exit;
    else
       退出循环的处理语句段
    end if;
end loop;

5. loop..exit..when..end loop 循环控制
采用 loop..exit..when..end loop 循环控制的语法结构与loop..exit..end loop 循环控制类似
exit when 实际上就相当于
if 条件 then
   exit;
end if;
   
6.while..loop..end loop 循环控制
while 条件 loop
     执行语句段
end loop;


7.for..in..loop..end 循环控制
for 循环变量 in [reverse] 循环下界..循环上界 loop
    循环处理语句段;
end loop;
最后一个出个例子
set serveroutput on
declare
   number1 integer:=80;
   number2 integer:=90;
   i integer:=0;
begin
   for i in 1..10 loop
     number1:=number1+1; 在mssql里是 sclect @number=@number+1
   end loop;
dbms_output.put_line('number1的值:'||to_char(number1));
end;  
本人学java 的 对plsql一看觉的很简单 和java比起来简单多了但是oracle 命令只是一部分更多的东西需要我去学习 自夸一下 哈哈

 

在plsql 多了事务处理命令

commit命令
commit事务提交命令。在oracle中为了保证数据的一致性在内存中将为每个客户机建立工作区,就是说在用commit命令之前的操作都在这个工作群里完成,只有在用commit命令之后才会把你写的命令写入到数据库中。
有个自动进行事务提交的命令
set auto on
关闭为 set auto off

rollback命令
rollback是事务回滚命令,在没有提交commit命令千,如果发现delete insert update等操需要恢复的话,可以用rollback命令会滚到上次commit时的状态。
set auto off 要先关闭自动提交
select * from scott.emp;
delete form scott.emp;
rollback
这个时候就可以看到 scott.emp还是以前的没有变化

savepoint命令
这个命令时保存点命令。事务通常由多个命令组成,可以将每个事务划分成若干个部分进行保存,这样回滚每个保存点,就不必回滚整个事务。
创建保存点 savepoint 保存点名
回滚保存点 rollback to 保存点名
来个例子
insert into scott.emp(empno,ename,sal) values(9000,'wang',2500); 先插入一个值
savepoint insertpoint; 创建一个还原点,名字叫insertpoint
rollback to insertpoint; 还原到那个还原点

下面开始说游标
这个东西在mssql里没有吧 我没有印象
游标不是c里面的指针,我一看到这个词就想到了指针可惜何c里面的指针大不一样 不要弄混了 估计没人会弄混。
游标可以说是一个临时的数据存放的地方
要用游标先要定义
cursor 游标名 is select 语句
cursor这是游标的关键字 selcet建立游标的查询命令
看个例子
set serveroutput on
declare
tempsal scott.emp.sal%type 定义了一个变量他是scott.emp.sal同一个类型
cursor mycursor is   定义一个游标mycursor
select * from scott.emp
where sal>tempsal;
begin
tempsal:=800;
open mycursor;   打开这个游标
end;
晕忘了 只是打开游标没有用 还要提取游标的数据
用fetch命令
fetch 游标名 into 变量1,变量2,。。。。;
或者
fetch 游标名 into 记录型变量名;
上面那个程序要改一下

set serveroutput on
declare
tempsal scott.emp.sal%type 定义了一个变量他是scott.emp.sal同一个类型
cursor mycursor is   定义一个游标mycursor
select * from scott.emp
where sal>tempsal
new scott.emp%rowtype; 有定义了一个新的变量
begin
tempsal:=800;
open mycursor;   打开这个游标
fetch mycursor into new; 读取游标数据把他添加到new中
dbms_output._line(to_char(new.sal)); 显示结果
close mysursor; close关闭这个游标
end;

游标的属性
1.%isopen属性
就是判断游标是否打开,如果没有打开就是用fetch语句提示错误
2.%found属性
就是测试前一个fetch语句是否有值,有就返回true 没有就返回false
3.%notfound属性 和上面的相反
4.%rowcount属性 判断游标的数据行数就是有多少数据

下面说下过程的概念 sql里没有
完整的过程的结构如下
create or replace 过程名 as
     声明语句段;
begin
     执行语句段;
exception
     异常处理语句段;
end;
过程是有名称的程序块,as关键词代替了无名块的declare


创建实例的过程
创建一个名为tempprocdeure的过程,create是创建过程的标识符,replace表示如果又同名的过程将覆盖原过程。定义了一个变量,其类型何testtable数据表中的currentdate字段的类型相同,都是日期型,将数据表中的recordnumber字段为88的currentdate字段内容送入变量中,然后输出结果。

set serveroutput on
creat or replace procedure tempuser.tempprocedure as
tempdate tempuser.testtable.currentdate%type;


begin
select currentdate
into   tempdate
from   testtable
where recordnumber=88;
dbms_output.put_line(to_char(tempdate));
end;
使用过程
set serveroutput on
begin
tempprocedure;
end;
下面说下带参数的过程
1.参数类型
in 读入参数 程序向过程传递数值
out 读出参数 过程向程序传递数值
in out 双向参数 程序过程互相传递数值
定义带参数的过程
set serveroutput on
creat or replace procedure scott.tempprocedure(
      tempdeptno in scott.dept.deptno%type,/*定义了一个in类型的变量*/
      tempdname out scott.dept.danme%type,/*定义了一个out类型的变量*/
      temploc in out scott.dept.loc%type)as /*定义了一个inout型的变量*/
      loc1 scott.dept.doc%type;
      dname1 scott.dept.dname%type;
begin
     select loc into loc1
     from scott.dept
     where deptno=tempdeptno;
     select danme into danme1
     from scott.dept
     where deptno=tempdeptno;
     temploc:='地址'||loc1;
     tempdname:='姓名'||dname1;


end;

定义好了 下面开始用了
set serveroutput on
declare
   myno scott.dept.deptno%type;
   mydname scott.dept.dname%type;
   myloc   scott.dept.loc%type;


begin
   myno:=10;
   mydname:='';
   myloc:='';
   scott.tempprocedure(myno,mydname,myloc);
   dbms_output.put_line(myno);
   dbms_output.put_line(mydname);
   dbms_output.put_line(myloc);
end;
搞定了
就是说用重新定义的三个变量当参数传递给上面定义的过程过程里带参数的变量可以接受这三个变量的值
用java语言来解释就是那个过程就是类 带三个参数
这三个变量就是数据 当然没有对象了哈哈毕竟不是java么哈哈

今天写到这里了 我要下班了 7.3


异常处理
就是程序中要处理特殊情况的办法

1. 定义异常处理
定义异常处理的语法如下:
declare
    异常名 exception;
2. 触发异常处理
触发异常处理的语法如下:
raise   异常名;
3. 处理异常
触发异常处理后,可以定义异常处理部分,语法如下:
Exception
When 异常名 1 then
     异常处理语句段 1;
When 异常名 2 then
     异常处理语句段 2;

下面的 PL/SQL 程序包含了完整的异常处理定义、触发、处理的过程。定义名为 salaryerror
的异常,在 scott.emp 数据表中查找 empno=7566 的记录,将其值放入变量 tempsal 中,判断
tempsal 值若不在 900 和2600 之间,说明该员工的薪水有问题,将激活异常处理,提示信息。

set serveroutput on
declare
   salaryerror exception;
   tempsal scott.emp.sal%type;
begin
   select sal into tempsal
   from scott.emp
   where empno=7566;
   if tempsal<900 or tempsal>2600 then
      raise salaryerror;
   end if;   
   exception
   when salaryerror then
     dbms_output.put_line('薪水超出范围');
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值