• sql plus及SQL语句


    1.sql plus

    1,SQL plus的定义
    sql plus是用来和oracle进行交互的工具。可电脑端使用,也可以在服务器上使用
    
    2,启动SQLplus
       在cmd界面直接输入SQLplus就可进入
    3,常用命令
             show  显示SQL plus中的信息
          connect (缩写conn) 先无条件断开数据库用户的链接,再次输入可再次建立链接
       disconnect (缩写desc) 断开当前连接
          注意:desc 为描述命令,只能在命令窗口执行
                语法:desc 表名   查询表的表头名字及类
              
               set    设置SQLplus中的相关信息
              help    获取SQLplus命令帮助
       clear scree    清屏
        exit或quit    退出
    
    4,创建用户
           第一部分:创建一个普通用户
       
                     语法:create user 用户名 identified  by 密码;       
    
           第二部分:授权此用户   
                
                     授权角色在oracle中有3个
                             DBA  系统管理权限  
                         connect  创建会话链接权限
                        resource  操作数据库对象的权限
          
                        语法:grant 权限,权限..to 用户名;
                  
    
                      修改密码:alert user 用户名 identified 新密码;
                      删除用户:drop user 用户名;
                      账户解锁:alert user 用户名 account unlock;
    
                      
    
             最后导入SQL脚本:第一,把脚本内容复制黏贴到SQLplus;
                              第二,@文件路径  文件名.后缀名

    2.sql查询语句

     1,SQL条件查询
          where字句用来过滤查询的数据,它对字面量大小写是敏感
    
         语法:
            select 列名1,列名2...
            from 表名
            where 筛选的条件;
       
      2.  --select后面是*,代表查列表的所有的内容
          select * from 表名;

    3.SQL运算符

    a.比较运算符
    >, <, =, <=, >=, !=(^=,<>) --不等于
    
       --查询在41部门的员工名字,工资  
          select 
           first_name,salary   --(此处布局为select语句的书写格式)
          from 
             s_emp           
          where
         dept_id=41;
    
    
        --找出工资大于1200的员工的全名、工资、职位(把两个名字连起来用||' '|| 在引号中间可放符号        汉字等根据客户需要)
          select 
              first_name||' '||last_name 全名,salary 工资,title 职位 
                                --汉字代表前面的别名(salary显示出来的列名是工资)
          from s_emp
          where salary >1200;
    
         b.逻辑运算符 
            and  ,or   --and优先级高于or
         
          --查出41部门中工资高于1200的员工名字,工资;
             select first_name, salary ,dept_id
                from s_emp
             where 
               dept_id = 41 and salary > 1200;   
          --查出41,50,42部门的员工名字,薪水;
             select first_name,salary from s_emp where dept_id=41 or dept_id=42  or dept_id=50;
         
     
         c.其它运算符 
    1.in()    相当于or 。 取多个数值,多个值之间使用逗号隔开
              not in() 相当于and。 显示出来的意思为不包含括号里面的内容
    
                例:deot_id in(41,42,50)相当于dept_id=41 or dept_id=42  or dept_id=50;
                 
                   --查出在41,42,50部门的员工名字,薪水;
                      select first_name,salary from s_emp where dept_id in(41,42,50); 
    
                例:dept_id between 2 and 6 在指定的范围之内,是全闭空间;相当于dept_id>=2 and                      dept_id<=6
                    dept_id>2 and dept_id<6 非全闭空间
                  
                   --找出工资在1200到1500之间的员工名字;(全闭和不全闭)
                    全闭:
                       select first_name,salary 
                       from s_emp 
                       where salary between 1200 and 1500;
                    非全闭:
                        select first_name,salary 
                        from s_emp 
                        where salary >1200 and salary <1500;
    
    
            
            2.is null
              is not null
              注意:查询数据时条件是否为null,我们使用关键字is null 或者is not null ,千万不能使                用=(即等号)
                 
                --找出工资大于1500并且没有提成的员工;
                  select * from s_emp 
                  where salary > 1500 and commission_pct is null;
    
            3.模糊查询:like ''
                       not like '' 
                       引号里面用通配符: _ 英语状态下的下划线,代表任意单个字符
                                          % 代表任意多个字符 
                      --查询名字是以M打头的员工;
                        select * from s_emp where first_name like 'M%';
                      
                      --查出姓名中第三个字母是e的员工;
                        select * from s_emp where first_name like '__e%';
                     
                      --查出姓名不带a的员工;
                        select * from s_emp where first_name not like'%a%'; 

    4.order   by排序

    排序字句:
      select 列名1,列名2...
      from 表名
      where 查询条件
      order by 列名 asc(升序,默认可以不写)|desc(降序)
    
       --找出查询职位是Stock Clerk的员工全名、工资,并按照工资的降序排序
         select first_name||' '||last_name 全名,salary 工资 
         from s_emp
         where title = 'Stock Clerk'
         order by salary desc;
    
       --查询职位中带vp(大写)的员工名字,工资,并按照工资降序排序;
          select first_name,salary,title  from s_emp where title like '%VP%' 
          order by salary desc;
    
       --查询出年薪(包含提成)低于25000的员工名称,职位,年薪,并按照年薪升序排序;
          select
             first_name 名字,title 职务,salary 薪资,salary*12*(1+nvl(commission_pct/100,0)) 年薪
          from
             s_emp
          where
             salary*12*(1+nvl(commission_pct/100,0))<25000 
          order by
             salary*12 asc;
  • 相关阅读:
    MySQL 行锁 表锁机制
    2017年总结-我的学习之路
    SpringData 基于SpringBoot快速入门
    SolrJ 复杂查询 高亮显示
    Solr7 安装部署 管理界面介绍
    Redis 高可用集群
    10分钟搭建服务器集群——Windows7系统中nginx与IIS服务器搭建集群实现负载均衡
    1分钟解决VS每次运行都显示“正在还原nuget程序包”问题
    C# Lambda表达式和linq表达式 之 匿名对象查询接收
    C#枚举的简单使用
  • 原文地址:https://www.cnblogs.com/lxy151/p/7806173.html
Copyright © 2020-2023  润新知