source
通过外部文件进行批量导入数据,或者执行 sql 语句。- 在桌面上新建一个
test.sql
文件,文件后缀为.sql
即可。
mysql> desc user; +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+----------------+
- 测试文件 sql,这里是相当于批量导入一些数据到 user 表中:
insert into user (name, age) values ('name1', 18); insert into user (name, age) values ('name2', 18); insert into user (name, age) values ('name3', 20); insert into user (name, age) values ('name4', 30); insert into user (name, age) values ('name5', 18); insert into user (name, age) values ('name6', 60); insert into user (name, age) values ('name7', 40); insert into user (name, age) values ('name8', 12); insert into user (name, age) values ('name9', 90); insert into user (name, age) values ('name10', 28);
- 然后打开数据库,通过
source
进行导入
mysql> source /Users/dengzemiao/Desktop/Other/test.sql; Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Query OK, 1 row affected (0.00 sec) ...... mysql> select * from user; +----+--------+------+ | id | name | age | +----+--------+------+ | 1 | name2 | 18 | | 2 | name3 | 20 | | 3 | name4 | 30 | | 4 | name5 | 18 | | 5 | name6 | 60 | | 6 | name7 | 40 | | 7 | name8 | 12 | | 8 | name9 | 90 | | 9 | name10 | 28 | +----+--------+------+
- 这个
.sql
文件中可以写任何 sql 语句进行使用,但是需要自己区分情况编写。