• OCP-1Z0-051-V9.02-122题


    122. View the Exhibit for the structure of the STUDENT and FACULTY tables.

    You need to display the faculty name followed by the number of students handled by the faculty at the

    base location.

    Examine the following two SQL statements:

    Statement 1

    SQL>SELECT faculty_name,COUNT(student_id)    

    FROM student JOIN faculty    

    USING (faculty_id, location_id)    

    GROUP BY faculty_name;

    Statement 2

    SQL>SELECT faculty_name,COUNT(student_id)    

    FROM student NATURAL JOIN faculty    

    GROUP BY faculty_name;

    Which statement is true regarding the outcome? 

    A. Only s tatement 1 executes successfully and gives the required result.

    B. Only statement 2 executes successfully and gives the required result.

    C. Both statements 1 and 2 execute successfully and give different results.

    D. Both statements  1 and 2 execute successfully and give the same required result.

    Answer: D
    答案解析:
    实验验证:
    1、首先创建这两张表

    scott@TEST0924> create table student
      2  (student_id number(2) not null,
      3  student_name varchar2(20),
      4  faculty_id varchar2(2),
      5  location_id number(2)
      6  );

    Table created.

     

    scott@TEST0924> create table faculty
      2  (faculty_id number(2) not null,
      3  faculty_name varchar2(20),
      4  location_id number(2)
      5  );

    Table created.

    2、向这两张表中插入数据

    scott@TEST0924> insert into student values(2,'zhansan3','20',11);

    1 row created.

    scott@TEST0924> insert into faculty values(20,'zhansan2',11);

    1 row created.

    3、执行查询语句,可成功执行

    scott@TEST0924> SELECT faculty_name,COUNT(student_id) FROM student JOIN faculty
      2  USING (faculty_id, location_id) GROUP BY faculty_name;

    FACULTY_NAME         COUNT(STUDENT_ID)
    -------------------- -----------------
    zhansan2                             1

    scott@TEST0924> SELECT faculty_name,COUNT(student_id) FROM student NATURAL JOIN faculty
      2  GROUP BY faculty_name;

    FACULTY_NAME         COUNT(STUDENT_ID)
    -------------------- -----------------
    zhansan2                             1

    4,如果向student表中的faculty_id插入字符类。

    scott@TEST0924> insert into student values(2,'zhansan3','a1',11);

    1 row created.

    5、查询失败

    scott@TEST0924> SELECT faculty_name,COUNT(student_id) FROM student NATURAL JOIN faculty
      2  GROUP BY faculty_name;
    SELECT faculty_name,COUNT(student_id) FROM student NATURAL JOIN faculty
    *
    ERROR at line 1:
    ORA-01722: invalid number


    scott@TEST0924> SELECT faculty_name,COUNT(student_id) FROM student JOIN faculty
      2  USING (faculty_id, location_id) GROUP BY faculty_name;
    USING (faculty_id, location_id) GROUP BY faculty_name
           *
    ERROR at line 2:
    ORA-01722: invalid number

    总结,当且仅当student表中的faculty_id列能隐式转换为数字类型时,这两个查询语句才可以查询成功。


    因为NATURAL JOIN是将两个表中具有相同名称的所有列都连接起来,即FACULTY_ID和LOCATION_ID,而USING子句也使用了这两个字段来连接,故结果相同。
  • 相关阅读:
    使用South时候由于两个相同id的文件引起的问题
    Python os模块
    Ubuntu的关机重启命令知识
    [BUGFIX]__import_pywin32_system_module__
    Django生产环境的部署-Apache-mod_wsgi
    我是如何将linux用在开发环境中的
    php抽奖概率算法
    PHP接收IOS post过来的json数据无法解析的问题
    python apache下出现The _imaging C module is not installed
    php 打印
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13316868.html
Copyright © 2020-2023  润新知