1 / 25

SQL1-ch9

SQL1-ch9. 使用 DDL 建立與管理表格. 題號. 80 題: 63 、 76 140 題: 6 、 24 、 44 、 71 、 77 、 92. 建立表格. 表格名稱開頭必須為字母. 建立表格. 每個表格 只能有一個 LONG 資料欄 只有 varchar2 、 char 、 number 、 raw 可以 設定欄位長度 。. 限制條件. Constraint ( 限制 ) Not Null 、 Unique 、 Primary Key 、 Foreign Key 、 Check 可為限制條件命名,或由資料庫自動取名。 分為兩種層次 欄位層次

zalman
Télécharger la présentation

SQL1-ch9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SQL1-ch9 使用DDL建立與管理表格

  2. 題號 • 80題:63、76 • 140題:6、24、44、71、77、92

  3. 建立表格 • 表格名稱開頭必須為字母

  4. 建立表格 • 每個表格只能有一個LONG資料欄 • 只有varchar2、char、number、raw可以設定欄位長度。

  5. 限制條件 • Constraint (限制) • Not Null、Unique、Primary Key、Foreign Key、Check • 可為限制條件命名,或由資料庫自動取名。 • 分為兩種層次 • 欄位層次 • 表格層次 • Not Null必須在欄位層次定義

  6. 限制條件

  7. Q76/80 Evaluate the following SQL statement: (Note that the numbers 2,3 etc in the SQL statement are line numbers and not part of the syntax) SQL> CREATE TABLE product 2 (prod_id NUMBER(3), 3 prod_name VARCHAR2(25), 4 qty NUMBER(7,2), 5 price NUMBER(10,2), 6 CONSTRAINT prod_id_pk PRIMARY KEY(prod_id), 7 CONSTRAINT prod_name_uq UNIQUE (prod_name), 8 CONSTRAINT price_nn NOT NULL (price)); What is the outcome of executing this command

  8. > CREATE TABLE product 2 (prod_id NUMBER(3), 3 prod_name VARCHAR2(25), 4 qty NUMBER(7,2), 5 price NUMBER(10,2), 6 CONSTRAINT prod_id_pk PRIMARY KEY(prod_id), 7 CONSTRAINT prod_name_uq UNIQUE (prod_name), 8 CONSTRAINT price_nn NOT NULL (price)); A. It generates an error at line 6. B. It generates an error at line 7. C. It generates an error at line 8. D. It executes successfully and creates the PRODUCTS table.

  9. UNIQUE

  10. PRIMAEY KEY • 主索引鍵可以是一個或一組欄位。 • 由2個欄位以上組合的主索引鍵,必須在表格層次定義。

  11. Q63/80 Which CREATE TABLE statement is valid? A. CREATE TABLE ord_details (ord_no NUMBER(2) PRIMARY KEY, item_no NUMBER(3) PRIMARY KEY, ord_date date NOT NULL); B. CREATE TABLE ord_details (ord_no NUMBER(2) UNIQUE, NOT NULL, item_no NUMBER(3), ord_date date DEFAULT SYSDATE NOT NULL);

  12. C. CREATE TABLE ord_details (ord_no NUMBER(2) , item_no NUMBER(3), ord_date date DEFAULT NOT NULL, CONSTRAINT ord_uq UNIQUE (ord_no), CONSTRAINT ord_pk PRIMARY KEY (ord_no)); D. CREATE TABLE ord_details (ord_no NUMBER(2), item_no NUMBER(3), ord_date date DEFAULT SYSDATE NOT NULL, CONSTRAINT ord_pk PRIMARY KEY (ord_no, item_no));

  13. Foreign key • 外來鍵:必須為父表格的值或空值

  14. Q92/140 Which two statements are true regarding constraints? (Choose two.) A.A foreign key cannot contain NULL values. B.A column with the UNIQUE constraint can contain NULL. C.A constraint is enforced only for the INSERT operation on a table. D.A constraint can be disabled even if the constraint column contains data. E.All the constraints can be defined at the column level as well as the table level.

  15. Foreign key • 刪除資料列 • On Delete Casecade:刪除父表格資料列時,一併刪除子表格資料列。 • On Delete Set Null:將子表格欄位設為Null

  16. Q71/140 View the Exhibit and examine the structure of ORDERS and ORDER_ITEMS tables. ORDER_ID is the primary key in the ORDERS table. It is also the foreign key in the ORDER_ITEMS table wherein it is created with the ON DELETE CASCADE option. Which DELETE statement would execute successfully? A. DELETE order_id FROM orders WHERE order_total < 1000; B. DELETE orders WHERE order_total < 1000; C. DELETE FROM orders WHERE (SELECT order_id FROM order_items); D. DELETE orders o, order_itemsi WHERE o.order_id = i.order_id;

  17. CHECK

  18. Q24/140 Evaluate the following CREATE TABLE commands: CREATE TABLE orders (ord_no NUMBER(2) CONSTRAINT ord_pk PRIMARY KEY, ord_date DATE, cust_id NUMBER(4)); CREATE TABLE ord_items (ord_no NUMBER(2), item_no NUMBER(3), qty NUMBER(3) CHECK (qty BETWEEN 100 AND 200), expiry_date date CHECK (expiry_date > SYSDATE), CONSTRAINT it_pk PRIMARY KEY (ord_no,item_no), CONSTRAINT ord_fk FOREIGN KEY(ord_no) REFERENCES orders(ord_no)); Why would the ORD_ITEMS table not get created?

  19. A. SYSDATE cannot be used with the CHECK constraint. B. The CHECK constraint cannot be used twice for the same table. C. The BETWEEN clause cannot be used for the CHECK constraint. D.ORD_NO and ITEM_NO cannot be used as a composite primary key because ORD_NO is also the FOREIGN KEY.

  20. 用子查詢建立表格 • 欄位數量必須與子表格數量一樣 • 新表格只會繼承資料類型定義,不會繼承完整規則。

  21. Q77/140 View the Exhibit and examine the structure of the ORDERS table. The ORDER_ID column is the PRIMARY KEY in the ORDERS table. Evaluate the following CREATE TABLE command: CREATE TABLE new_orders (ord_id, ord_date DEFAULT SYSDATE, cust_id) AS SELECT order_id,order_date,customer_id FROM orders; Which statement is true regarding the above command?

  22. CREATE TABLE new_orders (ord_id, ord_date DEFAULT SYSDATE, cust_id) AS SELECT order_id,order_date,customer_id FROM orders; A.The NEW_ORDERS table would not get created because the DEFAULT value cannot be specified in the column definition. B.The NEW_ORDERS table would get created and only the NOT NULL constraint defined on the specified columnswould be passed to the new table. (P9-32) C.The NEW_ORDERS table would not get created because the column names in the CREATE TABLE command and the SELECT clause do not match. D.The NEW_ORDERS table would get created and all the constraints defined on the specified columns in the ORDERS table would be passed to the new table.

  23. Q6/140 Which three statements are true? (Choose three.) A. Only one LONG column can be used per table. B. A TIMESTAMP data type column stores only time values with fractional seconds. C. The BLOB data type column is used to store binary data in an operating system file. D. The minimum column width that can be specified for a varchar2 data type column is one. E. The value for a CHAR data type column is blank-padded to the maximum defined column width.

  24. Q44/140 You need to create a table with the following column specifications: 1. Employee ID (numeric data type) for each employee 2. Employee Name, (character data type) which stores the employee name 3. Hire date, to store the date when the employee joined the organization 4. Status (character data type). It should contain the value ACTIVE if no data is entered. 5. Resume (character large object [CLOB] data type), which would contain the resume submitted by the employee Which is the correct syntax to create this table?

  25. A.CREATE TABLE EMP_1 (emp_id NUMBER(4), emp_name VARCHAR2(25), start_date DATE, e_status VARCHAR2(10) DEFAULT 'ACTIVE', resume CLOB(200)); B.CREATE TABLE 1_EMP (emp_id NUMBER(4), emp_name VARCHAR2(25), start_date DATE, emp_status VARCHAR2(10) DEFAULT 'ACTIVE', resume CLOB); C.CREATE TABLE 1_EMP (emp_id NUMBER(4), emp_name VARCHAR2(25), start_date DATE, emp_status VARCHAR2(10) DEFAULT "ACTIVE", resume CLOB); D.CREATE TABLE EMP_1 (emp_id NUMBER, emp_name VARCHAR2(25), start_date DATE, emp_status VARCHAR2(10) DEFAULT 'ACTIVE', resume CLOB);

More Related