• sqlplus登录信息,列出所有表,列在表结构,sqlplus行和列显示设置,别名,Null值问题,细木工,DISTINCT


    

    1 sqlplus登录方式:

             普通用户登录:

    登录egC:>sqlplusscott/11  (格式:sqlplus username/password)

    退出egquit退出

     

             超级用户登录

                       C:>sqlplus/nolog

                       SQL*Plus:Release 10.1.0.2.0 - Pr

                       Copyright(c) 1982, 2004, Oracle.

                       SQL>connect /as sysdba   //操作用户登录方式

                       已连接。

                                        

    2 查看用户下有什么表,命令:

             SQL>select * from tab;  

    taboracle中的一个keyword。

     

    3  关于数据字典:

    tab是数据字典:oracle超级管理员分配给普通用的资源....,tabkeyword....

     

    4  SQL>select * from dept; --查询表的全部记录

            

       DEPTNO(部门编号) DNAME 部门名称        LOC 位置

                       ------------------------ -------------

                               10 ACCOUNTING     NEW YORK

                               20 RESEARCH       DALLAS

                               30 SALES          CHICAGO

                               40 OPERATIONS     BOSTON

     

    5 SQL> desc dept;  --查询表结构

             名称                                      是否为空? 类型

             ------------------------------------------------- ----------------------------

             DEPTNO                                    NOT NULLNUMBER(2)

             DNAME                                            VARCHAR2(14)

             LOC                                               VARCHAR2(13)

                               

    6  基本命令

             A  set linesize 150  设置行宽

             B  set pagesize 140 设置页大小

    C  设置列宽

                SQL> col empno for 99999999999999999999

             D  给字符方式设置列宽:

                SQL> col ename for a20

            

                                                  

    E  window下清屏

               SQL>host cls

    F  Linux下清贫

      SQL>host clear

                               

    7  select语句(能够检索全部列、部分列、表达式、别名)

                        select * from emp;

                  ---显示员工姓名 员工编号 工作  月薪 年薪

                  select ename, empno, job, sal , sal*12from emp;

    8   别名

                       --1别名: 能够省掉""as

                       --2有空格时, 不能省掉""

     

    SQL> select ename as "员工姓名" , empno"编号", job 工作, sal "" , sal*12  年薪 from emp

     

                                         员工姓名         编号 工作                  年薪

                                         -------------------- --------- ---------- ----------

                                         SMITH            7369 CLERK            800       9600

                                         ALLEN            7499 SALESMAN        1600      19200

                                         WARD             7521 SALESMAN        1250      15000

                                         JONES            7566 MANAGER         2975      35700

                                         MARTIN           7654 SALESMAN        1250      15000

                                         BLAKE            7698 MANAGER         2850      34200

                                         CLARK            7782 MANAGER         2450      29400

                                         SCOTT            7788 ANALYST         3000     36000

                                         KING             7839 PRESIDENT       5000     60000

                                         TURNER           7844 SALESMAN        1500      18000

                                         ADAMS            7876 CLERK           1100      13200

                                         JAMES            7900 CLERK            950      11400

                                         FORD             7902 ANALYST         3000      36000

                                         MILLER           7934 CLERK           1300      15600

                                        

             ---显示员工姓名 员工编号 工作  月薪 年收入(月薪*12+comm)

    9 空值问题:

             selectename, empno, job, sal , sal*12+comm from emp ; (有问题,由于commnull的)

            

    空值问题注意:

                       1NULL空值不论什么数和null运算都为null    (null无穷大)

                       2空值不是空  null != null

             解决的方法:

                       null滤空函数 nvl (a,b) -- a是空的时候,返回b( 当中 b能够是数字/字符串)

                       ---字符串和日期在sql语言用单引號扩起来...

             案例说明:

    A   select ename, empno, job,sal , sal*12+ nvl(comm,0) 年收入from emp;

     

                                ENAME                     EMPNO JOB              SAL     年收入

                                ------------------------------ --------- ---------- ----------

                                SMITH                      7369 CLERK            800       9600

                                ALLEN                      7499 SALESMAN        1600      19500

                                WARD                       7521 SALESMAN        1250      15500

                                JONES                      7566 MANAGER         2975      35700

                                MARTIN                     7654 SALESMAN        1250      16400

                                BLAKE                      7698 MANAGER         2850      34200

                                CLARK                      7782 MANAGER         2450      29400

                                SCOTT                      7788 ANALYST         3000      36000

                                KING                       7839 PRESIDENT       5000     60000

                                TURNER                     7844 SALESMAN        1500      18000

                                ADAMS                      7876 CLERK           1100      13200

                                JAMES                      7900 CLERK            950      11400

                                FORD                       7902 ANALYST         3000      36000

                                MILLER                     7934 CLERK           1300      15600

                               

              B   查询奖金为空的员工

                                select* from emp where comm is null;

            结果:

                                EMPNOENAME      JOB              MGR HIREDATE              SAL       COMM    DEPTNO

                                -------------------- --------- ---------- -------------- ---------- ---------- ----------

                                      7369 SMITH      CLERK           7902 17-12-80            800                    20

                                      7566 JONES      MANAGER         7839 02-4 -81           2975                    20

                                      7698 BLAKE      MANAGER         7839 01-5 -81           2850                    30

                                      7782 CLARK      MANAGER         7839 09-6 -81           2450                    10

                                      7788 SCOTT      ANALYST         7566 19-4 -87           3000                    20

                                      7839 KING       PRESIDENT            17-11-81           5000                    10

                                      7876 ADAMS      CLERK           7788 23-5 -87           1100                    20

                                      7900 JAMES      CLERK           7698 03-12-81            950                    30

                                      7902 FORD       ANALYST         7566 03-12-81           3000                    20

                                      7934 MILLER     CLERK           7782 23-1 -82           1300                    10

            

              C  查询奖金不为空的员工

                           select * from emp where comm is not null

           结果:

                          EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM    DEPTNO

                       ------------------ --------- ---------- -------------- ---------- ---------- ----------

                           7499 ALLEN      SALESMAN        7698 20-2 -81           1600        300        30

                           7521 WARD       SALESMAN        7698 22-2 -81           1250        500         30

                           7654 MARTIN     SALESMAN        7698 28-9 -81           1250       1400         30

                           7844 TURNER     SALESMAN        7698 08-9 -81           1500          0         30

     

     

    10 连接符 ||

               select 'hello' || 'world' from emp;      

               select 'hello' || 'world' from dual;  --伪表

     

    dual是数据字典 (oracle超级管理员分配给普通用的资源....,tabkeyword....)

              SQL> select sysdate from dual;

                                         SYSDATE

                                         --------------

                                         07-10-14

                                        

                                         SQL>

    11:DISTINCT  

    A  作用去除反复的行

              select DISTINCT deptno from emp;

                                    DEPTNO

                                         ----------

                                             30

                                             20

                                        10

              B  DISTINCT修饰多个字段的时, 当部门编号和工种都一样的时候,觉得是反复的...

                                                                                                           否则 不同行.

                                                            ====DISTINCT2个字段

                                         selectDISTINCT deptno, job from emp

                                        

     

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    linux查看CPU和内存信息
    linux yum命令详解
    查看文件中关键字前后几行的内容
    vue.js+web storm安装及第一个vue.js
    android GPS: code should explicitly check to see if permission is available
    ASP.NET MVC Identity 使用自己的SQL Server数据库
    阿里云服务器,tomcat启动,一直卡在At least one JAR was scanned for TLDs yet contained no TLDs就不动了
    ASP.NET MVC4 MVC 当前上下文中不存在名称“Scripts”
    python 将windows字体中的汉字生成图片的方法
    Java android DES+Base64加密解密
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4820136.html
Copyright © 2020-2023  润新知