Inserting into multiple tables in MySQL
This article will introduce transactions and stored procedures for inserting into multiple tables in MySQL.
Inserting into multiple tables in MySQL
It is not possible to insert into multiple tables with a single MySQL command, but we can use MySQL transactions to meet the project requirements.
Let us create two tables named users and user_profiles. The users table has three attributes, user_id, user_name, and user_password, while the profiles table contains user_id, user_bio, and homepage as attributes.
See the following commands we used to create these two tables.
Sample code:
CREATE TABLE users(
user_id INT NOT NULL AUTO_INCREMENT,
user_name VARCHAR(45) NOT NULL,
user_password VARCHAR(45) NOT NULL,
PRIMARY KEY(user_id)
);
CREATE TABLE user_profiles(
user_id VARCHAR(45) NOT NULL,
user_bio VARCHAR(45) NOT NULL,
homepage VARCHAR(50) NOT NULL
);
Here, we have created two tables. Now, we can insert data into both tables at the same time in the following way.
Remember that the values of
user_profiles.user_id
andusers.user_id
are the same.
Sample code:
BEGIN;
INSERT INTO users (user_id,user_name, user_password)
VALUES (2,'username2', 'userpassword2');
SELECT @UserID := MAX(user_id) FROM users;
INSERT INTO user_profiles (user_id, user_bio, homepage)
VALUES(@UserID,'this is bio for username2', 'http://www.username2.com');
COMMIT;
Add two records and use the SELECT statement to view the results.
Output ( users table):
+---------+-----------+---------------+
| user_id | user_name | user_password |
+---------+-----------+---------------+
| 1 | username1 | userpassword1 |
| 2 | username2 | userpassword2 |
+---------+-----------+---------------+
2 rows in set (0.03 sec)
Output ( user_profiles table):
+---------+---------------------------+--------------------------+
| user_id | user_bio | homepage |
+---------+---------------------------+--------------------------+
| 1 | this is bio for username1 | http://www.username1.com |
| 2 | this is bio for username2 | http://www.username2.com |
+---------+---------------------------+--------------------------+
2 rows in set (0.00 sec)
Alternatively, we can create a stored procedure as follows to save time and effort.
Sample code:
DELIMITER ;;
CREATE PROCEDURE InsertALL()
BEGIN
INSERT INTO users (user_id,user_name, user_password)
VALUES (3,'username3', 'userpassword3');
SELECT @UserID := MAX(user_id) FROM users;
INSERT INTO user_profiles (user_id, user_bio, homepage)
VALUES(@UserID,'this is bio for username3', 'http://www.username3.com');
END ;;
DELIMITER ;
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:192 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:195 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:66 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re
Selecting multiple values using WHERE in MySQL
Publish Date:2025/04/22 Views:185 Category:MySQL
-
This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query. WHERE clause in SQL query WHERE The clause specifies the condi
Changing the connection timeout in MySQL
Publish Date:2025/04/22 Views:59 Category:MySQL
-
We are learning how to change the connection timeout in MySQL using Linux (Ubuntu 20.04) and Windows operating systems. Changing the connection timeout in MySQL Sometimes you keep losing connection to the MySQL server because the connect_ti
MySQL fix Data Is Truncated for a Column error
Publish Date:2025/04/22 Views:101 Category:MySQL
-
This article describes possible causes and solutions for the MySQL error Data is truncated for a column . Fix data truncated due to column error in MySQL Here, we will discuss the possible causes and solutions to eliminate MySQL data trunca
MySQL Error Server PID File Could Not Be Found Solution
Publish Date:2025/04/22 Views:192 Category:MySQL
-
In this article, we will study about the Error! Error Server PID File Could Not Be Found! in MySQL and its solution with full explanation. MySQL PID file The file that contains the process identification number or process ID of a running My
Get the last inserted ID using PHP MySQLi function
Publish Date:2025/04/22 Views:99 Category:MySQL
-
This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u