DEFAULT Values and the MERGE Statement. 2 home back first prev next last What Will I Learn? Understand when to specify a DEFAULT value. Construct and.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
(with Microsoft SQL Server) Svetlin Nakov Telerik Corporation
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.
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.
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.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Manipulating Large Data Sets. Lesson Agenda ◦ Manipulating data using subqueries ◦ Specifying explicit default values in the INSERT and UPDATE statements.
4 Copyright © 2007, Oracle. All rights reserved. Manipulating Large Data Sets.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Database Programming Sections 7 & 8 – Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
Copyright © 2004, Oracle. All rights reserved. MANIPULATING LARGE DATA SETS Oracle Lecture 10.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
3 Copyright © 2006, Oracle. All rights reserved. Manipulating Large Data Sets.
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
INSERT Statement. 2 home back first prev next last What Will I Learn? Give examples of why it is important to be able to alter the data in a database.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Creating Views. 2 home back first prev next last What Will I Learn? List three uses for views from the standpoint of a database administrator Explain,
Database Programming Sections 7–Multi-row sub queries, IN, ANY, ALL, Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE,
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Database Programming Sections 7– Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Programming Sections 7 & 8 – Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
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.
Interacting with the Oracle Server
SQL Creating and Managing Tables
Interacting with the Oracle Server
Manipulating Data.
Using the Set Operators
Database Management Systems 2
Database Programming Sections 7 & 8 – Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Writing Correlated Subqueries
Using the Set Operators
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Chapter 2 Views.
“Manipulating Data” Lecture 6.
(SQL) Manipulating Data
“Manipulating Data” Lecture 6.
Manipulating Data.
Chapter 2 Views.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
IST 318 Database Administration
Using the Set Operators
Manipulating Data Lesson 3.
SQL NOT NULL Constraint
Restricting and Sorting Data
Presentation transcript:

DEFAULT Values and the MERGE Statement

2 home back first prev next last What Will I Learn? Understand when to specify a DEFAULT value. Construct and execute a MERGE statement

3 home back first prev next last Why Learn It? Up to now, you have been updating data using a single INSERT statement. It has been relatively easy when adding records one at a time, but what if your company is very large and utilizes a data warehouse to store sales records and customer, payroll, accounting, and personal data? In this case, data is probably coming in from multiple sources and being managed by multiple people. Managing data one record at a time could be very confusing and very time consuming.

4 home back first prev next last Why Learn It? Operation Database Data Warehouse INSERTSELECTUPDATEDELETE 日常业务应用 SELECT 决策分析应用 数据批量定时更新

5 home back first prev next last Why Learn It? How do you determine what has been newly inserted or what has been recently changed? In this lesson, you will learn a more efficient method to update and insert data using a sequence of conditional INSERT and UPDATE commands in a single atomic statement. As you extend your knowledge in SQL, you'll appreciate effective ways to accomplish your work.

6 home back first prev next last DEFAULT A column in a table can be given a default value. –This option prevents null values from entering the columns if a row is inserted without a specified value for the column. –Using default values also allows you to control where and when the default value should be applied. –The default value can be a literal value, an expression, or a SQL function, such as SYSDATE and USER, but the value cannot be the name of another column. –The default value must match the data type of the column. –DEFAULT can be specified for a column when the table is created or altered.

7 home back first prev next last DEFAULT The example below shows a default value being specified at the time the table is created: CREATE TABLE my_employees ( hire_date DATE DEFAULT SYSDATE, first_name VARCHAR2(15), last_name VARCHAR2(15)); When rows are added to this table, SYSDATE will be added to any row that does not explicitly specify a hire_date value.

8 home back first prev next last Explicit DEFAULT with INSERT Explicit defaults can be used in INSERT and UPDATE statements. INSERT INTO my_employees(hire_date,first_name,last_name ) VALUES(DEFAULT, DEFAULT,'Smith'); If a default value was set for the hire_date column, Oracle sets the column to the default value. However, if no default value was set when the column was created, Oracle inserts a null value.

9 home back first prev next last Explicit DEFAULT with UPDATE Explicit defaults can be used in INSERT and UPDATE statements. INSERT INTO my_employees(hire_date,first_name,last_name ) VALUES(to_date(' ','DD-MM-YYYY'), 'Abel','John'); UPDATE my_employees SET hire_date = DEFAULT,first_name = DEFAULT WHERE last_name = 'John'; If a default value was set for the column, Oracle sets the column to the default value. However, if no default value was set when the column was created, Oracle inserts a null value.

10 home back first prev next last MERGE Using the MERGE statement accomplishes two tasks at the same time. –MERGE will INSERT and UPDATE simultaneously.  If a value is missing, a new one is inserted.  If a value exists, but needs to be changed, MERGE will update it. –To perform these kinds of changes to database tables, you need to have INSERT and UPDATE privileges on the target table and SELECT privileges on the source table. –Aliases can be used with the MERGE statement.

11 home back first prev next last MERGE SYNTAX MERGE INTO destination-table USING source-table ON matching-condition WHEN MATCHED THEN UPDATE SET …… WHEN NOT MATCHED THEN INSERT VALUES (……);  One row at a time is read from the source table, and its column values are compared with rows in the destination table using the matching condition.  If a matching row exists in the destination table, the source row is used to update column(s) in the matching destination row.  If a matching row does not exist, values from the source row are used to insert a new row into the destination table.

12 home back first prev next last MERGE Example This example uses the EMPLOYEES table (alias e) as a data source to insert and update rows in a copy of the table named COPY_EMP (alias c). MERGE INTO copy_emp c USING employees e ON (c.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET c.last_name = e.last_name, c.department_id = e.department_id WHEN NOT MATCHED THEN INSERT VALUES (e.employee_id, e.last_name, e.department_id); The next slide shows the result of this statement.

13 home back first prev next last MERGE Example EMPLOYEES rows 100 and 103 have matching rows in COPY_EMP, and so the matching COPY_EMP rows were updated. EMPLOYEE 142 had no matching row, and so was inserted into COPY_EMP.

14 home back first prev next last 演示如何从两个表向同一个表合并数据 drop table emp; create table emp as select employee_id,first_name,last_name,salary,department_name from employees e,departments d where e.department_id=d.department_id; select * from emp; delete from emp where employee_id<200; update emp set salary=0 where employee_id=200; -- MERGE 语句 MERGE INTO emp USING employees e ON(emp.employee_id = e.employee_id) WHEN MATCHED THEN UPDATE SET emp.last_name = e.last_name, emp.first_name = e.first_name, emp.salary = e.salary, emp.department_name = NVL((SELECT department_name FROM departments WHERE department_id=e.department_id),'No Department') WHEN NOT MATCHED THEN INSERT VALUES (e.employee_id,e.first_name, e.last_name, e.salary,NVL((SELECT department_name FROM departments WHERE department_id=e.department_id),'No Department'));

15 home back first prev next last Summary In this lesson you have learned to: –Understand when to specify a DEFAULT value. –Constructe and execute a MERGE statement