将要输入的命令组成一个文件,然后导入进去,执行一批的命令;
kevin.bat file show databases; use xu; show tables; desc test2; select * from test2; kevin@xu:~$ mysql -uroot -p < kevin.bat Enter password: Database information_schema kevin mysql performance_schema test xu Tables_in_xu test test2 Field Type Null Key Default Extra name varchar(20) YES NULL subname varchar(20) YES NULL telephone int(20) YES NULL address varchar(50) YES NULL name subname telephone address kevin xu 1231451 shenzhen sherry xu 1222451 shenzhen kevin@xu:~$ |
如果你想在语句出现错误的时候仍想继续执行脚本,则应使用--force命令行选项。
为什么要使用一个脚本?有很多原因:
· shell> mysql < batch-file | more
· shell> mysql < batch-file > mysql.out
当你以批模式运行mysql时,比起你交互地使用它时,其默认输出格式是不同的(更简明些)。
使用方式;必须加上mysql -t。为了回显以输出被执行的命令,使用mysql -vvv
mysql> source filename;
mysql> \. filename
kevin@xu:~$ mysql -uroot -p -t Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 46 Server version: 5.5.35-0ubuntu0.12.10.2 (Ubuntu) Copyright (c) 2000, 2013, 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> mysql> mysql> source /home/kevin/kevin.bat +--------------------+ | Database | +--------------------+ | information_schema | | kevin | | mysql | | performance_schema | | test | | xu | +--------------------+ 6 rows in set (0.00 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 +--------------+ | Tables_in_xu | +--------------+ | test | | test2 | +--------------+ 2 rows in set (0.00 sec) +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | subname | varchar(20) | YES | | NULL | | | telephone | int(20) | YES | | NULL | | | address | varchar(50) | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) +--------+---------+-----------+----------+ | name | subname | telephone | address | +--------+---------+-----------+----------+ | kevin | xu | 1231451 | shenzhen | | sherry | xu | 1222451 | shenzhen | +--------+---------+-----------+----------+ 2 rows in set (0.00 sec) |
select max(arg) from test -------------- +----------+ | max(arg) | +----------+ | 20 | +----------+ 1 row in set (0.00 sec) |
mysql> select name from test where arg=(select max(arg) from test) ; -------------- select name from test where arg=(select max(arg) from test) -------------- +--------+ | name | +--------+ | sherry | +--------+ 1 row in set (0.00 sec) |
select max(name) ,sex from test as t1 group by arg; -------------- select max(name) ,sex from test as t1 group by arg -------------- +-----------+------+ | max(name) | sex | +-----------+------+ | kevin | M | | sherry | N | +-----------+------+ 2 rows in set (0.00 sec) |
任务:对每项物品,找出最贵价格的物品的经销商。
可以用这样一个子查询解决该问题:
SELECT article, dealer, price FROM shop s1 WHERE price=(SELECT MAX(s2.price) FROM shop s2 WHERE s1.article = s2.article);
mysql> select @min_arg:=min(arg), @max_arg:=max(arg) from test; -------------- select @min_arg:=min(arg), @max_arg:=max(arg) from test -------------- +--------------------+--------------------+ | @min_arg:=min(arg) | @max_arg:=max(arg) | +--------------------+--------------------+ | 18 | 20 | +--------------------+--------------------+ 1 row in set (0.00 sec) |
下面的例子显示了如何使用位组函数来计算每个月中用户访问网页的天数。
CREATE TABLE t1 (year YEAR(4), month INT(2) UNSIGNED ZEROFILL,
day INT(2) UNSIGNED ZEROFILL);
INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2),
(2000,2,23),(2000,2,23);
INSERT INTO test (name) VALUES ('a','b','c')等等,可单独插入某一列或多列;
(AUTO_INCREMENT列是多列索引的一部分),如果你在任何组中删除有最大AUTO_INCREMENT值的行,将会重新用到AUTO_INCREMENT值。对于MyISAM表也如此,对于该表一般不重复使用AUTO_INCREMENT值。
如果AUTO_INCREMENT列是多索引的一部分,MySQL将使用该索引生成以AUTO_INCREMENT列开始的序列值。。
要想以AUTO_INCREMENT值开始而不是1,你可以通过CREATE TABLE或ALTER TABLE来设置该值,如下所示:
mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;