Insert and Query Temporary Table Data
In this step, you will learn how to insert data into the temporary table you created in the previous step and then query that data. This will demonstrate how to use temporary tables for storing and retrieving information within a session.
Continuing from the previous step, you should still be connected to the MySQL server with the mysql>
prompt. If you are not, reconnect using the following command:
mysql -u root -p
Enter the password password
when prompted.
Now, let's insert some data into the temp_employees
table. Use the INSERT INTO
statement to add records.
INSERT INTO temp_employees (name, department) VALUES ('Alice Smith', 'Sales');
INSERT INTO temp_employees (name, department) VALUES ('Bob Johnson', 'Marketing');
INSERT INTO temp_employees (name, department) VALUES ('Charlie Brown', 'IT');
These commands insert three rows into the temp_employees
table, with names and departments for each employee.
To verify that the data has been inserted correctly, you can query the table using the SELECT
statement.
SELECT * FROM temp_employees;
This command will retrieve all columns and rows from the temp_employees
table. The output should look similar to this:
+----+---------------+------------+
| id | name | department |
+----+---------------+------------+
| 1 | Alice Smith | Sales |
| 2 | Bob Johnson | Marketing |
| 3 | Charlie Brown | IT |
+----+---------------+------------+
3 rows in set (0.00 sec)
You can also use WHERE
clause to filter the data. For example, to select only employees in the 'Marketing' department:
SELECT * FROM temp_employees WHERE department = 'Marketing';
The output should be:
+----+-------------+------------+
| id | name | department |
+----+-------------+------------+
| 2 | Bob Johnson | Marketing |
+----+-------------+------------+
1 row in set (0.00 sec)
This demonstrates how to insert data into a temporary table and query it using standard SQL commands. The data will only be available within your current MySQL session.