Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The VeryTime,Stay informed and read the latest news today from The VeryTime, the definitive source.

How to Insert Into a MySQL Database

11

Instructions

1

Gather your data, since some tables have constraints disallowing the creation of a new row without all of the columns present. It's also faster to insert entire rows of data instead of inserting some data and adding to the row later on. This table has name, address, phone number and balance fields. Creating a new customer looks like: Example:
"John Doe", "24 Main St.", "555-5555", 0.00


2

Insert the data, keeping it in the same order as the table definition. This is because, with this first form of the INSERT command, you're inserting data for every column instead of listing the columns you want to insert. To get the order of the columns for the customers table, use the "DESCRIBE customers" command. Example:
INSERT INTO customers VALUES ("John Doe", "24 Main St.", "555-5555", 0.00);

3

Leave undefined columns as the default data if you can only insert some of the data. Each column already has a default value to use if you have no choice but to insert some of the data later. You have to pass a list of columns to assign to along with your data. In this example, since you only know the name and phone number of the customer, those are the only two fields you can insert. Example:
INSERT INTO customers (name,ph_number) VALUES ("John Doe","555-5555");

4

Optimize database access by making as few queries as possible. Do this by inserting multiple rows at the same time. Use multiple parenthesized datasets separated by commas after the VALUE keyword. Example:
INSERT INTO customers (name,ph_number) VALUES ("John Doe","555-5555"),("Jack Smith","555-6666"),("Jill Brown","555-7777");

5

Use a SELECT command instead of the VALUES keyword when you want to store the results of a SELECT query in another table. This can be used to export data from one table and import it into another with a single query. Make sure you get the order of the columns correct, as well. Here, data from an old customer database (the table old_customers) migrates to a new customer database. Example:
INSERT INTO customers (name,ph_number,address,balance) SELECT name,ph_number,address,balance FROM old_customers;

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.