Presentation is loading. Please wait.

Presentation is loading. Please wait.

Insert, Update, and Delete Statements DBMS Course.

Similar presentations


Presentation on theme: "Insert, Update, and Delete Statements DBMS Course."— Presentation transcript:

1 Insert, Update, and Delete Statements DBMS Course

2 Lesson Outlines  Using Insert Statement  Using Update Statement  Using Delete Statement 2

3 INSERT  The INSERT INTO statement is used to insert new records in a table.  It is possible to write the INSERT INTO statement in two forms: 1.The first form does not specify the column names where the data will be inserted, only their values: INSERT INTO table_name VALUES ( value1, value2, value3,...) 3

4 INSERT 2.The second form specifies both the column names and the values to be inserted: INSERT INTO table_name ( column1, column2,...) VALUES ( value1, value2, value3,...); 4

5 Rules of INSERT dataValueList must match columnList as follows: number of items in each list must be same; must be direct correspondence in position of items in two lists; data type of each item in dataValueList must be compatible with data type of corresponding column. 5

6 Example: INSERT … VALUES Insert a new row into Staff table supplying data for all columns. INSERT INTO Staff VALUES (‘SG16’, ‘Alan’, ‘Brown’, ‘Assistant’, ‘M’, Date‘1957-05-25’, 8300, ‘B003’) 6

7 Example: INSERT using Defaults Insert a new row into Staff table supplying data for all mandatory columns. INSERT INTO Staff (staffNo, fName, lName, position, salary, branchNo) VALUES (‘SG44’, ‘Anne’, ‘Jones’, ‘Assistant’, 8100, ‘B003’) Or INSERT INTO Staff VALUES (‘SG44’, ‘Anne’, ‘Jones’, ‘Assistant’, NULL, NULL, 8100, ‘B003’) 7

8 INSERT … SELECT  Second form of INSERT allows multiple rows to be copied from one or more tables to another: INSERT INTO TableName [ (columnList) ] SELECT... 8

9 UPDATE  The UPDATE statement is used to update existing records in a table.  Syntax: UPDATE TableName SET columnName1 = dataValue1 [, columnName2 = dataValue2...] [WHERE searchCondition] 9

10 UPDATE  TableName can be name of a base table.  SET clause specifies names of one or more columns that are to be updated.  WHERE clause is optional:  if omitted, named columns are updated for all rows in table;  if specified, only those rows that satisfy searchCondition are updated.  New dataValue(s) must be compatible with data type for corresponding column. 10

11 Example: UPDATE All Rows Give all Managers a 5% salary increase. UPDATE Staff SET salary = salary*1.05 WHERE position = ‘Manager’ 11

12 Example: UPDATE Multiple Columns Promote David Ford (staffNo=‘SG14’) to Manager and change his salary to $18,000. UPDATE Staff SET position = ‘Manager’, salary = 18000 WHERE staffNo = ‘SG14’ 12

13 DELETE  The DELETE statement is used to delete rows in a table.  Syntax DELETE FROM TableName [WHERE searchCondition]  TableName can be name of a base table or an updatable view.  searchCondition is optional; if omitted, all rows are deleted from table. This does not delete table. If search_condition is specified, only those rows that satisfy condition are deleted. 13

14 Example: DELETE Specific Rows Delete all renting's that relate to property number PG4. DELETE FROM Renting WHERE propertyNo = ‘PG4’ Delete all records from the renting table. DELETE FROM Renting 14

15


Download ppt "Insert, Update, and Delete Statements DBMS Course."

Similar presentations


Ads by Google