Inserting Data into Tables in MySQL
By: Sathya Narayana in MySQL Tutorials on 2010-10-24
To insert data, follow these steps:
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.
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.
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.
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
Use a dynamic table name in a SQL Server SELECT statement
Finding slow queries in MySQL - Enable slow query log.
mysqldumpslow in MySQL - Summarize slow query log.
Sample my.cnf (my.ini) for MySQL with 1GB RAM
Modify a auto_increment id column in mysql to accept a 5 digit random number instead
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