2 3 4 5 6 7 8

Insert statement

17

Before you can read something from the table, you need to put that something there in the first place, right? To create a record in SQL you use insert statement.

insert into `user` (`name`, `email`, `password`, `registered`) values
    ('Silvia', 'silvia@gmail.com', 'silvia1990', '2019-05-12');
Query OK, 1 row affected (0.06 sec)

We've specified a table name where we're going to insert data into. After that we've enumerated column names which values we're going to insert. And finally, we've put their corresponding data values. Those values should have the same order as column names in the statement before values keyword. They don't have to match the order of columns in the table. Order of names and values should match within the same insert statement only.

You can insert multiple records within the same insert statement:

insert into `user` (`name`, `email`, `password`, `registered`) values
    ('Angel', 'angel@springville.com', '1PzIjSWmj3', '2020-02-07'),
    ('Carolina', 'carolina@studio.org', 'funnybunny95', '2020-06-12'),
    ('Michael', 'michael@mail.com', '84738257342', '2020-09-24');
Query OK, 3 rows affected (0.12 sec)
Records: 3  Duplicates: 0  Warnings: 0

In real life database you shouldn't keep user's passwords in an unencrypted way. We'll discuss how to do this properly some later.

Alright, the records has been inserted into the table. Now it's time to learn how to read them.

Rate this post:
Lesson 4
Share this page:

Learning plan

What is a database and how to create it
3. Table
How to create a table — entities a database consists of
4. CRUD
Create, read, update, delete
5. Insert statement
How to create one or multiple records in a table
How to query records from a table
How to update previously created data in a table
How to delete data from a table