我已将 SQL 文件导入到新数据库。每次导入时,总是会显示这些错误消息。
mysql> source ./DataWarehouse/3/mysqlsampledatabase.sql
Query OK, 1 row affected, 1 warning (0.01 sec)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
Query OK, 0 rows affected (0.05 sec)
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
Query OK, 0 rows affected (0.05 sec)
ERROR 1050 (42S01): Table 'productlines' already exists
ERROR 1050 (42S01): Table 'products' already exists
ERROR 1050 (42S01): Table 'offices' already exists
ERROR 1050 (42S01): Table 'employees' already exists
ERROR 1050 (42S01): Table 'customers' already exists
Query OK, 0 rows affected (0.10 sec)
ERROR 1050 (42S01): Table 'orders' already exists
Query OK, 0 rows affected (0.09 sec)
ERROR 1062 (23000): Duplicate entry 'Classic Cars' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry 'S10_1678' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry '1002' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry '103' for key 'PRIMARY'
ERROR 1062 (23000): Duplicate entry '10100' for key 'PRIMARY'
Query OK, 273 rows affected (0.08 sec)
Records: 273 Duplicates: 0 Warnings: 0
Query OK, 2996 rows affected (0.38 sec)
Records: 2996 Duplicates: 0 Warnings: 0
此后,查询数据库就可以正常工作了。
我怀疑这些警告是因为已经存在的表而显示的(呃),但在我设置源文件之前,查询SHOW TABLES;
只显示Empty set (0.01 sec).
。我以前使用过 MSSQL Server 和 PHPMyAdmin,但这是我第一次使用基于 CLI 的服务器,所以我不是很了解。
它可以工作,但我不想每次打开它时都导入 sql 文件。我应该先保存数据库吗?还是我遗漏了什么?
*我在 ChromeOS 上使用 Linux Crostini。
答案1
事实证明我导入的 SQL 文件有一个 USE 查询。
我只是应该使用 SQL 文件中设置的数据库,或者删除所述 SQL 文件中的 USE 查询。
感谢@glenn jackmann 指出这一点!