The mysql stored procedure implements multi-table writing at the same time, constructs and creates commodity data

Preface
Usually in the process of performance testing, we need to construct performance test data, some of which can be directly constructed by calling API, but there may be some scenarios that require us to directly insert data into the database, usually for some more common In the scenario we have come across, we can insert it directly into a table in a loop, but if we encounter some scenarios, such as creating a product, it may be designed to insert multiple tables at the same time, so how do we achieve it?
Database table design
Let’s take an e-commerce project as an example to insert commodity data through the database. Before creating commodity data, let’s understand the table structure. We insert image description here
insert image description here
insert image description here
can see three tables, namely commodity table, product table and Inventory table, when we create a product, we need to insert data into these three tables respectively. Then we can see that the es_product product table needs to rely on the goods_id in the es_good table;
and the inventory table es_product_store table depends on goods_id and productid.

So if it is appropriate for mysql to handle data dependencies every time it is inserted? Here we can use mysql stored procedures.

mysql stored procedure

First, we create a function in which we define the variables our script needs. First of all, we need to understand the business requirements. Actually, to create a product, the product name and serial number are usually unique, so we define this data as a variable and parameterize it later.

BEGIN
		DECLARE i INT DEFAULT 0;
		DECLARE goods_name VARCHAR(20);  -- 商品名称
		DECLARE goods_sn VARCHAR(20);    -- 商品编号
END

Before writing the script, let's think about it. Each product has its own brand id and category id. Can we set this id to be the same for each product? The answer is that it is best not to, because our online system may be distributed. If we set the same category for each product, the data may all go to the same machine, then during our stress test, there may be Always stress test a machine.

Well, usually our brand ids are self-increased in the database. For example, there are 95 brands in our database, so can I write a script to set the brand id to a random number between 1-95. Below is the script that randomly generates between 1-95.

/*随机生成品牌ID,目前平台现有品牌ID,有1-95*/
SET brands_id = FLOOR(RAND()*(95-1) + 1);

Then let's take a look at how to edit the script that inserts into the product table. Here is just a simple sql script. It is not difficult to understand in response to friends who have learned programming.

BEGIN
		DECLARE i INT DEFAULT 0;
		DECLARE goods_name VARCHAR(20);  -- 商品名称
		DECLARE goods_sn VARCHAR(20);    -- 商品编号
		DECLARE good_id INT;             -- 商品id
		DECLARE product_id INT;          -- 产品id
		DECLARE brands_id INT;           -- 品牌id
		DECLARE cats_id INT;             -- 类目id
		

		
		/*循环构造商品数据*/
		WHILE i < 1 DO
		  
			/*通过字符串拼接,循环构造商品名称,如test_1*/
			SET goods_name = CONCAT('test_', i);
			
			/*构造订单号*/
			SET goods_sn = CONCAT('SN202108120000', i);
			
			/*随机生成品牌ID,目前平台现有品牌ID,有1-95*/
			SET brands_id = FLOOR(RAND()*(95-1) + 1);
			
			/*类目id,1-93*/
			SET cats_id = FLOOR(RAND()*(93-1) + 1);

			/*商品表*/
			INSERT INTO es_goods (
				NAME,
				sn,
				brand_id,
				cat_id,
				type_id,
				goods_type,
				weight,
				market_enable,
				intro,
				price,
				cost,
				mktprice,
				params,
				disabled,
				store,
				page_title,
				meta_keywords,
				meta_description,
				p1,
				p2,
				p3,
				thumbnail,
				big,
				small,
				original 
			)
			VALUES
				(
					goods_name,
					goods_sn,
					brands_id,
					cats_id,
					45,
					'normal',
					0.00,
					1,
					'<p>我爱金士顿333</p>',
					9.9,
					9.9,
					9.9,
					'[{
    
    "name":"基本信息","paramList":[{
    
    "name":"商品尺寸","value":"5.7 x 1.7 x 1 cm ","valueList":[]},{
    
    "name":"商品重量","value":"18g","valueList":[]}],"paramNum":2}]',
					0,
					100,
					'金士顿3',
					'金士顿3',
					'金士顿3',
					1,
					1,
					1,
					'https://oss-fg.feng-go.com/assets/pic/2021/07/23ed225b6c90514054bdcd242d8321da01.jpg',
					'https://oss-fg.feng-go.com/assets/pic/2021/07/23ed225b6c90514054bdcd242d8321da01.jpg',
					'https://oss-fg.feng-go.com/assets/pic/2021/07/23ed225b6c90514054bdcd242d8321da01.jpg',
					'https://oss-fg.feng-go.com/assets/pic/2021/07/23ed225b6c90514054bdcd242d8321da01.jpg' 
				);
				
				
				SET i = i + 1;
		END WHILE;
		
	


END

So we have implemented the insertion of the commodity table, how to get the goods_id of the commodity table? Mysql provides a function, last_insert_id(); this function can get the id of the last inserted sql, so can our product table and inventory table be realized?

SET product_id = last_insert_id();

Let's take a look at how to write data to multiple tables at the same time:

BEGIN
		DECLARE i INT DEFAULT 0;
		DECLARE goods_name VARCHAR(20);  -- 商品名称
		DECLARE goods_sn VARCHAR(20);    -- 商品编号
		DECLARE good_id INT;             -- 商品id
		DECLARE product_id INT;          -- 产品id
		DECLARE brands_id INT;           -- 品牌id
		DECLARE cats_id INT;             -- 类目id
		
		/*循环构造商品数据*/
		WHILE i < 1 DO
		  
			/*通过字符串拼接,循环构造商品名称,如test_1*/
			SET goods_name = CONCAT('test_', i);
			
			/*构造订单号*/
			SET goods_sn = CONCAT('SN202108120000', i);
			
			/*随机生成品牌ID,目前平台现有品牌ID,有1-95*/
			SET brands_id = FLOOR(RAND()*(95-1) + 1);
			
			/*类目id,1-93*/
			SET cats_id = FLOOR(RAND()*(93-1) + 1);

			/*商品表*/
			INSERT INTO es_goods (
				NAME,
				sn,
				brand_id,
				cat_id,
				type_id,
				goods_type,
				weight,
				market_enable,
				intro,
				price,
				cost,
				mktprice,
				params,
				disabled,
				store,
				page_title,
				meta_keywords,
				meta_description,
				p1,
				p2,
				p3,
				thumbnail,
				big,
				small,
				original 
			)
			VALUES
				(
					goods_name,
					goods_sn,
					brands_id,
					cats_id,
					45,
					'normal',
					0.00,
					1,
					'<p>我爱金士顿333</p>',
					9.9,
					9.9,
					9.9,
					'[{
    
    "name":"基本信息","paramList":[{
    
    "name":"商品尺寸","value":"5.7 x 1.7 x 1 cm ","valueList":[]},{
    
    "name":"商品重量","value":"18g","valueList":[]}],"paramNum":2}]',
					0,
					100,
					'金士顿3',
					'金士顿3',
					'金士顿3',
					1,
					1,
					1,
					'https://oss-fg.feng-go.com/assets/pic/2021/07/23ed225b6c90514054bdcd242d8321da01.jpg',
					'https://oss-fg.feng-go.com/assets/pic/2021/07/23ed225b6c90514054bdcd242d8321da01.jpg',
					'https://oss-fg.feng-go.com/assets/pic/2021/07/23ed225b6c90514054bdcd242d8321da01.jpg',
					'https://oss-fg.feng-go.com/assets/pic/2021/07/23ed225b6c90514054bdcd242d8321da01.jpg' 
				);
				
			/*获取上一条插入商品表的自增id*/
		  SET good_id = last_insert_id();
			
			
			/*插入产品表数据,goods_id从上一条sql中获取自增id*/
			INSERT INTO es_product ( goods_id, NAME, sn, store, price, specs, cost, weight )
			VALUES
				(
					good_id,
					goods_name,
					goods_sn,
					100,
					9.9,
					'白色、L',
					9.9,
					0.00 
				);
				
			/*获取产品表中的自增id*/	
			SET product_id = last_insert_id();
			
			
			INSERT INTO es_product_store ( goodsid, productid, depotid, store )
			VALUES
				(
					good_id,
					product_id,
					1,
				100);
				
				SET i = i + 1;
		END WHILE;
		
	

END

A simple creation of commodity sql is achieved~ Let's take a look at the data inserted into the database:insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_42274846/article/details/128134279