manipulating data

26
Manipulating Data

Upload: ginger

Post on 23-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Manipulating Data. Objectives. 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 Control transactions. Data Manipulation Language. A DML statement is executed when you: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Manipulating Data

Manipulating Data

Page 2: Manipulating Data

Objectives

At the end of this lesson, you should be able to:Describe each DML statementInsert rows into a tableUpdate rows in a tableDelete rows from a tableControl transactions

Page 3: Manipulating Data

Data Manipulation Language

A DML statement is executed when you:Add new rows to a tableModify existing rows in a tableRemove existing rows from a table

A transaction consists of a collection of DML statements that form a logical unit of work.

Page 4: Manipulating Data

Adding a New Row to a Table

Department dept_nbr dept_name location

10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston

New row

50 Development Detroit

Department dept_nbr dept_name location

10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston

“…insert a new row into DEPT table…”

50 Development Detroit

Page 5: Manipulating Data

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.

INSERT INTO table [(column [, column...])]VALUES (value [, value...]);

Page 6: Manipulating Data

Inserting New Rows

Insert a new row containing values for each column.Optionally list the columns in the INSERT clause.

List values in the default order of the columns in the table.Enclose character and date values within single quotation marks.

MySQL>INSERT INTO deptartment(dept_nbr, dept_name, location) 2 VALUES (50, 'Development', 'Detroit');1 row created.

Page 7: Manipulating Data

Inserting Rows with Null Values

Implicit method: Omit the column from the column list.

Explicit method: Specify the NULL keyword.

MySQL>INSERT INTO department (dept_nbr, dept_name ) 2 VALUES (60, 'MIS');1 row created.

MySQL>INSERT INTO deptartment 2 VALUES (70, 'FINANCE', NULL);1 row created.

Page 8: Manipulating Data

Inserting Special Values

TheCURDATE function records the current date and time.

MySQL> INSERT INTO employee (employee_nbr, name, job, 2 manager, hire_date, salary, 3 commission, dept_nbr) 4 VALUES (7196, 'Green', 'Salesman', 5 7782,CURDATE(), 2000, NULL, 6 10);1 row created.

Page 9: Manipulating Data

Inserting Multiple RowsUse multiple INSERT statements.

Submit all at once with each statement terminated by a semicolon.

MySQL>INSERT INTO department (dept_nbr, dept_name, 2 location) 3 VALUES (50, ‘Development’, ‘Detroit’);

INSERT INTO department (dept_nbr, dept_name, 2 location) 3 VALUES (60, ‘Huntsville’, ‘MIS’);

Page 10: Manipulating Data

Inserting Multiple Rows - AlternativeAs long as column names are specified or all data is in the proper order you may OMIT the additional INSERT value clauses.

Multiple sets of values require only a single VALUE clause

Each set enclosed in parentheses separated by commas.MySQL>INSERT INTO department (dept_nbr, dept_name,

2 location) 3 VALUES (50, ‘Development’, ‘Detroit’), 4 VALUES (60, ‘Huntsville’, ‘MIS’);

Page 11: Manipulating Data

“…delete a row from DEPT table…”

Removing a Row from a Table department dept_nbr dept_name location

10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston 50 Development Detroit 60 MIS ...

departmentdept_nbr dept_name location

10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston 60 MIS ...

Page 12: Manipulating Data

The DELETE Statement

You can remove existing rows from a table by using the DELETE statement.

DELETE [FROM] table[WHERE condition];

Page 13: Manipulating Data

Deleting Rows from a Table

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.

MySQL>DELETE FROM department 2 WHERE dept_name = 'Development'; 1 row deleted.

MySQL> DELETE FROM department;4 rows deleted.

Page 14: Manipulating Data

Deleting Rows: Integrity Constraint Error

MySQL>DELETE FROM department 2 WHERE dept_nbr = 10;

ERROR 1451: Cannot delete or update a pa……… You cannot delete a row that contains a primary key that

is used as a foreign key in another table.

Row: a foreign key constraint foils

Page 15: Manipulating Data

Database Transactions

May contain one of the following statements:DML statements (INSERT, UPDATE, DELETE) that make up one consistent change to the dataOne DDL statement (CREATE, ALTER, DROP)One DCL statement (GRANT, REVOKE)

Page 16: Manipulating Data

Explicit Transaction Processing

Begin with a START TRANSACTION statementEnd with one of the following events:

COMMIT or ROLLBACKDDL or DCL statement executes (automatic commit)Certain errors, exit, or system crash

Page 17: Manipulating Data

Advantages of COMMIT & ROLLBACK

Ensure data consistencyPreview data changes before making changes permanentGroup logically related operations

Page 18: Manipulating Data

DELETE

Controlling TransactionsTransaction

Savepoint A

ROLLBACK to Savepoint B

DELETE

Savepoint BCOMMIT

INSERTUPDATE

ROLLBACK to Savepoint A

INSERTUPDATEINSERT

ROLLBACK

INSERT

Page 19: Manipulating Data

State of the Data BeforeCOMMIT or ROLLBACK

The previous state of the data can be recovered because the database buffer is affected.The current user can review the results of the DML operations by using the SELECT statement.Other users cannot view the results of the DML statements by the current user.The affected rows are locked; other users cannot change the data within the affected rows.

Page 20: Manipulating Data

State of the Data After COMMIT

Data changes are made permanent in the database.The previous state of the data is permanently lost.All users can view the results.Locks on the affected rows are released; those rows are available for other users to manipulate.All savepoints are erased.

Page 21: Manipulating Data

Implicit Transaction Processing

Autocommit is the default transaction processing mode in MySQLAn automatic commit (database update) occurs under the following circumstances:

A DML statement is completedA DDL statement is issuedA DCL statement is issued

An automatic rollback occurs under an abnormal termination of MySQL or system failure

Page 22: Manipulating Data

Change Default Autocommit The default setting may be changed by issuing the following statement:

SET autocommit = 0; 0 = false1 = true

Setting the autocommit = 0 instructions the MySQL to not automatically commit changes, unless the flag is set back to true.The autocommit flag is per connection, and NOT server-wide

Page 23: Manipulating Data

Committing Data

Make the changes.

Commit the changes.

MySQL>UPDATE employee 2 SET dept_nbr = 10 3 WHERE employee_nbr = 7782;1 row updated.

MySQL> COMMIT;Commit complete.

Page 24: Manipulating Data

State of the Data After ROLLBACK

Discard all pending changes by using the ROLLBACK statement.

Data changes are undone.Previous state of the data is restored.Locks on the affected rows are released.

MySQL> DELETE FROM employee;14 rows deleted.MySQL> ROLLBACK;Rollback complete.

Page 25: Manipulating Data

Rolling Back Changes to a Marker

Create a marker within a current transaction by using the SAVEPOINT statement.Roll back to that marker by using the ROLLBACK TO SAVEPOINT statement.

MySQL> UPDATE...MySQL> SAVEPOINT update_done;Savepoint created.MySQL> INSERT...MySQL> ROLLBACK TO update_done;Rollback complete.

Page 26: Manipulating Data

SummaryStatement Description

INSERT Adds a new row to the table

UPDATE Modifies existing rows in the table

DELETE Removes existing rows from the table

START TRANSACTION Marks the beginning of explicit transaction processing

COMMIT Makes all pending changes permanent

SAVEPOINT Allows a rollback to the savepoint marker

ROLLBACK Discards all pending data changes

SET AUTOCOMMET 0 = False (OFF)1 = True (ON)