• Oracle 基础知识入门


    前记:

    近来项目用到Oracle数据库,大学学了点,后面基本忘记得差不多了,虽然基本语法跟sql 差不多,但是oracle知识是非常多的。

    这里简单说点基础知识,希望后面补上更多的关于ORacle知识博客。入门的朋友可以看看,高手就可以绕过了。

    不晓得你们用的什么工具,我用的Toad。用起来还是不错的。

    第一部分,创建数据,

    create table student
    (
    sName varchar(20) primary key,
    sAge int,
    sEmail varchar(100),
    sPhone varchar(20),
    sAddress varchar(100)
    )
    
    insert into student values('Jack',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Jack1',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Jack2',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Jack3',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Jack54',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Jack6',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Jack7',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Jack21',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Rose',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('rose1',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('rose2',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('rose4',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Adi',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Aditt',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Niyes',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Jassic',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Carken',21,'dfdf@qq.com','2134343','Singapore');
    insert into student values('Donview',21,'dfdf@qq.com','2134343','Singapore');
    commit;
    

    执行其他都会报错的.

    第二部分,看几个关于Spool的命令

      

    spool c:/test.log; --将下面的查询结果放在这个文件中,如果文件不存在,会自动创建
    
    select * from student;
    
    spool off;  --完成spool
    --执行后,你就可以去相应的目录去查看Log了。
    --再看一个例子
    set feedback on; --如果这里设置为off,则看不到18 rows selected
    set termout on; --如果这里设置为off,则看不到结果
    set echo on; --这里看到SQL>命令,就是这个开启的原因
    spool c:/test.log;
    
    select * from student;
    
    spool off;
    exit;
    

      

    结果(只显示了一部分):

    spool常用的设置
    set echo on;    //显示start启动的脚本中的每个sql命令,缺省为off
    set feedback on;  //回显本次sql命令处理的记录条数,缺省为on
    set heading off;   //输出域标题,缺省为on
    set pagesize 0;   //输出每页行数,缺省为24,为了避免分页,可设定为0。
    set termout on;   //显示脚本中的命令的执行结果,缺省为on
    set trimout on;   //去除标准输出每行的拖尾空格,缺省为off
    set trimspool on;  //去除重定向(spool)输出每行的拖尾空格,缺省为off

    上面的命令最好自己亲自动手测试下。因为我发现自己测试是跟下面的链接,其他前辈有出入。

    所以自己动手去实践下比较好。

    对于spool的相关了解,查看下面的这个链接

    http://blog.sina.com.cn/s/blog_6bccf0360101hzsh.html

    http://blog.csdn.net/shangyang326/article/details/3304621

    第三部分,几个oracle 脚本知识入门。

    主要查看下面这两个链接:

    http://www.oracle.com/technetwork/issue-archive/2013/13-mar/o23plsql-1906474.html

    http://docs.oracle.com/cd/B10500_01/appdev.920/a96624/a_samps.htm

    1,我们想查看姓名=Jack 的信息,这里只有一条记录。

    set serveroutput on; --要想看到打印结果,则必须开启这个命令。
    DECLARE
      l_name  varchar(100);
      --l_name  student.sName%TYPE; 相同的效果,推荐使用这个。
    BEGIN
      SELECT sName  INTO l_name
      FROM student
      WHERE sName = 'Jack';
    
      DBMS_OUTPUT.put_line ('find the name: '||l_name);
    END;
    
    结果:
    find the name: Jack
    PL/SQL procedure successfully completed.
    

    %RowType 的使用,获取某行的数据类型。  

    set serveroutput on;
    DECLARE
      rowData  student%ROWTYPE;
    BEGIN
      SELECT * INTO rowData
      FROM student
      WHERE sName = 'Jack';
    
      DBMS_OUTPUT.put_line ('find the name: '||rowData.sName);
      DBMS_OUTPUT.put_line ('find the age: '||rowData.SAGE);
      DBMS_OUTPUT.put_line ('find the email: '||rowData.sEmail);
      DBMS_OUTPUT.put_line ('find the phone: '||rowData.sPhone);
      DBMS_OUTPUT.put_line ('find the address: '||rowData.sAddress);
      
    END; 
    

     

    结果:

    find the name: Jack
    find the age: 22
    find the email: dfdf@qq.com
    find the phone: 2134343
    find the address: Singapore
    PL/SQL procedure successfully completed.
    

      

    关于Type的用法,查看

    http://blog.csdn.net/chen_linbo/article/details/6367871

    2, 查看姓名包含rose的信息(包含多条记录)。

    set serveroutput on;
    
    DECLARE
      cursor name_rose_cur is 
      select sName from student where upper(sName) like upper('%rose%');
      l_name  student.sName%TYPE;
    BEGIN
       open name_rose_cur;
       Loop
    	   fetch name_rose_cur into l_name;
    	   exit when name_rose_cur%NOTFOUND;
    	   
    	   DBMS_OUTPUT.put_line ('find the name: '||l_name);
       end loop;
       
       close name_rose_cur;
    END; 
    

     

    结果:

    find the name: Rose
    find the name: rose1
    find the name: rose2
    find the name: rose4
    PL/SQL procedure successfully completed.
    

      

    同样的功能可以用For循环来实现。

    set serveroutput on;
    DECLARE
    
      cursor name_rose_cur is 
      select * from student where upper(sName) like upper('%rose%');
    BEGIN
       for student_cur
        in name_rose_cur
       Loop
    	   DBMS_OUTPUT.put_line ('find the name: '||student_cur.sName);
       end loop;
    END; 
    

     这里的结果跟上面是一样的。 

    Oracle 水很深,希望再接再厉.

  • 相关阅读:
    P4049 [JSOI2007]合金
    CF1073C Vasya and Robot
    输出100以内奇数,偶数,质数,合数的脚本
    取/etc/password文件最后一个单词的最后一个字符
    window下进程退出后自动重启
    如何让DOS命令在新窗口打开
    dos命令关闭所有dos窗口
    使用jps查看JVM进程信息
    windows .bat批处理实现进程监控确保程序运行
    经典博客4(六尺帐篷)
  • 原文地址:https://www.cnblogs.com/lideng/p/3544152.html
Copyright © 2020-2023  润新知