Tài liệu OCA: Oracle Database 11g Administrator Certified Associate Study Guide- P8 ppt

50 416 0
Tài liệu OCA: Oracle Database 11g Administrator Certified Associate Study Guide- P8 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Review Questions 281 13. The following table describes the DEPARTMENTS table: Column Name dept_id dept_name mgr_id location_id Key Type pk Nulls/Unique NN FK Table Datatype NUMBER VARCHAR2 NUMBER NUMBER Length 4 30 6 4 Default Value None None None 99 Which of the following INSERT statements will raise an exception? A. INSERT INTO departments (dept_id, dept_name, location_id) VALUES(280,’Security’,1700); B. INSERT INTO departments VALUES(280,’Security’,1700); C. INSERT INTO departments VALUES(280,’Corporate Giving’,266,1700); D. None of these statements will raise an exception. 14. Refer to the DEPARTMENTS table structure in question 13. Two SQL statements are shown here. Choose the best option that describes the SQL statements. 1. INSERT INTO departments (dept_id, dept_name, mgr_id) VALUES(280,’Security’,1700); 2. INSERT INTO departments (dept_id, dept_name, mgr_id, location_id) VALUES(280,’Security’,1700, NULL); A. Statements 1 and 2 insert the same values to all columns in the table. B. Statements 1 and 2 insert different values to at least one column in the table. C. The location_id column must be included in the column list of statement 1. D. A NULL value cannot be inserted explicitly in statement 2. 95127c05.indd 281 2/17/09 12:20:00 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 282 Review Questions 15. The SALES table contains the following data: SELECT channel_id, COUNT(*) FROM sales GROUP BY channel_id; C COUNT(*) - ---------- T 12000 I 24000 How many rows will be inserted into the NEW_CHANNEL_SALES table with the following SQL statement? INSERT FIRST WHEN channel_id =’C’ THEN INTO catalog_sales (prod_id,time_id,promo_id ,amount_sold) VALUES (prod_id,time_id,promo_id,amount_sold) WHEN channel_id =’I’ THEN INTO internet_sales (prod_id,time_id,promo_id ,amount_sold) VALUES (prod_id,time_id,promo_id,amount_sold) WHEN channel_id IN (‘I’,’T’) THEN INTO new_channel_sales (prod_id,time_id,promo_id ,amount_sold) VALUES (prod_id,time_id,promo_id,amount_sold) SELECT channel_id,prod_id,time_id,promo_id,amount_sold FROM sales; A. 0 B. 12,000 C. 24,000 D. 36,000 16. How many rows will be counted in the last SQL statement that follows? SELECT COUNT(*) FROM emp; 120 returned INSERT INTO emp (emp_id) VALUES (140); SAVEPOINT emp140; 95127c05.indd 282 2/17/09 12:20:00 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Review Questions 283 INSERT INTO emp (emp_id) VALUES (141); INSERT INTO emp (emp_id) VALUES (142); INSERT INTO emp (emp_id) VALUES (143); TRUNCATE TABLE employees; INSERT INTO emp (emp_id) VALUES (144); ROLLBACK; SELECT COUNT(*) FROM emp; A. 121 B. 0 C. 124 D. 143 17. Which is the best option that describes the following SQL statement? 1. UPDATE countries 2. CNT_NAME = UPPER(CNT_NAME) 3. WHERE country_code BETWEEN 1 and 99; A. The statement is missing the keyword SET, but the statement will work just fine because SET is an optional keyword. B. The BETWEEN operator cannot be used in the WHERE clause used in an UPDATE statement. C. The function UPPER(CNT_NAME) should be changed to UPPER(‘CNT_NAME’). D. The statement is missing keyword SET; hence, the statement will fail. 18. The table ORDERS has 35 rows. The following UPDATE statement updates all 35 rows. Which is the best option? UPDATE orders SET ship_date = TRUNC(ship_date) WHERE ship_date != TRUNC(ship_date) A. When all rows in a table are updated, the LOCK TABLE orders IN EXCLUSIVE MODE statement must be executed before the UPDATE statement. B. No other session can query from the table until the transaction ends. C. Since all rows are updated, there is no need for any locking, and hence Oracle does not lock the records. D. The statement locks all the rows until the transaction ends. 95127c05.indd 283 2/17/09 12:20:00 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 284 Review Questions 19. Which of the following INSERT statements will raise an exception? A. INSERT INTO EMP SELECT * FROM NEW_EMP; B. INSERT FIRST WHEN DEPT_NO IN (12,14) THEN INSERT INTO EMP SELECT * FROM NEW_EMP; C. INSERT FIRST WHEN DEPT_NO IN (12,14) THEN INTO EMP SELECT * FROM NEW_EMP; D. INSERT ALL WHEN DEPT_NO IN (12,14) THEN INTO EMP SELECT * FROM NEW_EMP; 20. What will the salary of employee Arsinoe be at the completion of the following SQL statements? UPDATE emp SET salary = 1000 WHERE name = ‘Arsinoe’; SAVEPOINT Point_A; UPDATE emp SET salary = salary * 1.1 WHERE name = ‘Arsinoe’; SAVEPOINT Point_B; UPDATE emp SET salary = salary * 1.1 WHERE name = ‘Berenike’; SAVEPOINT point_C; ROLLBACK TO SAVEPOINT point_b; COMMIT; UPDATE emp SET salary = 1500 WHERE name = ‘Arsinoe’; SAVEPOINT point_d; ROLLBACK TO point_d; COMMIT; A. 1000 B. 1100 C. 1111 D. 1500 95127c05.indd 284 2/17/09 12:20:01 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Answers to Review Questions 285 Answers to Review Questions 1. D. When inserting from another table using a subquery, the VALUES clause should not be included. Options B and C are invalid syntaxes for the INSERT statement. 2. E. If a transaction is not currently open, any INSERT, UPDATE, MERGE, DELETE, SELECT FOR UPDATE, or LOCK statement will implicitly begin a transaction. 3. B, C. Option A will error out because when using columns in set, a subquery must be used as in option C. Option D is wrong because AND is used instead of a comma to separate col- umns in the SET clause. 4. A, D. COMMIT, ROLLBACK, and any DDL statement end a transaction—DDL is automatically committed. INSERT, UPDATE, and DELETE statements require a commit or rollback. 5. A. Option A uses a correlated subquery to match the correct employee. Option B selects all the rows in the subquery and hence will generate an error. Option C is not valid syntax. Option D will update all the rows in the table since the UPDATE statement does not have a WHERE clause. The WHERE clause preset belongs to the subquery. 6. B. In an UPDATE statement, the WHERE clause should come after the SET clause. 7. D. When deleting a row from a table, do not use column names. To change column values to NULL, use the UPDATE statement. 8. C. The FROM keyword in the DELETE statement is optional. Statement 3 is first building a subquery with the necessary condition and deleting the rows from the subquery. 9. B. When two savepoints are created with the same name, Oracle erases the older savepoint. In the code segment, the DELETE and the first INSERT are not rolled back. 10. C. When updating more than one column in a single UPDATE statement, separate the col- umns by a comma; do not use the AND operator. 11. D. Option A is updating the wrong table. Option B has the right syntax but will update all the rows in the EMPLOYEE table since there is no WHERE clause for the UPDATE statement. Since the WHERE clause is in the subquery, all the rows that do not belong to department 22 will be updated with a NULL. Options C and D are similar, except for the AND keyword instead of WHERE. 12. D. The first INSERT statement and the last INSERT statement will be saved in the database. The ROLLBACK TO A statement will undo the second and third inserts. 13. B. Option B will raise an exception because there are not enough column values for the implicit column list (all columns). 14. B. Since the location_id column is defined with a default value of 99, statement 1 will insert 99 for location_id. In statement 2, a NULL is explicitly inserted into the location_id column; Oracle will not replace the NULL with the default value defined. 95127c05.indd 285 2/17/09 12:20:01 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 286 Answers to Review Questions 15. B. The FIRST clause tells Oracle to execute only the first WHEN clause that evaluates to TRUE for each row. Since no rows have a channel_id of C, no rows would be inserted into the catalog_sales table; 24,000 rows have channel_id of I, so control would pass to the sec- ond WHEN clause 24,000 times, and the internet_sales table would get 24,000 rows. Since the second WHEN clause evaluates to TRUE and the INSERT FIRST option is specified, these rows would not make it to the third WHEN clause and would not be inserted into the new_ channel_sales table. Had the INSERT ALL option been used, these 24,000 rows would also get inserted into the new_channel_sales table; 12,000 rows have a channel_id of T, so control would pass all the way to the third WHEN clause for these rows, and 12,000 rows would get inserted into new_channel_sales. 16. C. The TRUNCATE statement is DDL and performs an implicit commit. After the TRUNCATE statement on the employees table, there are 124 rows in the emp table. The one row that got inserted was removed when the ROLLBACK statement was executed. 17. D. You must have the SET keyword in an UPDATE statement. The BETWEEN operator and any other valid operators are allowed in the WHERE clause. 18. D. When DML operations are performed, Oracle automatically locks the rows. You can query (read) the rows, but no other DML operation is allowed on those rows. When you read the rows, Oracle constitutes a read-consistent view using the undo segments. 19. B. The keywords INSERT INTO are required in single-table INSERT statements but are not valid in multiple-table INSERT statements. 20. D. The final rollback (to point_d) will roll the changes back to just after setting the salary to 1500. 95127c05.indd 286 2/17/09 12:20:01 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 6 Creating Tables and Constraints ORACLE DATABASE 11g: SQL FUNDAMENTALS I EXAM OBJECTIVES COVERED IN THIS CHAPTER: Using DDL Statements to Create and Manage Tables  Categorize the main database objects  Review the table structure  List the data types that are available for columns  Create a simple table  Explain how constraints are created at the time of table  creation Describe how schema objects work  95127c06.indd 287 2/18/09 6:45:46 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. An Oracle database has many different types of objects. Related objects are logically grouped together in a schema, which consists of various types of objects. The basic types of objects in an Oracle Database are tables, indexes, constraints, sequences, and synonyms. Though this chapter discusses tables and constraints, I will start the chapter with an over- view of the main database objects in Oracle. The table is the basic structure of data storage in Oracle. A table has columns as part of the definition and stores rows of data. In a relational database, the data in various tables may be related. A constraint can be considered as a rule or policy defined in the database to enforce data integrity and business rules. In this chapter, I will discuss creating tables and using constraints. Since the table is the most important type of object in an Oracle Data- base, it is important to know how to create tables and constraints on tables. Database Objects Overview Data in the Oracle Database is stored in tables. A table is the main database object. Many other database objects, whether or not they store data, are generally based on the tables. Let’s review the main database objects in Oracle that are relevant for this certification exam: Table A table is defined with columns and stores rows of data. A table should have at least one column. In Oracle, a table normally refers to a relational table. You can also create object tables. Object tables are created with user-defined datatypes. Temporary tables (called global temporary tables in Oracle) are used to hold temporary data specific to a transaction or session. A table can store a wide variety of data. Apart from storing text and numeric infor- mation, you can store date, timestamp, binary, or raw data (such as images, documents, and information about external files). A table can have virtual columns. As the name indicates, these types of columns do not consume storage space on disk; the database derives values in virtual columns from normal columns. Tables are discussed in the next sections of this chapter. View A view is a customized representation of data from one or more tables and/or views. Views are used as a window to show information from tables in a certain way or to restrict the information. Views are queries stored in the database that select data from one or more tables. They also provide a way to restrict data from certain users, thus providing an addi- tional level of security. 95127c06.indd 288 2/18/09 6:45:46 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Database Objects Overview 289 Sequence A sequence is a way to generate continuous numbers. Sequences are useful for generating unique serial numbers or key values. The sequence definition is stored in the data dictionary. Sequence numbers are generated independently of other database objects. Synonym A synonym is an alias for any table, view, sequence, or other accessible database object. Because a synonym is simply an alias, it requires no storage other than its definition in the data dictionary. Synonyms are useful because they hide the identity of the underlying object. The object can even be part of another database. A public synonym is accessible to all users of the database, and a private synonym is accessible only to its owner. Index An index is a structure associated with tables used to speed up the queries. An index is an access path to reach the desired row faster. Oracle has B-tree and bitmap indexes. Creating/dropping indexes does not affect the storage of data in the underlying tables. You can create unique or nonunique indexes. Unique indexes are created automatically by Oracle when you create a primary key or a unique key constraint in a table. A composite index has more than one column in the index. Views, sequences, synonyms, and indexes are discussed in Chapter 7, “Creating Schema Objects.” Oracle 11g has a wide array of database objects to suit various application requirements. These objects are not discussed in this book because they are not part of the certifica- tion exam at this time. Some of the other database objects that may be used in application development are clusters, dimensions, directories, functions, Java sources/classes, libraries, materialized views, and types. To learn more about the various Oracle 11g database schema objects, please refer to the Oracle documentation called “Oracle Database Administrators Guide 11g Release 1 (11.) Part Number B28310-04,” which is available online at www.oracle .com/pls/db111/db111.homepage . Schema Objects A schema is a collection of related database objects grouped together. For example, a schema can have tables, views, triggers, synonyms, and PL/SQL programs such as procedures. A schema is owned by a database user and has the same name as the user. If the database user does not own any database objects, then no schema is associated with the user. A schema is a logical grouping of database objects. A database user can have only one schema associated and is created when you create any database object. They may include any or all the basic database objects discussed earlier. Oracle 11g may also include the following types of structures in the schema. These objects are listed here only to give you an overview of schemas; creating and managing these objects are not part of the certification exam at this time. For the certification exam, prepare to know the schema objects discussed in this chapter and in Chapter 7. 95127c06.indd 289 2/18/09 6:45:47 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 290 Chapter 6  Creating Tables and Constraints Materialized view Materialized views are objects used to summarize and replicate data. They are similar to views but occupy storage space. Materialized views are mainly used in data-warehouse environments where data needs to be aggregated and stored so that queries and reports run faster. Materialized views can also be used to replicate data from another database. Dimension A dimension is a logical structure to define the relationship between columns in a table. Dimensions are defined in the data dictionary and do not occupy any storage space. The columns in a dimension can be from a single table or from multiple tables. An example of a dimension would be the relationship between country, state, and city in a table that stores address information. Cluster A cluster is a method of storing data from related tables at a common physical location. You can share the storage of rows in related tables for performance reasons if the access to the rows in the tables always involves join operations on the tables. For example, if you have an orders table and a customers table in the schema, you can query the orders table always joining the customers table, because that’s where you get the customer name associated with the customer ID. A cluster may be created for the orders and customers tables so that the rows associated with the same customer are stored in the same physical storage area (block). Database storage and blocks are discussed in Chapter 8, “Introducing Oracle 11g Components and Architecture.” Database links A database link is a schema object that enables you to access an object from a different database. SQL queries can reference tables and views belonging to the remote database by appending @db_link_name to the table or view. For example, to access the CUSTOMER_ORDERS table using a database link named LONDON_SALES , you would use CUSTOMER_ORDERS@LONDON_SALES . Triggers A trigger is a stored PL/SQL program that gets executed when a specified condi- tion occurs. A trigger can be defined on a table to “fire” when an insert, update, or delete operation occurs on the table. A trigger may also be defined on the database to “fire” when certain database conditions occur, such as starting the database, or when a database error occurs. Java objects Oracle Database 11g includes Java objects such as Java classes, Java sources, and Java resources. Java stored programs can be created using the different Java object types. PL/SQL programs PL/SQL stored programs include procedures, functions, and packages. A procedure is a PL/SQL programmatic construct. A function is similar to a procedure but always returns a value. A package is a grouping of related PL/SQL objects. Built-in Datatypes When creating tables, you must specify a datatype for each column you define. Oracle 11g is rich with various datatypes to store different kinds of information. By choosing the 95127c06.indd 290 2/18/09 6:45:47 AM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... digits In Oracle 11g it is recommended you use BINARY_FLOAT or BINARY_DOUBLE instead of the FLOAT datatype Date and Time Datatypes In pre Oracle9 i databases, the only datetime datatype available was DATE, which stores the date and time Oracle9 i Database introduced the TIMESTAMP and INTERVAL datatypes to enhance the storage and manipulation of date and time data Six datetime datatypes in Oracle 11g can... mandatory (NOT NULL) column ALTER TABLE orders ADD updated_by VARCHAR2 (30) DEFAULT ‘JOHN’ NOT NULL; Table altered In Oracle 11g, when you add a column with the NOT NULL constraint and the DEFAULT value, Oracle 11g does not update all the existing rows in the table with the default value Oracle 11g simply updates the dictionary and gets you the value from the dictionary when you query the newly added column... size, Oracle will return an error Oracle will not chop or truncate the inserted value to store it in the database column CLOB The syntax for the CLOB datatype is as follows: CLOB CLOB is one of the Large Object datatypes provided to store variable-length character data The maximum amount of data you can store in a CLOB column is based on the block size of the database CLOB can store up to (4GB–1)* (database. .. variable-length Unicode character data The maximum amount of data you can store in a NCLOB column is (4GB–1)* (database block size) You do not specify the size with this datatype definition LONG The syntax for the LONG datatype is as follows: LONG Using the LONG datatype is discouraged in Oracle Database 11g It is provided only for backward compatibility You should use the CLOB datatype instead of LONG LONG... LONG 292  Chapter 6    Creating Tables and Constraints n Character datatypes store alphanumeric data in the database character set or in the Unicode character set You define the database character set when you create the database The character set determines which languages can be represented in the database For example, US7ASCII is a 7-bit ASCII character set that can represent the English language and... BFILE is used to store information on external files The external file size can be up to 4GB Oracle stores only the file pointer in the database The actual file is stored on the operating system Of the four Large Object datatypes (CLOB, BLOB, NCLOB, and BFILE), only BFILE stores actual data outside the Oracle Database Row ID Datatypes Physical storage of each row in a table can be represented using a... TEMPORARY, Oracle creates a temporary relational table known as the global temporary table (GTT) whose definition is available to all sessions in the database, but the data is available only to the session that inserted data to it The GTT is truly a temporary table On other flavors of RDBMS, a permanent table created to hold temporary data is called a temporary table You can do the same with Oracle, but Oracle. .. 28-JUN-01 JOHN SQL> Adding Comments It is a good practice to document the purpose and any information on the type of data stored in the table in the database itself so that developers and administrators working on the database know the importance of the table/data Oracle provides the COMMENT statement to add documentation to a table or a column Comments on tables are added using the COMMENT ON TABLE statement,... COLUMN statement The following example provides comments for the sample table: COMMENT ON TABLE mytable IS Oracle 11g Study Guide Example Table’; Comment created COMMENT ON COLUMN mytable.column_1 is ‘First column in MYTABLE’; Comment created You can query the table and column information from the Oracle dictionary using the following views: USER_TABLES, ALL_TABLES, USER_TAB_COLUMNS, and ALL_TAB_COLUMNS... datatype, you will be able to store and retrieve data without compromising its integrity A datatype associates a predefined set of properties with the column The datatypes in Oracle 11g can be classified into five major categories Figure 6.1 shows the categories and the datatype names F i g u r e   6 1  ​ Oracle built-in datatypes  ​ Character CHAR VARCHAR2 CLOB LONG NCHAR NVARCHAR2 NCLOB Numeric NUMBER . about the various Oracle 11g database schema objects, please refer to the Oracle documentation called Oracle Database Administrators Guide 11g Release 1 (11.). the database to “fire” when certain database conditions occur, such as starting the database, or when a database error occurs. Java objects Oracle Database

Ngày đăng: 15/12/2013, 05:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan