Chapter 2. Entering Information

The last chapter examined creating a database and adding tables, so now you're ready to start adding data. Most RDBMSs provide management tools that allow you to view tables and the records they hold, as well as allowing you to add, modify, and delete the data. These tools are very convenient when you have small amounts of data or when you're just testing the database. However, you don't generally enter data using the management tools. Much more common is some sort of program or Web page that acts as a pleasant front end into which the user enters and views data. This chapter's focus is on how to use SQL statements to insert, update, or delete data contained in a database.

This chapter covers the three SQL statements that deal with altering data. The first is the INSERT INTO statement, which inserts new data. The UPDATE statement updates existing data in the database. Finally, this chapter covers the DELETE statement, which (surprise, surprise) deletes records.

Inserting New Data

The INSERT INTO statement makes inserting new data into the database very easy. All you need to do is specify into which table you want to insert data, into which columns to insert data, and finally what data to insert. The basic syntax is as follows:

INSERT INTO table_name (column_names) VALUES (data_values)

This line of code adds a record to the Category table:

INSERT INTO Category (CategoryId, Category) VALUES (1, 'Thriller');

You can see that inserting data is simply a matter ...

Get Beginning SQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.