1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Manipulating data docx

54 248 0

Đ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

Manipulating Data 11 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder11Ć2 Schedule: Timing Topic 45 minutes Lecture 40 minutes Practice 85 minutes Total Class Management Note: Files required for this lesson are: Demonstration: l11sel.sql, l11upd.sql Practice: None Manipulating Data 11Ć3 Objectives Once your tables have been created, you will need to add new rows, make changes to rows in a table, or delete rows by using data manipulation commands. This lesson covers using SQL commands to make changes to data. A number of these data manipulation commands make up a transaction, which you may either save or delete using transaction controls. At the end of this lesson, you should be able to D Insert new rows into a table. D Update existing rows in a table. D Delete rows from a table. D Explain transaction controls and their importance. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder11Ć4 Manipulating Data 11Ć5 Overview Data manipulation language (DML) is a core part of SQL. When you want to add, update, or delete data in the database, you execute a DML statement. A collection of DML statements that have not yet been made permanent is called a transaction, or a logical unit of work. Technical Note: The only way to manipulate data within the Oracle database is by means of the data manipulation SQL statements listed here. The statements can be issued directly in SQL*Plus or SQL*DBA, performed automatically by tools such as Developer/2000 and SQL*Loader, or programmed with tools such as the 3GL Precompilers. There is one exception: SQL*Loader has a Direct Mode option for the Oracle7 Server that loads rows into the database without using the INSERT statement. Every table has INSERT, UPDATE, and DELETE privileges associated with it. These privileges are automatically granted to the creator of the table, but in general they must be explicitly granted to other users. A new feature to Oracle 7.2 is that you can place a subquery in the place of the table name, essentially the same way a view is used. For example, UPDATE (SELECT * FROM s_dept) SET id = 50 WHERE id = 60; Introduction to Oracle: SQL and PL/SQL Using Procedure Builder11Ć6 Manipulating Data 11Ć7 Adding a New Row to a Table You can add new rows to a table by issuing the INSERT command. Syntax INSERT INTO table [(column [, column .])] VALUES (value [, value .]); where: table is the table name. column is the name of the column in the table to populate. value is the corresponding value for the column. Note: This command with the VALUES clause adds only one row at a time to a table. Inserting a Row into a Table Because you can insert a new row that contains values for each column, therefore the column list is not required in the INSERT clause. However, the values must be listed according to the default order of the columns in the table. SQL> DESCRIBE s_dept Name Null? Type ------------------------- -------- -------------- ID NOT NULL NUMBER(7) NAME NOT NULL VARCHAR2(25) REGION_ID NUMBER(7) SQL> INSERT INTO s_dept 2 VALUES (11, ’Finance’, 2); 1 row created. For clarity, use the column list in the INSERT clause. Enclose character and date values within single quotation marks; do not enclose numeric values within single quotation marks. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder11Ć8 Class Management Note: Common errors that can occur during user input: 1.Mandatory value missing for a NOT NULL column 2.Duplicate value violates uniqueness constraint 3.Foreign key constraint violated 4.CHECK constraint violated 5.Datatype mismatch 6.Value too wide to fit in column Manipulating Data 11Ć9 Adding a New Row to a Table continued Inserting Null Values Method Description Implicit Omit the column from the column list. Explicit Specify the NULL keyword in the VALUES list. Specify the empty string (‘’) in the VALUES list; for character strings and dates only. Example Enter a new department omitting the region number. Because the region number is not listed in the INSERT clause, a null value is entered implicitly for the region number in this row. SQL> INSERT INTO s_dept (id, name) 2 VALUES (12, ’MIS’); 1 row created. Example Alternatively, you can enter a null value into a row explicitly by using the NULL keyword for the value. SQL> INSERT INTO s_dept 2 VALUES (13, ’Administration’, NULL); 1 row created. Be sure that the targeted column allows null values by verifying the Null? status from the SQL*Plus DESCRIBE command. Technical Note: The Oracle7 Server automatically enforces all datatypes, data ranges, and data integrity constraints. Any column that is not listed explicitly obtains a null value in the new row. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder11Ć10 [...]... selected row and its children are deleted from their respective tables Manipulating Data 11Ć33 11Ć34 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Transaction Processing The Oracle7 Server ensures data consistency based on transactions Transactions give you more flexibility and control when changing data, and assure data consistency in the event of user process failure or system failure... 10; update s_emp * ERROR at line 1: ORA-02291: integrity constraint (USR.S_EMP_DEPT_ID_FK) violated - parent key not found Manipulating Data 11Ć27 Class Management Note: The DELETE statement does not ask for confirmation However, the delete operation is not made permanent until the data transaction is committed Therefore, you can undo the operation with the ROLLBACK command if you make a mistake 11Ć28... deleted For more information, see Oracle7 Server SQL Reference, Release 7.3, “DELETE.” Manipulating Data 11Ć29 11Ć30 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Deleting Rows continued If you do not include a WHERE clause in your DELETE command, all rows in the table will be deleted Example Eliminate all data from the TEST table SQL> DELETE FROM test; 25,000 rows deleted Confirm the deletions... 1 row updated SQL> UPDATE 2 SET 3 WHERE s_emp dept_id = 32, salary = 2550 id = 1; 1 row updated Confirm both data changes SQL> SELECT 2 FROM 3 WHERE ID 2 1 id, last_name, salary, dept_id s_emp id IN (1,2); LAST_NAME SALARY DEPT_ID - Ngao 1450 10 Velasquez 2550 32 Manipulating Data 11Ć23 11Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Updating Rows continued If you... columns in the column list of the INSERT clause must match the number of values in the subquery Class Management Note: Please do not get into too many details on copying rows from another table Manipulating Data 11Ć19 11Ć20 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Updating Rows You can modify existing rows by using the UPDATE command Syntax UPDATE SET [WHERE where: table table... columns may unexpectedly cause several rows to be updated For example, identifying a single row in the S_EMP table by last name is dangerous because several employees may have the same last name Manipulating Data 11Ć21 Class Management Note: DEMO: l11sel.sql PURPOSE: Show the students the initial values DEMO: l11upd.sql PURPOSE: Perform the updates and view the results 11Ć22 Introduction to Oracle:... SELECT id, last_name, first_name, 2 userid, start_date, salary 3 FROM s_emp 4 WHERE id = 26; ID LAST_NAME FIRST_NAME USERID START_DAT - SALARY -26 Smith Donna SFCL26 01-JAN-96 Manipulating Data 11Ć11 11Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Adding a New Row to a Table continued Inserting Specific Date and Time Values When inserting a date value, the format... the changes SQL> UPDATE 2 SET s_emp commission_pct = 10; 25 rows updated SQL> SELECT 2 FROM id, commission_pct s_emp; ID COMMISSION_PCT - -1 10 2 10 3 10 4 10 5 10 6 10 25 rows selected Manipulating Data 11Ć25 11Ć26 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Updating Rows continued Integrity Constraint Error If you attempt to update a record with a value that is tied to... salary, start_date) 3 VALUES (26, ’Donna’, 4 ’Smith’, USER, NULL, 5 TO_DATE(’01-JAN-96 08:00’, 6 ’DD-MON-YY HH:MI’)); 1 row created If the RR format is set, the century may not be the current one Manipulating Data 11Ć13 Class Management Note: Be sure to mention the following points about the example: 1 The names of the SQL*Plus substitution parameters do not have to match the corresponding column names... 61 Enter value for department_name: Accounting Enter value for region_id: 2 1 row created For date and character values, the ampersand and the variable name are enclosed in single quotation marks Manipulating Data 11Ć15 Class Management Note: Be sure to mention the following points about the script: 1 Do not prefix the SQL*Plus substitution parameter with the ampersand in the ACCEPT command 2 Use a dash . Builder11Ć4 Manipulating Data 11Ć5 Overview Data manipulation language (DML) is a core part of SQL. When you want to add, update, or delete data in the database,. delete rows by using data manipulation commands. This lesson covers using SQL commands to make changes to data. A number of these data manipulation commands

Ngày đăng: 21/12/2013, 06:17

Xem thêm: Tài liệu Manipulating data docx

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN