Download presentation
Presentation is loading. Please wait.
Published byMichael Holt Modified over 9 years ago
1
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
2
Guide to Oracle 10g2 Lesson A Objectives After completing this lesson, you should be able to: Run a script to create database tables automatically Insert data into database tables Create database transactions and commit data to the database Create search conditions in SQL queries Update and delete database records and truncate tables
3
Guide to Oracle 10g3 Lesson A Objectives (continued) Create and use sequences to generate surrogate key values automatically Grant and revoke database object privileges
4
Guide to Oracle 10g4 Using Scripts to Create Database Tables Script Text file that contains one or more SQL commands Run a script Type start at SQL prompt Blank space Full path and filename of script file
5
Guide to Oracle 10g5 Running a script: SQL> START path_to_script_file; Path cannot contain any blank spaces SQL Scripts
6
Guide to Oracle 10g6 Using SQL*PLUS to Insert Data After successfully running the script to create the tables, you already to begin adding data to them. In business setting, programs called forms are used to automate the data entry process. Program developers who create forms often use the SQL INSERT statement in the form program code to insert data into tables.
7
Guide to Oracle 10g7 Using the INSERT Command Basic syntax for inserting into every column: INSERT into tablename VALUES (column1_value, column2_value, … ); Basic syntax for inserting into selected columns INSERT into tablename (columnname1, columnname2, … ); VALUES (column1_value, column2_value, … );
8
Guide to Oracle 10g8 Using the INSERT Command (continued) Ensure all foreign keys that new row references have already been added to database
9
Guide to Oracle 10g9
10
10
11
Guide to Oracle 10g11 Inserting Selected Table Fields
12
Guide to Oracle 10g12 Format Models Also called format mask Used to specify different output format from default For NUMBER data types 9 represents digit For DATE/TIMESTAMP data types Choose formats for year day, date, etc.
13
Guide to Oracle 10g13 Inserting Date and Interval Values Inserting values into DATE columns Use TO_DATE function to convert string to DATE Syntax TO_DATE('date_string', 'date_format_model') Inserting values into INTERVAL columns Syntax TO_YMINTERVAL('years-months') TO_DSINTERVAL('days HH:MI:SS.99')
14
Guide to Oracle 10g14 Inserting Date Values
15
Guide to Oracle 10g15
16
Guide to Oracle 10g16
17
Guide to Oracle 10g17
18
Guide to Oracle 10g18 Creating Transactions and Committing New Data When you create a new table or update the structure of an existing table, the DBMS changes the rows immediately and makes the change visible to other users. This is not the case when you insert, update, or delete data rows. The commands for operations that add, update, or delete data are called action queries. All three action queries need to succeed, or non of them should succeed. After the user enters all of the action queries in a transaction, he or she can either COMMIT (save) all of the changes or ROLL BACK (discard) all of the changes
19
Guide to Oracle 10g19 The purpose of transaction processing is enable every user to see a consistent view of the database. To achieve this consistency, a user cannot view or update data values that are part of another user’s uncommitted transaction because these uncommitted transactions, which are called pending transactions, might be rolled back. The Oracle DBMS implements transaction processing by locking data rows associated with pending transactions. When the DBMS locks a row, other users cannot view or modify the row. When the user commits the transaction, the DBMS releases the lock on the rows, and other users can view and update the rows again.
20
Guide to Oracle 10g20 A transaction starts when you type one or more DML commands in SQL*Plus A transaction ends when you issue either the COMMIT or ROLLBACK command SQL>COMMIT; SQL>ROLLBACK; Transactions
21
Guide to Oracle 10g21 Committing and Rolling Back Data COMMIT Makes transaction command changes permanent in the database and visible to other users ROLLBACK Rolls back transaction command changes and restores database to its state before the transaction
22
Guide to Oracle 10g22
23
Guide to Oracle 10g23 Used to mark individual sections of a transaction You can roll back a transaction to a savepoint Savepoint
24
Guide to Oracle 10g24 Syntax: UPDATE tablename SET column1 = new_value, column2 = new_value, … WHERE search_condition; Records can be updated in only one table at a time Can update multiple records if they all match the search condition Updating Records
25
Guide to Oracle 10g25
26
Guide to Oracle 10g26 The general syntax of a SQL search condition is: WHERE columnname comparison_operator search_expression Search Conditions
27
Guide to Oracle 10g27
28
Guide to Oracle 10g28 Defining Search Expressions NUMBER example WHERE f_id = 1 Character data example WHERE s_class = 'SR' DATE example WHERE s_dob = TO_DATE('01/01/1980', ‘MM/DD/YYYY')
29
Guide to Oracle 10g29
30
Guide to Oracle 10g30 Creating Complex Search Conditions A complex search condition combines multiple search conditions using the AND,OR, and NOT logical operators. EX The following complex search condition matches all rows in which BLDG_CODE is ‘CR’ and the capacity is greater than 50: WHERE bldg_code =‘CR’ AND capacity >50 The following search condition matches all course section rows that meet either on Tuesday and Thursday or on Monday, Wednesday, and Friday (at Northwoods University): WHERE day=‘MW’ OR day =‘UTH’
31
Guide to Oracle 10g31 Deleting Table Rows You use the SQL DELETE action to remove specific rows from a database table, and you truncate the table to remove all of the table rows. The SQL DELETE Action Query The general syntax for DELETE action query is: DELETE FROM tablename WHERE search condition; EX In the following set of steps, you delete Tammy Jones from the STUDENT table. You specify ‘Tammy’ and ‘Jones’ in the action query’s search condition.
32
Guide to Oracle 10g32
33
Guide to Oracle 10g33 Deletes multiple records if search condition specifies multiple records If search condition is omitted, all table records are deleted You can’t delete a record if it contains a primary key value that is referenced as a foreign key Deleting Records
34
Guide to Oracle 10g34 Truncating Tables When you need to delete all of the rows in a table quickly, you can truncate the table, which means you remove all of the table data without saving any rollback information. TRUNCATE TABLE tablename; You cannot truncate a table that has foreign key constraints enabled.
35
Guide to Oracle 10g35 Ex: Ex: You truncate the LOCATION table to delete all of its rows. Recall that the LOC_ID column in the LOCATION table is a foreign key in both the FACULTY table and the COURSE_SECTION table, so you must first disable the LOC_ID foreign key constraints in the FACULTY and COOURSE_SECTION tables. Then you truncate the LOCATION table.
36
Guide to Oracle 10g36
37
Guide to Oracle 10g37 Sequential list of numbers that is automatically generated by the database Used to generate values for primary key identifier Has no real relationship to row to which it is assigned other than to identify it uniquely Surrogate key values automatically generated using a sequence. Sequences
38
Guide to Oracle 10g38
39
Guide to Oracle 10g39 Creating New Sequences CREATE SEQUENCE command DDL command No need to issue COMMIT command
40
Guide to Oracle 10g40 General Syntax Used to Create a New Sequence
41
Guide to Oracle 10g41
42
Guide to Oracle 10g42 Viewing Sequence Information Query the SEQUENCE Data Dictionary View:
43
Guide to Oracle 10g43 Using Sequences CURRVAL Returns most recent sequence value retrieved during the current user session. NEXTVAL Next available sequence value sequence_name.NEXTVAL
44
Guide to Oracle 10g44
45
Guide to Oracle 10g45 Using Sequences (continued) DUAL Simple table in system user schema More efficient to retrieve pseudocolumns from DUAL SELECT sequence_name.NEXTVAL FROM DUAL; DBMS uses user sessions To ensure that all sequence users receive unique sequence numbers
46
Guide to Oracle 10g46 Pseudocolumns Acts like a column in a database query Actually a command that returns a specific values Used to retrieve: Current system date Name of the current database user Next value in a sequence
47
Guide to Oracle 10g47 Pseudocolumn Examples Pseudocolumn Name Output CURRVALMost recently retrieved sequence value NEXTVALNext value in a sequence SYSDATECurrent system date from database server USERUsername of current user
48
Guide to Oracle 10g48 - Retrieving the current system date : SELECT SYSDATE FROM DUAL; - Retrieving the name of the current user: SELECT USER FROM DUAL; - DUAL is a system table that is used with pseudocolumns Using Pseudocolumns
49
Guide to Oracle 10g49 EX To create the ITEMID_SEQUENCE SQL> CREATE SEQUENCE itemid_sequence START WITH 996 NOMAXVALUE NOCACHE; sequence created To access the next value in a sequence and use that value when you insert a new data record, use the following general command: INSERT INTO VALUES( _.NEXTVAL,,,… This command assumes that the primary key associated with the sequence is the first table field.
50
Guide to Oracle 10g50 SQL> INSERT INTO item VALUES(itemid_sequence.NEXTVAL, ’Heavey duty day pack’, ‘outdoor gear’); SQL> Select itemid, itemdesc From item; ITEMID ITEMDESC 894 ------- 897 ------- 559 ------- 786 ------ 996 Heavy duty day pack
51
Guide to Oracle 10g51 SQL > Insert into item values(item_sequence.nextval,’mountain parka’,’clothing’) ITEMID ITEMDESC 894 ------- 897 ------- 559 ------- 786 ------ 996 Heavy duty day pack 997 Mountain parka SQL> select itemid_sequence.nextval from dual; Nextval 998
52
Guide to Oracle 10g52 Deleting Sequences To delete a sequence from the database, you use the DROP SEQUENCE DDL command. Ex: to drop LOC_ID_SEQUENCE, you use the command DROP SEQUENCE loc_id_sequence;
53
Guide to Oracle 10g53 Permissions that you can grant to other users to allow them to access or modify your database objects Granting object privileges: GRANT privilege1, privilege2, … ON object_name TO user1, user 2, …; Revoking object privileges: REVOKE privilege1, privilege2, … ON object_name FROM user1, user 2, …; Object Privileges
54
Guide to Oracle 10g54 Examples of Object Privileges PrivilegeDescription ALTERAllows user to change object’s structure using the ALTER command DROPAllows user to drop object SELECTAllows user to view object INSERT, UPDATE, DELETE Allows user to insert, update, delete table data ALLAllows user to perform any operation on object
55
Guide to Oracle 10g55
56
Guide to Oracle 10g56
57
Guide to Oracle 10g57
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.