HomeHome   About ScottAbout Scott   FacebookFacebook   TwitterTwitter   Email NewsletterNewsletter   RSS FeedRSS Feed

We’ll be progressing from simply selecting records from a database to actually changing the data that resides in it. In this installment, we’ll add data with SQL’s insert statement.

The insert statement is a very straightforward one, and you’ll be surprised on how easy it is to add records to a database. Here is a simplistic example of what the code looks like:

INSERT INTO table_name (column_name1, column_name2, column_name3, column_name4)
VALUES ('value_1','value_2','value_3','value_4')

The first of the two lines needed begins with the insert into clause, proceeded by the name of the table, then in parenthesis all the columns you wish to insert data into. The second line starts with the term value followed by the individual records that are to be inserted into the table. The specific records are enclosed in single quotation marks, separated by commas, and the entire group lays inside parenthesis.

To help you understand this better, let’s take a peek at what the code would look like if we were using our previous “people_list” table:

INSERT INTO people_list (first_name, last_name, age, sex, species)
VALUES ('Tom','Servo','37','male','bot')

If we executed this code, we would have inserted a new row in the “people_list” table. Each value would be inserted in the order of which they were given in the first line of the code; “Jack” would be placed in the “first_name” colmun, etc. So you want to be sure that the values in the second line are typed in the same order as the column names in the first line, or else when you query the table later for data you might get some bizarre records popping up. And you also want to make sure that the number of values does not exceed or fall short of the number of column names.

Coming soon: part four!

Other installments in the SQL Nuts and Bolts series:
Part One | Part Two

SQL Nuts and Bolts (Part 3)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.