If not, insert, if there is, update according to the unique index


1. Create table structure
CREATE TABLE Test_Table
(
    ID BIGINT NOT NULL AUTO_INCREMENT,
    user_name VARCHAR (15),
    password VARCHAR (5),
    age INTEGER,
    sex CHAR(1),
    PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8 COLLATE =utf8_bin
;
2. Create a unique index
ALTER TABLE Test_Table
ADD UNIQUE INDEX Test_Table_In(user_name ASC, CONTRACT_COUNTRY ASC);

3. sql statement
INSERT INTO Test_Table(id, user_name, password, age, sex) VALUES(:user_name, :password, : age, :sex) ON DUPLICATE KEY UPDATE password=:password, age=:age, sex=:sex

4. Instance
   (1) Lily, 19880321, 21,
  there will be a record with id=1 in the female database
   (2) Lily, 19880321, 22, female
        Jim, 19870212, 23,
  there will be two records in the male database, Lily's age is updated to 22, and an additional record with id=2

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327038150&siteId=291194637