Watch our 3-minute video to find out how you can learn SQL with a live instructor.
Additional Resources

Inserting, Updating and Deleting Records

In this lesson of the SQL tutorial, you will learn...
  1. To insert records into a table.
  2. To update records in a table.
  3. To delete records from a table.

Inserting new records into a table is not difficult. Dangerously, it is even easier to update and delete records.

INSERT

To insert a record into a table, you must specify values for all fields that do not have default values and cannot be NULL.

Syntax
INSERT INTO table
(columns)
VALUES (values);

The second line of the above statement can be excluded if all required columns are inserted and the values are listed in the same order as the columns in the table. We recommend you include the second line all the time though as the code will be easier to read and update and less likely to break as the database is modified.

Code Sample: InsertsUpdatesDeletes/Demos/Insert.sql

-- Insert a New Employee

/******************************
Both of the inserts below will work in SQL Server

Oracle
******************************/
INSERT INTO Employees
(LastName, FirstName, Title, TitleOfCourtesy,
 BirthDate, HireDate, Address, City, Region,
 PostalCode, Country, HomePhone, Extension)
VALUES ('Dunn','Nat','Sales Representative','Mr.','19-Feb-1970',
 '15-Jan-2004','4933 Jamesville Rd.','Jamesville','NY',
 '13078','USA','315-555-5555','130');

/******************************
MySQL
******************************/
INSERT INTO Employees
(LastName, FirstName, Title, TitleOfCourtesy,
 BirthDate, HireDate, Address, City, Region,
 PostalCode, Country, HomePhone, Extension)
VALUES ('Dunn','Nat','Sales Representative','Mr.','1970-02-19',
 '2004-01-15','4933 Jamesville Rd.','Jamesville','NY',
 '13078','USA','315-555-5555','130');
Code Explanation

If the INSERT is successful, the output will read something to this effect:

(1 row(s) affected)

Exercise: Inserting Records

Duration: 5 to 15 minutes.

In this exercise, you will practice inserting records.

  1. Insert yourself into the Employees table.
    • Include the following fields: LastName, FirstName, Title, TitleOfCourtesy, BirthDate, HireDate, City, Region, PostalCode, Country, HomePhone, ReportsTo
  2. Insert an order for yourself in the Orders table.
    • Include the following fields: CustomerID, EmployeeID, OrderDate, RequiredDate
  3. Insert order details in the Order_Details table.
    • Include the following fields: OrderID, ProductID, UnitPrice, Quantity, Discount

UPDATE

The UPDATE statement allows you to update one or more fields for any number of records in a table. You must be very careful not to update more records than you intend to!

Syntax
UPDATE table
SET field = value,
 field = value,
 field = value
WHERE conditions;

Code Sample: InsertsUpdatesDeletes/Demos/Update.sql

-- Update an Employee

UPDATE Employees
SET FirstName = 'Nathaniel'
WHERE FirstName = 'Nat';
Code Explanation

If the UPDATE is successful, the output will read something to this effect:

(1 row(s) affected)

DELETE

The DELETE statement allows you to delete one or more records in a table. Like with UPDATE, you must be very careful not to delete more records than you intend to!

Syntax
DELETE FROM Employees
WHERE conditions;

Code Sample: InsertsUpdatesDeletes/Demos/Delete.sql

-- Delete an Employee

DELETE FROM Employees
WHERE FirstName = 'Nathaniel';
Code Explanation

If the DELETE is successful, the output will read something to this effect:

(1 row(s) affected)

Exercise: Updating and Deleting Records

Duration: 5 to 15 minutes.

In this exercise, you will practice updating and deleting records.

  1. Update your record in the Employees table to include some Notes.
  2. Raise the unit price of all products in the Products table by 10% for all products that are out of stock. This should affect 5 rows.
  3. Try to delete yourself from the Employees table. Could you?
  4. If you were not allowed to delete yourself from the Employees table, figure out what other records you have to delete so that you can.

Inserting, Updating and Deleting Records Conclusion

In this lesson of the SQL tutorial, you have learned how to insert, update, and delete records. Remember to be careful with updates and deletes. If you forget or mistype the WHERE clause, you could lose a lot of data.

To continue to learn SQL go to the top of this page and click on the next lesson in this SQL Tutorial's Table of Contents.
Last updated on 2009-05-11

Use of http://www.learn-sql-tutorial.com (Website) implies agreement to the following:

Copyright Information

All pages and graphics on Website are the property of Webucator, Inc. unless otherwise specified.

None of the content on Website may be redistributed or reproduced in any way, shape, or form without written permission from Webucator, Inc.

No Printing or saving of pages or content on Website

This content may not be printed or saved. It is for online use only.


Linking to Website

You may link to any of the pages on Website; however, you may not include the content in a frame or iframe without written permission from Webucator, Inc.


Warranties

Website is provided without warranty of any kind. There are no guarantees that use of the site will not be subject to interruptions. All direct or indirect risk related to use of the site is borne entirely by the user. All code and explanations provided on this site are provided without warranties to correctness, performance, fitness, merchantability, and/or any other warranty (whether expressed or implied).


For individual private use only

You agree not to use this online manual to deliver or receive training. If you are delivering or attending a class that is making use of this online manual, you are in violation of our terms of service. Please report any abuse to courseware@webucator.com. If you would like to deliver or receive training using this manual, please fill out the form at http://www.webucator.com/Contact.cfm