The MySQL INSERT query is used to insert a new row in a MySQL database. Aside from select queries, insert queries are the next most commonly used form of database query.
After all, we need to insert data into the database before we can actually run some other query on it.
In the tutorial which follows, we will look at how to properly use the MySQL insert query to insert data into our database.
The vast majority of the time, this statement will follow the following syntax:
INSERT INTO table(col1, col2, ...) VALUES('data1', 'data2', ...);
That is, you specify the name of the table that you want to insert into, the columns for which you are inserting data, and the data itself in the same order in which you specified the columns.
Suppose we had the following table called "employees" in a database:
first_name | last_name |
---|---|
Paul | Pitterson |
Francine | Beecham |
Raul | DiNozzo |
If we wanted to add a new employee called Bill May, we would add the new row using the following SQL:
INSERT INTO employees(first_name, last_name) VALUES('Bill', 'May');
After we run that query on the database, the "employees" table would look like:
first_name | last_name |
---|---|
Paul | Pitterson |
Francine | Beecham |
Raul | DiNozzo |
Bill | May |
Keep in mind that if you are inserting data into a single field, the word "VALUES" would need to be changed to "VALUE" to be considered correct, and reasonably so, since that is proper English. Remember that SQL statements are English-like commands so it is fair enough to follow some amount of English grammar.
As we can see, it is very simple to insert data into a database using the MySQL INSERT query. After we have inserted data into our table, we want to ensure that it is there by using an MySQL Select Query.
We hope this MySQL Insert tutorial has been useful.