Insert Data into a MySQL Database
Now that we've created our tables, let's add some data.
INSERT statement allows you to add data to your database tables. The syntax goes like this:
This inserts data into one row. The order of the values provided must correspond with the columns that the values are to be inserted into.
If you are inserting data into all columns, you can omit the column names and just do this:
To populate multiple rows, use a comma to separate each row, like this:
The above example populates 4 rows and assumes there are 3 columns in each row.
The following script can be used to dump data into our FruitShop database. We use two
INSERT statements — one for each table we want to populate. The first populates the Units table, the second populates the Fruit table.
Execute the following SQL script against the FruitShop database:
Check your Data
You can check that your data was inserted by executing the following SQL statements.
Check the Fruit Table
Select all records from the Fruit table by running the following statement:
Check the Units Table
Select all records from the Units table table by running the following statement:
We just used a
SELECT statement to query the data in our database. Let's look more closely at MySQL queries.