Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Using Database in Web Development-Part 4

Monday, May 12th, 2014 by Servage


Alright, we’re making some progress now. You have a database and you’ve created some tables. What about putting some data into the tables?

What is an INSERT statement?

 I’m sure that it’s not hard to guess what the INSERT statement does. However, how it works will probably need a little explanation. In order to use INSERT you will need to learn about a couple of other important keywords, INTO and VALUES.

Let’s start with INTO. In order for the DBMS to know where to put the data that you are adding it has to know what table you want the data to be added to. Using the “contact” table that we created in part 2 we will begin to create an INSERT statement to enter data:

INSERT INTO contacts

This part of the statement tells the DBMS that we are about to insert a new row of data into the table “contacts”.

Vocabulary Note: Row refers to a row of data in a table with each piece of data in its proper column.

So, how does the DBMS know what columns we want to insert data into? That’s next:

   INSERT INTO contacts (first_name,

This tells the DBMS that we are creating a row of data that will be placing information in the following columns: “first_name”, “last_name” and “email”. What about “contact_id”? Well, “contact_id” is our column that automatically generates a new number each time a new row is created so there is no point in entering a number manually. In fact, you could get an error if you try to do so.

The next keyword that you need to know is VALUES. I’m sure you will be shocked to learn that VALUES is where you list what is to be inserted into your new row. For example:

   INSERT INTO contacts (first_name,
   VALUES ('John',

Notice that the data is listed in the same order that we listed the column names above. If you list your data in a different order you could end up with data in the wrong columns and create a major disaster for yourself.

What if you don’t have anything to enter into a column? Maybe you are missing some data. Well, let’s say for the sake of argument you are missing John’s last name. Here is how you would insert that data:

   INSERT INTO contacts (first_name,
   VALUES ('John',

What is that NULL thing? NULL is how you tell the DBMS that you have nothing to enter in this column. Be careful, though. If you try to use a NULL in a column that must have a value in it you will get an error.

Should I always list all of my table columns? Yes. By listing each column you spell out exactly what you are doing. That way if during some future update someone else was reading your code they could easily see what you had intended to do, especially if the table had been added to or changed over time.

What is an UPDATE statement?

Again, it’s pretty obvious what UPDATE does. Now that you have some data in your table you may want to update it. So, let’s update John’s email address.

We know that John’s ID number is 1. (We’ll go through how you retrieve someone’s ID number in Part 4.) So, let’s update John’s email address to

   UPDATE contacts
   SET email = ''
   WHERE contact_id = 1;

Alright, here’s the breakdown. UPDATE tells the DBMS the we are going to change something in the table named “contacts”.

SET says we are going to change a row in the “email” column to “”. You can set as many columns as you like by separating each item with a comma.

WHERE then let’s the DBMS know which row we want to change. In this case we are changing the row where our ID number is 1. Notice the 1 does not have any single quotes around it. That’s because the “contact_id” column is an integer (number) and not a string (group of characters). If you did try to put quotes around the 1 it would cause an error.

Sources for Further Reading

Using Database in Web Development-Part 4, 3.7 out of 5 based on 3 ratings
Categories: Guides & Tutorials


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.