Sample Header Ad - 728x90

MySQL's mysql_affected_rows() detects `SELECT INTO` as affected row

0 votes
1 answer
233 views
Dump of the test database:
-- MariaDB dump 10.19  Distrib 10.9.6-MariaDB, for Linux (x86_64)
--
-- Host: localhost    Database: book
-- ------------------------------------------------------
-- Server version	10.9.6-MariaDB

--
-- Table structure for table publisher
--

CREATE TABLE publisher (
  ID int(10) unsigned NOT NULL AUTO_INCREMENT,
  PublisherName varchar(100) NOT NULL,
  PRIMARY KEY (ID),
  UNIQUE KEY publisher_UN (PublisherName)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;


--
-- Dumping data for table publisher
--

LOCK TABLES publisher WRITE;

INSERT INTO publisher VALUES (1,'TestCase');

UNLOCK TABLES;

--
-- Dumping routines for database 'book'
--

DELIMITER ;;
CREATE  PROCEDURE ensurePublisher(
	IN v_PublisherName VARCHAR(100)
)
    MODIFIES SQL DATA
BEGIN
	DECLARE pubID INT unsigned;

	SELECT ID INTO pubID FROM publisher WHERE PublisherName = v_PublisherName LIMIT 1;

	IF ISNULL(pubID) THEN
	    INSERT INTO publisher (PublisherName) VALUES (v_PublisherName);
	END IF;
END ;;
DELIMITER ;

-- Dump completed on 2023-06-10 15:24:11
Doing a CALL ensurePublisher("TestCase"); query 100 times will return mysql_affected_rows() 1 even though duplicates won't INSERT (unique key on PublisherName). Is that intended behavior? --- Please note that this is a minimal example I came up with to show the issue I have. This one could easily just be INSERT IGNORE INTO.
Asked by Delicious Bacon (99 rep)
Jun 10, 2023, 01:47 PM
Last activity: Jun 14, 2025, 11:06 AM