zoukankan      html  css  js  c++  java
  • Oracle 数据库的基本用法

    创建表
    create table classes(
     classId number (2),
     cname varcher2(40),
     birthday date
    );

    添加一个字段
    alter table student add (classId number(2));

    修改字段长度
    alter table student modify (xm varcher2(30));

    修改字段的类型/或是名字(不能有数据)
    alter table student modify (xm  char(30));

    删除一个字段
    alter table student drop column sal;

    修改表的名字
    rename student to stu;

    删除表
    drop table student;

    插入所有字段数据
    insert into student values ('001','saline','女','01-05-05',10);

    修改日期输入格式
    alter session set nls_date_format = 'yyyy-mm-dd';//临时生效,重启后不起作用

    插入部分字段
    insert into student (xh,xm,sex) values ('001',;'女',);

    插入空值
    insert into student (xh, xm,sex,birthday) values ('021','BL','男',null);

    一条插入语句可以插入多行数据
    insert into kkk (myid,myname,mydept) select empno.ename,deptno from emp where deptno=10;

    查询空值/(非空)的数据
    select * from student where birthday is null (/not null);

    修改(更新)数据
    update student set sal=sal/2 where sex ='男';

    更改多项数据
    update emp set(job,sal,comm)=(select job,sal,comm from emp where ename ='SMITH') where ename ='SCOTT';

    删除数据

    1.保存返回点
    savepoint aa;
    2.删除数据
       delete from student;//删除表的数据

       drop table student ;  //删除表的结构和数据

       delete form  student where xh = '001';//删除一条数据

       truncate table student ;//删除表的所有记录,表的结构还在,不写日志,无法再找回的记录,速度快。


    查看表结构
     desc student;

    查看指定列
    select sex,xh,xm from student

    如何消除重复
    select distinct deptno,job from student

    打开显示操作时间的开关
    set timing on;

    为表格加大的数据行(用于测试反应时间)
    insert into users (userID,username,userprss)select * from users;

    统计表中有多少条记录
    select count(*) from users;

    屏蔽列内相同的记录
    select distinct deptno,job from emp;

    查询指定列的某个数据相关的数据
    select deptno,job,sal from emp where ename ='smith';

    使用算数表达式
    select sal* 12 from emp;

    使用类的别名
    select ename “姓名”,sal*12 as “年收入”from emp;

    处理(null)空值
    select sal*12+nvl(comm,0)*13 “年工资”,ename,comm from emp;

    连接字符串 (||)
    select ename || ‘is a’|| job from emp;

    where 子句的使用
    select ename ,sal from emp where sal >3000;//number 的范围确定
    select ename,hiredate from emp where hiredate >'1-1-1982';//日期的范围确定
    select ename,sal from emp where sal >=2000 and sal <= 2500; //组合条件

    Like操作符:’%’、’_’

    SQL>select ename,sal from emp where ename like ‘S%’;              //第一个字符【名字第一个字符为S的员工的信息(工资)】

    SQL>select ename,sal from emp where ename like ‘__O%’;          //其它字符【名字第三个字符为O的员工的信息(工资)】

     

    批量查询

    SQL>select * from emp where in(123,456,789);       //查询一个条件的多个情况的批量处理

     

    查询某个数据行的某列为空的数据的相关数据

    SQL >select * from emp where mgr is null;

     

    条件组合查询(与、或)

    SQL>select * from emp where (sal>500 or job=’MANAGER’) and ename like ‘J%’;

     

    Order by 排序

    【1】SQL>select * from emp order by sal (asc);       //从低到高[默认]

    【2】SQL>select * from emp order by sal desc;      //从高到低

    【3】SQL>select * from emp order by deptno (asc),sal  desc;     //组合排序

    【4】SQL>select ename,sal*12 “年薪” from emp order by “年薪” (asc);

    SQL> select ename,(sal+nvl(comm,0))*12 as "年薪" from emp order by "年薪";

     

    资料分组(max、min、avg、sum、count

    SQL>select max(sal),min(sal) from emp;

    SQL>select ename,sal from emp where sal=(select max(sal)  from emp); //子查询,组合查询

    SQL> select * from emp where sal>(select avg(sal) from emp); //子查询,组合查询

    SQL> update emp set sal=sal*1.1 where sal<(select avg(sal) from emp) and hiredate<'1-1月-1982';      //将工资小于平均工资并且入职年限早于1982-1-1的人工资增加10%

     

    Group by 和 having 子句

    //group by用于对查询出的数据进行分组统计

    //having 用于限制分组显示结果

    SQL>select avg(sal),max(sal),deptno from emp group by deptno;        //显示每个部门的平均工资和最低工资

    SQL>select avg(sal),max(sal),deptno from emp group by deptno;        //显示每个部门的平均工资和最低工资

    SQL> select avg (sal),max(sal),deptno from emp group by deptno having avg(sal)>2000;

    SQL> select avg (sal),max(sal),deptno from emp group by deptno having avg(sal)>2000 order by  avg(sal);

     

    多表查询

    笛卡尔集:规定多表查询的条件是至少不能少于:表的个数-1

    SQL> select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno=a2.deptno;

    SQL> select a1.dname,a2.ename,a2.sal from dept a1,emp a2 where a1.deptno=a2.deptno and a1.deptno=10;     //显示部门编号为10的部门名、员工名和工资

    SQL> select a1.ename,a1.sal,a2.grade from emp a1,salgrade a2 where a1.sal between a2.losal and a2.hisal;

    SQL> select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno=a2.deptno order by a1.deptno;                   //多表排序

    SQL> select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno;       //     自连接(多表查询的特殊情况)

    SQL> select worker.ename,boss.ename from emp worker,emp boss where worker.mgr=boss.empno and worker.ename='FORD';

     

    子查询

    SQL> select * from emp where deptno=(select deptno from emp where ename='SMITH');

    SQL> select distinct job from emp where deptno=10;

     

    SQL> select * from emp where job in (select distinct job from emp where deptno=10);   

    // 如何查询和部门10的工作相同的雇员的名字、岗位、工资、部门号。

    SQL> select ename ,sal,deptno from emp where sal>all (select sal from emp where deptno=30);//如何查询工资比部门30的所有员工的工资高的员工的姓名、工资和部门号

    SQL> select ename ,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);

    SQL> select * from emp where (deptno,job)=(select deptno,job from emp where ename='SMITH');

     

    内嵌视图

    //当在from子句中使用子查询的时候,必须给子查询指定别名

    SQL>select a2.ename,a2.sal,a2.deptno,a1.mysal from emp a2,(select deptno,avg(sal) (as ) mysal from emp group by deptno) a1 where a2.deptno=a1.deptno and a2.sal>a1.mysal;

     

     

    分页

    SQL >select a1.*,rownum rn from (select * from emp) a1;//orcle为表分配的行号

    SQL> select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<=10) where rn>=6;

    //查询内容的变化

    1. 所有的改动(指定查询列)只需更改最里面的子查询
    2. (排序)只需更改最里面的子查询

     

     

    子查询(用查询结果创建新表)

    SQL> create table mytable (id,name,sal,job,deptno) as select empno,ename,sal,job,deptno from emp;

     

    合并查询

    union(求并集), union  all  , intersect(取交集),   minus (差集)

    SQL> select ename,sal,job from emp where sal>2500;

    SQL> select ename,sal,job from emp where job='MANAGER';

     

    SQL> select ename,sal,job from emp where sal>2500 union select ename,sal,job from emp where job='MANAGER';    // union(求并集)

     

    Java连接数据库

     

    事务

    SQL>commit;        //事务            (第一次创建,第二次提交)当退出数据库时,系统自动提交事务

    SQL>savepoint a1;              //创建保存点                     (保存点的个数没有限制)

    SQL>rollback to aa;    //使用保存点回滚到aa

    SQL>rollback;           //回滚到事务创建开始

    只读事务

    SQL>set transaction read only

     

    Java中的事务

    Ct.setAutoCommit(false);    //设置事务自动提交为否

    Ct.commit();          //提交事务

     

     

    字符函数

    lower(char)将字符串转换为小写的格式

    upper(char)将字符串装换为大写的格式

    length(char)返回字符串的长度

    substr(char,m,n)取字符串的子串

     

    SQL>select lower(ename) from emp;

    SQL>select ename from emp where length(ename)=5;

  • 相关阅读:
    iOS拓展---[转载]视频相关,一定要看呀
    iOS拓展---碰到奇葩需求
    iOS拓展---【转载】成熟的夜间模式解决方案
    2018年度总结
    linux常用命令
    自我认识
    SpringBoot入门最详细教程
    SVN分支的合并和同步
    java Data、String、Long三种日期类型之间的相互转换
    徐胜治:我要留下真正的东西
  • 原文地址:https://www.cnblogs.com/cuixiaomeng/p/11017783.html
Copyright © 2011-2022 走看看