Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS 4200 - Spring 2015 Wednesday 1/28/2015 Chapter 3A.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Virtual training week 4 structured query language (SQL)
Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.
Introduction to Structured Query Language (SQL)
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.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Creating Database Tables © Abdou Illia MIS Spring /21/2015.
Introduction to Structured Query Language (SQL)
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Oracle Data Definition Language (DDL)
Guide to Oracle10G1 Using SQL Queries to Insert, Update, Delete, and View Data Chapter 3.
Oracle for Software Developers. What is a relational database? Data is represented as a set of two- dimensional tables. (rows and columns) One or more.
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
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.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
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.
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 structure and space Management. Database Structure An ORACLE database has both a physical and logical structure. By separating physical and logical.
ITBIS373 Database Development Lecture 2 - Chapter 2 Creating And Modifying Database Tables.
ITBIS373 Database Development Lecture 3a - Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database structure and space Management. Segments The level of logical database storage above an extent is called a segment. A segment is a set of extents.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. Introduction.
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.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
1 Creating and Maintaining Database Objects Part 1 Database Systems.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
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.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Chapter 12 Additional Database Objects. Chapter Objectives  Define the purpose of a sequence and state how it can be used by an organization  Explain.
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.
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Enhanced Guide to Oracle 10g
Database structure and space Management
SQL Creating and Managing Tables
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Chapter 5 Sequences.
Manipulating Data.
Contents Preface I Introduction Lesson Objectives I-2
Chapter 2: Creating And Modifying Database Tables
Presentation transcript:

Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS Spring 2015 Wednesday 1/28/2015 Chapter 3A

2 Lesson 3A Objectives You should know how 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  Create and use sequences to generate surrogate key values automatically

3 Using Scripts to Create Database Tables  SQL Script –Text file that contains one or more SQL statements –Contains only SQL statements –File extension must be.sql  Run a script –At SQL prompt, type one of the following:  start path\filemane path\filemane –Example: start oralab00.sql F:\MIS4200\script1.sql CREATE TABLE location (loc_id NUMBER(5), bldg_code NUMBER(3) room VARCHAR2(20); DESCRIBE location ALTER TABLE location ADD (capacity NUMBER(5); Script1.sql

4 Using the INSERT Command  Basic syntax for inserting data into every column: INSERT INTO tablename VALUES ( column1_value, column2_value, … ); –Must list values in same order as in CREATE TABLE –If a data value is unknown, must type NULL –If character data, must use single quotation marks –Value in quotations is case sensitive  Basic syntax for inserting into selected columns INSERT INTO tablename ( columnname1, columnname2, … ) VALUES ( column1_value, column2_value, … ); Note: To specify 454 St. John’s Place, must type '454 St. John ''s Place' Question: If you couldn’t remember the columns’ order for the table you want to insert data in, what command can you use in SQL Plus to verify? INSERT INTO student VALUES (‘JO100’, ‘Jones’, ‘Tammy’, ‘R’, ‘1817 Eagleridge Circle’, ‘Tallahassee’, ‘FL’, ‘32811’, ‘ ’, ‘SR’, TO_DATE(‘07/14/1984’,‘MM/DD/YYYY’), ‘8891’, 1, TO_YMINTERVAL(‘3-2’)); Example

5 Using the INSERT Command (cont.)  Ensure all foreign keys that new row references have already been added to database.  Cannot insert a foreign key value unless the corresponding primary key is in the primary table.

6 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. With the $ mask, appears as $ With the $ mask, how will appear 1500? ?

7 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') –Example: TO_DATE ('08/24/2010', 'MM/DD/YYYY’)  Inserting values into INTERVAL columns –Syntax TO_YMINTERVAL('years-months') TO_DSINTERVAL('days HH:MI:SS.99') INSERT INTO student VALUES (‘JO100’, ‘Jones’, ‘Tammy’, ‘R’, ‘1817 Eagleridge Circle’, ‘Tallahassee’, ‘FL’, ‘32811’, ‘ ’, ‘SR’, TO_DATE(‘07/14/1984’,‘MM/DD/YYYY’), ‘8891’, 1, TO_YMINTERVAL(‘3-2’)); Example

8 Inserting LOB Column Locators  Oracle stores LOB data in separate (alternate) physical location from other types of data  LOB locator needs to be created to –Hold information that identifies LOB data type, and –Point to alternate memory location  Syntax for creating blob locator EMPTY_BLOB() INSERT INTO faculty (f_id, f_last, f_first, f_image) VALUES (2, ‘Zhulin’, ‘Mark’, EMPTY_BLOB());

9 Creating Transactions and Committing New Data  Transaction –Represents logical unit of work (or action queries) –All of action queries must succeed or no transaction can succeed  When a problem occurs and prevents some queries in a transaction to succeed, Oracle allows you rollback  Rollback –Discard changes in transaction using ROLLBACK  Commit –Save changes in transaction using COMMIT

10 Creating Transactions & Committing Data (cont)  Purpose of transaction processing –Enable users to see consistent view of database –Preventing users from viewing or updating data that are part of a pending (uncommitted) transaction  New transaction begins when SQL*Plus started and command executed  Transaction ends when current transaction committed  ROLLBACK command restores database to point before last commit

11 Rollback and Savepoints  Savepoints are used to rollback transactions to a certain point.

12 Creating Search Conditions in SQL Queries  Search condition –Expression that seeks to match specific table rows  Syntax WHERE columnname comparison_operator search_expression  Example: DELETE FROM student WHERE s_id = ‘JO100’

13 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') Creating Complex Search Conditions  Complex search condition c ombines multiple search conditions using logical operators  AND logical operator: True if both conditions true  OR logical operator: True if one condition true  NOT logical operator: Matches opposite of search condition WHERE bldg_code = ‘CR’ AND capacity > 50 Example

14 Updating Table Rows  UPDATE action query syntax UPDATE tablename SET column1 = new_value1, column2 = new_value2, … WHERE search condition; Question: In a previous class session, we learned about the ALTER TABLE command. What is the difference between the ALTER TABLE and the UPDATE commands?

15 Deleting Table Rows  The DELETE action query removes specific rows  Syntax: DELETE FROM tablename WHERE search condition;  The TRUNCATE action query removes all rows –TRUNCATE TABLE tablename;  Cannot truncate table with foreign key constraints –Must disable constraints, first, using ALTER TABLE tablename DISABLE CONSTRAINT constraint_name;

16 Deleting Table Rows (continued)  Child row: a row containing a value as foreign key –Cannot delete row if it has child row. In other words, you cannot delete a “parent” row … Unless you, first, delete row in which foreign key value exists –Cannot delete LOCATION row for loc_id = 9 unless you delete FACULTY row for f_id = 1 FACULTY F_IDF_LASTF_FIRSTF_MILOC_ID 1MarxTeresaI9 LOCATION LOC_IDBLDG_CODEROOMCAPACITY 9BUS4241 Child row “Parent” row

17 Creating New Sequences  A sequence is a series of number like 1, 2, 3, …  A sequence can be created as a database object  CREATE SEQUENCE is used to create a sequence –CREATE SEQUENCE is a DDL command –No need to issue COMMIT command because (it’s a DDL command)  Example: CREATE SEQUENCE loc_id_sequence START WITH 20;  CACHE stores 20 sequence numbers by default  CYCLE: when a minimum and a maximum are set, CYCLE allows the sequence to restart from minimum when the maximum is reached.

18 Viewing Sequence Information  The USER_SEQUENCES data dictionary view contains –sequence_name –sequence_minvalue –sequence_maxvalue, etc.  Example (for viewing sequences’ info): SELECT sequence_name, sequence_minvalue FROM user_sequences;

19 Using Sequences  A pseudocolumn –acts like column in database table –is actually a command that returns specific value  CURRVAL –sequence_name.CURRVAL r eturns most recent sequence value retrieved  NEXTVAL –sequence_name.NEXTVAL returns n ext available sequence value INSERT INTO location VALUES (loc__id_sequence.NEXTVAL, ‘CC, ‘105’, 150); Example

20 Using Sequences (continued)  DUAL –Simple table in the 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