Now, let us populate the blog
database that we had created in the previous section with some sample data.
Here is how we can insert data into the users
table:
INSERT INTO users (id, name, email, password, created_at, updated_at, deleted)
VALUES (1, 'Alice', 'alice@test.com', 'password123', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0),
(2, 'Bob', 'bob@test.com', 'password456', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0),
(3, 'Charlie', 'charlie@test.com', 'password789', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0);
Let us explain the above SQL query:
INSERT INTO
is a SQL command used to insert new rows into a table.users
is the name of the table.id
,name
,email
,password
,created_at
,updated_at
, anddeleted
are the columns in the table.VALUES
specifies the values to be inserted into the columns.UNIX_TIMESTAMP()
is a MySQL function that returns the current Unix timestamp.
Now, let us insert data into the posts
table:
INSERT INTO posts (id, user_id, title, body, created_at, updated_at, deleted)
VALUES (1, 1, 'First Post', 'This is the first post.', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0),
(2, 2, 'Second Post', 'This is the second post.', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0),
(3, 3, 'Third Post', 'This is the third post.', UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0);
In this query:
INSERT INTO
is used to insert new rows into theposts
table.id
,user_id
,title
,body
,created_at
,updated_at
, anddeleted
are the columns in the table.VALUES
specifies the values to be inserted into the columns.UNIX_TIMESTAMP()
is used to set thecreated_at
andupdated_at
columns to the current Unix timestamp.
Now, you have successfully added data to the users
and posts
tables in the blog
database. You can use these tables to store user information and blog posts in your application.