Table of Contents

INSERT

To add data to your database, you would use the INSERT statement. You can insert data into one table at a time only.

The syntax is the following:

INSERT INTO table_name(column_name_1,column_name_2,column_name_n) VALUES('value_1', 'value_2', 'value_3');

You would start with the INSERT INTO statement, followed by the table that you want to insert the data into. Then you would specify the list of the columns that you want to insert the data into. Finally, with the VALUES statement, you specify the data that you want to insert.

The important part is that you need to keep the order of the values based on the order of the columns that you've specified.

In the above example the value_1 would go into column_name_1, the value_2 would go into column_name_2 and the value_3 would go into column_name_x

Let's use the table that we created in the last chapter and insert 1 user into our users table:

INSERT INTO users(username, email, active) VALUES('greisi', '[email protected]', true);

Rundown of the insert statement:

  • INSERT INTO users: first, we specify the INSERT INTO keywords which tells MySQL that we want to insert data into the users table.
  • users (username, email, active): then, we specify the table name users and the columns that we want to insert data into.
  • VALUES: then, we specify the values that we want to insert in.

Inserting multiple records

We've briefly covered this in one of the previous chapters, but in some cases, you might want to add multiple records in a specific table.

Let's say that we wanted to create 5 new users, rather than running 5 different queries like this:

INSERT INTO users(username, email, active) VALUES('user1', '[email protected]', true);
INSERT INTO users(username, email, active) VALUES('user1', '[email protected]', true);
INSERT INTO users(username, email, active) VALUES('user1', '[email protected]', true);
INSERT INTO users(username, email, active) VALUES('user1', '[email protected]', true);
INSERT INTO users(username, email, active) VALUES('user1', '[email protected]', true);

What you could do is to combine this into one INSERT statement by providing a list of the values that you want to insert as follows:

INSERT INTO users
  ( username, email, active )
VALUES
  ('user1', '[email protected]', true),
  ('user2', '[email protected]', true),
  ('user3', '[email protected]', true),
  ('user4', '[email protected]', true),
  ('user5', '[email protected]', true);

That way, you will add 5 new entries in your users table with a single INSERT statement. This is going to be much more efficient.