Wednesday, May 28, 2008

INSERT information into your database with SQL

Inserting information into your database is also very simple. Just use the INSERT keyword, along with the data values that you want to put into your database. This creates a new row, or 'record', of information.

INSERT INTO Persons VALUES ('Jerry Smith', '32', 'Level 21')

This creates a new row which includes Jerry Smith, his age, and his level. You can also leave information empty in a new row, by telling SQL which fields you are adding. Do this with a slightly modifying INSERT INTO statement.

INSERT INTO Persons (Name, Level) VALUES ('Jack Smith', 'Level 8')

Notice that we don't know how old Jack is? This simply leaves Jack's age empty in the database. Sometimes, your database may not allow an empty (also called a NULL or NIL) value, so you might get an error. In that case, you might need to set something like 0 for a default age.

No comments: