CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.

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

SQL2-ch2 管理綱要物件.
Data Definition Language (DDL)
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Virtual training week 4 structured query language (SQL)
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
9-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language A DML statement is executed when you: – Add new rows to a table.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
Dale Roberts 1 Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
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.
Oracle8 - The Complete Reference. Koch & Loney1 Chapter 17 Creating, Dropping, and Altering Tables and Views Presented by Victor Matos.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Introduction to Structured Query Language (SQL)
Oracle Data Definition Language (DDL)
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.
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.
Creating a Table Create a table, “emp”, containing: –empno – a 4 digit employee number –ename – up to 10 character string –job – up to 9 character string.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
SQL FUNDAMENTALS SQL ( Structured Query Language )
DATA MANIPULATION andCONTROL
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
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.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
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.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Relational Database Management System(RDBMS) Structured Query Language(SQL)
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Including Constraints
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Interacting with the Oracle Server
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
(SQL) Manipulating Data
SQL Statements SELECT INSERT UPDATE DELETE CREATE ALTER DROP RENAME
Oracle Data Definition Language (DDL)
Presentation transcript:

CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows you to insert a row or rows into tables or views. –Syntax1: INSERT INTO tablename / viewname ( column1, column2,... ) VALUES ( value1, value2,... ) –Syntax2: INSERT INTO tablename / viewname ( column1, column2,... ) subquery –Column names are optional, but then the values must be in order of the columns.

CSCI N311: Oracle Database Programming 5-2 Chapter 15: Changing Data: insert, update, delete Examples: –The following statement inserts a row into the DEPT table: INSERT INTO dept VALUES (50, ’PRODUCTION’, ’SAN FRANCISCO’) –The following statement inserts a row with six columns into the EMP table. One of these columns is assigned NULL and another is assigned a number in scientific notation: INSERT INTO emp (empno, ename, job, sal, comm, deptno) VALUES (7890, ’JINKS’, ’CLERK’, 1.2E3, NULL, 40) The following statement has the same effect: INSERT INTO (select empno, ename, job, sal, comm, deptno from emp) VALUES (7890, ’JINKS’, ’CLERK’, 1.2E3, NULL, 40) The following statement copies managers and presidents or employees whose commission exceeds 25% of their salary into the BONUS table: INSERT INTO bonus SELECT ename, job, sal, comm FROM emp WHERE comm > 0.25 * sal OR job IN (’PRESIDENT’, ’MANAGER’)

CSCI N311: Oracle Database Programming 5-3 Chapter 15: Changing Data: insert, update, delete Rollback / Commit: –These commands allow you to reverse changes, or make your changes permanent. –Example from book:

CSCI N311: Oracle Database Programming 5-4 Chapter 15: Changing Data: insert, update, delete Notes about Commit : –No special privileges are required for you to commit your work. –When sqlplus terminates or any other program which save data to Oracle, an implicit commit is issued for any open transactions. –Transactions begin when: –a SQL statement is first issued, –immediately after a commit, –immediately after a rollback. –Savepoint allows you to rollback to a specific point in a transaction. Example: UPDATE emp SET sal = 2000 WHERE ename = ’BLAKE’ SAVEPOINT blake_sal UPDATE emp SET sal = 1500 WHERE ename = ’CLARK’ SAVEPOINT clark_sal SELECT SUM(sal) FROM emp ROLLBACK TO SAVEPOINT blake_sal UPDATE emp SET sal = 1300 WHERE ename = ’CLARK’ COMMIT

CSCI N311: Oracle Database Programming 5-5 Chapter 15: Changing Data: insert, update, delete Delete: –The following statement deletes all rows from a table named TEMP_ASSIGN. DELETE FROM temp_assign –The following statement deletes from the employee table all sales staff who made less than $100 commission last month: DELETE FROM emp WHERE JOB = ’SALESMAN’ AND COMM < 100 –The following statement has the same effect: DELETE FROM (select * from emp) WHERE JOB = ’SALESMAN’ AND COMM < 100

CSCI N311: Oracle Database Programming 5-6 Chapter 15: Changing Data: insert, update, delete Update: –The following statement gives null commissions to all employees with the job TRAINEE: UPDATE emp SET comm = NULL WHERE job = ’TRAINEE’ –The following statement promotes JONES to manager of Department 20 with a $1,000 raise (assuming there is only one JONES): UPDATE emp SET job = ’MANAGER’, sal = sal , deptno = 20 WHERE ename = ’JONES’

CSCI N311: Oracle Database Programming 5-7 –Set the number of sets won to zero for all competitors resident in Stratford. UPDATE MATCHES SET WON = 0 WHERE PLAYERNO IN ( SELECT PLAYERNO FROM PLAYERS WHERE TOWN = ‘Stratford’) –Set all penalties lower than average to $10. UPDATE PENALTIES SET AMOUNT = 10 WHERE AMOUNT < ( SELECT AVG(AMOUNT) FROM PENALTIES) –Sets # of sets won and lost for match 2 same as match 1. UPDATE MATCHES SET ( WON, LOST ) = ( SELECT WON, LOST FROM MATCHES FROM MATCHES WHERE MATCHNO = 1 ) WHERE MATCHNO = 2 Additional UPDATE Examples

CSCI N311: Oracle Database Programming 5-8 Chapter 15: Changing Data: insert, update, delete Notes for Insert / Update / Delete: –For you to insert / update / delete values in a table, the table must be in your own schema or you must have INSERT / UPDATE / DELETE privilege on the table. –For you to insert / update / delete values in the base table of a view, the owner of the schema containing the view must have INSERT / UPDATE / DELETE privilege on the base table. Also, if the view is in a schema other than your own, you must have INSERT / UPDATE / DELETE privilege on the view. –You cannot insert / update / delete rows in a view if the view’s defining query contains one of the following constructs: –join –set operator –GROUP BY clause –group function –DISTINCT operator

CSCI N311: Oracle Database Programming 5-9 Update and Delete Questions –Increase all penalties which are higher than the average by 20%. –Update player #95’s data to reflect the fact that he has moved to the same address (street and town) as player #6. –Delete all players who live in the same town as player #44, but keep the data about player #44.

CSCI N311: Oracle Database Programming 5-10 Chapter 18: Creating, Dropping, and Altering Tables and Views Creating a Table: –Basic Syntax: CREATE TABLE tablename ( column_atype NOT NULL, column_btype,...); –Data Types: –CHAR( n ) character data of fixed size up to 2000 characters. –VARCHAR2( n ) variable length character data up to 4000 characters. –NUMBER integer data –NUMBER( n ) integer data, length n –NUMBER( m,n ) floating point data, m = total length of digits, n = digits after decimal point –DATE date/time data –Uncommon Data Types: –LONG Similar to VARCHAR2 but Oracle’s character functions cannot operate on this type. –RAW holds binary data, up to 255 bytes –LONG RAW holds large binary data, eg. sound, video, etc.

CSCI N311: Oracle Database Programming 5-11 Chapter 18: Creating, Dropping, and Altering Tables and Views Constraint Clauses: –Candidate Keys: Column(s) which uniquely identify rows in a table. This could be used as the primary key. –Primary Keys: Column(s) used to specify and enforce uniqueness among rows in a table. create table employee ( employee_idvarchar(8) not null, column x,...., primary key (employee_id) ) ; –Foreign Keys: Column(s) used to reference rows in another table. These values are actually the primary keys in the other table. alter table employee add constraint emp02 foreign key (dept_id) references department(dept_id); –Check Constraint: A constraint that forces a set of values (domain) for a column.

CSCI N311: Oracle Database Programming 5-12 Chapter 18: Creating, Dropping, and Altering Tables and Views Creating a Table from a Table: Includes the data: create table RAIN as select City, Precipitation from TROUBLE; Does not include the data, only the table definition: create table RAIN as select City, Precipitation from TROUBLE where 1=2;

CSCI N311: Oracle Database Programming 5-13 Chapter 18: Creating, Dropping, and Altering Tables and Views Constraint Examples: SQL> desc customer Name Null? Type ID NUMBER NAME VARCHAR2(40) STATE CHAR(2) SQL> desc state2 Name Null? Type STATE_CODE NOT NULL CHAR(2) STATE_NAME VARCHAR2(50) SQL> alter table state2 2 add constraint PK_State2 3 primary key (state_code); Table altered. SQL> alter table customer 2 add constraint cust_anotherone 3 foreign key (state) 4 references state2(state_code); Table altered.

CSCI N311: Oracle Database Programming 5-14 Chapter 18: Creating, Dropping, and Altering Tables and Views Dropping Tables (p. 375): DROP TABLE tablename; –Dropping a table removes the table, both it’s data and definition. Truncating Tables (p. 321, 375): TRUNC TABLE tablename; –Truncating a table removes only the data. –Table’s definition is still available. Altering Tables: Tables can be altered to: –change column definitions, –add new columns, –enable or disable constraints constraints, –or change storage parameters. Problem: Adding NOT NULL to column definitions. ALTER TABLE emp ADD (thriftplan NUMBER(7,2), loancode CHAR(1) NOT NULL); ALTER TABLE emp MODIFY (thriftplan NUMBER(9,2));

CSCI N311: Oracle Database Programming 5-15 Chapter 18: Creating, Dropping, and Altering Tables and Views Views –Special considerations about views: You cannot insert if the underlying table has any NOT NULL columns that are not in the view. You cannot insert/update if any one of the view’s columns referenced in the insert/update contains functions or calculations. You cannot insert/update/delete if the view contains group by, distinct, or a reference to rownum. –View Stability: dropping the underlying tables of a view prevents the view from working. altering the underlying tables of a view are OK. –Order By in Views: ORDER BY does not work in views! GROUP BY still consolidates rows in views, a side affect of GROUP BY is ordering rows. –READ ONLY: The read only clause prevents update/insert/delete of rows in a view.

CSCI N311: Oracle Database Programming 5-16 Chapter 18: Creating, Dropping, and Altering Tables and Views New with Oracle8: –Index-Only Tables: stores data as if all data is in an index. Faster access. –Partitioned Tables: breaks up data in a table into partitions, faster for access for huge tables, easier to manage load/backup/recover.