27
SQL SQL 语语语 语语语 SQL*PLUS SQL*PLUS O O racle9i racle9i 语语语语语 语语语语语

SQL 语言与 SQL*PLUS

  • Upload
    maili

  • View
    130

  • Download
    9

Embed Size (px)

DESCRIPTION

SQL 语言与 SQL*PLUS. O racle9i 数据库 语言. SQL 命令. SQL 关键字 ( 以下命令为 SQL 命令,以“;”结束) Alter 、 Audit 、 Commit 、 Comment 、 Create 、 Delete Drop 、 Grant 、 Insert 、 Lock 、 NoAudit 、 Rename Revoke 、 Rollback 、 Select 、 Update 、 Validate 。. SQL*PLUS 命令. - PowerPoint PPT Presentation

Citation preview

Page 1: SQL 语言与 SQL*PLUS

SQLSQL 语言与语言与 SQL*PLUSSQL*PLUSOOracle9i racle9i 数据库语言数据库语言

Page 2: SQL 语言与 SQL*PLUS

[email protected]

SQLSQL 命令命令 SQL 关键字(以下命令为 SQL 命令,以“;”结束)

Alter 、 Audit 、 Commit、 Comment 、

Create 、 Delete

Drop 、 Grant 、 Insert 、 Lock 、 NoAudit 、 Rename

Revoke 、 Rollback 、 Select 、 Update 、 Valid

ate 。

Page 3: SQL 语言与 SQL*PLUS

[email protected]

SQL*PLUSSQL*PLUS 命令命令 SQL*PLUS 关键字(以下命令以“ CR” 结束) @ 、 # 、 / 、 Accept 、 Append 、 Break 、

Btitle 、 Change 、 Clear 、 Column 、 Compute 、 Connect 、 Copy 、 Define 、 Del 、 Describe 、 Disconnect 、 Document 、 Edit 、 Exit 、 Get 、 Help 、 Host 、 Input 、 List 、 Newpage 、 Pause 、 Quit 、 Remark 、 Run 、 Save 、 Set 、 Show 、 Spool 、 Start 、 Timing 、 Ttitle 、 Undefine 、

Page 4: SQL 语言与 SQL*PLUS

[email protected]

编辑和运行编辑和运行 SQLSQL 缓冲区的缓冲区的 SQL*PLUSSQL*PLUS 命命令令 命令 缩写 解释

APPEND text A text 行尾增加 text CHANGE C/old/new 在当前行中将 old 换为 /new CHANGE C/text/ 在当前行中删除 textCLEAR Buffer CL Buff 清除缓冲区

DEL DEL 删除当前行 INPUT I 增加一行 INPUT text I text 增加有 text 组成的行 LINE L 显示缓冲区内容 LINE n L n 显示第 n 行 RUN R 运行 SQL 缓冲区命令

Page 5: SQL 语言与 SQL*PLUS

[email protected]

Oracle Quiz Oracle Quiz

《 QL 测 试》 Oracle Assessment Tests

Page 6: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

You query the database with this command: SELECT name FROM employee WHERE name LIKE ‘_a%’;Which names are displayed?A. names starting with ‘a’B. names starting with ‘a’ or ‘A’C. names containing ‘a’ as the second letterD. names containing ‘a’ as any letter except the first

Page 7: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

For which task would it be most appropriate to use the keyword DISTINCT?A. Identify duplicate rows in a table.B. Identify which column has unique data.C. Eliminate duplicate columns in a table.D. Eliminate duplicate rows in the result set.

Page 8: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

You query the database with this SQLstatement: SELECT name,NVL(salary,0) FROM employee WHRER salary IS NULL ORDER BY name;What is displayed for the SALARY columnwhen a NULL value is returned? A. 0 B. NULL C.spaces D.nothing

Page 9: SQL 语言与 SQL*PLUS

[email protected]

For which task would you use the BETWEEN operator?A. Query the database for unknown values.B. Query the database for a range of values.C. Query the database for a character pattern.D. Query the database for values in a specified list.

(Answers: C D A B)

《《 Oracle Assessment TestsOracle Assessment Tests 》》

Page 10: SQL 语言与 SQL*PLUS

[email protected]

Oracle Quiz Oracle Quiz

《 DML,DCL 测 试》 Oracle Assessment Tests

Page 11: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

What happens when you update a tables without a WHERE clause?

A. The statement will not execute.B. Only the rows specified will be updated.C. All of the rows in the table will be updated.D. The statement will execute,but no change will be made.

Page 12: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

Which commands cause a transaction to end?A. ALTERB. GRANT.C. DELETE.D. INSERTE. UPDATE.F. ROLLBACK

Page 13: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

The Correct Answers 1.C

2.A B F

Page 14: SQL 语言与 SQL*PLUS

[email protected]

Oracle Quiz Oracle Quiz

《 DDL 测 试》 Oracle Assessment Tests

Page 15: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》 You query the database with this command:

SELECT object_name FROM user_objects WHERE object_type =‘TABLE’;Which values are displayed?A. names of all objects you ownB. only the names of tables you ownC. names of all objects you have privileges toD. only the names of tables you have privileges to access.

Page 16: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》 You query the database with this command:

SELECT object_name FROM all_objects WHERE object_type =‘TABLE’;Which values are displayed?A. only names of table you ownB. only names of objects you ownC. Only names of all objects you have privileges to access.D. only the names of all the tables you can access.

Page 17: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》 You attempt to create the ALPHA_3000 table

with this statement:1. CREATE TABLE alpha_30002. (3000_id number(9)3. CONSTRAINT alpha_3000_id_pk PRIMARY KEY,4. name VARCHAR2(25),5. title VARCHAR2(25),6. Idname VARCHAR2(25)7. CONSTRAINT alpha_3000_id_nn NOT NULL); What line in the statement will cause an error? A. 1 B. 2 C.3 D.7

Page 18: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

You alter the database with this command: ALTER TABLE employee ADD (end_date DATE);Which task is accomplished ?A. A constraint is added to an existing column.B. A constraint is modified on an existing column.C. A new column with a constraint is added to the table.D. A new column with no constraint is added to the table.

Page 19: SQL 语言与 SQL*PLUS

[email protected]

You attempt to create a view with this command: CREATE VIEW last_first_vu ASSELECT first_name||’ ’|| last_name“Employee Names” FROM employee ORDER BY last_name,first_name;Which clause causes an error? A. FROM employee.B. ORDER BY last_name,first_name.C. CREATE VIEW last_first_vuD. SELECT first_name||’ ’||last_name”Employee Names”

《《 Oracle Assessment TestsOracle Assessment Tests 》》

Page 20: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

You alter the database with this command: RENAME streets to CITY; Which task is accomplished ?A. The streets user is renamed city.B. The STREETS table is renamed CITY.C. The STREETS column is renamed CITY.D. The streets constraints is renamed city.

Page 21: SQL 语言与 SQL*PLUS

[email protected]

《《 Oracle Assessment TestsOracle Assessment Tests 》》

The Correct Answers B,D,B,D,B,B

Page 22: SQL 语言与 SQL*PLUS

[email protected]

使用 使用 SQL*PLUSSQL*PLUS 命令命令 SQL*PLUS 是 Oracle 对 SQL 语言的扩充 , 是一中交互式的表报生成工具 . 它使用 SQL 命令从 Oracle 中查询信息 , 用

SQL*PLUS 命令设置控制表报的输出格式 , 对 SQL 命令加以扩充 , 可以对题目 , 列标题 , 以及汇总信息进行控制 .

定义表头与表尾 定义表头 : SQL>Ttitle <option> 描述 定义表尾 : SQL>Btitle <option> 描述 option: Left 左对齐 Center 居中 Right 右对齐 使用 TITLE 则报表输出在每页加上日期及页号

Page 23: SQL 语言与 SQL*PLUS

[email protected]

SQL*PLUSSQL*PLUS 命令总结命令总结

Page 24: SQL 语言与 SQL*PLUS

[email protected]

聚组函数的使用聚组函数的使用 聚组函数是从一组中返回汇总信息,聚组函数有 SUM,COUNT

COUNT DISTINCT,MAX,MIN,AVG,STDDEV 计算 max,min,avg,sum

a.SQL>SELECT MIN(sal),MAX(sal),AVG(sal), SUM(sal) FROM emp;b. 找出具有最高工资的员工? SQL>SELECT ename,job,sal FROM emp WHERE sal=(SELECT MAX(sal) FROM emp);

COUNT 与 COUNT DISTINCTa. 求 emp 表中总行数? SQL>SELECT COUNT(*) FROM emp;b. 求 job 的行数? SQL>SELECT COUNT(job) FROM emp;c. 求有多少工中? SQL>SELECT COUNT(DISTINCT job) FROM emp;

Page 25: SQL 语言与 SQL*PLUS

[email protected]

SELECT 字句中目标的一致性不能把单行函数与聚组函数混在一起使用,如

SQL>SELECT ename,SUM(sal) FROM emp; 单行目标   聚组函数

使用 GROUP BY 分组a. 查询从事每一个工种的员工的工资总和? SQL>SELECT job,SUM(sal) FROM emp GROUP BY job;b. 求出从事每一个工种的员工总数? SQL>SELECT job,COUNT(*) FROM emp GROUP BY job;c. 查询每个部门的最高工资及工资总和?

句子语法错误句子语法错误 !!

Page 26: SQL 语言与 SQL*PLUS

[email protected]

SQL>SELECT deptno,SUM(sal),MAX(sal) FROM emp GROUP by deptno;

按多个条件分组SQL>SELECT deptno,job,COUNT(*) FROM emp GROUP BY deptno,job;

使用 Having, 选则满足条件的组SQL>SELECT deptno,SUM(sal) FROM emp GROUP BY deptno HAVING SUM(SAL)>8000;

Page 27: SQL 语言与 SQL*PLUS

[email protected]

        查询语句语法总结查询语句语法总结 SQL>select 列 1, 列 2,…

from 基表 where 条件表达式   group by 分组列 , 分组列2 ,… having 分组条件表达式 order by 排序列 1, 排序列 2,… ;求 , 除去秘书 CLERK 职业 , 那些部门工资总和超过 $3000?

SQL>select deptno,sum(Sal) from emp

where job !=‘CLERK’ group by deptno

having sum(sal)>3000 order by sum(sal);