I have a table defined as follows:
CREATE TABLE `mydb`.`users` (
`userID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`userName` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`createdDate` datetime NOT NULL,
`active` tinyint(1) NOT NULL,
`lastUpdatedDate` datetime NOT NULL,
PRIMARY KEY (`userID`,`userName`) USING BTREE
)
However, when I run this query:
REPLACE INTO users SET userName ='Joe', active=1, lastUpdatedDate=now()
it inserts multiple rows with a userName Joe even though that is a primary key. I think because both userID and userName are primary keys, the key is only when both of them match. However, I can't remove the primary key on the userID as mysql throws an error. I want the userName to be distinct so there is only one record for each userName, and I want the replace statement to just update the lastUpdatedDate if the userName already exists (or insert it if it doesn't exist). How can I accomplish this?