SlideShare a Scribd company logo
https://sahibulsaif.wordpress.com/wisdom/bismillah-nature/
SQL – INSERT, UPDATE, DELETE
CSC271 – DATABASE SYSTEMS
PREVIOUS LECTURE
Today’s Lecture
This lecture contains DML commands to manipulate data:
 CRUD function
 Create record [INSERT INTO]
 Read/Retrieve Record [SELECT]
 Update Record [UPDATE]
 Delete Record [DELETE]
DATA MANIPULATION LANGUAGE
 A DML statement is executed when you:
 Add new rows to a table
 INSERT
 Modify existing rows in a table
 UPDATE
 Remove existing rows from a table
 DELETE
5
ADDING A NEW ROWTO A TABLE
6
DEPARTMENTS
New
row
…insert a new row into the DEPARMENTS
table…
THE INSERT STATEMENT SYNTAX
 Add new rows to a table by using the INSERT statement.
 Only one row is inserted at a time with this syntax.
7
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);
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.
8
INSERT INTO departments(department_id, department_name,
manager_id, location_id)
VALUES (70, 'Public Relations', 100, 1700);
1 row created.
INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);
1 row created.
INSERT INTO departments (department_id,
department_name )
VALUES (30, 'Purchasing');
1 row created.
INSERTING ROWS WITH NULLVALUES
 Implicit method: Omit the column from the column list.
9
• Explicit method: Specify the NULL keyword in the VALUES clause.
INSERT INTO employees (employee_id,
first_name, last_name,
email, phone_number,
hire_date, job_id, salary,
commission_pct, manager_id,
department_id)
VALUES (113,
'Louis', 'Popp',
'LPOPP', '515.124.4567',
SYSDATE, 'AC_ACCOUNT', 6900,
NULL, 205, 100);
1 row created.
INSERTING SPECIALVALUES
The SYSDATE function records the current date and time.
10
COPYING ROWS FROM ANOTHERTABLE
 Write your INSERT statement with a subquery.
 Do not use the VALUES clause.
 It copies all data from employees table into emp table
11
INSERT INTO emp
SELECT * FROM employees;
4 rows created.
COPYING SPECIFIC COLUMNS DATA FROM
ANOTHER TABLE
 Match the number of columns in the INSERT clause to those in the subquery.
12
INSERT INTO emp(id, name, salary)
SELECT employee_id, last_name, salary
FROM employees;
4 rows created.
COPYING SPECIFIC ROWS FROM ANOTHERTABLE
 You can filter rows before entering into new table
 It first search out the CS employees and then insert its respective data (id, name, salary)
into emp_CS table
13
INSERT INTO emp_CS(id, name, salary)
SELECT employee_id, last_name, salary
FROM employees;
WHERE emp_dept = ‘CS’;
4 rows created.
CHANGING DATA IN ATABLE
EMPLOYEES
Update rows in the EMPLOYEES table.
THE UPDATE STATEMENT SYNTAX
 Modify existing rows with the UPDATE statement.
 Update more than one row at a time, if required.
15
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
UPDATE employees
SET department_id = 70
WHERE employee_id = 113;
1 row updated.
UPDATING ROWS IN A TABLE
 Specific row or rows are modified if you specify the WHERE clause.
 All rows in the table are modified if you omit the WHERE clause.
16
UPDATE copy_emp
SET department_id = 110;
22 rows updated.
Delete a row from the DEPARTMENTS table.
REMOVING A ROW FROM A TABLE
17
DEPARTMENTS
THE DELETE STATEMENT
You can remove existing rows from a table by using the DELETE
statement.
18
DELETE [FROM] table
[WHERE condition];
DELETING ROWS FROM A TABLE
 Specific rows are deleted if you specify the WHERE clause.
 All rows in the table are deleted if you omit the WHERE clause.
19
DELETE FROM departments
WHERE department_name = 'Finance';
1 row deleted.
DELETE FROM copy_emp;
22 rows deleted.
http://www.thomasformo.com/category/qa/

More Related Content

PPTX
DML Statements.pptx
PPT
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
PPTX
SQL Tutorial for Beginners
PPT
PPT
SQL WORKSHOP::Lecture 9
PPT
Manipulating data
PPT
Les08 (manipulating data)
DML Statements.pptx
asdasdasdasdsadasdasdasdasdsadasdasdasdsadsadasd
SQL Tutorial for Beginners
SQL WORKSHOP::Lecture 9
Manipulating data
Les08 (manipulating data)

Similar to Lecture 21 Insert Update Delete.pptx (20)

PPT
e computer notes - Manipulating data
PPT
PPT
PDF
full detailled SQL notesquestion bank (1).pdf
PPTX
SQL LECTURE.pptx
PPTX
Data Manipulation Language
PDF
sql language
PPTX
Null values, insert, delete and update in database
DOCX
Msql query
PPT
Lab_04.ppt opreating system of computer lab
PPTX
2. DML_INSERT_DELETE_UPDATE
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
PPTX
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
DOC
PPTX
Fundamentals of Database management system Lab Manual.pptx
PPTX
DML using oracle
PPTX
Unit - II.pptx
PPT
Intro to tsql unit 9
PPT
SQL. It education ppt for reference sql process coding
PDF
Data manipulation language
e computer notes - Manipulating data
full detailled SQL notesquestion bank (1).pdf
SQL LECTURE.pptx
Data Manipulation Language
sql language
Null values, insert, delete and update in database
Msql query
Lab_04.ppt opreating system of computer lab
2. DML_INSERT_DELETE_UPDATE
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
Fundamentals of Database management system Lab Manual.pptx
DML using oracle
Unit - II.pptx
Intro to tsql unit 9
SQL. It education ppt for reference sql process coding
Data manipulation language
Ad

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Computing-Curriculum for Schools in Ghana
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Anesthesia in Laparoscopic Surgery in India
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Updated Idioms and Phrasal Verbs in English subject
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
master seminar digital applications in india
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Final Presentation General Medicine 03-08-2024.pptx
What if we spent less time fighting change, and more time building what’s rig...
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Computing-Curriculum for Schools in Ghana
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Types and Its function , kingdom of life
Anesthesia in Laparoscopic Surgery in India
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
STATICS OF THE RIGID BODIES Hibbelers.pdf
History, Philosophy and sociology of education (1).pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Complications of Minimal Access Surgery at WLH
Weekly quiz Compilation Jan -July 25.pdf
Updated Idioms and Phrasal Verbs in English subject
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
UNIT III MENTAL HEALTH NURSING ASSESSMENT
A systematic review of self-coping strategies used by university students to ...
master seminar digital applications in india
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Ad

Lecture 21 Insert Update Delete.pptx

  • 2. SQL – INSERT, UPDATE, DELETE CSC271 – DATABASE SYSTEMS
  • 4. Today’s Lecture This lecture contains DML commands to manipulate data:  CRUD function  Create record [INSERT INTO]  Read/Retrieve Record [SELECT]  Update Record [UPDATE]  Delete Record [DELETE]
  • 5. DATA MANIPULATION LANGUAGE  A DML statement is executed when you:  Add new rows to a table  INSERT  Modify existing rows in a table  UPDATE  Remove existing rows from a table  DELETE 5
  • 6. ADDING A NEW ROWTO A TABLE 6 DEPARTMENTS New row …insert a new row into the DEPARMENTS table…
  • 7. THE INSERT STATEMENT SYNTAX  Add new rows to a table by using the INSERT statement.  Only one row is inserted at a time with this syntax. 7 INSERT INTO table [(column [, column...])] VALUES (value [, value...]);
  • 8. 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. 8 INSERT INTO departments(department_id, department_name, manager_id, location_id) VALUES (70, 'Public Relations', 100, 1700); 1 row created.
  • 9. INSERT INTO departments VALUES (100, 'Finance', NULL, NULL); 1 row created. INSERT INTO departments (department_id, department_name ) VALUES (30, 'Purchasing'); 1 row created. INSERTING ROWS WITH NULLVALUES  Implicit method: Omit the column from the column list. 9 • Explicit method: Specify the NULL keyword in the VALUES clause.
  • 10. INSERT INTO employees (employee_id, first_name, last_name, email, phone_number, hire_date, job_id, salary, commission_pct, manager_id, department_id) VALUES (113, 'Louis', 'Popp', 'LPOPP', '515.124.4567', SYSDATE, 'AC_ACCOUNT', 6900, NULL, 205, 100); 1 row created. INSERTING SPECIALVALUES The SYSDATE function records the current date and time. 10
  • 11. COPYING ROWS FROM ANOTHERTABLE  Write your INSERT statement with a subquery.  Do not use the VALUES clause.  It copies all data from employees table into emp table 11 INSERT INTO emp SELECT * FROM employees; 4 rows created.
  • 12. COPYING SPECIFIC COLUMNS DATA FROM ANOTHER TABLE  Match the number of columns in the INSERT clause to those in the subquery. 12 INSERT INTO emp(id, name, salary) SELECT employee_id, last_name, salary FROM employees; 4 rows created.
  • 13. COPYING SPECIFIC ROWS FROM ANOTHERTABLE  You can filter rows before entering into new table  It first search out the CS employees and then insert its respective data (id, name, salary) into emp_CS table 13 INSERT INTO emp_CS(id, name, salary) SELECT employee_id, last_name, salary FROM employees; WHERE emp_dept = ‘CS’; 4 rows created.
  • 14. CHANGING DATA IN ATABLE EMPLOYEES Update rows in the EMPLOYEES table.
  • 15. THE UPDATE STATEMENT SYNTAX  Modify existing rows with the UPDATE statement.  Update more than one row at a time, if required. 15 UPDATE table SET column = value [, column = value, ...] [WHERE condition];
  • 16. UPDATE employees SET department_id = 70 WHERE employee_id = 113; 1 row updated. UPDATING ROWS IN A TABLE  Specific row or rows are modified if you specify the WHERE clause.  All rows in the table are modified if you omit the WHERE clause. 16 UPDATE copy_emp SET department_id = 110; 22 rows updated.
  • 17. Delete a row from the DEPARTMENTS table. REMOVING A ROW FROM A TABLE 17 DEPARTMENTS
  • 18. THE DELETE STATEMENT You can remove existing rows from a table by using the DELETE statement. 18 DELETE [FROM] table [WHERE condition];
  • 19. DELETING ROWS FROM A TABLE  Specific rows are deleted if you specify the WHERE clause.  All rows in the table are deleted if you omit the WHERE clause. 19 DELETE FROM departments WHERE department_name = 'Finance'; 1 row deleted. DELETE FROM copy_emp; 22 rows deleted.

Editor's Notes

  • #5: Data Manipulation Language Data manipulation language (DML) is a core part of SQL. When you want to add, update, or delete data in the database, you execute a DML statement. A collection of DML statements that form a logical unit of work is called a transaction. Consider a banking database. When a bank customer transfers money from a savings account to a checking account, the transaction might consist of three separate operations: decrease the savings account, increase the checking account, and record the transaction in the transaction journal. The Oracle server must guarantee that all three SQL statements are performed to maintain the accounts in proper balance. When something prevents one of the statements in the transaction from executing, the other statements of the transaction must be undone. Instructor Note DML statements can be issued directly in iSQL*Plus, performed automatically by tools such as Oracle Forms Services, or programmed with tools such as the 3GL precompilers. Every table has INSERT, UPDATE, and DELETE privileges associated with it. These privileges are automatically granted to the creator of the table, but in general they must be explicitly granted to other users. Starting with Oracle 7.2, you can place a subquery in the place of the table name in an UPDATE statement, essentially the same way you use a view.
  • #6: Adding a New Row to a Table The slide graphic illustrates adding a new department to the DEPARTMENTS table.
  • #7: Adding a New Row to a Table (continued) You can add new rows to a table by issuing the INSERT statement. In the syntax: table is the name of the table column is the name of the column in the table to populate value is the corresponding value for the column Note: This statement with the VALUES clause adds only one row at a time to a table.
  • #8: Adding a New Row to a Table (continued) Because you can insert a new row that contains values for each column, the column list is not required in the INSERT clause. However, if you do not use the column list, the values must be listed according to the default order of the columns in the table, and a value must be provided for each column. DESCRIBE departments For clarity, use the column list in the INSERT clause. Enclose character and date values within single quotation marks; it is not recommended to enclose numeric values within single quotation marks. Number values should not be enclosed in single quotes, because implicit conversion may take place for numeric values assigned to NUMBER data type columns if single quotes are included.
  • #9: Methods for Inserting Null Values Be sure that you can use null values in the targeted column by verifying the Null? status with the iSQL*Plus DESCRIBE command. The Oracle Server automatically enforces all data types, data ranges, and data integrity constraints. Any column that is not listed explicitly obtains a null value in the new row. Common errors that can occur during user input: Mandatory value missing for a NOT NULL column Duplicate value violates uniqueness constraint Foreign key constraint violated CHECK constraint violated Data type mismatch Value too wide to fit in column
  • #10: Inserting Special Values by Using SQL Functions You can use functions to enter special values in your table. The slide example records information for employee Popp in the EMPLOYEES table. It supplies the current date and time in the HIRE_DATE column. It uses the SYSDATE function for current date and time. You can also use the USER function when inserting rows in a table. The USER function records the current username. Confirming Additions to the Table SELECT employee_id, last_name, job_id, hire_date, commission_pct FROM employees WHERE employee_id = 113;
  • #11: Copying Rows from Another Table You can use the INSERT statement to add rows to a table where the values are derived from existing tables. In place of the VALUES clause, you use a subquery. Syntax INSERT INTO table [ column (, column) ] subquery; In the syntax: table is the table name column is the name of the column in the table to populate subquery is the subquery that returns rows into the table The number of columns and their data types in the column list of the INSERT clause must match the number of values and their data types in the subquery. To create a copy of the rows of a table, use SELECT * in the subquery. INSERT INTO copy_emp SELECT * FROM employees; For more information, see Oracle9i SQL Reference, “SELECT,” subqueries section. Instructor Note Please run the script 8_cretabs.sql to create the COPY_EMP and SALES_REPS tables before demonstrating the code examples. Do not get into too many details on copying rows from another table.
  • #12: Copying Rows from Another Table You can use the INSERT statement to add rows to a table where the values are derived from existing tables. In place of the VALUES clause, you use a subquery. Syntax INSERT INTO table [ column (, column) ] subquery; In the syntax: table is the table name column is the name of the column in the table to populate subquery is the subquery that returns rows into the table The number of columns and their data types in the column list of the INSERT clause must match the number of values and their data types in the subquery. To create a copy of the rows of a table, use SELECT * in the subquery. INSERT INTO copy_emp SELECT * FROM employees; For more information, see Oracle9i SQL Reference, “SELECT,” subqueries section. Instructor Note Please run the script 8_cretabs.sql to create the COPY_EMP and SALES_REPS tables before demonstrating the code examples. Do not get into too many details on copying rows from another table.
  • #13: Copying Rows from Another Table You can use the INSERT statement to add rows to a table where the values are derived from existing tables. In place of the VALUES clause, you use a subquery. Syntax INSERT INTO table [ column (, column) ] subquery; In the syntax: table is the table name column is the name of the column in the table to populate subquery is the subquery that returns rows into the table The number of columns and their data types in the column list of the INSERT clause must match the number of values and their data types in the subquery. To create a copy of the rows of a table, use SELECT * in the subquery. INSERT INTO copy_emp SELECT * FROM employees; For more information, see Oracle9i SQL Reference, “SELECT,” subqueries section. Instructor Note Please run the script 8_cretabs.sql to create the COPY_EMP and SALES_REPS tables before demonstrating the code examples. Do not get into too many details on copying rows from another table.
  • #14: Changing Data in a Table The slide graphic illustrates changing the department number for employees in department 60 to department 30.
  • #15: Updating Rows You can modify existing rows by using the UPDATE statement. In the syntax: table is the name of the table column is the name of the column in the table to populate value is the corresponding value or subquery for the column condition identifies the rows to be updated and is composed of column names expressions, constants, subqueries, and comparison operators Confirm the update operation by querying the table to display the updated rows. For more information, see Oracle9i SQL Reference, “UPDATE.” Note: In general, use the primary key to identify a single row. Using other columns can unexpectedly cause several rows to be updated. For example, identifying a single row in the EMPLOYEES table by name is dangerous, because more than one employee may have the same name.
  • #16: Updating Rows (continued) The UPDATE statement modifies specific rows if the WHERE clause is specified. The slide example transfers employee 113 (Popp) to department 70. If you omit the WHERE clause, all the rows in the table are modified. SELECT last_name, department_id FROM copy_emp; Note: The COPY_EMP table has the same data as the EMPLOYEES table.
  • #17: Removing a Row from a Table The slide graphic removes the Finance department from the DEPARTMENTS table (assuming that there are no constraints defined on the DEPARTMENTS table). Instructor Note After all the rows have been eliminated with the DELETE statement, only the data structure of the table remains. A more efficient method of emptying a table is with the TRUNCATE statement. You can use the TRUNCATE statement to quickly remove all rows from a table or cluster. Removing rows with the TRUNCATE statement is faster than removing them with the DELETE statement for the following reasons: The TRUNCATE statement is a data definition language (DDL) statement and generates no rollback information. It is covered in a subsequent lesson. Truncating a table does not fire the delete triggers of the table. If the table is the parent of a referential integrity constraint, you cannot truncate the table. Disable the constraint before issuing the TRUNCATE statement.
  • #18: Deleting Rows You can remove existing rows by using the DELETE statement. In the syntax: table is the table name condition identifies the rows to be deleted and is composed of column names, expressions, constants, subqueries, and comparison operators Note: If no rows are deleted, a message “0 rows deleted.” is returned: For more information, see Oracle9i SQL Reference, “DELETE.” Instructor Note The DELETE statement does not ask for confirmation. However, the delete operation is not made permanent until the data transaction is committed. Therefore, you can undo the operation with the ROLLBACK statement if you make a mistake.
  • #19: Deleting Rows (continued) You can delete specific rows by specifying the WHERE clause in the DELETE statement. The slide example deletes the Finance department from the DEPARTMENTS table. You can confirm the delete operation by displaying the deleted rows using the SELECT statement. SELECT * FROM departments WHERE department_name = 'Finance'; no rows selected. If you omit the WHERE clause, all rows in the table are deleted. The second example on the slide deletes all the rows from the COPY_EMP table, because no WHERE clause has been specified. Example Remove rows identified in the WHERE clause. DELETE FROM employees WHERE employee_id = 114; 1 row deleted. DELETE FROM departments WHERE department_id IN (30, 40); 2 rows deleted.