Inserting Data into Tables in MySQL

By: Sathya Narayana Emailed: 1729 times Printed: 2338 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

To insert data, follow these steps:

  1. For example, to insert data for a book titled Lord of the Things, you'd enter the following:

    mysql> INSERT INTO book (title, price) VALUES
              -> ('Lord of the Things', 9.99);
    

    Note that you specify the fields (or columns) that are to be populated by this command after the table name and then list the values for these fields in the same order. All fields and values are separated by commas, and strings are delimited by single or double quotes.

  2. To check that the data has been placed in your table correctly, run the following command:

    mysql> SELECT id, title, price, status FROM book;
    

    This should display something similar to this:

    +----+--------------------+-------+--------+
    | id | title              | price | status |
    +----+--------------------+-------+--------+
    | 1  | Lord of the Things | 9.99  | P      |
    +----+--------------------+-------+--------+
    

    Notice that as an auto-increment field, the id column has automatically been set to 1 for this first record. Also, the status column has the value P because this is the default you set earlier.

  3. Now you'll add details for another couple of books. You can insert more than one record at a time by giving more than one set of values, each separated by commas. Run the following SQL command:

    mysql> INSERT INTO book (title, price) VALUES
        -> ('Mr Bunny\'s Guide to JDO', 14.99),
        -> ('Parachuting for You and Your Kangaroo', 19.99);
    

    Notice that the first of these two books demonstrates a title string that contains a single quote character. You can't use a quote character as is because MySQL would assume that you're finishing the string at that point and return an error when it came to the rest of our string.

    To get around this, you have to use escape characters, where you precede the quote with a backslash character (\) to make \'. Astute readers might be wondering what you'd do if your book title contained the sequence \'. You'd then need to use the \\ escape character for the backslash, followed by the one for the single quote, to make \\\' altogether. Alternatively, you could simply change your string to use double quotes at either end because as long as the type of quotes (single or double) surrounding the string is different from those inside it, there won't be any confusion.

  4. Check that all three books have been added as expected by running the same SELECT statement you used in step 2. Note how the id field is incremented for each new record.


MySQL Home | All MySQL Tutorials | Latest MySQL Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Sathya Narayana
Querying Data with the JSTL in JSP
Deleting Data in MySQL
Modifying data and using WHERE clause in MySQL
Querying the Database in MySQL
Inserting Data into Tables in MySQL
Changing the Structure of an Existing Table in MySQL
The Exception Object in JSP
The Application Object in JSP
The Session Object in JSP
The Out Object in JSP
The Response Object in JSP
The Request Object in JSP
Tag Libraries in JSP
Scriptlets and Expressions in JSP
Declarations in JSP

More Tutorials in MySQL
Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access them.
ERROR 1251: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Changing the Structure of an Existing Table in MySQL
Inserting Data into Tables in MySQL
Querying the Database in MySQL
Modifying data and using WHERE clause in MySQL
Deleting Data in MySQL
What is SQL Injection
MySQL Strengths and Weaknesses
Table __________ is marked as crashed and should be repaired.

More Latest News
Most Viewed Articles (in MySQL )
Changing the Structure of an Existing Table in MySQL
Table __________ is marked as crashed and should be repaired.
Inserting Data into Tables in MySQL
Querying the Database in MySQL
Modifying data and using WHERE clause in MySQL
Deleting Data in MySQL
ERROR 1251: Client does not support authentication protocol requested by server; consider upgrading MySQL client
What is SQL Injection
MySQL Strengths and Weaknesses
Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access them.
Most Emailed Articles (in MySQL)
MySQL Strengths and Weaknesses
Inserting Data into Tables in MySQL
Modifying data and using WHERE clause in MySQL
Deleting Data in MySQL
ERROR 1251: Client does not support authentication protocol requested by server; consider upgrading MySQL client
Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access them.
Table __________ is marked as crashed and should be repaired.
What is SQL Injection
Changing the Structure of an Existing Table in MySQL
Querying the Database in MySQL