1.建立表结构
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.建立唯一索引
ALTER TABLE Test_Table
ADD UNIQUE INDEX Test_Table_In(user_name ASC, CONTRACT_COUNTRY ASC);
3.sql语句
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.实例
(1)Lily ,19880321,21,female
数据库中会存在一条id=1的记录
(2)Lily,19880321,22,female
Jim,19870212,23,male
数据库中会存在两条记录,Lily的age更新成22,多了一条id=2 的记录