ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Virtual training week 4 structured query language (SQL)
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 Chapter 2: Creating and Modifying Database Tables.
A Guide to Oracle9i1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
1 Chapter 3: Using Oracle to Add, View, and Update Data.
Transaction Sen Zhang. Creating Transactions and Committing New Data Transaction: series of action queries that represent a logical unit of work User.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Creating Database Tables © Abdou Illia MIS Spring /21/2015.
Guide to Oracle 10g1 Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
A Guide to MySQL 7. 2 Objectives Understand, define, and drop views Recognize the benefits of using views Use a view to update data Grant and revoke users’
Introduction to Structured Query Language (SQL)
1 Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS Spring 2015 Wednesday 1/28/2015 Chapter 3A.
Structured Query Language S Q L. What is SQL It is a database programming language developed by IBM in the early 1970’s. It is used for managing and retrieving.
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
Guide to Oracle10G1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
ASP.NET Programming with C# and SQL Server First Edition
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Chapter 6 Additional Database Objects
Introduction to SEQUEL. What is SEQUEL? Acronym for Structural English Query Language Acronym for Structural English Query Language Standard language.
ITBIS373 Database Development
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Quick review of SQL And conversion to Oracle SQL.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
1 Chapter 2: Creating and Modifying Database Objects.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Database Programming Sections 12 – Sequences, Indexes, and Synonymns.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Dr. Chen, Oracle Database System (Oracle) 1 Chapter 7 User Creation and Management Jason C. H. Chen, Ph.D. Professor of MIS School of Business Gonzaga.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
 CONACT UC:  Magnific training   
SQL Basics Review Reviewing what we’ve learned so far…….
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Enhanced Guide to Oracle 10g
SQL Creating and Managing Tables
Manipulating Data.
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Chapter 5 Sequences.
A Guide to SQL, Eighth Edition
Manipulating Data.
Contents Preface I Introduction Lesson Objectives I-2
Presentation transcript:

ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data

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

Guide to Oracle 10g3 Lesson A Objectives (continued) Create and use sequences to generate surrogate key values automatically Grant and revoke database object privileges

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

Guide to Oracle 10g5  Running a script: SQL> START path_to_script_file;  Path cannot contain any blank spaces SQL Scripts

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.

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, … );

Guide to Oracle 10g8 Using the INSERT Command (continued) Ensure all foreign keys that new row references have already been added to database

Guide to Oracle 10g9

10

Guide to Oracle 10g11 Inserting Selected Table Fields

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.

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')

Guide to Oracle 10g14 Inserting Date Values

Guide to Oracle 10g15

Guide to Oracle 10g16

Guide to Oracle 10g17

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

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.

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

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

Guide to Oracle 10g22

Guide to Oracle 10g23  Used to mark individual sections of a transaction  You can roll back a transaction to a savepoint Savepoint

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

Guide to Oracle 10g25

Guide to Oracle 10g26  The general syntax of a SQL search condition is: WHERE columnname comparison_operator search_expression Search Conditions

Guide to Oracle 10g27

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')

Guide to Oracle 10g29

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’

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.

Guide to Oracle 10g32

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

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.

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.

Guide to Oracle 10g36

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

Guide to Oracle 10g38

Guide to Oracle 10g39 Creating New Sequences CREATE SEQUENCE command DDL command No need to issue COMMIT command

Guide to Oracle 10g40 General Syntax Used to Create a New Sequence

Guide to Oracle 10g41

Guide to Oracle 10g42 Viewing Sequence Information Query the SEQUENCE Data Dictionary View:

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

Guide to Oracle 10g44

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

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

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

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

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.

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 Heavy duty day pack

Guide to Oracle 10g51 SQL > Insert into item values(item_sequence.nextval,’mountain parka’,’clothing’) ITEMID ITEMDESC Heavy duty day pack 997 Mountain parka SQL> select itemid_sequence.nextval from dual; Nextval 998

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;

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

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

Guide to Oracle 10g55

Guide to Oracle 10g56

Guide to Oracle 10g57