1Z0-051考试笔记

ORACLE 如何建立表外键
例:学生表student (id, name , sex )  
  成绩表score (id ,math )  


如何创建表,要求 有主键,有约束 解:
  CREATE TABLE STUDENT(ID CHAR(10), NAME VARCHAR(8),SEX CHAR(1));  
  ALTER TABLE STUDENT ADD CONSTRAINT PK_STUDENT PRIMARY KEY(ID);  
  CREATE TABLE SCORE( ID CHAR(10),MATH NUMBER(5,2));  
  ALTER TABLE SCORE ADD CONSTRAINT FK_SCROE FOREIGN KEY(ID) REFERENCES STUDENT(ID);
* 主键与外键:
  键是表中的列(可以是一列,也可以是几列),主键用于唯一的标识表中的数据项;外键用于连接父表和子表。而所谓的父表和子表是根据3NF
  范式的要求,为了消除传递依赖,将原表拆成2个相互关联的表,而这个关联就是外键。
 
ALTER TABLE zxt RENAME COLUMN mcc TO mc;


Q2.
CONSTRAINT name FOREIGN KEY (column_name) REFERENCES table_name (column_name);


Q3.


SQL> select hiredate+'10' from emp;  增加10天


HIREDATE+
---------
27-DEC-80
02-MAR-81
04-MAR-81
SQL> select hiredate from emp
  2  ;


HIREDATE
---------
17-DEC-80
20-FEB-81




Q4.
MAX()和MIN()函数不仅可以作用于数值型数据,也可以作用于字符串或是日期时间数据类型的数据。


实例MAX()函数用于字符型数据


可见,对于字符串也可以求其最大值。


说明
 对字符型数据的最大值,是按照首字母由A~Z的顺序排列,越往后,其值越大。当然,对于汉字则是按照其全拼拼音排列的,若首字符相同,则比较下一个字符,以此类推。


当然,对与日期时间类型的数据也可以求其最大/最小值,其大小排列就是日期时间的早晚,越早认为其值越小,如下面的实例。


Q5.'
sub  queries
A. Multiple columns or expressions can be compared between the main query and sub query
B. Main query and sub query can get data from different tables
C. Sub queries can contain GROUP BY and ORDER BY clauses




Q6.
IN比较符的多行子查询
在多行子查询中必须使用多行比较运算符 , IN比较符返回子查询的中的每一个值,一旦有与该值相等的数据行则输出这些满足条件的数据行。 
例子10-21查询那些是所在岗位中工资最低的员工信息。
SQL> select ename,job,sal,hiredate
  2  from emp
  3  where sal  in (select min(sal)
  4                                from emp
  5                                group by job); 


Q7.
the two statement produce identical results as ORDER BY 2 will take the second column as sorting column.


Q8.
SUBQUERIES can be used in the SELECT list and in the FROM, WHERE, and HAVING clauses of a query.
A subquery can have any of the usual clauses for selection and projection. The following are required clauses:
A SELECT list
A FROM clause
The following are optional clauses:
WHERE
GROUP BY
HAVING
The subquery (or subqueries) within a statement must be executed before the parent query that calls it, in order that the results of the subquery can be passed to the
parent  


SQL> SELECT   package_id, SUM (package_line_id)
  2      FROM (SELECT package_id, package_line_id, 1 dummy
  3              FROM kdlv_package_lines
  4             WHERE package_id BETWEEN 30006 AND 30028)
  5  GROUP BY (SELECT 'package_id'
  6              FROM DUAL)
  7  /
GROUP BY (SELECT 'package_id'
          *
ERROR at line 5:
ORA-22818: subquery expressions not allowed here


Q9.


Which three SQL statements would display the value 1890.55 as $1,890.55? (Choose three.)
A. SELECT TO_CHAR(1890.55,'$99G999D00')
FROM DUAL;
B. SELECT TO_CHAR(1890.55,'$9,999V99')
FROM DUAL;
C. SELECT TO_CHAR(1890.55,'$0G000D00')
FROM DUAL;
D. SELECT TO_CHAR(1890.55,'$99G999D99')
FROM DUAL;
E. SELECT TO_CHAR(1890.55,'$9,999D99')
FROM DUAL;
Answer: A,C,D


Q10
 It produces an error because the ORDER BY clause should appear only at the end of a compound query-that is, with the last SELECT statement
Using the ORDER BY Clause in Set Operations
The ORDER BY clause can appear only once at the end of the compound query. Component queries cannot have individual ORDER BY clauses. The ORDER BY
clause recognizes only the columns of the first SELECT query.
By default, the first column of the first SELECT query is used to sort the output in an ascending order


Q12
SQL have character, numeric, date, conversion function.
Incorrect answer:
ASQL have character, numeric, date, conversion function. CSQL have character, numeric, date, conversion function. DSQL have character, numeric, date, conversion
function. FSQL have character, numeric, date, conversion function.


Q15.AC


Q16 D
You need to create a report of the 10 students who achieved the highest ranking in the course INT SQL and who completed the course in the year 1999.
D. SELECT student_id, marks, ROWNUM "Rank"
FROM (SELECT student_id, marks
FROM students
WHERE (finish_date BETWEEN '01-JAN-99 AND '31-DEC-99' AND course_id = `INT_SQL'
ORDER BY marks DESC)
WHERE ROWNUM <= 10 ;


如果你要是用 rownum查询只能使用
SQL> select rownum ,empno ,ename,job,mgr,hiredate from emp where rownum < 5; 查询前四条的记录!


ROWNUM EMPNO ENAME JOB MGR HIREDATE
---------- ---------- ---------- --------- ---------- ------------------
1 7369 SMITH CLERK 7902 17-DEC-80
2 7499 ALLEN SALESMAN 7698 20-FEB-81
3 7521 WARD SALESMAN 7698 22-FEB-81
4 7566 JONES MANAGER 7839 02-APR-81




Q17
B
SYSDATE cannot be used with the CHECK constraint


CHECK Constraint
The CHECK constraint defines a condition that each row must satisfy. The condition can use the same constructs as the query conditions, with the following exceptions:
References to the CURRVAL, NEXTVAL, LEVEL, and ROWNUM pseudocolumns(伪列) Calls to SYSDATE, UID, USER, and USERENV functions
Queries that refer to other values in other rows
A single column can have multiple CHECK constraints that refer to the column in its definition. There is no limit to the number of CHECK constraints that you can define
on a column. CHECK constraints can be defined at the column level or table level.
CREATE TABLE employees
(...
salary NUMBER(8,2) CONSTRAINT emp_salary_min
CHECK (salary > 0),


Q19
Oracle INSERT WITH CHECK OPTION的用法
insert into (