Insert
eharetea

Insert data into a database using the INSERT statement in MySQL.

MySQL statements.

Date Created:Friday December 29th, 2006 03:41 AM
Date Modified:Friday August 01st, 2008 12:45 AM

INSERT is used to put data in the database.

Lets say this represents our "folders" table.

+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| folder_id | int(10) unsigned | NO   | PRI |         | auto_increment |
| parent_id | int(10) unsigned | NO   |     |         |                |
| title     | char(32)         | NO   |     |         |                |
+-----------+------------------+------+-----+---------+----------------+

# first we specify which columns we are modifying, then specify the values.
# the folder_id would automatically get a value

INSERT INTO folders (parent_id, title) VALUES ('21','new_category');

# this does not specify what columns we are modifying, so all columns must be entered.

INSERT INTO folders VALUES (null,'21','new_category');


# this is a way you can enter in multiple records:

INSERT INTO folders (parent_id,title) VALUES ('1','Dan'), ('2','Peter'), ('3','Will');


# using SHA(), the value is returned as a 40-digit hex number, useful for safely storing passwords.
# NOW() returns the current date and time.

INSERT INTO users VALUES (null,'Dan','Lynch','dan@some-email.com',SHA('password'),NOW());