2. dml_insert_delete_update

12
2 Manipulating Data in Tables Ms. Amrit Kaur [email protected]

Upload: amrit-kaur

Post on 15-Feb-2017

82 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2. DML_INSERT_DELETE_UPDATE

2Manipulating Data in Tables

Ms. Amrit [email protected]

Page 2: 2. DML_INSERT_DELETE_UPDATE

Storing Data in a Table

• The data that you can add in a table is a row.• INSERT Statement is used to add row in a

table.

INSERT INTO tablename [column_list)]VALUES ( {value_list | select_statement})

Page 3: 2. DML_INSERT_DELETE_UPDATE

• Inserting New Data

• Inserting Partial Data

• Copying Data from Existing Table

• Inserting Data Interactively– substitution operator (&) is used for reading data from

keyboard.

Page 4: 2. DML_INSERT_DELETE_UPDATE

Guidelines for Inserting Rows

• The number of data values must be same as the number of columns in the table or column list.

• The order of inserting the information must be same as the order in which columns are listed in table or column list

• The data type of information must match with the data types of the column.

Page 5: 2. DML_INSERT_DELETE_UPDATE

Storing Data Using SubqueriesINSERT INTO table_name [ (column1 [, column2 ]) ] SELECT [ *|column1 [, column2 ] FROM table1[ WHERE search_condition];

Page 6: 2. DML_INSERT_DELETE_UPDATE

UPDATING Data in a Table

• UPDATE statement is use to make changes in an existing row(s).

• We can update one or more than one column of a row.

UPDATE table_nameSET column_name=value [, column_name=value, ...][WHERE condition]

Page 7: 2. DML_INSERT_DELETE_UPDATE

Guidelines for Updating Rows

• An update can be done only one table at a time.

• If an update violates integrity constraints, entire update is rollback.

• If WHERE clause is not used, all rows of a table will be changed to new values.

Page 8: 2. DML_INSERT_DELETE_UPDATE

Updating Data using SubqueriesUPDATE table_name SET column_name = { new_value | [(SELECT [*|column1[,column2

] FROM table_name) ][WHERE condition]

Page 9: 2. DML_INSERT_DELETE_UPDATE

Deleting Data from a Table

• DELETE statement is used to delete row(s) from a table. DELETE FROM table_name

[WHERE search_condition]

• TRUNCATE statement can also be used to delete row(s) from a table.

TRUNCATE TABLE table_name

Page 10: 2. DML_INSERT_DELETE_UPDATE

DELETE vs TRUNCATE

• DELETE is a DML statement whereas TRUNCATE is DDL statement.

• DELETE used to remove rows and WHERE clause is used to remove some rows whereas TRUNCATE command delete all rows

• DELETE operations can be rolled back whereas TRUCATE operation cannot be rolled back.

Page 11: 2. DML_INSERT_DELETE_UPDATE

DELETE vs TRUNCATE

• DELETE will fire trigger whereas No triggers will be fired with TRUNCATE command.

• DELETE does not free the space containing the table whereas TRUNCATE free space containing table and can be used by another table.

Page 12: 2. DML_INSERT_DELETE_UPDATE

Delete Data using SubqueriesDELETE FROM table_name[WHERE column_name operator [(SELECT column_name FROM table_name [ WHERE condition])