Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如果是新数据,如何插入;如果已经存在,如何更新_Mysql_Sql_Database_Sql Update_Sql Insert - Fatal编程技术网

Mysql 如果是新数据,如何插入;如果已经存在,如何更新

Mysql 如果是新数据,如何插入;如果已经存在,如何更新,mysql,sql,database,sql-update,sql-insert,Mysql,Sql,Database,Sql Update,Sql Insert,我在这里看着这个问题/答案:但我很困惑。我有这张桌子(顾客): SQL 我总是通过driver\u id进行搜索cus\u id是主键和AUTO\u INCREMENT。并始终更新姓名和年龄 所以我会使用这个声明 INSERT INTO customers (driver_id, name, age) VALUES(1234, "Bobby", 21) ON DUPLICATE KEY UPDATE name="Bobby", age=21 现在

我在这里看着这个问题/答案:但我很困惑。我有这张桌子(顾客):

SQL

我总是通过
driver\u id
进行搜索
cus\u id
是主键和
AUTO\u INCREMENT
。并始终更新
姓名
年龄

所以我会使用这个声明

INSERT INTO customers (driver_id, name, age) VALUES(1234, "Bobby", 21) ON DUPLICATE KEY UPDATE    
name="Bobby", age=21
现在的问题是,什么是复制密钥?我没有通过
cus\u id
搜索副本。我和他们的
驱动程序\u id
重复搜索

因此,在上面的语句中,它将使用
cus\u id=1
更新行,而不是插入行,因为
驱动程序id
1234
已经存在

因此,该表将如下所示:

cus_id | driver_id | name   | age
1      | 1234      | Bobby  | 21
2      | 987       | James  | 21
3      | 5000      | Jane   | 23
这是您的样品:

创建表格

MariaDB [bernd]> CREATE TABLE customers (
                   cus_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
                   driver_id INT(11),
                   name VARCHAR(32),
                   age int(11)
                );
                Query OK, 0 rows affected (0.01 sec)
现在插入数据

    MariaDB [bernd]> INSERT INTO customers (driver_id,name,age) VALUES
     (1234,'Bob',20),
     (987,'James',21),
     (5000,'Jane',23);
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
MariaDB [bernd]> SELECT * FROM customers;
+--------+-----------+-------+------+
| cus_id | driver_id | name  | age  |
+--------+-----------+-------+------+
|      1 |      1234 | Bob   |   20 |
|      2 |       987 | James |   21 |
|      3 |      5000 | Jane  |   23 |
+--------+-----------+-------+------+
3 rows in set (0.00 sec)
MariaDB [bernd]> SELECT * FROM customers;
+--------+-----------+-------+------+
| cus_id | driver_id | name  | age  |
+--------+-----------+-------+------+
|      1 |      1234 | Bobby |   21 |
|      2 |       987 | James |   21 |
|      3 |      5000 | Jane  |   23 |
+--------+-----------+-------+------+
3 rows in set (0.00 sec)
查看数据

    MariaDB [bernd]> INSERT INTO customers (driver_id,name,age) VALUES
     (1234,'Bob',20),
     (987,'James',21),
     (5000,'Jane',23);
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
MariaDB [bernd]> SELECT * FROM customers;
+--------+-----------+-------+------+
| cus_id | driver_id | name  | age  |
+--------+-----------+-------+------+
|      1 |      1234 | Bob   |   20 |
|      2 |       987 | James |   21 |
|      3 |      5000 | Jane  |   23 |
+--------+-----------+-------+------+
3 rows in set (0.00 sec)
MariaDB [bernd]> SELECT * FROM customers;
+--------+-----------+-------+------+
| cus_id | driver_id | name  | age  |
+--------+-----------+-------+------+
|      1 |      1234 | Bobby |   21 |
|      2 |       987 | James |   21 |
|      3 |      5000 | Jane  |   23 |
+--------+-----------+-------+------+
3 rows in set (0.00 sec)
添加唯一键

MariaDB [bernd]> ALTER TABLE customers ADD UNIQUE (driver_id);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
还可以使用CREATETABLE语句直接添加键

插入具有重复驱动程序id的记录

MariaDB [bernd]> INSERT INTO customers (driver_id, name, age) VALUES(1234, "Bobby", 21) ON DUPLICATE KEY UPDATE
    -> name="Bobby", age=21;
Query OK, 2 rows affected (0.01 sec)
查看更改的数据

    MariaDB [bernd]> INSERT INTO customers (driver_id,name,age) VALUES
     (1234,'Bob',20),
     (987,'James',21),
     (5000,'Jane',23);
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
MariaDB [bernd]> SELECT * FROM customers;
+--------+-----------+-------+------+
| cus_id | driver_id | name  | age  |
+--------+-----------+-------+------+
|      1 |      1234 | Bob   |   20 |
|      2 |       987 | James |   21 |
|      3 |      5000 | Jane  |   23 |
+--------+-----------+-------+------+
3 rows in set (0.00 sec)
MariaDB [bernd]> SELECT * FROM customers;
+--------+-----------+-------+------+
| cus_id | driver_id | name  | age  |
+--------+-----------+-------+------+
|      1 |      1234 | Bobby |   21 |
|      2 |       987 | James |   21 |
|      3 |      5000 | Jane  |   23 |
+--------+-----------+-------+------+
3 rows in set (0.00 sec)
现在的问题是,什么是复制密钥

您的表定义只有一个键,
cus\u id
。而且它是自动递增的,所以它永远不会被复制——毕竟,
insert
语句应该让数据库选择下一个值

如果希望
driver\u id
是唯一的,则需要在该列上设置唯一约束或索引。最简单的方法是在创建表时:

CREATE TABLE customers (
    cus_id int(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    driver_id int(4) NOT NULL UNIQUE
    name varchar(32),
    age int(3)
);
但是,您也可以在事实之后将其添加为唯一约束或索引:

alter table customers add constraint unq_customers_driver_id unique(driver_id);
或者:

create unique index unq_customers_driver_id on customers(driver_id);

不幸的是,MySQL不允许您“选择”处理哪个重复密钥。任何重复的钥匙都会触发重复钥匙更新时的
逻辑。

在驾驶员id上创建一个唯一索引,或者如果存在,则将索引更改为唯一索引one@BerndBuffen你能给我举个例子吗顺便说一句,int声明后面括号里的数字几乎是meaningless@Strawberry真正地所以我不应该把它放进去?我只是想节省空间,我知道我永远不会超过9999,是的,我不是在编造。在这种情况下,您可能需要SMALLINT。尽管要知道“空间”是非常便宜的。很有可能某人的
cus\u id
和某人的
driver\u id
可能是相同的。那么我的解决方案是什么呢。我的主要目的是确保我不需要在我的表中插入一个重复的person,并且在person已经存在时更新person。正如前面提到的@GordonLinoff一样,MySQL不允许您选择哪个键。那么我对这个问题的解决方案是什么呢?因为一个人的
cus\u id
和另一个人的
driver\u id
很可能是相同的。那么我的解决方案是什么呢。我的主要目的是确保我不必在我的表中插入重复的人,并在他们已经存在的情况下更新他们