• java:Oracle(table的增删改查,data的增删改查)


    1、oracle命名规范:和Java的命名规范很像
        1.严格区分大小写
        2.所有的sql语句都要以';'结尾
        3.所有的sql 都要使用空格区分:sqlplus空格/空格as空格sysdba回车
        4.无论是表名还是列名都必须以字母开头 java中的class必须要以大写字母开头,遵循驼峰命名方式
        5.不能使用ORACLE的保留字
        6.不能与用户定义的其它对象重名
        7.数字,$#_,字母(只能包含A-Z,a-z,0-9,_,$,#)字母加上_
       
    emp表中有一列叫emp_no(员工的编号)
    2、连接oracle有两种方式:
        清理命令行:cls
        oracle的清理命令行:host cls
         1.PL/SQL连接
       2.原生命令行连接(CMD) sqlplus回车 请输入用户名:scoot 请输入密码(输入的密码在命令行上是不可见):回车 user scott locked!(用户被锁了,无法登陆!)

        加锁命令: 首先要登陆管理员用户 sqlplus / as sysdba:使用超级(系统)管理员登录 alter user oracle的用户名 account locked     sysdbs:system database administator(超级管理员) 解锁命令: 首先要登陆管理员用户 sqlplus / as sysdba alter user oracle的用户名 account unlock 查看当前用户登录sql: show user; 修改用户密码:(修改的是密码!) 首先要登陆管理员用户 在oracle中,以后只要看到account这个单词,就一定是用来修改用户的某些信息 但是只需要修改密码的时候就不需要account关键字 alter user oracle的用户名 identified by 123; 解锁并修改用户密码: oracle有自己的错误代码ORA-00922:选项缺失或无效 alter user scott account unlock identified by 新密码; 查看当前用户状态: select username,account_status from dba_status where username='scott'; 在登录oracle完毕后,如何切换用户: conn 用户名;(适用于黑框)
    3.oracle的table(表): 数据库是需要存储数据,数据库中的表把存储的数据进行按规定存放。 雇员(emp):存放员工的信息 部门dept:存放部门的信息 table:表名,列,定义列的数据类型,行
      
      列的类型: 最常用的三个类型: 1.varchar2 2.number 3.date/timestap char和varchar2都是字符串类型: char是固定长度的字符串。char(10),这个里面就必须要有10个char类型字符 比如:男,女;省份(河南省) varchar2是不固定长度的字符串。 推荐使用varchar2 CLOB类型(String): 字符集据:很长很长很长很长的字符串,最大可以存储4个G的数据。 decimal(其实就是number的一个长类型:比number类型存储的数据量要大):decimal(100,3):100为多少位整数,3位小数 表数据的唯一标识: 主键:是为了约束每一行数据的唯一性。 遵循特点: 不为空,且唯一。 建表的时候添加主键: create table 表名 (列名 列的类型 primary key not null); 表建立完成以后,指定列为主键 前提是:没有null的数据&&没有重复的数据。 alter table 表名 add constraint(约束,限制) PK_表名_指定的列名 primary key(指定的主键那一列); 表的建立(表名,列名,列的类型): create table 需要创建的表名 (列名和列的类型,列名和列的类型); 创建一个学生表: create table stus (stu_no number(5), stu_name varchar2(20),........); 建立表的同时添加主键: create table 表名(列名 列的类型 primary key not null, 列名 列的类型, 列名 列的类型...); 表的删除: drop table 需要删除的表名; 只清空表中的数据,并不删除表: truncate table 表名; 表的修改: 修改表名: alter table 修改前的表的名字 rename to 新的表名 修改列名: alter table 表名 reanme column 修改的列名 to 新的列名 添加列: alter table 表名 add(需要添加的列名 列的类型); 删除列名: alter table 表名 drop column 需要删除的列名; 修改列的类型: alter table 表名 modify (需要修改的列名 修改的列的类型);
       数据的修改:

           1.增加数据
            insert into 表名 (列名) values (列名对应的值);(使用pl/sql操作oracle中时,插入varchar2类型数据必须加单引号!
          2.删除数据
            delete from teacher where name='hahaha';
            delete from teacher where id=8;
          3.修改数据
            update teacher set age=29 where id=7;
          4. 通过where关键字进行过滤查询数据
            select * from teacher where id =1;

      视图:
         1.创建
           
    create view 视图名 as (select * from 哪个表)(可以将查询得到的表创建成一个视图)

          2.创建替换更名设置权限只读
            create  or replace  view 视图名 as(select st.id  from stu2 st) with read only  
         3.删除
           
    drop view 视图名
         4.增加数据
           
    insert into 视图名 (列名) values (列名对应的值);  
          5.修改数据

            update 视图名 s set s.AGE=99,s.SALARY=100,s.NAME='张三' where s.ID=7
          6.查询
            select * from 视图名
            
    4.PL/SQL: PL/SQL是一种sql语言 PL/SQL是一个oracle的连接工具 users:包含了所有oracle的用户(在sysdba用户登录模式下,来完成用户名,登录密码,权限配置) tables:包含了当前用户的所有表 简单的查询语句: select * from 表名; 查询出表中的所有数据 oracle中的注释: --
         
    data:

    1.通过sql语句插入一条数据
      --
    insert into 表名 (列名) values (列名对应的值);   


      -- 在使用insert into语句时,如果是字符串类型的数据,就必须要加上单引号''---->中文单引号‘’---->英文单引号''   -- 在使用insert into语句时,如果设定了非空字段,就一定要插入值,否则会报错。   -- 并且insert into teacher(列名) values(列所对应的值);--->要一一对应   insert into teacher (id,name,age,description) values(5,'tianqi',36,'我是田七');   insert into teacher (id, name, age) values(6,'赵八', 34);   --insert into teacher(id,name,description) values(7, '王九', '我是王九');   insert into teacher (name, id, age, description) values('hahaha', 7, 30, '我是一个哈哈哈');

    2.通过sql进行删除数据   -- 在删除的过程中,尽量要使用唯一键进行删除,或者使用主键进行删除   delete from teacher where name='hahaha';   delete from teacher where id=8; 3.通过sql语句修改一条数据   -- modify/update   -- modify在oracle中适用于列的类型修改,也就是对表的修改进行操作   -- update关键字在oracle中适用于对数据的修改   -- 使用update关键字修改数据时,一定要用唯一键或者主键进行修改   -- 在修改数据时,如果修改多个数据,中间用逗号隔开,字符串带上单引号   update teacher set id=7, name='我是哈哈哈', age=35, description='我是测试数据' where id=10;   update teacher set age=29 where id=7;   --where要讲 in, =, <, >, between and, !=(<>), >=, <=, or, is null, is not null, not in, like "_L%", like "%_a%" escape 'a' 4.通过where关键字进行过滤查询    =关键符号   select * from teacher where id =1;    大于和小于   select * from teacher where id > 1;   select * from teacher where id < 9;   大于等于和小于等于   select * from teacher where id >= 5;   select * from teacher where id <= 1;   实现区间(范围)查询   select * from teacher where id >= 2 and id <= 10;   between and 是包含两边的极限数据(闭区间)   select * from teacher where id between 2 and 10;   !=(不等于)   select * from teacher where id != 5;   select * from teacher where id <> 5;   or关键字(或者)   select * from teacher where id =1 or id = 2;   and关键字(并且)   select * from teacher where id = 6 and name = '赵八';   in关键字(主要的作用:批量操作)   select * from teacher where id in(1, 5, 10);  
     is null 过滤出来所有为null的数据   select * from teacher where description is null;   is not null 过滤出来所有不为null的数据   select * from teacher where description is not null;   not in(过滤出不在范围内的所有数据)   select id from teacher where id > 5只查询出了指定的id   select * from teacher where id not in(select id from teacher where id > 5);   select * from teacher where id not in(1,5,7);   select * from teacher where name not in('zhangsan', 'lisi', 'wangwu');   模糊查询   --like一般情况下要和%结合起来用,%其实一个占位符,如果把%写在前面,匹配以赵结尾的所有名字,反之匹配以赵开头的所有名字     如果所需要查询的字段前后都加上%,只要包含该查询字段就全部查找出来     select * from teacher where name like '%a%';   

        以_a%进行模糊查询的时候,会匹配以第二位为'a'的字符串     --_就是代表了任意一个字符     select * from teacher where name like '_a%';     -- 查询以_开头的所有数据     -- 需要把a给去掉     -- 匹配规则:使用escape的时候会,如果_写在需要查询字段的前面,oracle会自动把_转移为任意一个字符     -- 只有把下划线写在需要查询字段的后面,才能使用escape关键字去掉多余字段,只保留下划线。     如果'%_x%',使用escape关键字时,会将要去掉后面的一位转义     select * from teacher where name like '%x_d%' escape 'x';     

        *查询     -- 在sql中,可以使用简单的运算符(+,-,*,/)     -- 查询某张表的所有数据     select * from teacher;     -- 查询某张表的指定数据     select name, age from teacher;     -- 查询某张表的指定数据(计算)     select name, age from teacher where id=1;     -- 十年后张三多大?     select name, age+10 from teacher where id=1;     

        给age字段起别名     -- 给字段起别名:别名是为了更好对字段进行描述,也就是说起一个别名并不能随意起,要适用于这个字段的意思     select name, age+10 as age from teacher where id=1;     -- 通常情况下,这里的as关键字可以省略     select name, age+10 age from teacher where id=1;     -- 也可以给表起一个别名(小名)     select t.name from teacher t;          
        更改了列名,里面加了个空格
        
          如果别名中间出现了空格,就会报错(找不到from关键字)
        
        只需要给别名加一个双引号(必须要是双引号)
        select name, age+10 "zhangsan age" from teacher where id=1;
        
        更改了列名,数据内容
        -- 姓名:zhangsan 年龄:33<---\     -- 姓名:也需要加上引号(一定要使用单引号)     select '插入数据中的字段'|| name '自己起的列名' from teacher;     查询出所有的老师信息,并且以年龄从小到大排列-->order by     -- order by 默认是升序--->asc升序,如果以升序进行排列通常情况下asc省略     select * from teacher order by age asc;     查询出所有的老师信息,并且以年龄从大到小排序---->desc降序     select * from teacher order by age desc;     -- 如果两个老师年纪相同,就以姓名的首字母排序     select * from teacher order by age desc, name;     去重(关键字:distinct)     select distinct age from teacher;     select distinct t.age, t.name from teacher t;     select distinct name from teacher;多表查询     dual,计算1+1等于几?     select 1+1 from dual;-- dual其实也是一张表,这一表是虚拟的,没有列和数据,当查询的时候会进行赋予列和数据,用于方便用户测试使用     -- 查询当前oracle登录的用户     select user from dual;     --sysdate,sysdate-,months_netween(,),add_months(,),next_day(,'星期几'),last_day,to_day 5.时间函数     获取当前的时间:sysdate     select sysdate from dual;     sysdate-时间,查询的是指定日期距离当前日期的天数     select t.hire_date, t.name, sysdate, sysdate-hire_date from teacher t;     add_months:对指定日期月份进行加减计算     select add_months(sysdate,3) from dual;     select add_months(sysdate,-3) from dual;     months_between:指定日期距离当前日期的月份     select t.hire_date, sysdate, months_between(sysdate, hire_date) from teacher t;     next_day:查询出指定日期和指定星期几的日期:只能往后不能往前     select next_day(sysdate, '星期一') from dual;     last_day:查询出当月的最后一天     select last_day(sysdate) from dual;     select t.hire_date, last_day(hire_date) from teacher t;     


        to_date:时间转换函数     -- 默认转换为年月日,不带时分秒     --但是oracle查询当前日期会自带时分秒     -- 使用to_date函数进行转换的时候,查询出的数据仍然遵循oracle的日期规范     -- to_date:只能把年月日时分秒转换为年月日,或者把年月日转换为时分秒,不能转换oracle的日期格式规范     -- oracle中没有任何一个函数可以修改oracle日期格式规范     select to_date('1987/08/21 19:18:02', 'yyyy-mm-dd hh24:MI:ss') from dual;     --lower(), upper(), initcat(), length(), replace(替换的列名,'z','Z')(把小z替换成大Z),substr(需要截取的列名,开始截取的位置,最后截取的位置), concat(,),ceil,float,round,trunc    

        to_char:把日期转化为字符串
          提取年
          select to_char(sysdate,'yyyy')from dual
          提取月
          select to_char(sysdate,'mm')from dual
          提取日

           select to_char(sysdate,'dd')from dual

           提取小时(hh是12制的,hh24是24进制)

           select to_char(sysdate,'hh')from dual 

              提取具体时间
           select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')from dual

         to_number:把字符串转为数字类型 (必须为数字类型的字符串)

           select to_number('123456')from dual 

         count(*)或count(任意数字):计算数据的数量,其实就是把数据全转化为括号内的数字,再进行统计,查询      出来是多出一个count(?)列。

         实际开发中一定要使用count(1)不要使用count(*)!

          

    6.oracle中的函数
    
        -- lower
        -- 把所有教师名字(英文)全部转换为小写
        select lower(name) from teacher;
        -- upper
        -- 把所有教师名字(英文)全部转换为大写
        select upper(name) from teacher;
        --initcap:首字母开头大写
        select initcap('oracle') from dual;
        select initcap(name) from teacher;
    
        --length:获取字符串的长度
        select length(name) from teacher where id =1;
    
        --replace:有三个参数
        -- 第一个参数:列名(字段);第二个参数:需要替换字段中的某一个字符;第三参数:替换的字符
        select * from teacher where id =1;
        select replace(name,'z','Q') from teacher where id =1;
    
        -- substr:和Java的subString差不多
        -- substr有两个参数:第一个参数:列名,第二个参数:从哪一位开始截取(包含最后一位截取的数):name,要截取前两位:subtr('name',2);--->得到的是ame
        select substr(name, 3) from teacher where id =1;
        -- 如果第二个参数为负数的话,就倒着从最后一位开始往前截取
        select substr('zhangsan', -3) from dual;
        --substr:
        select * from teacher;
        --subtr:有三个参数:第一个参数:列名,第二个参数:从哪一位开始截取(闭区间),第三个参数:截取到多少位(会从第二个参数的截取位置往后开始进行截取)
        -- oracle中,截取的位置会从0或者1起始(0和1的位置是一样的)
        select substr('zhangsan',0,5) from teacher where id = 1;
        select substr('zhangsan',1,5) from teacher where id = 1;
    
        -- ceil:向上取整
        select ceil('12345.1') from dual;
        -- floor:向下取整
        select floor('12345.1') from dual;
        -- round:四舍五入
        select round('12345.1') from dual;
        select round('12345.7') from dual;
        select round('12845.6', -3) from dual;
    
        -- trunc:截断:会取整数
        -- 第二个参数:保留的小数位
        -- 如果没有小数,并且第二个参数为正数:就原样显示,不会加上.00
        select trunc('123444.87978787',2) from dual;
        -- 如果没有小数,并且第二个参数为负数:会把整数位从后往前写为0
        select trunc('123456', -3) from dual;
    
        -- concat:拼接字符串--->代替||
        select concat('zhang','san') from dual;
        select concat('姓名是:',name) from teacher;




  • 相关阅读:
    POJ1239
    HDU 2829 四边形不等式优化
    返回数字二进制的最高位位数o(n)
    矩阵快速幂 模板
    HDU4718 The LCIS on the Tree(LCT)
    HDU4010 Query on The Trees(LCT)
    HDU3487 Play With Chains(Splay)
    CF444C DZY Loves Colors
    HDU4836 The Query on the Tree(树状数组&&LCA)
    HDU4831&&4832&&4834
  • 原文地址:https://www.cnblogs.com/kuangzhisen/p/7100843.html
Copyright © 2020-2023  润新知