Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 54 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
54
Dung lượng
342,16 KB
Nội dung
Manipulating Data
11
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder11Ć2
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.
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
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.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder11Ć10
[...]... “DELETE.” ManipulatingData 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 SQL> SELECT 2 FROM * test; no rows selected ManipulatingData 11Ć31... records, then you receive the “child record found” violation ORA-02292 ManipulatingData 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... 1 row created For date and character values, the ampersand and the variable name are enclosed in single quotation marks ManipulatingData 11Ć15 11Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Adding a New Row to a Table continued Creating a Script to Manipulate Data You can save your command with substitution variables to a file and execute the file Each time you execute the command,... 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 ManipulatingData 11Ć23 11Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Updating Rows continued If you... salary, title, start_date FROM s_emp WHERE start_date < ’01-JAN-94’; 10 rows created The number of columns in the column list of the INSERT clause must match the number of values in the subquery ManipulatingData 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... subqueries, and comparison operators Confirm the update operation by querying the table to display the updated rows For more information, see Oracle7 Server SQL Reference, Release 7.3, “UPDATE.” ManipulatingData 11Ć21 11Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Updating Rows continued Example Transfer employee number 2 to department 10 Transfer employee number 1 to department... 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 ManipulatingData 11Ć13 11Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Adding a New Row to a Table continued Inserting Values by Using Substitution Variables You can produce an INSERT command... 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 ManipulatingData 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... violation ORA-02291 SQL> UPDATE 2 SET 3 WHERE s_emp dept_id = 60 dept_id = 10; update s_emp * ERROR at line 1: ORA-02291: integrity constraint (USR.S_EMP_DEPT_ID_FK) violated - parent key not found ManipulatingData 11Ć27 11Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Deleting Rows You can remove existing rows by using the DELETE command Syntax DELETE [FROM] table [WHERE condition];... 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 ManipulatingData 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 . 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,. Manipulating Data
11
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder11Ć2
Manipulating Data 11Ć3
Objectives
Once