1. 1oracle命名规范:和Java的命名规范很像
  2. 1.严格区分大小写
  3. 2.所有的sql语句都要以';'结尾
  4. 3.所有的sql 都要使用空格区分:sqlplus空格/空格as空格sysdba回车
  5. 4.无论是表名还是列名都必须以字母开头 java中的class必须要以大写字母开头,遵循驼峰命名方式
  6. 5.不能使用ORACLE的保留字
  7. 6.不能与用户定义的其它对象重名
  8. 7.数字,$#_,字母(只能包含A-Z,a-z,0-9,_,$,#)字母加上_

  9. emp表中有一列叫emp_no(员工的编号)
  1. 2、连接oracle有两种方式:
  2. 清理命令行:cls
  3. oracle的清理命令行:host cls
  4. 1.PL/SQL连接
       2.原生命令行连接(CMD)
  5. sqlplus回车
  6. 请输入用户名:scoot
  7. 请输入密码(输入的密码在命令行上是不可见):回车
  8. user scott locked!(用户被锁了,无法登陆!)
  9.  
  10.     加锁命令:
  11. 首先要登陆管理员用户
  12. sqlplus / as sysdba:使用超级(系统)管理员登录
  13. alter user oracle的用户名 account locked
  14.     sysdbs:system database administator(超级管理员)
  15. 解锁命令:
  16. 首先要登陆管理员用户
  17. sqlplus / as sysdba
  18. alter user oracle的用户名 account unlock
  19. 查看当前用户登录sql
  20. show user;
  21. 修改用户密码:(修改的是密码!)
  22. 首先要登陆管理员用户
  23. oracle中,以后只要看到account这个单词,就一定是用来修改用户的某些信息
  24. 但是只需要修改密码的时候就不需要account关键字
  25. alter user oracle的用户名 identified by 123;
  26. 解锁并修改用户密码:
  27. oracle有自己的错误代码ORA-00922:选项缺失或无效
  28. alter user scott account unlock identified by 新密码;
  29. 查看当前用户状态:
  30. select username,account_status from dba_status where username='scott';
  31. 在登录oracle完毕后,如何切换用户:
  32. conn 用户名;(适用于黑框)

  33. 3.oracletable(表):
  34. 数据库是需要存储数据,数据库中的表把存储的数据进行按规定存放。
  35. 雇员(emp):存放员工的信息
  36. 部门dept:存放部门的信息
  37. table:表名,列,定义列的数据类型,行

  38.   
      列的类型:
  39. 最常用的三个类型:
  40. 1.varchar2 2.number 3.date/timestap
  41. charvarchar2都是字符串类型:
  42. char是固定长度的字符串。char(10),这个里面就必须要有10char类型字符
  43. 比如:男,女;省份(河南省)
  44. varchar2是不固定长度的字符串。
  45. 推荐使用varchar2
  46. CLOB类型(String):
  47. 字符集据:很长很长很长很长的字符串,最大可以存储4G的数据。
  48. decimal(其实就是number的一个长类型:比number类型存储的数据量要大):decimal(100,3):100为多少位整数,3位小数
  49.  
  50. 表数据的唯一标识:
  51. 主键:是为了约束每一行数据的唯一性。
  52. 遵循特点:
  53. 不为空,且唯一。
  54. 建表的时候添加主键:
  55. create table 表名 (列名 列的类型 primary key not null);
  56. 表建立完成以后,指定列为主键
  57. 前提是:没有null的数据&&没有重复的数据。
  58. alter table 表名 add constraint(约束,限制) PK_表名_指定的列名 primary key(指定的主键那一列);
  59.  
  60. 表的建立(表名,列名,列的类型):
  61. create table 需要创建的表名 (列名和列的类型,列名和列的类型);
  62. 创建一个学生表:
  63. create table stus (stu_no number(5), stu_name varchar2(20),........);
  64. 建立表的同时添加主键:
  65. create table 表名(列名 列的类型 primary key not null, 列名 列的类型, 列名 列的类型...);
  66.  
  67. 表的删除:
  68. drop table 需要删除的表名;
  69. 只清空表中的数据,并不删除表:
  70. truncate table 表名;
  71.  
  72. 表的修改:
  73. 修改表名:
  74. alter table 修改前的表的名字 rename to 新的表名
  75. 修改列名:
  76. alter table 表名 reanme column 修改的列名 to 新的列名
  77. 添加列:
  78. alter table 表名 add(需要添加的列名 列的类型);
  79. 删除列名:
  80. alter table 表名 drop column 需要删除的列名;
  81. 修改列的类型:
  82. alter table 表名 modify (需要修改的列名 修改的列的类型);
  83.    数据的修改:

       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.   视图:
         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 视图名
            
  2.  
  3. 4.PL/SQL:
  4. PL/SQL是一种sql语言
  5. PL/SQL是一个oracle的连接工具
  6. users:包含了所有oracle的用户(在sysdba用户登录模式下,来完成用户名,登录密码,权限配置)
  7. tables:包含了当前用户的所有表
  8.  
  9. 简单的查询语句:
  10. select * from 表名;
  11. 查询出表中的所有数据
  12. oracle中的注释:
  13. --
         
    data:
  1. 1.通过sql语句插入一条数据
  2.   -- insert into 表名 (列名) values (列名对应的值);
  3.   
  4.  
  5.   -- 在使用insert into语句时,如果是字符串类型的数据,就必须要加上单引号''---->中文单引号‘’---->英文单引号''
  6.   -- 在使用insert into语句时,如果设定了非空字段,就一定要插入值,否则会报错。
  7.   -- 并且insert into teacher(列名) values(列所对应的值);--->要一一对应
  8.   insert into teacher (id,name,age,description) values(5,'tianqi',36,'我是田七');
  9.   insert into teacher (id, name, age) values(6,'赵八', 34);
  10.   --insert into teacher(id,name,description) values(7, '王九', '我是王九');
  11.   insert into teacher (name, id, age, description) values('hahaha', 7, 30, '我是一个哈哈哈');
  12.  
  13. 2.通过sql进行删除数据
  14.   -- 在删除的过程中,尽量要使用唯一键进行删除,或者使用主键进行删除
  15.   delete from teacher where name='hahaha';
  16.   delete from teacher where id=8;
  17.  
  18. 3.通过sql语句修改一条数据
  19.   -- modify/update
  20.   -- modifyoracle中适用于列的类型修改,也就是对表的修改进行操作
  21.   -- update关键字在oracle中适用于对数据的修改
  22.   -- 使用update关键字修改数据时,一定要用唯一键或者主键进行修改
  23.   -- 在修改数据时,如果修改多个数据,中间用逗号隔开,字符串带上单引号
  24.   update teacher set id=7, name='我是哈哈哈', age=35, description='我是测试数据' where id=10;
  25.   update teacher set age=29 where id=7;
  26.   --where要讲 in, =, <, >, between and, !=(<>), >=, <=, or, is null, is not null, not in, like "_L%", like "%_a%" escape 'a'
  27.  
  28. 4.通过where关键字进行过滤查询
  29.    =关键符号
  30.   select * from teacher where id =1;
  31.    大于和小于
  32.   select * from teacher where id > 1;
  33.   select * from teacher where id < 9;
  34.   大于等于和小于等于
  35.   select * from teacher where id >= 5;
  36.   select * from teacher where id <= 1;
  37.   实现区间(范围)查询
  38.   select * from teacher where id >= 2 and id <= 10;
  39.   between and 是包含两边的极限数据(闭区间)
  40.   select * from teacher where id between 2 and 10;
  41.  
  42.   !=(不等于)
  43.   select * from teacher where id != 5;
  44.   select * from teacher where id <> 5;
  45.   or关键字(或者)
  46.   select * from teacher where id =1 or id = 2;
  47.   and关键字(并且)
  48.   select * from teacher where id = 6 and name = '赵八';
  49.   in关键字(主要的作用:批量操作)
  50.   select * from teacher where id in(1, 5, 10);
  51.  
     is null 过滤出来所有为null的数据
  52.   select * from teacher where description is null;
  53.   is not null 过滤出来所有不为null的数据
  54.   select * from teacher where description is not null;
  55.  
  56.   not in(过滤出不在范围内的所有数据)
  57.   select id from teacher where id > 5只查询出了指定的id
  58.   select * from teacher where id not in(select id from teacher where id > 5);
  59.   select * from teacher where id not in(1,5,7);
  60.   select * from teacher where name not in('zhangsan', 'lisi', 'wangwu');
  61.  
  62.   模糊查询
  63.   --like一般情况下要和%结合起来用,%其实一个占位符,如果把%写在前面,匹配以赵结尾的所有名字,反之匹配以赵开头的所有名字
  64.     如果所需要查询的字段前后都加上%,只要包含该查询字段就全部查找出来
  65.     select * from teacher where name like '%a%';
  66.   
  67.  
  68.     _a%进行模糊查询的时候,会匹配以第二位为'a'的字符串
  69.     --_就是代表了任意一个字符
  70.     select * from teacher where name like '_a%';
  71.     -- 查询以_开头的所有数据
  72.     -- 需要把a给去掉
  73.     -- 匹配规则:使用escape的时候会,如果_写在需要查询字段的前面,oracle会自动把_转移为任意一个字符
  74.     -- 只有把下划线写在需要查询字段的后面,才能使用escape关键字去掉多余字段,只保留下划线。
  75.     如果'%_x%',使用escape关键字时,会将要去掉后面的一位转义
  76.     select * from teacher where name like '%x_d%' escape 'x';
  77.     
  78.  
  79.     *查询
  80.     -- sql中,可以使用简单的运算符(+,-,*,/)
  81.     -- 查询某张表的所有数据
  82.     select * from teacher;
  83.     -- 查询某张表的指定数据
  84.     select name, age from teacher;
  85.     -- 查询某张表的指定数据(计算)
  86.     select name, age from teacher where id=1;
  87.     -- 十年后张三多大?
  88.     select name, age+10 from teacher where id=1;
  89.     
  90.  
  91.     age字段起别名
  92.     -- 给字段起别名:别名是为了更好对字段进行描述,也就是说起一个别名并不能随意起,要适用于这个字段的意思
  93.     select name, age+10 as age from teacher where id=1;
  94.     -- 通常情况下,这里的as关键字可以省略
  95.     select name, age+10 age from teacher where id=1;
  96.     -- 也可以给表起一个别名(小名)
  97.     select t.name from teacher t;
  98.     
  99.     
        更改了列名,里面加了个空格
        
  1.       如果别名中间出现了空格,就会报错(找不到from关键字)
        
        只需要给别名加一个双引号(必须要是双引号)
  1.     select name, age+10 "zhangsan age" from teacher where id=1;
  2.     
        更改了列名,数据内容

  3.     -- 姓名:zhangsan 年龄:33<---\\
  4.     -- 姓名:也需要加上引号(一定要使用单引号)
  5.     select '插入数据中的字段'|| name '自己起的列名' from teacher;
  6.  
  7.     查询出所有的老师信息,并且以年龄从小到大排列-->order by
  8.     -- order by 默认是升序--->asc升序,如果以升序进行排列通常情况下asc省略
  9.     select * from teacher order by age asc;
  10.     查询出所有的老师信息,并且以年龄从大到小排序---->desc降序
  11.     select * from teacher order by age desc;
  12.     -- 如果两个老师年纪相同,就以姓名的首字母排序
  13.     select * from teacher order by age desc, name;
  14.  
  15.     去重(关键字:distinct)
  16.     select distinct age from teacher;
  17.     select distinct t.age, t.name from teacher t;
  18.     select distinct name from teacher;多表查询
  19.  
  20.     dual计算1+1等于几?
  21.     select 1+1 from dual;-- dual其实也是一张表,这一表是虚拟的,没有列和数据,当查询的时候会进行赋予列和数据,用于方便用户测试使用
  22.     -- 查询当前oracle登录的用户
  23.     select user from dual;
  24.     --sysdatesysdate-,months_netween(,),add_months(,),next_day(,'星期几'),last_dayto_day
  25. 5.时间函数
  26.     获取当前的时间:sysdate
  27.     select sysdate from dual;
  28.     sysdate-时间,查询的是指定日期距离当前日期的天数
  29.     select t.hire_date, t.name, sysdate, sysdate-hire_date from teacher t;
  30.  
  31.     add_months:对指定日期月份进行加减计算
  32.     select add_months(sysdate,3) from dual;
  33.     select add_months(sysdate,-3) from dual;
  34.     months_between:指定日期距离当前日期的月份
  35.     select t.hire_date, sysdate, months_between(sysdate, hire_date) from teacher t;
  36.     next_day:查询出指定日期和指定星期几的日期:只能往后不能往前
  37.     select next_day(sysdate, '星期一') from dual;
  38.     last_day:查询出当月的最后一天
  39.     select last_day(sysdate) from dual;
  40.  
  41.     select t.hire_date, last_day(hire_date) from teacher t;
  42.     
  43.  
  44.     to_date:时间转换函数
  45.     -- 默认转换为年月日,不带时分秒
  46.     --但是oracle查询当前日期会自带时分秒
  47.     -- 使用to_date函数进行转换的时候,查询出的数据仍然遵循oracle的日期规范
  48.     -- to_date:只能把年月日时分秒转换为年月日,或者把年月日转换为时分秒,不能转换oracle的日期格式规范
  49.     -- oracle中没有任何一个函数可以修改oracle日期格式规范
  50.     select to_date('1987/08/21 19:18:02', 'yyyy-mm-dd hh24:MI:ss') from dual;
  51.     --lower(), upper(), initcat(), length(), replace(替换的列名,'z','Z')(把小z替换成大Z),substr(需要截取的列名,开始截取的位置,最后截取的位置), concat(,),ceilfloatroundtrunc
  52.    
  53.  
  54.     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(*)!

      

  1. 6.oracle中的函数
  2.  
  3.     -- lower
  4.     -- 把所有教师名字(英文)全部转换为小写
  5.     select lower(name) from teacher;
  6.     -- upper
  7.     -- 把所有教师名字(英文)全部转换为大写
  8.     select upper(name) from teacher;
  9.     --initcap:首字母开头大写
  10.     select initcap('oracle') from dual;
  11.     select initcap(name) from teacher;
  12.  
  13.     --length:获取字符串的长度
  14.     select length(name) from teacher where id =1;
  15.  
  16.     --replace:有三个参数
  17.     -- 第一个参数:列名(字段);第二个参数:需要替换字段中的某一个字符;第三参数:替换的字符
  18.     select * from teacher where id =1;
  19.     select replace(name,'z','Q') from teacher where id =1;
  20.  
  21.     -- substr:和JavasubString差不多
  22.     -- substr有两个参数:第一个参数:列名,第二个参数:从哪一位开始截取(包含最后一位截取的数):name,要截取前两位:subtr('name',2);--->得到的是ame
  23.     select substr(name, 3) from teacher where id =1;
  24.     -- 如果第二个参数为负数的话,就倒着从最后一位开始往前截取
  25.     select substr('zhangsan', -3) from dual;
  26.     --substr:
  27.     select * from teacher;
  28.     --subtr:有三个参数:第一个参数:列名,第二个参数:从哪一位开始截取(闭区间),第三个参数:截取到多少位(会从第二个参数的截取位置往后开始进行截取)
  29.     -- oracle中,截取的位置会从0或者1起始(01的位置是一样的)
  30.     select substr('zhangsan',0,5) from teacher where id = 1;
  31.     select substr('zhangsan',1,5) from teacher where id = 1;
  32.  
  33.     -- ceil:向上取整
  34.     select ceil('12345.1') from dual;
  35.     -- floor:向下取整
  36.     select floor('12345.1') from dual;
  37.     -- round:四舍五入
  38.     select round('12345.1') from dual;
  39.     select round('12345.7') from dual;
  40.     select round('12845.6', -3) from dual;
  41.  
  42.     -- trunc:截断:会取整数
  43.     -- 第二个参数:保留的小数位
  44.     -- 如果没有小数,并且第二个参数为正数:就原样显示,不会加上.00
  45.     select trunc('123444.87978787',2) from dual;
  46.     -- 如果没有小数,并且第二个参数为负数:会把整数位从后往前写为0
  47.     select trunc('123456', -3) from dual;
  48.  
  49.     -- concat:拼接字符串--->代替||
  50.     select concat('zhang','san') from dual;
  51.     select concat('姓名是:',name) from teacher;
  1.  

java:Oracle(table的增删改查,data的增删改查)的更多相关文章

  1. Oracle table names are case sensitive (normally all uppercase)

    oracle_fdw error desc: postgres=# select * from test; ERROR:  Oracle table "sangli"." ...

  2. java oracle thin 和 oci 连接方式实现多数据库的故障切换

    java oracle thin 和 oci 连接方式实现多数据库的故障切换 一.thin方式 该种方式简便易用非经常见. 当中URL为 jdbc:oracle:thin:@(DESCRIPTION= ...

  3. sqoop导出hive数据到mysql错误: Caused by: java.lang.RuntimeException: Can't parse input data

    Sqoop Export数据到本地数据库时出现错误,命令如下: sqoop export \ --connect 'jdbc:mysql://202.193.60.117/dataweb?useUni ...

  4. oracle数据库创建表且主键自增

    唠叨几句:几年前的知识忘却了,整理一下笔记,提供一下方便 1.创建数据库表 设置主键 create table users( userid number(10) primary key, /*主键,自 ...

  5. JDBC:JAVA & Oracle

    JDBC:JAVA & Oracle 本文中未加标注的资源均来自于PolyU数据库课程的实验材料.仅作为学习使用,如有侵权,请联系删除 JDBC是什么 我之前写过一篇关于数据库和JAVA的博文 ...

  6. Oracle数据库查看表空间是否为自增的

    表空间是有数据文件组成的,所以看表空间是否自增即看数据文件,如下查自增的表空间: select tablespace_name,file_name,autoextensible from dba_da ...

  7. Oracle Table Function

    Oracle Table Function在Oracle9i时引入.完美的兼容了view和存储过程的长处: 应用举例: 1.Table()函数: set feedback off create or ...

  8. java的可查的异常和不可查的异常讲解

    java的可查的异常和不可查的异常讲解: Java的异常(包括Exception和Error)分为:可查的异常(checked exceptions)和不可查的异常(unchecked excepti ...

  9. java oracle的2种分页方法

    java oracle的2种分页方法 一物理分页: <!-- 分页查询所有的博客信息 --> <select id="findBlogs" resultType= ...

  10. 重置SQLSERVER表的自增列,让自增列重新计数【转】

    很多时候我们需要重置某个表的自增列,让自增列重新从1开始记数.最蠢的方法当然是把该表删掉再重新建表了.其实,还有其它的方法可以重置自增列的值: 方法一:使用TRUNCATE TABLE语句: TRUN ...

随机推荐

  1. LeetCode_Bit Manipulation

    231. Power of Two Given an integer, write a function to determine if it is a power of two. class Sol ...

  2. vs code 保存显示无法写入文件的解决方法

    右键文件夹点击属性 选择安全 把当前用户权限都勾选上就可以了

  3. idea生成实体类

    1.点击View->Tool Windows->Database 2.点击Datebase框的加号,DateSource,选择对应的数据源,配置对应信息,点击Test Connection ...

  4. JS大文件上传断点续传解决方案

    1 背景 用户本地有一份txt或者csv文件,无论是从业务数据库导出.还是其他途径获取,当需要使用蚂蚁的大数据分析工具进行数据加工.挖掘和共创应用的时候,首先要将本地文件上传至ODPS,普通的小文件通 ...

  5. Codeforces 932 E Team Work ( 第二类斯特林数、下降阶乘幂、组合数学 )

    题目链接 题意 : 其实就是要求 分析 : 先暴力将次方通过第二类斯特林数转化成下降幂 ( 套路?) 然后再一步步化简.使得最外层和 N 有关的 ∑ 划掉 这里有个技巧就是 将组合数的表达式放到一边. ...

  6. Java程序,JVM之间的关系

    java程序是跑在JVM上的,严格来讲,是跑在JVM实例上的.一个JVM实例其实就是JVM跑起来的进程,二者合起来称之为一个JAVA进程.各个JVM实例之间是相互隔离的. 每个java程序都运行于某个 ...

  7. maven web项目中运行stucts2报404的解决方案

    从这篇文章看见的https://www.cnblogs.com/xxqxxq/p/5938821.html 1.将stucts.xml中所有<action>全部注释掉,重新运行 如果运行成 ...

  8. crossdomain.xml解决跨域问题

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  9. maven 配置国内镜像仓库加速获取jar包的配置方法

    在 maven 的 conf/settings.xml 中配置 <mirrors> <!-- mirror | Specifies a repository mirror site ...

  10. 【git】本地git bash连接远程库github

    重要参考: https://www.liaoxuefeng.com/wiki/896043488029600 https://segmentfault.com/a/1190000003728094 正 ...