mysql load 主键验证_MySQL
程序员文章站
2022-04-10 14:49:20
...
load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';
1.如果id不是主键,会不停增加。
mysql> load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';
Query OK, 2 rows affected (0.02 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from test;
+------+-----------+
| id | name |
+------+-----------+
| 1 | wang |
| 2 | xia |
| 3 | wang-load |
| 4 | xia-load |
+------+-----------+
4 rows in set (0.00 sec)
mysql> load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';
Query OK, 2 rows affected (0.04 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from test;+------+-----------+
| id | name |
+------+-----------+
| 1 | wang |
| 2 | xia |
| 3 | wang-load |
| 4 | xia-load |
| 3 | wang-load |
| 4 | xia-load |
+------+-----------+
6 rows in set (0.00 sec)
2.如果id是主键,会skip
mysql> alter table test add PRIMARY KEY (id);
Query OK, 2 rows affected (0.38 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test;+----+------+
| id | name |
+----+------+
| 1 | wang |
| 2 | xia |
+----+------+
2 rows in set (0.00 sec)
mysql> load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';
Query OK, 2 rows affected (0.05 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from test;+----+-----------+
| id | name |
+----+-----------+
| 1 | wang |
| 2 | xia |
| 3 | wang-load |
| 4 | xia-load |
+----+-----------+
4 rows in set (0.00 sec)
修改TXT文件
mysql> load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';Query OK, 0 rows affected (0.00 sec)
Records: 2 Deleted: 0 Skipped: 2 Warnings: 0
mysql> select * from test;+----+-----------+
| id | name |
+----+-----------+
| 1 | wang |
| 2 | xia |
| 3 | wang-load |
| 4 | xia-load |
+----+-----------+
4 rows in set (0.00 sec)
1.如果id不是主键,会不停增加。
mysql> load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';
Query OK, 2 rows affected (0.02 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from test;
+------+-----------+
| id | name |
+------+-----------+
| 1 | wang |
| 2 | xia |
| 3 | wang-load |
| 4 | xia-load |
+------+-----------+
4 rows in set (0.00 sec)
mysql> load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';
Query OK, 2 rows affected (0.04 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from test;+------+-----------+
| id | name |
+------+-----------+
| 1 | wang |
| 2 | xia |
| 3 | wang-load |
| 4 | xia-load |
| 3 | wang-load |
| 4 | xia-load |
+------+-----------+
6 rows in set (0.00 sec)
2.如果id是主键,会skip
mysql> alter table test add PRIMARY KEY (id);
Query OK, 2 rows affected (0.38 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test;+----+------+
| id | name |
+----+------+
| 1 | wang |
| 2 | xia |
+----+------+
2 rows in set (0.00 sec)
mysql> load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';
Query OK, 2 rows affected (0.05 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from test;+----+-----------+
| id | name |
+----+-----------+
| 1 | wang |
| 2 | xia |
| 3 | wang-load |
| 4 | xia-load |
+----+-----------+
4 rows in set (0.00 sec)
修改TXT文件
mysql> load data LOCAL infile '~/Documents/load_test.txt' INTO TABLE test FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '/n';Query OK, 0 rows affected (0.00 sec)
Records: 2 Deleted: 0 Skipped: 2 Warnings: 0
mysql> select * from test;+----+-----------+
| id | name |
+----+-----------+
| 1 | wang |
| 2 | xia |
| 3 | wang-load |
| 4 | xia-load |
+----+-----------+
4 rows in set (0.00 sec)
下一篇: Mysql错误消息 语言设置_MySQL
推荐阅读
-
向Mysql主键自增长表中添加数据并返回主键
-
MySQL源码探索01连接处理和登录验证以及协议类型
-
MySQL中主键为0与主键自排约束的关系详解(细节)
-
Mysql注入中的outfile、dumpfile、load_file函数详解
-
mysql出现Error performing load command的解决方法
-
mysql遇到load data导入文件数据出现1290错误的解决方案
-
Oracle与Mysql主键、索引及分页的区别小结
-
mysql下创建字段并设置主键的php代码
-
面试|简单描述MySQL中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
-
php+mysql 实现身份验证代码