2. dml_insert_delete_update
TRANSCRIPT
2Manipulating Data in Tables
Ms. Amrit [email protected]
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})
• Inserting New Data
• Inserting Partial Data
• Copying Data from Existing Table
• Inserting Data Interactively– substitution operator (&) is used for reading data from
keyboard.
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.
Storing Data Using SubqueriesINSERT INTO table_name [ (column1 [, column2 ]) ] SELECT [ *|column1 [, column2 ] FROM table1[ WHERE search_condition];
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]
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.
Updating Data using SubqueriesUPDATE table_name SET column_name = { new_value | [(SELECT [*|column1[,column2
] FROM table_name) ][WHERE condition]
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
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.
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.
Delete Data using SubqueriesDELETE FROM table_name[WHERE column_name operator [(SELECT column_name FROM table_name [ WHERE condition])