zoukankan      html  css  js  c++  java
  • oracle学习笔记3:基本的SQL语句

    oracle基本的SQL语句和SQLSERVER基本一样,在这里只简单列出与SQLSERVER不一样的地方

    1.select * from orderinfo where address = 'abcd'  与 address = 'ABCD'

       得到的结果是不一样的,也就是说oracle字符区分大小写,这一点特别要注意。

    2.查询语句中,如果表引用了别名,则字段也必须的用别名.字段名

       select orderid, ordercode from orderinfo o是一条错误的查询语句,正确的如下:

       select o.orderid, o.ordercode from orderinfo o或者select orderid , ordercode from orderinfo

    3.A{operator}any B:表示A与B中的任何一个元素进行operator运算符的比较,只要有一个比较值为true,就返回数据行

       A{operator}all B:表示A与B中的所有元素进行operator运算符的比较,只有所有元素的比较值都为true,就返回数据行

       select * from orderinfo where orderid = any(1,2,3)    表示orderid等于1或2或3则返回数据行

       select * from orderinfo where orderid <> all(1,2,3)    表示orderid不等于1并且不等于2并且不等于3则返回数据行

    4.与null进行比较要用 is null这与sqlserver一样,在这里记一下

    5.自然连接natural join 

      select o.orderid, u.userid from orderinfo o natural join userinfo u where o.orderid = 1

      oracle把两个表中名称相同的列自动连接

    6.子查询中的in, any ,all

       select * from orderinfo where userid in (select userid from userinfo where username = 'abcd')与sqlserver一样

       select * from orderinfo where userid > any (select userid from userinfo where username = 'abcd')

       只要userid大于任何一个子查询中的userid则返回数据行

       select * from orderinfo where userid > all(select userid from userinfo where username ='abcd')

       userid必须大于子查询中所有的userid才能返回数据行

    7.insert语句

      insert into orderinfo(orderid) values(orderinfo_seq.nextval)

      orderinfo_seq.nextval获取序列下一个值,前提必须创建了orderinfo_seq序列

      批量插入数据:

      insert into orderinfo_tmp select * from orderinfo where orderid = 1

    8.update语句

      update orderinfo set mobilephone = '13333333333' where orderid = 1

      通过查询更新:

      update orderinfo set (mobilephone, address, productnumeric, amount) = (select mobilephone,

      address, productnumeric, amount from orderinfo where orderid = 1)

      where orderid = 2

    9.Delete,truncate语句

       delete from orderinfo where orderid = 1

       truncate table orderinfo --删除orderinfo表中所有的记录,并且不记录日志,所以可以很快的删除记录,但是无法恢复

    10.事务

    oracle11g中的事务是隐式自动开始的,它不需要用户显示地执行开始事务语句,但对于事务的结束处理,则需要用户进行指定的操作,

    通常在以下情况oracle认为一个事务结束了:

    1.执行commit语句提交事务

    2.执行rollback语句撤消事务

    3.执行一条数据定义语句,如create,drop,alter等语句,如果语句执行成功,oracle系统会自动执行commit命令,否则系统会自动执行

       rollback命令

    4.执行一个数据控制命令,比如grant,revoke,这种语句执行完毕,系统会自动执行commit命令

    5.正常地断开数据库连接,正常退出sqlplus环境,系统会自动执行commit命令,否则系统自动执行rollback命令

    综合上述五种情况,归根到底事务的结束要么执行commit命令,要么执行rollback命令

    提交事务:

    insert into orderinfo (orderid,...) values(orderinfo_seq.nextval,...);

    commit;

    回滚事务:

    insert into orderinfo (orderid,...) values(orderinfo_seq.nextval,...);

    rollback;

    事务可以回滚到一个点,如:

    truncate table orderinfo

    insert into orderinfo (orderid,...) values(orderinfo_seq.nextval,...);

    savepoint sp;

    insert into orderinfo (orderid,...) values(orderinfo_seq.nextval,...);

    rollback to savepoint sp;

    commit;

    上述语句执行后数据表中只有一条记录,也就是每一条插入语句所插入的数据

    12.分页查询

    select * from(
    select o.*, rownum r from orderinfo o where rownum <=20 order by o.orderid asc)
    where r > 10

    查询第11-20条记录

      

  • 相关阅读:
    原生JS回去顶部
    5月31日の勉強レポート
    5月30日の勉強レポート
    (转)日语自我介绍大全
    5月29日の勉強レポート
    5月28日の勉強レポート
    5月27日の勉強レポート
    5月26日の勉強レポート
    5月25日の勉強レポート
    5月24日の勉強レポート
  • 原文地址:https://www.cnblogs.com/gjhjoy/p/3533570.html
Copyright © 2011-2022 走看看