Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 48 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
48
Dung lượng
1,21 MB
Nội dung
A Guide to Oracle9i 1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3 A Guide to Oracle9i 2 Lesson A Objectives • Learn how to run a script to create database tables automatically • Learn how to insert data into database tables • Learn how to create database transactions and commit data to the database • Create search conditions in SQL queries • Understand how to update and delete database records, and how to truncate tables • Learn how to create and use sequences to generate surrogate key values automatically • Learn how to grant and revoke database object privileges A Guide to Oracle9i 3 Using Scripts to Create Database Tables • One or more SQL commands saved in a text file • Usually have .sql extension • To run from SQL*Plus: – Start full file path (c:\temp\myfile.sql) – @full file path (@c:\temp\myfile.sql) – Extension can be omitted if it is .sql A Guide to Oracle9i 4 Inserting Data into Tables • INSERT command adds new records • Field values should match column order, or be specified in command • INSERT INTO faculty (F_ID, F_LAST, F_FIRST, F_MI, LOC_ID) VALUES (1, 'Cox', 'Kim', 'J', 9); A Guide to Oracle9i 5 Format Models • Used to format data retrieved from database • Can be used to format a date to display time or a number to display as a currency A Guide to Oracle9i 6 Numerical Format Models A Guide to Oracle9i 7 Date Format Models A Guide to Oracle9i 8 Date Format Models A Guide to Oracle9i 9 Inserting Date and Interval Values • Use to_date function to convert a character string to a date – Specify date string and matching format model – TO_DATE('08/24/2004', 'MM/DD/YYYY') – TO_DATE('10:00 AM', 'HH:MI AM') • Use functions to convert character strings to intervals – TO_YMINTERVAL('4-9') inserts a positive interval of 4 years, 9 months – TO_DSINTERVAL('0 01:15:00') inserts a positive interval of 4 days, 1 hour, 15 minutes, 0 seconds A Guide to Oracle9i 10 Inserting LOBs • Before inserting LOB must insert a LOB locator • LOB locator: a structure that contains information that identifies the LOB data type and points to the alternate memory location • Write a program or use a utility to add LOB data to database • Use EMPTY_BLOB() function to insert a LOB locator [...]... object_name FROM user1, user2, ; To grant or revoke privileges for everyone use PUBLIC as user A Guide to Oracle9i 21 Lesson B Objectives • Learn how to write SQL queries to retrieve data from a single database table • Create SQL queries that perform calculations on retrieved data • Use SQL group functions to summarize retrieved data A Guide to Oracle9i 22 Retrieving Data from a Single Database Table • • • SELECT... and pagesize to desired values A Guide to Oracle9i 29 Formatting Numbers and Dates • Use to_ char function with format models • TO_ CHAR(field_name, 'format_model') • SELECT inv_id, TO_ CHAR(inv_price, '$99,999.99') FROM inventory WHERE item_id = 1; A Guide to Oracle9i 30 Lesson C Objectives • Learn how to create SQL queries that join multiple tables • Learn how to create nested SQL queries • Understand... perform calculations and manipulate retrieved data values A Guide to Oracle9i 25 Oracle9i SQL Group Functions • Group functions: perform an operation on a group of queried rows and returns a single result A Guide to Oracle9i 26 Group By • Use to group output by the field with duplicate values and apply group functions to the grouped data A Guide to Oracle9i 27 Having • • • • Use to place a search condition... Roll Back in SQL* Plus • • • Transactions begin automatically with first command Type COMMIT to commit changes Type ROLLBACK to roll back changes A Guide to Oracle9i 12 Savepoints • • A bookmark that designates the beginning of an individual section of a transaction Changes are rolled back to savepoint A Guide to Oracle9i 13 Creating Search Conditions in SQL Queries • An expression that seeks to match specific... Learn how to create nested SQL queries • Understand how to combine query results using set operators • Create and use database views A Guide to Oracle9i 31 Joining Multiple Tables • • • • • Join: combine data from multiple database tables using foreign key references SELECT field1, field2, FROM table1, table2 WHERE table1.joinfield = table2.joinfield AND search_condition(s); If tables share field names,... in SELECT, UPDATE and DELETE statements • WHERE fieldname comparison_operator search_expression • WHERE S_ID = 1 A Guide to Oracle9i 14 Comparison Operators A Guide to Oracle9i 15 Defining Search Expressions • Character strings – Must be enclosed in single quotes – Case sensitive • Dates – Use to_ date function with date string and format model • Intervals – Use to_ yminterval and to_ dsinterval with... to_ dsinterval with interval string format model A Guide to Oracle9i 16 Creating Complex Search Conditions • Combines multiple search conditions using the AND, OR, and NOT logical operators • AND – both conditions must be true • OR – one or both condition must be true • NOT – opposite of actual value • Use () to group logical operators A Guide to Oracle9i 17 Updating and Deleting Existing Table Records • • UPDATE:... ascending, use DESC to sort descending A Guide to Oracle9i 24 Using Calculations in SQL Queries • • • • • Calculations are performed by DBMS, result only sent to client Can use arithmetic operators (+, -, *, /) Place calculation in select clause: SELECT price * quantity FROM … Calculations can be performed on NUMBER, DATE and INTERVAL fields only Single-row functions: built in Oracle functions to perform calculations... Search conditions can be added to join condition using AND operator A Guide to Oracle9i 32 Inner Join • Join two tables based on values in one table being equal to values in another table • Also known as equality join, equijoin or natural join • Returns results only if records exist in both tables A Guide to Oracle9i 33 Joining Via Linking Table A Guide to Oracle9i 34 Using a Query Design Diagram •... ownername.tablename WHERE search_condition; To select: – All rows omit where clause – All fields, use *: SELECT * FROM … – Only unique field values: SELECT DISTINCT fieldname Search condition: – Use comparison and logical operators – IS NULL/IS NOT NULL to match/exclude NULL values – IN/NOT IN to match set values – LIKE with wildcards % and _ to match character strings A Guide to Oracle9i 23 Sorting Query Output . Guide to Oracle9i 1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3 A Guide to Oracle9i 2 Lesson A Objectives • Learn how to run a script to create database tables automatically • Learn. how to insert data into database tables • Learn how to create database transactions and commit data to the database • Create search conditions in SQL queries • Understand how to update and delete. update and delete database records, and how to truncate tables • Learn how to create and use sequences to generate surrogate key values automatically • Learn how to grant and revoke database object