“Manipulating Data” Lecture 6.

Slides:



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

Virtual training week 4 structured query language (SQL)
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
Working with Tables: Data Management and Retrieval Dr. Bernard Chen Ph.D. University of Central Arkansas.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
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 (DDL & DML Commands)
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
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,
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
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.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
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 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.
Database Programming Sections 7–Multi-row sub queries, IN, ANY, ALL, Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE,
Copyright © 2004, Oracle. All rights reserved. Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes ORACLE.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Database Programming Sections 7– Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Database Programming Sections 7 & 8 – Data Manipulation Language (DML) transaction, INSERT, implicit, explicit, USER, UPDATE, DELETE, integrity constraint,
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 Schedule: Timing Topic 60 minutes Lecture
Insert, Update and the rest…
ATS Application Programming: Java Programming
Manipulating Data.
Manipulating Data.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Introduction to Oracle9i: SQL
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
Manipulating Data.
Writing Basic SQL SELECT Statements
Using DDL Statements to Create and Manage Tables
Manipulating Data.
处理数据 Schedule: Timing Topic 60 minutes Lecture 30 minutes Practice
Chapter 2 Views.
(SQL) Manipulating Data
Manipulating Data.
“Manipulating Data” Lecture 6.
Manipulating Data.
Using DDL Statements to Create and Manage Tables
Writing Basic SQL SELECT Statements
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)
Using DDL Statements to Create and Manage Tables
IST 318 Database Administration
Including Constraints
Presentation transcript:

“Manipulating Data” Lecture 6

Data Manipulation Language 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

Data Manipulation Language 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

The INSERT Statement Syntax INSERT statement is used to ADD new rows to a table . Syntax INSERT INTO table [(column1 [, column2...])] VALUES (value1 [, value2...]); Only one row is inserted at a time with this syntax.

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. INSERT INTO departments(department_id, department_name, manager_id, location_id) VALUES (70, 'Public Relations', 100, 1700); Enclose character and date values within single quotation marks.

”Inserting Rows with “Null Values Implicit method: Omit the column from the column list. INSERT INTO departments (department_id, department_name ) VALUES (30, 'Purchasing'); Explicit method: Specify the NULL keyword in the VALUES clause. INSERT INTO departments VALUES (100, 'Finance', NULL, NULL); Listing column names is mandatory column names are not listed because it’s optional here

Note : listing the column names in the INSERT clause became mandatory in two cases: 1- Insert a row containing values for some columns 2- Insert a row containing values for all columns but don’t know the defult order of the columns

Data Manipulation Language 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

Updating Rows in a Table : Syntax Update statement is used to change a table’s data Syntax UPDATE tablename SET column1 = new_value [, column2 = new_value, ...] [WHERE condition]; condition identifies the rows to be updated and is composed of (column names expressions, constants, subqueries, and comparison operators ) In general, use the primary key to identify a single row. Using other columns can unexpectedly cause several rows to be updated

Updating Rows in a Table : Example of updating all rows Increase the salary of all employees by 100 $ Update employees Set salary=salary+100; Duplicate the salary of all employees ……………………………………………………………………

Updating Rows in a Table : Example of updating specific rows The WHERE clause is used with the Update statement to change the data of a specific row(s) UPDATE employees SET department_id = 70 WHERE employee_id = 113; 1 row updated Omitting the WHRE clause will change the data in all rows SET department_id = 110; 22 rows updated.

Updating Rows in a Table (Integrity Constraint Error) UPDATE employees SET department_id = 55 WHERE department_id = 110; * Department number 55 does not exist ERROR at line 1: ORA-02291: integrity constraint (HR.EMP_DEPT_FK) violated - parent key not found

Updating Rows in a Table (Integrity Constraint Error(Cont.)) If you attempt to update a record with a value that is tied to an integrity constraint, an error is returned. In the example on the slide, department number 55 does not exist in the parent table, DEPARTMENTS, and so you receive the parent key violation ORA-02291.

Data Manipulation Language 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

Removing a Row from a Table You can remove existing rows from a table by using the DELETE statement. DELETE [FROM] tablename [WHERE condition]; condition identifies the rows to be deleted and is composed of (column names expressions, constants, subqueries, and comparison operators ) If no rows are deleted, a message “0 rows deleted.” is returned

Removing a Row from a Table Example: DELETE FROM departments WHERE department_id IN (30, 40); 2 rows deleted

Removing a Row from a Table Specific rows will be deleted if you specify the WHERE clause. DELETE FROM departments WHERE department_name = 'Finance'; 1 row deleted. All rows in the table will be deleted if you omit the WHERE clause. DELETE FROM copy_emp; 22 rows deleted.

Removing a Row from a Table ( (Integrity Constraint Error DELETE FROM departments WHERE department_id = 60; You cannot delete a row that contains a primary key * that is used as a foreign key in another table. ERROR at line 1:ORA-02292: integrity constraint (HR.EMP_DEPT_FK) violated - child record found

Removing a Row from a Table ( (Integrity Constraint Error If you attempt to delete a record with a value that is tied to an integrity constraint , an error is returned. The example in the slide tries to delete department number 60 from the DEPARTMENTS table, but it results in an error because department number is used as a foreign key in the EMPLOYEES table. If the parent record that you attempt to delete has child records, then you receive the child record found violation ORA-02292.

Removing a Row from a Table ( (Integrity Constraint Error The following statement works because there are no employees in department 70: DELETE FROM departments WHERE department_id = 70;