Library tutorials & articles

MySQL Tutorial

The INSERT statement

The insert statement is used to insert records into a table. The keywords used in an insert statement are summarised in the following table.

Keyword Description
INSERT Inserts data into a table
INTO Specifies the name of the table to insert the data
VALUES The data for the fields in the table

The following example inserts two records into the search table.

insert.sql

INSERT INTO search (Category,
        Page,
        Directory,
        LinkName,
        Keywords,
        Description)
    VALUES ('ASP',
        'cookies.html',
        'http://www.juicystudio.com/tutorial/asp/',
        'Reading Cookies',
        'request,cookies,key,subkey,domain',
        'Using the Request object to read cookies in ASP');

INSERT INTO search (Category,
        Page,
        Directory,
        LinkName,
        Keywords,
        Description)
    VALUES ('PHP',
        'cookies.html',
        'http://www.juicystudio.com/tutorial/php/',
        'Using Cookies',
        'cookies,setting,retrieving,reading,setcookie,isset',
        'Using cookies in PHP');

The file may then be used with MySQL as follows:

mysql> \. insert.sql

If a field in your table allows NULL values, you may omit the field from the INSERT statement.

Characters that require Escaping

The following characters have a special meaning to MySQL, and will need to be escaped by putting a backslash before them in order that they are interpreted correctly.

Characters Name
' Single quote
" Double quote
\ Blackslash
% Percent
_ Underscore

The following example shows how you would escape an underscore in the string, "last_modified".

'last\_modified'

You can escape single quotes by placing two single quotes together, as in the following example.

'Don''t care!'

Comments

  1. 18 May 2009 at 17:00

    More good mysql examples:

    http://www.examplenow.com/mysql/alter

    --John

  2. 05 Aug 2008 at 13:25

     

    Dear Sir/ Madam

        I need your help, how to done replication method in MySQL Server. or if any automatic schedule scripting awailble. pls send my mail id as soon as possible.

    i will waiting for your mail

    email : vinaymeetu@ymail.com, or vinaywaiting4u@gmail.com,

    Regards

    Vinay.G.Liyar

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of MySQL Tutorial.

Leave a comment

Sign in or Join us (it's free).

Gez Lemon I'm available for contract work. Please visit Juicify for details.

Related podcasts

  • Scaling Large Web Sites with Joe Stump, Lead Architect at DIGG

    Have you ever wanted to learn how top 100 web sites are architected? Deep Fried Bytes hosts Keith Elder and Chris Woodruff sat down with Joe Stump, Lead Architect at DIGG to discuss scaling large web sites, his life, development experiences and team building. Listen to the showThanks to our gues...

Events coming up

  • Dec 3

    The Auckland PHP December meetup

    Auckland, New Zealand

    Topic: Magento E-Commerce platform Speaker: Robert Popovic, LERO9, Robert is the Technical Director and co-founder of LERO9. Robert attended the Electrotechnical Faculty at The University of Belgrade where he graduated with a Masters in Computer Science and Information Technology. Robert has worked exclusively in the field of web and software development throughout his career.

We'd love to hear what you think! Submit ideas or give us feedback