[mysql]基本構文実行

  • CREATE TABLE
mysql> create table HCDR.bureau_balance ( AA integer, BB numeric(5,0), CC varchar(3));
Query OK, 0 rows affected (0.07 sec)
mysql> DROP TABLE HCDR.bureau_balance;                                          Query OK, 0 rows affected (0.05 sec)
  • LOAD DATA
mysql> LOAD DATA INFILE '/Users/XXXX/bureau_balance.csv' INTO TABLE HCDR.bureau_balance;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ureau_balance' at line 1

うーん、エラーが出ました

mysql> SELECT @@global.secure_file_priv;
+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
| NULL                      |
+---------------------------+
1 row in set (0.00 sec)

/etc/my.cnfを作り、空の設定に

$ sudo vi /etc/my.cnf
[mysqld]
secure-file-priv = ""

sqlサーバを再起動し、確認をすると、空になったことがわかる

$ mysql.server restart
Shutting down MySQL
.. SUCCESS! 
Starting MySQL
.. SUCCESS! 
$ mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11 Homebrew

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT @@global.secure_file_priv;
+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
|                           |
+---------------------------+
1 row in set (0.00 sec)

まだエラーが出てる。。。

mysql> LOAD DATA INFILE '/Users/XXXX/bureau_balance.csv' INTO TABLE HCDR.bureau_balance IGNORE 1 LINES;
ERROR 1265 (01000): Data truncated for column 'AA' at row 1

区切り文字がデフォルトだと、commaになってなかったので、エラーが出ていた。なので、下記の形でTERMINATED BY を使って区切り文字を指定してあげる

LOAD DATA INFILE '/Users/XXXX/bureau_balance.csv' INTO TABLE HCDR.b    ureau_balance FIELDS TERMINATED BY ',' IGNORE 1 LINES;
  • SHELLから実行
mysql -uroot -p HCDR < create_bureau_balance.sql
DROP TABLE HCDR.bureau_balance;
-- などのsqlを記入