4-1 Copyright  Oracle Corporation, 1998. All rights reserved. Data Manipulation Language (DML)

Slides:



Advertisements
Similar presentations
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Advertisements

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.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows in a table Delete rows from a table.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
CH3 Part1 Single-Row Functions
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
SQL (DDL & DML Commands)
Program with PL/SQL. Interacting with the Oracle Server.
Subqueries.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
An Introduction To SQL - Part 1 (Special thanks to Geoff Leese)
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
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.
20 Copyright © Oracle Corporation, All rights reserved. Oracle9 i Extensions to DML and DDL Statements.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
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.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
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. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
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.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Creating Views Database Systems Objectives Explain the concept of a view. Create simple and complex views. Retrieve data through a view. Alter the.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
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.
Manipulating Data.
Introduction to Oracle9i: SQL
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Interacting with the Oracle Server
“Manipulating Data” Lecture 6.
(SQL) Manipulating Data
“Manipulating Data” Lecture 6.
Manipulating Data.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Presentation transcript:

4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)

4-2 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML) A DML statement is executed when you: – Add new rows to a table – Modify existing rows in a table – Remove existing rows from a table A transaction consists of a collection of DML statements that form a logical unit of work. A DML statement is executed when you: – Add new rows to a table – Modify existing rows in a table – Remove existing rows from a table A transaction consists of a collection of DML statements that form a logical unit of work.

4-3 Copyright  Oracle Corporation, All rights reserved. Adding Rows

4-4 Copyright  Oracle Corporation, All rights reserved. Adding a New Row to a Table DEPT DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON New row 50 DEVELOPMENT DETROIT DEPT DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON “…insert a new row into DEPT table…” 50 DEVELOPMENT DETROIT

4-5 Copyright  Oracle Corporation, All rights reserved. The INSERT Statement Add new rows to a table by using the INSERT statement. Only one row is inserted at a time with this syntax. Add new rows to a table by using the INSERT statement. Only one row is inserted at a time with this syntax. INSERT INTOtable [(column [, column...])] VALUES(value [, value...]); INSERT INTOtable [(column [, column...])] VALUES(value [, value...]);

4-6 Copyright  Oracle Corporation, All rights reserved. Inserting New Rows Insert a new row containing values for each column. List values in the default order of the columns in the table. Optionally list the columns in the INSERT clause. Enclose character and date values within single quotation marks. Insert a new row containing values for each column. List values in the default order of the columns in the table. Optionally list the columns in the INSERT clause. Enclose character and date values within single quotation marks. SQL> INSERT INTOdept (deptno, dname, loc) 2 VALUES(50, 'DEVELOPMENT', 'DETROIT'); 1 row created.

4-7 Copyright  Oracle Corporation, All rights reserved. Inserting Rows with Null Values Implicit method: Omit the column from the column list. SQL> INSERT INTOdept (deptno, dname ) 2 VALUES(60, 'MIS'); 1 row created. Explicit method: Specify the NULL keyword. SQL> INSERT INTOdept 2 VALUES(70, 'FINANCE', NULL); 1 row created.

4-8 Copyright  Oracle Corporation, All rights reserved. Inserting Values by Using Substitution Variables Create an interactive script by using SQL*Plus substitution parameters. SQL> INSERT INTOdept (deptno, dname, loc) 2 VALUES (&department_id, 3 '&department_name', '&location'); 80 Enter value for department_id: 80 EDUCATION Enter value for department_name: EDUCATION ATLANTA Enter value for location: ATLANTA 1 row created.

4-9 Copyright  Oracle Corporation, All rights reserved. Copying Rows from Another Table Write your INSERT statement with a subquery. Do not use the VALUES clause. Match the number of columns in the INSERT clause to those in the subquery. Write your INSERT statement with a subquery. Do not use the VALUES clause. Match the number of columns in the INSERT clause to those in the subquery. SQL> INSERT INTO managers(id, name, salary, hiredate) 2 SELECTempno, ename, sal, hiredate 3 FROM emp 4 WHEREjob = 'MANAGER'; 3 rows created.

4-10 Copyright  Oracle Corporation, All rights reserved. Update Rows

4-11 Copyright  Oracle Corporation, All rights reserved. Changing Data in a Table EMP “…update a row in EMP table…” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER JONESMANAGER EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER JONESMANAGER 20...

4-12 Copyright  Oracle Corporation, All rights reserved. The UPDATE Statement Modify existing rows with the UPDATE statement. Update more than one row at a time, if required. Modify existing rows with the UPDATE statement. Update more than one row at a time, if required. UPDATEtable SETcolumn = value [, column = value] [WHERE condition]; UPDATEtable SETcolumn = value [, column = value] [WHERE condition];

4-13 Copyright  Oracle Corporation, All rights reserved. Updating Rows in a Table Specific row or rows are modified when you specify the WHERE clause. All rows in the table are modified if you omit the WHERE clause. Specific row or rows are modified when you specify the WHERE clause. All rows in the table are modified if you omit the WHERE clause. SQL> UPDATE emp 2 SET deptno = 20 3 WHERE empno = 7782; 1 row updated. SQL> UPDATE employee 2 SET deptno = 20; 14 rows updated. SQL> UPDATE employee 2 SET deptno = 20; 14 rows updated.

4-14 Copyright  Oracle Corporation, All rights reserved. Updating Rows Based on Another Table Use subqueries in UPDATE statements to update rows in a table based on values from another table. SQL>UPDATEemployee 2SETdeptno = (SELECTdeptno 3FROMemp 4WHEREempno = 7788) 5WHEREjob = (SELECTjob 6FROMemp 7WHEREempno = 7788); 2 rows updated. SQL>UPDATEemployee 2SETdeptno = (SELECTdeptno 3FROMemp 4WHEREempno = 7788) 5WHEREjob = (SELECTjob 6FROMemp 7WHEREempno = 7788); 2 rows updated.

4-15 Copyright  Oracle Corporation, All rights reserved. UPDATE emp * ERROR at line 1: ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK) violated - parent key not found UPDATE emp * ERROR at line 1: ORA-02291: integrity constraint (USR.EMP_DEPTNO_FK) violated - parent key not found SQL> UPDATEemp 2 SETdeptno = 55 3 WHEREdeptno = 10; SQL> UPDATEemp 2 SETdeptno = 55 3 WHEREdeptno = 10; Updating Rows: Integrity Constraint Error Department number 55 does not exist

4-16 Copyright  Oracle Corporation, All rights reserved. Delete Rows

4-17 Copyright  Oracle Corporation, All rights reserved. “…delete a row from DEPT table…” Removing a Row from a Table DEPT DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON 50 DEVELOPMENT DETROIT 60MIS... DEPT DEPTNO DNAME LOC ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON 60MIS...

4-18 Copyright  Oracle Corporation, All rights reserved. The DELETE Statement You can remove existing rows from a table by using the DELETE statement. DELETE [FROM] table [WHERE condition]; DELETE [FROM] table [WHERE condition];

4-19 Copyright  Oracle Corporation, All rights reserved. Specific row or rows are deleted when you specify the WHERE clause. All rows in the table are deleted if you omit the WHERE clause. Specific row or rows are deleted when you specify the WHERE clause. All rows in the table are deleted if you omit the WHERE clause. Deleting Rows from a Table SQL> DELETE FROMdepartment 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted. SQL> DELETE FROMdepartment 2 WHERE dname = 'DEVELOPMENT'; 1 row deleted. SQL> DELETE FROMdepartment; 4 rows deleted. SQL> DELETE FROMdepartment; 4 rows deleted.

4-20 Copyright  Oracle Corporation, All rights reserved. Deleting Rows Based on Another Table Use subqueries in DELETE statements to remove rows from a table based on values from another table. SQL> DELETE FROMemployee 2 WHEREdeptno = 3 (SELECT deptno 4 FROM dept 5 WHERE dname ='SALES'); 6 rows deleted.

4-21 Copyright  Oracle Corporation, All rights reserved. Deleting Rows: Integrity Constraint Error SQL> DELETE FROMdept 2 WHEREdeptno = 10; SQL> DELETE FROMdept 2 WHEREdeptno = 10; DELETE FROM dept * ERROR at line 1: ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK) violated - child record found DELETE FROM dept * ERROR at line 1: ORA-02292: integrity constraint (USR.EMP_DEPTNO_FK) violated - child record found You cannot delete a row that contains a primary key that is used as a foreign key in another table.