Mysql 培训
1. Mysql 培训
1.1. 培训目的
本文档是针对MySQL 数据库方面的基础培训,为了使项目组成员能够达到使用MySQL 数据库的目的。
1.2. 培训对象
开发者
1.3. 经常使用词及符号说明
经常使用词:
Mysql:一种免费的跨平台的数据库系统
E:/mysql:表示是在dos 命令窗体下面
mysql> 表示是在mysql 的命令行下
1.4. 參考信息
http://dev.mysql.com/doc/refman/5.0/en/index.html
2. MYSQL
2.1. 连接MYSQL
格式: mysql -h主机地址 -uusername -p用户password
连接远程机器:
E:/mysql>mysql -h10.4.3.188 -uptsdb -p
等价写法
E:/mysql>mysql --host=10.4.3.188 --user=ptsdb --password
连接本地机器:
E:/mysql>mysql -uroot -p
等价写法
E:/mysql>mysql --user=root -password
(注:u与root能够不用加空格,其它也一样)
注意事项:环境变量path 里面要设定mysql的bin的路径:
C:/Program Files/MySQL/MySQL Server 5.0/bin
2.2. 改动password
方法一:使用mysqladmin
格式:mysqladmin -uusername -p旧password password 新password
例1:E:/mysql>mysqladmin -uroot password root
注:由于開始时root没有password,所以-p旧password一项就能够省略了。
例2:再将root的password改为root123。
E:/mysql>mysqladmin -uroot -proot password root123
方法二:直接更新 user 表
mysql>UPDATE user SET password=PASSWORD("test123") WHERE user='test';
mysql> FLUSH PRIVILEGES;
mysql> SET PASSWORD FOR test=PASSWORD('test123');
mysql> FLUSH PRIVILEGES;
方法三:使用 grant
格式:grant 权限 on 数据库.表格| 其它 to 用户@主机 IDENTIFIED BY 口令
例1:给test用户在本地localhost 全部权限(除了GRANT OPTION),口令为 test
(相当于改动了test 用户的口令)
mysql>grant all on *.* to test@localhost identified by "test";
等同于
mysql>grant all on *.* to test @localhost identified by PASSWORD " *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 ";
例2、添加�一个用户testpassword为abc,让他能够在不论什么主机上登录,并对test数据库有查询、插入、改动、删除的权限。首先用以root用户连入MYSQL,然后键入下面命令:
mysql>grant select,insert,update,delete on test.* to test@"%" Identified by "abc";
在mysql.user 表中,有两个test 用户
一个test 用户,在本地有全部的权限
另外的test 用户,在全部主机上有增删改查权限
同样能够取消部分权限(全部)
mysql>revoke insert,update,delete on test.* from test@"%"
mysql>REVOKE ALL PRIVILEGES, GRANT OPTION FROM test@"%"
然后 mysql> FLUSH PRIVILEGES;
Test 用户不再使用用了,也能够删除
mysql>Delete from user where user='test' and host='%'
mysql> FLUSH PRIVILEGES;
注意:例2添加�的用户是比較危急的,你想如某个人知道test的password,那么他就能够在internet上的不论什么一台电脑上登录你的mysql数据库并对你的数据库test为所欲为了(能够通过限定主机)
mysql>grant select,insert,update,delete on test.* to test@"IP地址" Identified by "abc";
2.3. 显示命令
显示数据库列表:
mysql>show databases;
mysql>show schemas; --mysql 5.0.2
显示表格
mysql>show tables from mydb;
显示表格状态
Mysql>SHOW TABLE STATUS;
显示字符集:
mysql> SHOW CHARACTER SET;
显示创建表:
mysql> show create table quote;
显示用户权限:
mysql> SHOW GRANTS FOR 'test'@'localhost';
mysql>SHOW GRANTS;
mysql>SHOW GRANTS FOR CURRENT_USER;
mysql>SHOW GRANTS FOR CURRENT_USER();
显示index:
mysql>SHOW INDEX FROM mydb.mytable;
显示表结构:
mysql>desc mydb.tablename;
mysql>show columns from mydb.tablename;
显示MySQL数据库的版本号:
mysql>select version();
显示函数
mysql>Select * from mysql.func;
显示存储过程
mysql>Select * from mysql.proc;
显示存储引擎
mysql> SHOW ENGINES;
显示变量:
mysql>SHOW VARIABLES;
显示状态:
Mysql> SHOW STATUS;
显示进程
Mysql>SHOW PROCESSLIST
显示 INNODB 状态
Mysql>SHOW INNODB STATUS
显示连接状态
Mysql>SHOW STATUS LIKE '%CONNECT%';
显示线程状态
Mysql>SHOW STATUS LIKE '%THREAD%';
等等..
2.4. 创建.改动.删除
2.4.1. 创建数据库
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification [, create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
| [DEFAULT] COLLATE collation_name
比如:
CREATE DATABASE IF NOT EXISTS ddd --假设不存在,则创建.
CHARACTER SET 'ujis' --设定字符集
COLLATE 'ujis_japanese_ci';
2.4.2. 创建表
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options] [select_statement]
样例:
CREATE TABLE if not exists `Admin_User` (
`id` int(11) NOT NULL auto_increment, --PRIMARY KEY,
`livedoorId` varchar(255) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`auth` int(11) default '0',
PRIMARY KEY (`id`) --设定主健
) ENGINE=MyISAM DEFAULT CHARSET=ujis ?设定字符集
ENGINE=MyISAM 默认存储引擎
The binary portable storage engine that is the default storage engine used by MySQL
对于每一个MyISAM 存储引擎的表,在硬盘上存在3个文件
File Purpose
tbl_name.frm Table format (definition) file
tbl_name.MYD Data file
tbl_name.MYI Index file
ENGINE= InnoDB
Transaction-safe tables with row locking and foreign keys.
ENGINE = BDB
Transaction-safe tables with page locking.
还有其它的内存引擎 MEMORY 归档 ARCHIVE 等等
ISAM 不再使用了
2.4.3. 创建索引
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
[USING index_type]
ON tbl_name (index_col_name,...)
index_col_name:
col_name [(length)] [ASC | DESC]
将customer 表的name 字段前十个字符做为索引
CREATE INDEX part_of_name ON customer (name(10));
MYSQL 5.0 特性
存储引擎为 MyISAM, InnoDB, or BDB 的表格上,能够在有null值的字段上创建索引
存储引擎为 MyISAM, InnoDB, or BDB 的表格上,能够在BLOB TEXT 上创建索引
仅仅有在MyISAM 类型表格上,能够在CHAR, VARCHAR, and TEXT 字段类型上创建FULLTEXT 索引
Storage Engine Allowable Index Types
MyISAM BTREE
InnoDB BTREE
MEMORY/HEAP HASH, BTREE
能够指定索引类型
Example:
CREATE TABLE testtable (id INT) ENGINE = MEMORY;
CREATE INDEX id_index USING BTREE ON testtable (id);
2.4.4. 改动表
ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification] ...
IGNORE 忽略主健反复的错误,假设反复,採用第一条,其余删除
样例:同一时候多个操作
mysql> ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;
样例, 重命名 INTEGER 字段,从 a 到 b:
mysql> ALTER TABLE t1 CHANGE a b INTEGER;
样例 改动字段类型,仍然须要新旧字段名称,即使字段名称同样:
mysql> ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;
也能够使用modify
mysql> ALTER TABLE t1 MODIFY b BIGINT NOT NULL;
在mysql 5.0 能够使用FIRST or AFTER 字段来加入�add 字段,默认是在最后
modify change 也能够使用
在mysql 5.0 InnoDB 存储引擎支持 ALTER TABLE 删除外健:
mysql>ALTER TABLE yourtablename DROP FOREIGN KEY fk_symbol;
样例:
创建表
mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));
重命名表
mysql> ALTER TABLE t1 RENAME t2;
MODIFY a 字段为 TINYINT NOT NULL , 而且 change 字段 b,从 CHAR(10) 到 CHAR(20) 并改名为c:
mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
加入�新字段 d:
mysql> ALTER TABLE t2 ADD d TIMESTAMP;
在a d 上添加�索引:
mysql> ALTER TABLE t2 ADD INDEX (d), ADD INDEX (a);
删除字段c:
mysql> ALTER TABLE t2 DROP COLUMN c;
加入�一个自己主动增长的字段c ,而且加入�c 为主健:
mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> ADD PRIMARY KEY (c);
2.4.4.1. 改动外键
语法:
ALTER TABLE tbl_name
ADD [CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
[ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
[ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
样例:
create table aa (id1 int not null,name varchar(20),primary key(id1))type=InnoDB;
create table b(id2 int not null,lessonname varchar(20),primary key(id2))type=InnoDB;
alter table b add FOREIGN KEY id (id2) references aa(id1);
2.4.5. 删除
删除数据库
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
样例: mysql>drop DATABASE IF EXISTS testdb;
删除表
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
样例: mysql>drop TABLE IF EXISTS testTable;
删除索引
DROP INDEX index_name ON tbl_name
样例: mysql>drop index testIndex on testTable;
重命名
RENAME TABLE tbl_name TO new_tbl_name
[, tbl_name2 TO new_tbl_name2] ...
样例:RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
2.5. 数据库备份恢复
数据库备份
数据库备份命令:
mysqldump --opt --user=username --password=password --default_character-set=字符集 -B数据库> 输出的sql文件
样例:
E:/mysql>Mysqldump --user=ptsdb --password=ptsdb --default_character-set=ujis --opt pts>dump.sql
參见批处理文件
数据库导入命令:
mysql --user=username --password=password --default_character-set=字符集 [数据库]<导入的sql 语句
E:/mysql>mysql -uptsdb -pptsdb --default-character-set=ujis
样例: 对于InnoDB(没有设定字符集)
Following mysqldump import example for InnoDB tables is at least 100x faster than previous examples.
1. mysqldump --opt --user=username --password database > dumbfile.sql
2. Edit the dump file and put these lines at the beginning:
SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS=0;
3. Put these lines at the end:
SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
SET AUTOCOMMIT = 1;
4. mysql --user=username --password database < dumpfile.sql
參数说明:
--add-drop-database
Add a DROP DATABASE statement before each CREATE DATABASE statement.
--add-drop-table
Add a DROP TABLE statement before each CREATE TABLE statement.
--all-databases, -A
Dump all tables in all databases. This is the same as using the --databases option and naming all the databases on the command line.
--databases, -B
Dump several databases. Normally, mysqldump treats the first name argument on the command line as a database name
and following names as table names. With this option, it treats all name arguments as database names.
CREATE DATABASE IF NOT EXISTS db_name and USE db_name statements are included in the output before each new database.
--host=host_name, -h host_name
Dump data from the MySQL server on the given host. The default host is localhost.
--opt
This option is shorthand; it is the same as specifying --add-drop-table --add-locks --create-options
--disable-keys --extended-insert --lock-tables --quick --set-charset.
It should give you a fast dump operation and produce a dump file that can be reloaded into a MySQL server quickly.
In MySQL 5.0, --opt is on by default, but can be disabled with --skip-opt.
To disable only certain of the options enabled by --opt, use their --skip forms;
for example, --skip-add-drop-table or --skip-quick.
另一些其它參数,有兴趣能够学习
2.6. 表数据备份
mysql>use test;
mysql> CREATE TABLE imptest(id INT, n VARCHAR(30));
Query OK, 0 rows affected (0.03 sec)
方法一:
导出使用:Mysqldump
E:/mysql>mysqldump -uptsdb -pptsdb -where "id>='100'"
test imptest
E:/mysql>mysqldump -uptsdb -pptsdb test imptest>e:/mysql/imp/imptest2.txt
导入使用 mysql
mysql -uptsdb -pptsdb< imptest2.txt
方法二:
导出使用 select into OUTFILE
mysql> select * from imptest where id=101 into OUTFILE 'e://mysql//imp//test3.txt' FIELDS TERMINATED BY ',';
导入使用 LOAD DATA INFILE
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[FIELDS
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char' ]
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]]
样例:
mysql> LOAD DATA INFILE 'e://mysql//imp//test3.txt' INTO TABLE imptest FIELDS TERMINATED BY ',';
导入使用mysqlimport:
E:/mysql>mysqlimport -uptsdb -pptsdb --local test E:/mysql/imp/imp.txt
mysqlimport: Error: Table 'test.imp' doesn't exist, when using table: imp
E:/mysql>mysqlimport -uptsdb -pptsdb --local test E:/mysql/imp/imptest.txt
test.imptest: Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
注意:文件名称必须跟表名同样
參数
-d or --delete 新数据导入数据表中之前删除数据数据表中的全部信息
-f or --force 无论是否遇到错误,mysqlimport将强制继续插入数据
-i or --ignore mysqlimport跳过或者忽略那些有同样唯一
-r or -replace 这个选项与-i选项的作用相反;此选项将替代 表中有同样唯一keyword的记录
2.7. 数据管道导入导出
E:/mysql>mysql -h10.5.1.66 -uroot -proot --default-character-set=name frontdb_20060415(databasename)
>e:/mysql/test.txt (输出的sql 语句)
1:>;create databases newname(在新的server上建立空的数据库)
2:#/usr/local/mysql/bin/mysqldump databasename >*.sql(在旧的server上导出数据库)
3:#/usr/local/mysql/bin/mysql databasename < *.sql(在新的server上导入*.sql)
注意数据库名要一一相应.
2.8. 各种字段的取值范围
TINYINT 1 byte
-128 - 127
TINYINT UNSIGNED 1 byte
0 ? 255 即0-(28-1)
SMALLINT 2 bytes
-32768 - 32767 即-215至(215-1)
SMALLINT UNSIGNED 2 bytes
0 - 65535 即0至(216-1)
MEDIUMINT 3 bytes
-8388608 - 8388607 即?223至(223-1)
MEDIUMINT UNSIGNED 3 bytes
0 - 16777215 即0至(224-1)
INT 或 INTEGER 4 bytes
-2147483648 - 2147483647 即?231至(231-1)
INT UNSIGNED 或 INTEGER UNSIGNED 4 bytes
0 - 4294967295 即0至(232-1)
BIGINT 8 bytes
-9223372036854775808 - 9223372036854775807 即?263至(263-1)
BIGINT UNSIGNED 8 bytes
0 - 18446744073709551615 即0至(264-1)
FLOAT 4 bytes
-3.402823466E+38 - -1.175494351E-38
0
1.175494351E-38 - 3.402823466E+38
DOUBLE 或 DOUBLE PRECISION 或 REAL 8 bytes
-1.7976931348623157E+308 - -2.2250738585072014E-308
0
2.2250738585072014E-308 - 1.7976931348623157E+308
DECIMAL[(M,[D])] 或 NUMERIC(M,D) 不定
由M(整个数字的长度,包含小数点,小数点左边的位数,小数点右边的位数,但不包含负号)和
D(小数点右边的位数)来决定,M缺省为10,D缺省为0
DATE 3 bytes
1000-01-01 - 9999-12-31
DATETIME 8 bytes
1000-01-01 00:00:00 - 9999-12-31 23:59:59
TIMESTAMP 4 bytes
1970-01-01 00:00:00 - 2037-12-31 23:59:59
TIME 3 bytes
-838:59:59' to 838:59:59
YEAR[(2|4)] 1 byte
缺省为4位格式,4位格式取值范围为1901 - 2155,0000,2位格式取值范围为70-69(1970-2069)
CHAR(M) [BINARY] 或 NCHAR(M) [BINARY] M bytes
M的范围为1 - 255,假设没有BINARY项,则不分大写和小写,NCHAR表示使用缺省的字符集.在数据库 中以空格补足,但在取出来时末尾的空格将被去掉.
[NATIONAL] VARCHAR(M) [BINARY]
Before 5.0.3 M的范围为0?255 L+1 bytes L<=M
5.0.3 以及以后 M 范围为 0-65535
L+1 bytes L<=M 0<=M<=256
L+2 bytes L<=M 256<=65535
在数据库中末尾的空格将自己主动去掉.
TINYBLOB 或 TINYTEXT L+1 bytes
255(2^8-1)个字符
BLOB 或 TEXT L+2 bytes
65535(2^16-1)个字符
MEDIUMBLOB 或 MEDIUMTEXT L+3 bytes
16777215 (2^24-1)个字符
LONGBLOB 或 LONGTEXT L+4 bytes
4294967295 (2^32-1)个字符
ENUM('value1','value2',...) 1 or 2 bytes
能够总共同拥有65535个不同的值
SET('value1','value2',...) 1/2/3/4/8 bytes
最多有64个成员
2.9. 查询
2.9.1. limit
LIMIT子句能够用来限制由SELECT语句返回过来的数据数量,它有一个或两个參数,假设给出两个參数,
第一个參数指定返回的第一行在全部数据中的位置,从0開始(注意不是1),第二个參数指定最多返回行
数。比如:
select * from table LIMIT 5,10; #返回第6-15行数据
select * from table LIMIT 5; #返回前5行
select * from table LIMIT 0,5; #返回前5行
2.9.2. join 具体解释
还是先 Create table 吧
create table emp(
id int not null primary key,
name varchar(10)
);
create table emp_dept(
dept_id varchar(4) not null,
emp_id int not null,
emp_name varchar(10),
primary key (dept_id,emp_id));
insert into emp() values
(1,"Dennis-1"),
(2,"Dennis-2"),
(3,"Dennis-3"),
(4,"Dennis-4"),
(5,"Dennis-5"),
(6,"Dennis-6"),
(7,"Dennis-7"),
(8,"Dennis-8"),
(9,"Dennis-9"),
(10,"Dennis-10");
insert into emp_dept() values
("R&D",1,"Dennis-1"),
("DEv",2,"Dennis-2"),
("R&D",3,"Dennis-3"),
("Test",4,"Dennis-4"),
("Test",5,"Dennis-5");
("dddd",20,"eeee");
>> left join
-------------
select a.id,a.name,b.dept_id
from emp a left join emp_dept b on (a.id=b.emp_id);
# 挑出左边的 table emp 中的全部资料,即使 emp_dept 中没有的资料也挑出来,没有的就用 NULL 来显示,
# 也即显示资料是以左边的 table emp 中的资料为基础
mysql> select a.id,a.name,b.dept_id
-> from emp a left join emp_dept b on (a.id=b.emp_id);
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 1 | Dennis-1 | R&D |
| 2 | Dennis-2 | DEv |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 挑出 table emp 中有而 table emp_dept 中没有的人员资料
select a.id,a.name,b.dept_id
from emp a left join emp_dept b on (a.id=b.emp_id)
where b.dept_id IS NULL;
mysql> select a.id,a.name,b.dept_id
-> from emp a left join emp_dept b on (a.id=b.emp_id)
-> where b.dept_id IS NULL;
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 把 table emp_dept 放在左边的情形(当然以 emp_dept 中的数据为基础来显示资料,emp 中比emp_dept 中多的资料也就不会显示出来了):
select a.id,a.name,b.dept_id
from emp_dept b left join emp a on (a.id=b.emp_id);
mysql> select a.id,a.name,b.dept_id
-> from emp_dept b left join emp a on (a.id=b.emp_id);
+------+----------+---------+
| id | name | dept_id |
+------+----------+---------+
| NULL | NULL | dddd |
| 2 | Dennis-2 | DEv |
| 1 | Dennis-1 | R&D |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
+------+----------+---------+
>> right join
---------------
select a.id,a.name,b.dept_id
from emp a right join emp_dept b on (a.id=b.emp_id);
# 挑资料时以右边 table emp_dept 中的资料为基础来显示资料
mysql> select a.id,a.name,b.dept_id
-> from emp a right join emp_dept b on (a.id=b.emp_id);
+------+----------+---------+
| id | name | dept_id |
+------+----------+---------+
| NULL | NULL | dddd |
| 2 | Dennis-2 | DEv |
| 1 | Dennis-1 | R&D |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
+------+----------+---------+
6 rows in set (0.00 sec)
# 我们再把 table 的位置交换一下,再用 right join 试试
select a.id,a.name,b.dept_id
from emp_dept b right join emp a on (a.id=b.emp_id);
mysql> select a.id,a.name,b.dept_id
-> from emp_dept b right join emp a on (a.id=b.emp_id);
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 1 | Dennis-1 | R&D |
| 2 | Dennis-2 | DEv |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 是不是和 left join 一样了?
>> inner join STRAIGHT_JOIN
select a.id,a.name,b.dept_id
from emp a ,emp_dept b
where a.id=b.emp_id;
mysql> select a.id,a.name,b.dept_id
-> from emp a ,emp_dept b
-> where a.id=b.emp_id;
+----+----------+---------+
| id | name | dept_id |
+----+----------+---------+
| 2 | Dennis-2 | DEv |
| 1 | Dennis-1 | R&D |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
+----+----------+---------+
2.9.3. 别名 alias
你能够在GROUP BY、ORDER BY或在HAVING部分中使用别名引用列。别名也能够用来为列取一个更好点的名字:
SELECT SQRT(a*b) as rt FROM table_name GROUP BY rt HAVING rt > 0;
SELECT id,COUNT(*) AS cnt FROM table_name GROUP BY id HAVING cnt > 0;
SELECT id AS "Customer identity" FROM table_name;
注意,你的 ANSI SQL 不同意你在一个WHERE子句中引用一个别名。这是由于在WHERE代码被运行时,列值还可能没有终结。比例如以下列查询是不合法:
SELECT id,COUNT(*) AS cnt FROM table_name WHERE cnt > 0 GROUP BY id;
WHERE语句被运行以确定哪些行应该包含GROUP BY部分中,而HAVING用来决定应该仅仅用结果集合中的哪些行。
2.9.4. 正则
正則表達式(regex)是定义复杂查询的一个强有力的工具。
这里是一个简单的资料,它忽略了一些具体的信息。
正則表達式定义了一个字符串的规则。最简单的正則表達式不包含不论什么保留字。比如,
正則表達式hello仅仅和字符串“hello”匹配。
一般的正則表達式使用了某些特殊的结构,所以它能匹配很多其它的字符串。比如,正则
表达式hello|word既能匹配字符串“hello”也能匹配字符串“word”。
举一个更复杂一点的样例,正則表達式B[an]*s能够匹配字符串“Bananas”、“Baaaaa
s”
、“Bs”以及其它不论什么以B开头以s结尾的字符串,中间能够包含随意个a和随意个n的组
合。
一个正則表達式中的能够使用下面保留字
^
所匹配的字符串以后面的字符串开头
mysql> select "fonfo" REGEXP "^fo$"; -> 0(表示不匹配)
mysql> select "fofo" REGEXP "^fo"; -> 1(表示匹配)
$
所匹配的字符串曾经面的字符串结尾
mysql> select "fono" REGEXP "^fono$"; -> 1(表示匹配)
mysql> select "fono" REGEXP "^fo$"; -> 0(表示不匹配)
..
匹配不论什么字符(包含新行)
mysql> select "fofo" REGEXP "^f.*"; -> 1(表示匹配)
mysql> select "fonfo" REGEXP "^f.*"; -> 1(表示匹配)
a*
匹配随意0-n多个a(包含空串)
mysql> select "Ban" REGEXP "^Ba*n"; -> 1(表示匹配)
mysql> select "Baaan" REGEXP "^Ba*n"; -> 1(表示匹配)
mysql> select "Bn" REGEXP "^Ba*n"; -> 1(表示匹配)
a+
匹配随意1-n多个a(不包含空串)
mysql> select "Ban" REGEXP "^Ba+n"; -> 1(表示匹配)
mysql> select "Bn" REGEXP "^Ba+n"; -> 0(表示不匹配)
a?
匹配0-1个a
mysql> select "Bn" REGEXP "^Ba?n"; -> 1(表示匹配)
mysql> select "Ban" REGEXP "^Ba?n"; -> 1(表示匹配)
mysql> select "Baan" REGEXP "^Ba?n"; -> 0(表示不匹配)
de|abc
匹配de或abc
mysql> select "pi" REGEXP "pi|apa"; -> 1(表示匹配)
mysql> select "axe" REGEXP "pi|apa"; -> 0(表示不匹配)
mysql> select "apa" REGEXP "pi|apa"; -> 1(表示匹配)
mysql> select "apa" REGEXP "^(pi|apa)$"; -> 1(表示匹配)
mysql> select "pi" REGEXP "^(pi|apa)$"; -> 1(表示匹配)
mysql> select "pix" REGEXP "^(pi|apa)$"; -> 0(表示不匹配)
(abc)*
匹配随意多个(0-n个)abc(包含空串)
mysql> select "pi" REGEXP "^(pi)*$"; -> 1(表示匹配)
mysql> select "pip" REGEXP "^(pi)*$"; -> 0(表示不匹配)
mysql> select "pipi" REGEXP "^(pi)*$"; -> 1(表示匹配)
{1}
{2,3}
这是一个更全面的方法,它能够实现前面好几种保留字的功能
a*
能够写成a{0,}
a+
能够写成a{1,}
a?
能够写成a{0,1}
在{}内仅仅有一个整型參数i,表示字符仅仅能出现i次;在{}内有一个整型參数i,
后面跟一个“,”,表示字符能够出现i次或i次以上;在{}内仅仅有一个整型參数i,
后面跟一个“,”,再跟一个整型參数j,表示字符仅仅能出现i次以上,j次下面
(包含i次和j次)。当中的整型參数必须大于等于0,小于等于 RE_DUP_MAX(默认是25
5)。
假设有两个參数,第二个必须大于等于第一个
[a-dX]
匹配“a”、“b”、“c”、“d”或“X”
[^a-dX]
匹配除“a”、“b”、“c”、“d”、“X”以外的不论什么字符。
“[”、“]”必须成对使用
mysql> select "aXbc" REGEXP "[a-dXYZ]"; -> 1(表示匹配)
mysql> select "aXbc" REGEXP "^[a-dXYZ]$"; -> 0(表示不匹配)
mysql> select "aXbc" REGEXP "^[a-dXYZ]+$"; -> 1(表示匹配)
mysql> select "aXbc" REGEXP "^[^a-dXYZ]+$"; -> 0(表示不匹配)
mysql> select "gheis" REGEXP "^[^a-dXYZ]+$"; -> 1(表示匹配)
mysql> select "gheisa" REGEXP "^[^a-dXYZ]+$"; -> 0(表示不匹配)
------------------------------------------------------------
[[.characters.]]
表示比較元素的顺序。在括号内的字符顺序是唯一的。可是括号中能够包含通配符,
所以他能匹配很多其它的字符。举例来说:正則表達式[[.ch.]]*c匹配chchcc的前五个字符
。
[=character_class=]
表示相等的类,能够取代类中其它相等的元素,包含它自己。比如,假设o和(+)是
一个相等的类的成员,那么[[=o=]]、[[=(+)=]]和[o(+)]是全然等价的。
[:character_class:]
在括号中面,在[:和:]中间是字符类的名字,能够代表属于这个类的全部字符。
字符类的名字有: alnum、digit、punct、alpha、graph、space、blank、lower、uppe
r、cntrl、print和xdigit
mysql> select "justalnums" REGEXP "[[:alnum:]]+"; -> 1(表示匹配)
mysql> select "!!" REGEXP "[[:alnum:]]+"; -> 0(表示不匹配)
[[:<:>
[[:>:]]
分别匹配一个单词开头和结尾的空的字符串,这个单词开头和结尾都不是包含在alnum中
的字符也不能是下划线。
mysql> select "a word a" REGEXP "[[:<:>:]]"; -> 1(表示匹配)
mysql> select "a xword a" REGEXP "[[:<:>:]]"; -> 0(表示不匹配)
mysql> select "weeknights" REGEXP "^(wee|week)(knights|nights)$"; -> 1(表示
匹配)
2.9.5. Select 中使用 IF Statement
mysql> select * from test;
+------+------+------+-------+
| dept | id | sex | name |
+------+------+------+-------+
| 1 | 1 | 0 | wang |
| 2 | 2 | 1 | zhang |
| 3 | 3 | 0 | li |
+------+------+------+-------+
3 rows in set (0.00 sec)
mysql> select dept,id,if(sex=0,'女','男') sex,name from test;
+------+------+-----+-------+
| dept | id | sex | name |
+------+------+-----+-------+
| 1 | 1 | 女 | wang |
| 2 | 2 | 男 | zhang |
| 3 | 3 | 女 | li |
+------+------+-----+-------+
3 rows in set (0.00 sec)
2.9.6. Select中使用CASE Statement
mysql> select dept,id,(case sex when '0' then '女' else '男' end) as sex,name from test;
+------+------+------+-------+
| dept | id | sex | name |
+------+------+------+-------+
| 1 | 1 | 女 | wang |
| 2 | 2 | 男 | zhang |
| 3 | 3 | 女 | li |
+------+------+------+-------+
mysql> select (case dept when '1' then 'no1' when '2' then 'no2' else 'other' end) as dept from test;
+-------+
| dept |
+-------+
| no1 |
| no2 |
| other |
+-------+
3 rows in set (0.00 sec)
注意: 相当于Oracle 中的decode 和case when
在统计报表中非常实用处
2.10. 存储过程和函数
CREATE PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
CREATE FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type
func_parameter:
param_name type
type:
Any valid MySQL data type
characteristic:
LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'
routine_body:
Valid SQL procedure statement or statements
样例:创建一个过程hello,显示问候语
mysql> delimiter ;
mysql> drop PROCEDURE if exists hello;
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql>
mysql> CREATE PROCEDURE hello (IN s varchar(20))
-> BEGIN
-> SELECT CONCAT('Hello,',s,'!') as hello;
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call hello('wangyl');
+---------------+
| hello |
+---------------+
| Hello,wangyl! |
+---------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
样例2: 查询t表的记录总数
mysql> delimiter //
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @a;
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
样例:创建一个函数
mysql> delimiter ;
mysql> drop FUNCTION if exists dateFunction;
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql> CREATE FUNCTION dateFunction (iCase int)
-> RETURNS varchar(50)
-> begin
-> DECLARE iType int;
-> DECLARE sReturn varchar(50);
-> set iType =icase +1;
-> case iType
-> when 1 then select DATE_FORMAT(NOW(),'%Y-%m-%d') into sReturn;
-> when 2 then select DATE_FORMAT(NOW(),'%W %M %Y') into sReturn;
-> else
-> select NOW() into sReturn;
-> end case;
-> return sReturn;
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select dateFunction(0);
+-----------------+
| dateFunction(0) |
+-----------------+
| 2005-11-14 |
+-----------------+
1 row in set (0.00 sec)
mysql> select dateFunction(1);
+----------------------+
| dateFunction(1) |
+----------------------+
| Monday November 2005 |
+----------------------+
1 row in set (0.00 sec)
mysql> select dateFunction(2);
+---------------------+
| dateFunction(2) |
+---------------------+
| 2005-11-14 15:05:43 |
+---------------------+
1 row in set (0.00 sec)
2.11. 补充:trigger
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
CREATE TABLE test1(a1 INT);
CREATE TABLE test2(a2 INT);
CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE test4(
a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
b4 INT DEFAULT 0
);
DELIMITER |
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END|
DELIMITER ;
INSERT INTO test3 (a3) VALUES
(NULL), (NULL), (NULL), (NULL), (NULL),
(NULL), (NULL), (NULL), (NULL), (NULL);
INSERT INTO test4 (a4) VALUES
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0);
If you insert the following values into table test1 as shown here:
mysql> INSERT INTO test1 VALUES
-> (1), (3), (1), (7), (1), (8), (4), (4);
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0
Then the data in the four tables will be as follows:
mysql> SELECT * FROM test1;
+------+
| a1 |
+------+
| 1 |
| 3 |
| 1 |
| 7 |
| 1 |
| 8 |
| 4 |
| 4 |
+------+
8 rows in set (0.00 sec)
mysql> SELECT * FROM test2;
+------+
| a2 |
+------+
| 1 |
| 3 |
| 1 |
| 7 |
| 1 |
| 8 |
| 4 |
| 4 |
+------+
8 rows in set (0.00 sec)
mysql> SELECT * FROM test3;
+----+
| a3 |
+----+
| 2 |
| 5 |
| 6 |
| 9 |
| 10 |
+----+
5 rows in set (0.00 sec)
mysql> SELECT * FROM test4;
+----+------+
| a4 | b4 |
+----+------+
| 1 | 3 |
| 2 | 0 |
| 3 | 1 |
| 4 | 2 |
| 5 | 0 |
| 6 | 0 |
| 7 | 1 |
| 8 | 1 |
| 9 | 0 |
| 10 | 0 |
+----+------+
10 rows in set (0.00 sec)
2.12. 删除 DELETE Syntax
Single-table syntax:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
Multiple-table syntax:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
tbl_name[.*] [, tbl_name[.*]] ...
FROM table_references
[WHERE where_condition]
Or:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
FROM tbl_name[.*] [, tbl_name[.*]] ...
USING table_references
[WHERE where_condition]
对于删除多个表,不能使用order by 和limit
多表删除语法一: ,仅仅是在删除 在from 前面的表所中匹配的记录
DELETE t1, t2 FROM t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
从t1,t2,t3 表选出要删除的记录,仅仅是删除t1,t2 表中所匹配得这些记录.
多表删除语法二: 在from 列出的表中删除选中的记录.
DELETE FROM t1, t2 USING t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
从t1,t2,t3 表选出要删除的记录(使用using ),仅仅是删除t1,t2 表中所匹配得这些记录.
注意: 假设您使用一个别名,您必须使用别名.
DELETE t1 FROM test AS t1, test2 WHERE ...
支持多个数据之间的多表删除,可是在这样的情况下,你必须指定表,而不能使用别名:
DELETE test1.tmp1, test2.tmp2 FROM test1.tmp1, test2.tmp2 WHERE ...
当前,您不能从同样表的子查询中,删除同一表的数据
抛出异常的爱 1 星期前
1016错误:文件无法打开,使用后台修复或者使用phpmyadmin进行修复。
1044错误:数据库用户权限不足,请联系空间商解决
1045错误:数据库server/数据库username/数据库名/数据库password错误,请联系空间商检查帐户。
1054错误:程序文件跟数据库有冲突,请使用正确的程序文件上传上去覆盖。
1146错误:数据表缺失,请恢复备份数据.
1005:创建表失败
1006:创建数据库失败
1007:数据库已存在,创建数据库失败
1008:数据库不存在,删除数据库失败
1009:不能删除数据库文件导致删除数据库失败
1010:不能删除数据文件夹导致删除数据库失败
1011:删除数据库文件失败
1012:不能读取系统表中的记录
1020:记录已被其它用户改动
1021:硬盘剩余空间不足,请加大硬盘可用空间
1022:keyword反复,更改记录失败
1023:关闭时错误发生
1024:读文件错误
1025:更改名字时错误发生
1026:写文件错误
1032:记录不存在
1036:数据表是仅仅读的,不能对它进行改动
1037:系统内存不足,请重新启动数据库或重新启动server
1038:用于排序的内存不足,请增大排序缓冲区
1040:已到达数据库的最大连接数,请加大数据库可用连接数
1041:系统内存不足
1042:无效的主机名
1043:无效连接
1044:当前用户没有訪问数据库的权限
1045:不能连接数据库,username或password错误
1048:字段不能为空
1049:数据库不存在
1050:数据表已存在
1051:数据表不存在
1054:字段不存在
1065:无效的SQL语句,SQL语句为空
1081:不能建立Socket连接
1114:数据表已满,不能容纳不论什么记录
1116:打开的数据表太多
1129:数据库出现异常,请重新启动数据库
1130:连接数据库失败,没有连接数据库的权限
1133:数据库用户不存在
1141:当前用户无权訪问数据库
1142:当前用户无权訪问数据表
1143:当前用户无权訪问数据表中的字段
1146:数据表不存在
1147:没有定义用户对数据表的訪问权限
1149:SQL语句语法错误
1158:网络错误,出现读错误,请检查网络连接状况
1159:网络错误,读超时,请检查网络连接状况
1160:网络错误,出现写错误,请检查网络连接状况
1161:网络错误,写超时,请检查网络连接状况
1062:字段值反复,入库失败
1169:字段值反复,更新记录失败
1177:打开数据表失败
1180:提交事务失败
1181:回滚事务失败
1203:当前用户和数据库建立的连接已到达数据库的最大连接数,请增大可用的数据库连接数或重新启动数据库
1205:加锁超时
1211:当前用户没有创建用户的权限
1216:外键约束检查失败,更新子表记录失败
1217:外键约束检查失败,删除或改动主表记录失败
1226:当前用户使用的资源已超过所同意的资源,请重新启动数据库或重新启动server
1227:权限不足,您无权进行此操作
1235:MySQL版本号过低,不具有本功能
2002:Can't connect to ...通常意味着没有一个MySQLserver运行在系统上或当试图连接mysqldserver时,你正在使用一个错误的套接字文件或TCP/IP端口。
说明:以上仅仅是常见错误号,而且解决的方法也不一定肯定管用!具体请參考MYSQL手冊(各大下载站均有下载)
很多其它具体错误代码
错误名称 / 错误代码 SQL状态
ER_HASHCHK 1000 HY000
ER_NISAMCHK 1001 HY000
ER_NO 1002 HY000
ER_YES 1003 HY000
ER_CANT_CREATE_FILE 1004 HY000
ER_CANT_CREATE_TABLE 1005 HY000
ER_CANT_CREATE_DB 1006 HY000
ER_DB_CREATE_EXISTS 1007 HY000
ER_DB_DROP_EXISTS 1008 HY000
ER_DB_DROP_DELETE 1009 HY000
ER_DB_DROP_RMDIR 1010 HY000
ER_CANT_DELETE_FILE 1011 HY000
ER_CANT_FIND_SYSTEM_REC 1012 HY000
ER_CANT_GET_STAT 1013 HY000
ER_CANT_GET_WD 1014 HY000
ER_CANT_LOCK 1015 HY000
ER_CANT_OPEN_FILE 1016 HY000
ER_FILE_NOT_FOUND 1017 HY000
ER_CANT_READ_DIR 1018 HY000
ER_CANT_SET_WD 1019 HY000
ER_CHECKREAD 1020 HY000
ER_DISK_FULL 1021 HY000
ER_DUP_KEY 1022 23000
ER_ERROR_ON_CLOSE 1023 HY000
ER_ERROR_ON_READ 1024 HY000
ER_ERROR_ON_RENAME 1025 HY000
ER_ERROR_ON_WRITE 1026 HY000
ER_FILE_USED 1027 HY000
ER_FILSORT_ABORT 1028 HY000
ER_FORM_NOT_FOUND 1029 HY000
ER_GET_ERRNO 1030 HY000
ER_ILLEGAL_HA 1031 HY000
ER_KEY_NOT_FOUND 1032 HY000
ER_NOT_FORM_FILE 1033 HY000
ER_NOT_KEYFILE 1034 HY000
ER_OLD_KEYFILE 1035 HY000
ER_OPEN_AS_READONLY 1036 HY000
ER_OUTOFMEMORY 1037 HY001
ER_OUT_OF_SORTMEMORY 1038 HY001
ER_UNEXPECTED_EOF 1039 HY000
ER_CON_COUNT_ERROR 1040 08004
ER_OUT_OF_RESOURCES 1041 08004
ER_BAD_HOST_ERROR 1042 08S01
ER_HANDSHAKE_ERROR 1043 08S01
ER_DBACCESS_DENIED_ERROR 1044 42000
ER_ACCESS_DENIED_ERROR 1045 42000
ER_NO_DB_ERROR 1046 42000
ER_UNKNOWN_COM_ERROR 1047 08S01
ER_BAD_NULL_ERROR 1048 23000
ER_BAD_DB_ERROR 1049 42000
ER_TABLE_EXISTS_ERROR 1050 42S01
ER_BAD_TABLE_ERROR 1051 42S02
ER_NON_UNIQ_ERROR 1052 23000
ER_SERVER_SHUTDOWN 1053 08S01
ER_BAD_FIELD_ERROR 1054 42S22
ER_WRONG_FIELD_WITH_GROUP 1055 42000
ER_WRONG_GROUP_FIELD 1056 42000
ER_WRONG_SUM_SELECT 1057 42000
ER_WRONG_VALUE_COUNT 1058 21S01
ER_TOO_LONG_IDENT 1059 42000
ER_DUP_FIELDNAME 1060 42S21
ER_DUP_KEYNAME 1061 42000
ER_DUP_ENTRY 1062 23000
ER_WRONG_FIELD_SPEC 1063 42000
ER_PARSE_ERROR 1064 42000
ER_EMPTY_QUERY 1065 42000
ER_NONUNIQ_TABLE 1066 42000
ER_INVALID_DEFAULT 1067 42000
ER_MULTIPLE_PRI_KEY 1068 42000
ER_TOO_MANY_KEYS 1069 42000
ER_TOO_MANY_KEY_PARTS 1070 42000
ER_TOO_LONG_KEY 1071 42000
ER_KEY_COLUMN_DOES_NOT_EXITS 1072 42000
ER_BLOB_USED_AS_KEY 1073 42000
ER_TOO_BIG_FIELDLENGTH 1074 42000
ER_WRONG_AUTO_KEY 1075 42000
ER_READY 1076 00000
ER_NORMAL_SHUTDOWN 1077 00000
ER_GOT_SIGNAL 1078 00000
ER_SHUTDOWN_COMPLETE 1079 00000
ER_FORCING_CLOSE 1080 08S01
ER_IPSOCK_ERROR 1081 08S01
ER_NO_SUCH_INDEX 1082 42S12
ER_WRONG_FIELD_TERMINATORS 1083 42000
ER_BLOBS_AND_NO_TERMINATED 1084 42000
ER_TEXTFILE_NOT_READABLE 1085 HY000
ER_FILE_EXISTS_ERROR 1086 HY000
ER_LOAD_INFO 1087 HY000
ER_ALTER_INFO 1088 HY000
ER_WRONG_SUB_KEY 1089 HY000
ER_CANT_REMOVE_ALL_FIELDS 1090 42000
ER_CANT_DROP_FIELD_OR_KEY 1091 42000
ER_INSERT_INFO 1092 HY000
ER_UPDATE_TABLE_USED 1093 HY000
ER_NO_SUCH_THREAD 1094 HY000
ER_KILL_DENIED_ERROR 1095 HY000
ER_NO_TABLES_USED 1096 HY000
ER_TOO_BIG_SET 1097 HY000
ER_NO_UNIQUE_LOGFILE 1098 HY000
ER_TABLE_NOT_LOCKED_FOR_WRITE 1099 HY000
ER_TABLE_NOT_LOCKED 1100 HY000
ER_BLOB_CANT_HAVE_DEFAULT 1101 42000
ER_WRONG_DB_NAME 1102 42000
ER_WRONG_TABLE_NAME 1103 42000
ER_TOO_BIG_SELECT 1104 42000
ER_UNKNOWN_ERROR 1105 HY000
ER_UNKNOWN_PROCEDURE 1106 42000
ER_WRONG_PARAMCOUNT_TO_PROCEDURE 1107 42000
ER_WRONG_PARAMETERS_TO_PROCEDURE 1108 HY000
ER_UNKNOWN_TABLE 1109 42S02
ER_FIELD_SPECIFIED_TWICE 1110 42000
ER_INVALID_GROUP_FUNC_USE 1111 42000
ER_UNSUPPORTED_EXTENSION 1112 42000
ER_TABLE_MUST_HAVE_COLUMNS 1113 42000
ER_RECORD_FILE_FULL 1114 HY000
ER_UNKNOWN_CHARACTER_SET 1115 42000
ER_TOO_MANY_TABLES 1116 HY000
ER_TOO_MANY_FIELDS 1117 HY000
ER_TOO_BIG_ROWSIZE 1118 42000
ER_STACK_OVERRUN 1119 HY000
ER_WRONG_OUTER_JOIN 1120 42000
ER_NULL_COLUMN_IN_INDEX 1121 42000
ER_CANT_FIND_UDF 1122 HY000
ER_CANT_INITIALIZE_UDF 1123 HY000
ER_UDF_NO_PATHS 1124 HY000
ER_UDF_EXISTS 1125 HY000
ER_CANT_OPEN_LIBRARY 1126 HY000
ER_CANT_FIND_DL_ENTRY 1127 HY000
ER_FUNCTION_NOT_DEFINED 1128 HY000
ER_HOST_IS_BLOCKED 1129 HY000
ER_HOST_NOT_PRIVILEGED 1130 HY000
ER_PASSWORD_ANONYMOUS_USER 1131 42000
ER_PASSWORD_NOT_ALLOWED 1132 42000
ER_PASSWORD_NO_MATCH 1133 42000
ER_UPDATE_INFO 1134 HY000
ER_CANT_CREATE_THREAD 1135 HY000
ER_WRONG_VALUE_COUNT_ON_ROW 1136 21S01
ER_CANT_REOPEN_TABLE 1137 HY000
ER_INVALID_USE_OF_NULL 1138 42000
ER_REGEXP_ERROR 1139 42000
ER_MIX_OF_GROUP_FUNC_AND_FIELDS 1140 42000
ER_NONEXISTING_GRANT 1141 42000
ER_TABLEACCESS_DENIED_ERROR 1142 42000
ER_COLUMNACCESS_DENIED_ERROR 1143 42000
ER_ILLEGAL_GRANT_FOR_TABLE 1144 42000
ER_GRANT_WRONG_HOST_OR_USER 1145 42000
ER_NO_SUCH_TABLE 1146 42S02
ER_NONEXISTING_TABLE_GRANT 1147 42000
ER_NOT_ALLOWED_COMMAND 1148 42000
ER_SYNTAX_ERROR 1149 42000
ER_DELAYED_CANT_CHANGE_LOCK 1150 HY000
ER_TOO_MANY_DELAYED_THREADS 1151 HY000
ER_ABORTING_CONNECTION 1152 08S01
ER_NET_PACKET_TOO_LARGE 1153 08S01
ER_NET_READ_ERROR_FROM_PIPE 1154 08S01
ER_NET_FCNTL_ERROR 1155 08S01
ER_NET_PACKETS_OUT_OF_ORDER 1156 08S01
ER_NET_UNCOMPRESS_ERROR 1157 08S01
ER_NET_READ_ERROR 1158 08S01
ER_NET_READ_INTERRUPTED 1159 08S01
ER_NET_ERROR_ON_WRITE 1160 08S01
ER_NET_WRITE_INTERRUPTED 1161 08S01
ER_TOO_LONG_STRING 1162 42000
ER_TABLE_CANT_HANDLE_BLOB 1163 42000
ER_TABLE_CANT_HANDLE_AUTO_INCREMENT 1164 42000
ER_DELAYED_INSERT_TABLE_LOCKED 1165 HY000
ER_WRONG_COLUMN_NAME 1166 42000
ER_WRONG_KEY_COLUMN 1167 42000
ER_WRONG_MRG_TABLE 1168 HY000
ER_DUP_UNIQUE 1169 23000
ER_BLOB_KEY_WITHOUT_LENGTH 1170 42000
ER_PRIMARY_CANT_HAVE_NULL 1171 42000
ER_TOO_MANY_ROWS 1172 42000
ER_REQUIRES_PRIMARY_KEY 1173 42000
ER_NO_RAID_COMPILED 1174 HY000
ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE 1175 HY000
ER_KEY_DOES_NOT_EXITS 1176 HY000
ER_CHECK_NO_SUCH_TABLE 1177 42000
ER_CHECK_NOT_IMPLEMENTED 1178 42000
ER_CANT_DO_THIS_DURING_AN_TRANSACTION 1179 25000
ER_ERROR_DURING_COMMIT 1180 HY000
ER_ERROR_DURING_ROLLBACK 1181 HY000
ER_ERROR_DURING_FLUSH_LOGS 1182 HY000
ER_ERROR_DURING_CHECKPOINT 1183 HY000
ER_NEW_ABORTING_CONNECTION 1184 08S01
ER_DUMP_NOT_IMPLEMENTED 1185 HY000
ER_FLUSH_MASTER_BINLOG_CLOSED 1186 HY000
ER_INDEX_REBUILD 1187 HY000
ER_MASTER 1188 HY000
ER_MASTER_NET_READ 1189 08S01
ER_MASTER_NET_WRITE 1190 08S01
ER_FT_MATCHING_KEY_NOT_FOUND 1191 HY000
ER_LOCK_OR_ACTIVE_TRANSACTION 1192 HY000
ER_UNKNOWN_SYSTEM_VARIABLE 1193 HY000
ER_CRASHED_ON_USAGE 1194 HY000
ER_CRASHED_ON_REPAIR 1195 HY000
ER_WARNING_NOT_COMPLETE_ROLLBACK 1196 HY000
ER_TRANS_CACHE_FULL 1197 HY000
ER_SLAVE_MUST_STOP 1198 HY000
ER_SLAVE_NOT_RUNNING 1199 HY000
ER_BAD_SLAVE 1200 HY000
ER_MASTER_INFO 1201 HY000
ER_SLAVE_THREAD 1202 HY000
ER_TOO_MANY_USER_CONNECTIONS 1203 42000
ER_SET_CONSTANTS_ONLY 1204 HY000
ER_LOCK_WAIT_TIMEOUT 1205 HY000
ER_LOCK_TABLE_FULL 1206 HY000
ER_READ_ONLY_TRANSACTION 1207 25000
ER_DROP_DB_WITH_READ_LOCK 1208 HY000
ER_CREATE_DB_WITH_READ_LOCK 1209 HY000
ER_WRONG_ARGUMENTS 1210 HY000
ER_NO_PERMISSION_TO_CREATE_USER 1211 42000
ER_UNION_TABLES_IN_DIFFERENT_DIR 1212 HY000
ER_LOCK_DEADLOCK 1213 40001
ER_TABLE_CANT_HANDLE_FULLTEXT 1214 HY000
ER_CANNOT_ADD_FOREIGN 1215 HY000
ER_NO_REFERENCED_ROW 1216 23000
ER_ROW_IS_REFERENCED 1217 23000
ER_CONNECT_TO_MASTER 1218 08S01
ER_QUERY_ON_MASTER 1219 HY000
ER_ERROR_WHEN_EXECUTING_COMMAND 1220 HY000
ER_WRONG_USAGE 1221 HY000
ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT 1222 21000
ER_CANT_UPDATE_WITH_READLOCK 1223 HY000
ER_MIXING_NOT_ALLOWED 1224 HY000
ER_DUP_ARGUMENT 1225 HY000
ER_USER_LIMIT_REACHED 1226 42000
ER_SPECIFIC_ACCESS_DENIED_ERROR 1227 HY000
ER_LOCAL_VARIABLE 1228 HY000
ER_GLOBAL_VARIABLE 1229 HY000
ER_NO_DEFAULT 1230 42000
ER_WRONG_VALUE_FOR_VAR 1231 42000
ER_WRONG_TYPE_FOR_VAR 1232 42000
ER_VAR_CANT_BE_READ 1233 HY000
ER_CANT_USE_OPTION_HERE 1234 42000
ER_NOT_SUPPORTED_YET 1235 42000
ER_MASTER_FATAL_ERROR_READING_BINLOG 1236 HY000
ER_WRONG_FK_DEF 1237 42000
ER_KEY_REF_DO_NOT_MATCH_TABLE_REF 1238 HY000
ER_CARDINALITY_COL 1239 21000
ER_SUBSELECT_NO_1_ROW 1240 21000
ER_UNKNOWN_STMT_HANDLER 1241 HY000
ER_CORRUPT_HELP_DB 1242 HY000
ER_CYCLIC_REFERENCE 1243 HY000
ER_AUTO_CONVERT 1244 HY000
ER_ILLEGAL_REFERENCE 1245 42S22
ER_DERIVED_MUST_HAVE_ALIAS 1246 42000
ER_SELECT_REDUCED 1247 01000
ER_TABLENAME_NOT_ALLOWED_HERE 1248 42000
ER_NOT_SUPPORTED_AUTH_MODE 1249 08004
ER_SPATIAL_CANT_HAVE_NULL 1250 42000
ER_COLLATION_CHARSET_MISMATCH 1251 42000
ER_SLAVE_WAS_RUNNING 1252 HY000
ER_SLAVE_WAS_NOT_RUNNING 1253 HY000
ER_TOO_BIG_FOR_UNCOMPRESS 1254 HY000
ER_ZLIB_Z_MEM_ERROR 1255 HY000
ER_ZLIB_Z_BUF_ERROR 1256 HY000
ER_ZLIB_Z_DATA_ERROR 1257 HY000
ER_CUT_VALUE_GROUP_CONCAT 1258 HY000
ER_WARN_TOO_FEW_RECORDS 1259 01000
ER_WARN_TOO_MANY_RECORDS 1260 01000
ER_WARN_NULL_TO_NOTNULL 1261 01000
ER_WARN_DATA_OUT_OF_RANGE 1262 01000
ER_WARN_DATA_TRUNCATED 1263 01000
ER_WARN_USING_OTHER_HANDLER 1264 01000
ER_CANT_AGGREGATE_COLLATIONS 1265 42000
ER_DROP_USER 1266 42000
ER_REVOKE_GRANTS 1267 42000
1.1. 培训目的
本文档是针对MySQL 数据库方面的基础培训,为了使项目组成员能够达到使用MySQL 数据库的目的。
1.2. 培训对象
开发者
1.3. 经常使用词及符号说明
经常使用词:
Mysql:一种免费的跨平台的数据库系统
E:/mysql:表示是在dos 命令窗体下面
mysql> 表示是在mysql 的命令行下
1.4. 參考信息
http://dev.mysql.com/doc/refman/5.0/en/index.html
2. MYSQL
2.1. 连接MYSQL
格式: mysql -h主机地址 -uusername -p用户password
连接远程机器:
E:/mysql>mysql -h10.4.3.188 -uptsdb -p
等价写法
E:/mysql>mysql --host=10.4.3.188 --user=ptsdb --password
连接本地机器:
E:/mysql>mysql -uroot -p
等价写法
E:/mysql>mysql --user=root -password
(注:u与root能够不用加空格,其它也一样)
注意事项:环境变量path 里面要设定mysql的bin的路径:
C:/Program Files/MySQL/MySQL Server 5.0/bin
2.2. 改动password
方法一:使用mysqladmin
格式:mysqladmin -uusername -p旧password password 新password
例1:E:/mysql>mysqladmin -uroot password root
注:由于開始时root没有password,所以-p旧password一项就能够省略了。
例2:再将root的password改为root123。
E:/mysql>mysqladmin -uroot -proot password root123
方法二:直接更新 user 表
mysql>UPDATE user SET password=PASSWORD("test123") WHERE user='test';
mysql> FLUSH PRIVILEGES;
mysql> SET PASSWORD FOR test=PASSWORD('test123');
mysql> FLUSH PRIVILEGES;
方法三:使用 grant
格式:grant 权限 on 数据库.表格| 其它 to 用户@主机 IDENTIFIED BY 口令
例1:给test用户在本地localhost 全部权限(除了GRANT OPTION),口令为 test
(相当于改动了test 用户的口令)
mysql>grant all on *.* to test@localhost identified by "test";
等同于
mysql>grant all on *.* to test @localhost identified by PASSWORD " *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 ";
例2、添加�一个用户testpassword为abc,让他能够在不论什么主机上登录,并对test数据库有查询、插入、改动、删除的权限。首先用以root用户连入MYSQL,然后键入下面命令:
mysql>grant select,insert,update,delete on test.* to test@"%" Identified by "abc";
在mysql.user 表中,有两个test 用户
一个test 用户,在本地有全部的权限
另外的test 用户,在全部主机上有增删改查权限
同样能够取消部分权限(全部)
mysql>revoke insert,update,delete on test.* from test@"%"
mysql>REVOKE ALL PRIVILEGES, GRANT OPTION FROM test@"%"
然后 mysql> FLUSH PRIVILEGES;
Test 用户不再使用用了,也能够删除
mysql>Delete from user where user='test' and host='%'
mysql> FLUSH PRIVILEGES;
注意:例2添加�的用户是比較危急的,你想如某个人知道test的password,那么他就能够在internet上的不论什么一台电脑上登录你的mysql数据库并对你的数据库test为所欲为了(能够通过限定主机)
mysql>grant select,insert,update,delete on test.* to test@"IP地址" Identified by "abc";
2.3. 显示命令
显示数据库列表:
mysql>show databases;
mysql>show schemas; --mysql 5.0.2
显示表格
mysql>show tables from mydb;
显示表格状态
Mysql>SHOW TABLE STATUS;
显示字符集:
mysql> SHOW CHARACTER SET;
显示创建表:
mysql> show create table quote;
显示用户权限:
mysql> SHOW GRANTS FOR 'test'@'localhost';
mysql>SHOW GRANTS;
mysql>SHOW GRANTS FOR CURRENT_USER;
mysql>SHOW GRANTS FOR CURRENT_USER();
显示index:
mysql>SHOW INDEX FROM mydb.mytable;
显示表结构:
mysql>desc mydb.tablename;
mysql>show columns from mydb.tablename;
显示MySQL数据库的版本号:
mysql>select version();
显示函数
mysql>Select * from mysql.func;
显示存储过程
mysql>Select * from mysql.proc;
显示存储引擎
mysql> SHOW ENGINES;
显示变量:
mysql>SHOW VARIABLES;
显示状态:
Mysql> SHOW STATUS;
显示进程
Mysql>SHOW PROCESSLIST
显示 INNODB 状态
Mysql>SHOW INNODB STATUS
显示连接状态
Mysql>SHOW STATUS LIKE '%CONNECT%';
显示线程状态
Mysql>SHOW STATUS LIKE '%THREAD%';
等等..
2.4. 创建.改动.删除
2.4.1. 创建数据库
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification [, create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
| [DEFAULT] COLLATE collation_name
比如:
CREATE DATABASE IF NOT EXISTS ddd --假设不存在,则创建.
CHARACTER SET 'ujis' --设定字符集
COLLATE 'ujis_japanese_ci';
2.4.2. 创建表
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options] [select_statement]
样例:
CREATE TABLE if not exists `Admin_User` (
`id` int(11) NOT NULL auto_increment, --PRIMARY KEY,
`livedoorId` varchar(255) NOT NULL default '',
`password` varchar(255) NOT NULL default '',
`auth` int(11) default '0',
PRIMARY KEY (`id`) --设定主健
) ENGINE=MyISAM DEFAULT CHARSET=ujis ?设定字符集
ENGINE=MyISAM 默认存储引擎
The binary portable storage engine that is the default storage engine used by MySQL
对于每一个MyISAM 存储引擎的表,在硬盘上存在3个文件
File Purpose
tbl_name.frm Table format (definition) file
tbl_name.MYD Data file
tbl_name.MYI Index file
ENGINE= InnoDB
Transaction-safe tables with row locking and foreign keys.
ENGINE = BDB
Transaction-safe tables with page locking.
还有其它的内存引擎 MEMORY 归档 ARCHIVE 等等
ISAM 不再使用了
2.4.3. 创建索引
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
[USING index_type]
ON tbl_name (index_col_name,...)
index_col_name:
col_name [(length)] [ASC | DESC]
将customer 表的name 字段前十个字符做为索引
CREATE INDEX part_of_name ON customer (name(10));
MYSQL 5.0 特性
存储引擎为 MyISAM, InnoDB, or BDB 的表格上,能够在有null值的字段上创建索引
存储引擎为 MyISAM, InnoDB, or BDB 的表格上,能够在BLOB TEXT 上创建索引
仅仅有在MyISAM 类型表格上,能够在CHAR, VARCHAR, and TEXT 字段类型上创建FULLTEXT 索引
Storage Engine Allowable Index Types
MyISAM BTREE
InnoDB BTREE
MEMORY/HEAP HASH, BTREE
能够指定索引类型
Example:
CREATE TABLE testtable (id INT) ENGINE = MEMORY;
CREATE INDEX id_index USING BTREE ON testtable (id);
2.4.4. 改动表
ALTER [IGNORE] TABLE tbl_name
alter_specification [, alter_specification] ...
IGNORE 忽略主健反复的错误,假设反复,採用第一条,其余删除
样例:同一时候多个操作
mysql> ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;
样例, 重命名 INTEGER 字段,从 a 到 b:
mysql> ALTER TABLE t1 CHANGE a b INTEGER;
样例 改动字段类型,仍然须要新旧字段名称,即使字段名称同样:
mysql> ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;
也能够使用modify
mysql> ALTER TABLE t1 MODIFY b BIGINT NOT NULL;
在mysql 5.0 能够使用FIRST or AFTER 字段来加入�add 字段,默认是在最后
modify change 也能够使用
在mysql 5.0 InnoDB 存储引擎支持 ALTER TABLE 删除外健:
mysql>ALTER TABLE yourtablename DROP FOREIGN KEY fk_symbol;
样例:
创建表
mysql> CREATE TABLE t1 (a INTEGER,b CHAR(10));
重命名表
mysql> ALTER TABLE t1 RENAME t2;
MODIFY a 字段为 TINYINT NOT NULL , 而且 change 字段 b,从 CHAR(10) 到 CHAR(20) 并改名为c:
mysql> ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
加入�新字段 d:
mysql> ALTER TABLE t2 ADD d TIMESTAMP;
在a d 上添加�索引:
mysql> ALTER TABLE t2 ADD INDEX (d), ADD INDEX (a);
删除字段c:
mysql> ALTER TABLE t2 DROP COLUMN c;
加入�一个自己主动增长的字段c ,而且加入�c 为主健:
mysql> ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT,
-> ADD PRIMARY KEY (c);
2.4.4.1. 改动外键
语法:
ALTER TABLE tbl_name
ADD [CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
[ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
[ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
样例:
create table aa (id1 int not null,name varchar(20),primary key(id1))type=InnoDB;
create table b(id2 int not null,lessonname varchar(20),primary key(id2))type=InnoDB;
alter table b add FOREIGN KEY id (id2) references aa(id1);
2.4.5. 删除
删除数据库
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
样例: mysql>drop DATABASE IF EXISTS testdb;
删除表
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
样例: mysql>drop TABLE IF EXISTS testTable;
删除索引
DROP INDEX index_name ON tbl_name
样例: mysql>drop index testIndex on testTable;
重命名
RENAME TABLE tbl_name TO new_tbl_name
[, tbl_name2 TO new_tbl_name2] ...
样例:RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
2.5. 数据库备份恢复
数据库备份
数据库备份命令:
mysqldump --opt --user=username --password=password --default_character-set=字符集 -B数据库> 输出的sql文件
样例:
E:/mysql>Mysqldump --user=ptsdb --password=ptsdb --default_character-set=ujis --opt pts>dump.sql
參见批处理文件
数据库导入命令:
mysql --user=username --password=password --default_character-set=字符集 [数据库]<导入的sql 语句
E:/mysql>mysql -uptsdb -pptsdb --default-character-set=ujis
样例: 对于InnoDB(没有设定字符集)
Following mysqldump import example for InnoDB tables is at least 100x faster than previous examples.
1. mysqldump --opt --user=username --password database > dumbfile.sql
2. Edit the dump file and put these lines at the beginning:
SET AUTOCOMMIT = 0;
SET FOREIGN_KEY_CHECKS=0;
3. Put these lines at the end:
SET FOREIGN_KEY_CHECKS = 1;
COMMIT;
SET AUTOCOMMIT = 1;
4. mysql --user=username --password database < dumpfile.sql
參数说明:
--add-drop-database
Add a DROP DATABASE statement before each CREATE DATABASE statement.
--add-drop-table
Add a DROP TABLE statement before each CREATE TABLE statement.
--all-databases, -A
Dump all tables in all databases. This is the same as using the --databases option and naming all the databases on the command line.
--databases, -B
Dump several databases. Normally, mysqldump treats the first name argument on the command line as a database name
and following names as table names. With this option, it treats all name arguments as database names.
CREATE DATABASE IF NOT EXISTS db_name and USE db_name statements are included in the output before each new database.
--host=host_name, -h host_name
Dump data from the MySQL server on the given host. The default host is localhost.
--opt
This option is shorthand; it is the same as specifying --add-drop-table --add-locks --create-options
--disable-keys --extended-insert --lock-tables --quick --set-charset.
It should give you a fast dump operation and produce a dump file that can be reloaded into a MySQL server quickly.
In MySQL 5.0, --opt is on by default, but can be disabled with --skip-opt.
To disable only certain of the options enabled by --opt, use their --skip forms;
for example, --skip-add-drop-table or --skip-quick.
另一些其它參数,有兴趣能够学习
2.6. 表数据备份
mysql>use test;
mysql> CREATE TABLE imptest(id INT, n VARCHAR(30));
Query OK, 0 rows affected (0.03 sec)
方法一:
导出使用:Mysqldump
E:/mysql>mysqldump -uptsdb -pptsdb -where "id>='100'"
test imptest
E:/mysql>mysqldump -uptsdb -pptsdb test imptest>e:/mysql/imp/imptest2.txt
导入使用 mysql
mysql -uptsdb -pptsdb< imptest2.txt
方法二:
导出使用 select into OUTFILE
mysql> select * from imptest where id=101 into OUTFILE 'e://mysql//imp//test3.txt' FIELDS TERMINATED BY ',';
导入使用 LOAD DATA INFILE
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[FIELDS
[TERMINATED BY 'string']
[[OPTIONALLY] ENCLOSED BY 'char']
[ESCAPED BY 'char' ]
]
[LINES
[STARTING BY 'string']
[TERMINATED BY 'string']
]
[IGNORE number LINES]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]]
样例:
mysql> LOAD DATA INFILE 'e://mysql//imp//test3.txt' INTO TABLE imptest FIELDS TERMINATED BY ',';
导入使用mysqlimport:
E:/mysql>mysqlimport -uptsdb -pptsdb --local test E:/mysql/imp/imp.txt
mysqlimport: Error: Table 'test.imp' doesn't exist, when using table: imp
E:/mysql>mysqlimport -uptsdb -pptsdb --local test E:/mysql/imp/imptest.txt
test.imptest: Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
注意:文件名称必须跟表名同样
參数
-d or --delete 新数据导入数据表中之前删除数据数据表中的全部信息
-f or --force 无论是否遇到错误,mysqlimport将强制继续插入数据
-i or --ignore mysqlimport跳过或者忽略那些有同样唯一
-r or -replace 这个选项与-i选项的作用相反;此选项将替代 表中有同样唯一keyword的记录
2.7. 数据管道导入导出
E:/mysql>mysql -h10.5.1.66 -uroot -proot --default-character-set=name frontdb_20060415(databasename)
>e:/mysql/test.txt (输出的sql 语句)
1:>;create databases newname(在新的server上建立空的数据库)
2:#/usr/local/mysql/bin/mysqldump databasename >*.sql(在旧的server上导出数据库)
3:#/usr/local/mysql/bin/mysql databasename < *.sql(在新的server上导入*.sql)
注意数据库名要一一相应.
2.8. 各种字段的取值范围
TINYINT 1 byte
-128 - 127
TINYINT UNSIGNED 1 byte
0 ? 255 即0-(28-1)
SMALLINT 2 bytes
-32768 - 32767 即-215至(215-1)
SMALLINT UNSIGNED 2 bytes
0 - 65535 即0至(216-1)
MEDIUMINT 3 bytes
-8388608 - 8388607 即?223至(223-1)
MEDIUMINT UNSIGNED 3 bytes
0 - 16777215 即0至(224-1)
INT 或 INTEGER 4 bytes
-2147483648 - 2147483647 即?231至(231-1)
INT UNSIGNED 或 INTEGER UNSIGNED 4 bytes
0 - 4294967295 即0至(232-1)
BIGINT 8 bytes
-9223372036854775808 - 9223372036854775807 即?263至(263-1)
BIGINT UNSIGNED 8 bytes
0 - 18446744073709551615 即0至(264-1)
FLOAT 4 bytes
-3.402823466E+38 - -1.175494351E-38
0
1.175494351E-38 - 3.402823466E+38
DOUBLE 或 DOUBLE PRECISION 或 REAL 8 bytes
-1.7976931348623157E+308 - -2.2250738585072014E-308
0
2.2250738585072014E-308 - 1.7976931348623157E+308
DECIMAL[(M,[D])] 或 NUMERIC(M,D) 不定
由M(整个数字的长度,包含小数点,小数点左边的位数,小数点右边的位数,但不包含负号)和
D(小数点右边的位数)来决定,M缺省为10,D缺省为0
DATE 3 bytes
1000-01-01 - 9999-12-31
DATETIME 8 bytes
1000-01-01 00:00:00 - 9999-12-31 23:59:59
TIMESTAMP 4 bytes
1970-01-01 00:00:00 - 2037-12-31 23:59:59
TIME 3 bytes
-838:59:59' to 838:59:59
YEAR[(2|4)] 1 byte
缺省为4位格式,4位格式取值范围为1901 - 2155,0000,2位格式取值范围为70-69(1970-2069)
CHAR(M) [BINARY] 或 NCHAR(M) [BINARY] M bytes
M的范围为1 - 255,假设没有BINARY项,则不分大写和小写,NCHAR表示使用缺省的字符集.在数据库 中以空格补足,但在取出来时末尾的空格将被去掉.
[NATIONAL] VARCHAR(M) [BINARY]
Before 5.0.3 M的范围为0?255 L+1 bytes L<=M
5.0.3 以及以后 M 范围为 0-65535
L+1 bytes L<=M 0<=M<=256
L+2 bytes L<=M 256<=65535
在数据库中末尾的空格将自己主动去掉.
TINYBLOB 或 TINYTEXT L+1 bytes
255(2^8-1)个字符
BLOB 或 TEXT L+2 bytes
65535(2^16-1)个字符
MEDIUMBLOB 或 MEDIUMTEXT L+3 bytes
16777215 (2^24-1)个字符
LONGBLOB 或 LONGTEXT L+4 bytes
4294967295 (2^32-1)个字符
ENUM('value1','value2',...) 1 or 2 bytes
能够总共同拥有65535个不同的值
SET('value1','value2',...) 1/2/3/4/8 bytes
最多有64个成员
2.9. 查询
2.9.1. limit
LIMIT子句能够用来限制由SELECT语句返回过来的数据数量,它有一个或两个參数,假设给出两个參数,
第一个參数指定返回的第一行在全部数据中的位置,从0開始(注意不是1),第二个參数指定最多返回行
数。比如:
select * from table LIMIT 5,10; #返回第6-15行数据
select * from table LIMIT 5; #返回前5行
select * from table LIMIT 0,5; #返回前5行
2.9.2. join 具体解释
还是先 Create table 吧
create table emp(
id int not null primary key,
name varchar(10)
);
create table emp_dept(
dept_id varchar(4) not null,
emp_id int not null,
emp_name varchar(10),
primary key (dept_id,emp_id));
insert into emp() values
(1,"Dennis-1"),
(2,"Dennis-2"),
(3,"Dennis-3"),
(4,"Dennis-4"),
(5,"Dennis-5"),
(6,"Dennis-6"),
(7,"Dennis-7"),
(8,"Dennis-8"),
(9,"Dennis-9"),
(10,"Dennis-10");
insert into emp_dept() values
("R&D",1,"Dennis-1"),
("DEv",2,"Dennis-2"),
("R&D",3,"Dennis-3"),
("Test",4,"Dennis-4"),
("Test",5,"Dennis-5");
("dddd",20,"eeee");
>> left join
-------------
select a.id,a.name,b.dept_id
from emp a left join emp_dept b on (a.id=b.emp_id);
# 挑出左边的 table emp 中的全部资料,即使 emp_dept 中没有的资料也挑出来,没有的就用 NULL 来显示,
# 也即显示资料是以左边的 table emp 中的资料为基础
mysql> select a.id,a.name,b.dept_id
-> from emp a left join emp_dept b on (a.id=b.emp_id);
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 1 | Dennis-1 | R&D |
| 2 | Dennis-2 | DEv |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 挑出 table emp 中有而 table emp_dept 中没有的人员资料
select a.id,a.name,b.dept_id
from emp a left join emp_dept b on (a.id=b.emp_id)
where b.dept_id IS NULL;
mysql> select a.id,a.name,b.dept_id
-> from emp a left join emp_dept b on (a.id=b.emp_id)
-> where b.dept_id IS NULL;
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 把 table emp_dept 放在左边的情形(当然以 emp_dept 中的数据为基础来显示资料,emp 中比emp_dept 中多的资料也就不会显示出来了):
select a.id,a.name,b.dept_id
from emp_dept b left join emp a on (a.id=b.emp_id);
mysql> select a.id,a.name,b.dept_id
-> from emp_dept b left join emp a on (a.id=b.emp_id);
+------+----------+---------+
| id | name | dept_id |
+------+----------+---------+
| NULL | NULL | dddd |
| 2 | Dennis-2 | DEv |
| 1 | Dennis-1 | R&D |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
+------+----------+---------+
>> right join
---------------
select a.id,a.name,b.dept_id
from emp a right join emp_dept b on (a.id=b.emp_id);
# 挑资料时以右边 table emp_dept 中的资料为基础来显示资料
mysql> select a.id,a.name,b.dept_id
-> from emp a right join emp_dept b on (a.id=b.emp_id);
+------+----------+---------+
| id | name | dept_id |
+------+----------+---------+
| NULL | NULL | dddd |
| 2 | Dennis-2 | DEv |
| 1 | Dennis-1 | R&D |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
+------+----------+---------+
6 rows in set (0.00 sec)
# 我们再把 table 的位置交换一下,再用 right join 试试
select a.id,a.name,b.dept_id
from emp_dept b right join emp a on (a.id=b.emp_id);
mysql> select a.id,a.name,b.dept_id
-> from emp_dept b right join emp a on (a.id=b.emp_id);
+----+-----------+---------+
| id | name | dept_id |
+----+-----------+---------+
| 1 | Dennis-1 | R&D |
| 2 | Dennis-2 | DEv |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
| 6 | Dennis-6 | NULL |
| 7 | Dennis-7 | NULL |
| 8 | Dennis-8 | NULL |
| 9 | Dennis-9 | NULL |
| 10 | Dennis-10 | NULL |
+----+-----------+---------+
# 是不是和 left join 一样了?
>> inner join STRAIGHT_JOIN
select a.id,a.name,b.dept_id
from emp a ,emp_dept b
where a.id=b.emp_id;
mysql> select a.id,a.name,b.dept_id
-> from emp a ,emp_dept b
-> where a.id=b.emp_id;
+----+----------+---------+
| id | name | dept_id |
+----+----------+---------+
| 2 | Dennis-2 | DEv |
| 1 | Dennis-1 | R&D |
| 3 | Dennis-3 | R&D |
| 4 | Dennis-4 | Test |
| 5 | Dennis-5 | Test |
+----+----------+---------+
2.9.3. 别名 alias
你能够在GROUP BY、ORDER BY或在HAVING部分中使用别名引用列。别名也能够用来为列取一个更好点的名字:
SELECT SQRT(a*b) as rt FROM table_name GROUP BY rt HAVING rt > 0;
SELECT id,COUNT(*) AS cnt FROM table_name GROUP BY id HAVING cnt > 0;
SELECT id AS "Customer identity" FROM table_name;
注意,你的 ANSI SQL 不同意你在一个WHERE子句中引用一个别名。这是由于在WHERE代码被运行时,列值还可能没有终结。比例如以下列查询是不合法:
SELECT id,COUNT(*) AS cnt FROM table_name WHERE cnt > 0 GROUP BY id;
WHERE语句被运行以确定哪些行应该包含GROUP BY部分中,而HAVING用来决定应该仅仅用结果集合中的哪些行。
2.9.4. 正则
正則表達式(regex)是定义复杂查询的一个强有力的工具。
这里是一个简单的资料,它忽略了一些具体的信息。
正則表達式定义了一个字符串的规则。最简单的正則表達式不包含不论什么保留字。比如,
正則表達式hello仅仅和字符串“hello”匹配。
一般的正則表達式使用了某些特殊的结构,所以它能匹配很多其它的字符串。比如,正则
表达式hello|word既能匹配字符串“hello”也能匹配字符串“word”。
举一个更复杂一点的样例,正則表達式B[an]*s能够匹配字符串“Bananas”、“Baaaaa
s”
、“Bs”以及其它不论什么以B开头以s结尾的字符串,中间能够包含随意个a和随意个n的组
合。
一个正則表達式中的能够使用下面保留字
^
所匹配的字符串以后面的字符串开头
mysql> select "fonfo" REGEXP "^fo$"; -> 0(表示不匹配)
mysql> select "fofo" REGEXP "^fo"; -> 1(表示匹配)
$
所匹配的字符串曾经面的字符串结尾
mysql> select "fono" REGEXP "^fono$"; -> 1(表示匹配)
mysql> select "fono" REGEXP "^fo$"; -> 0(表示不匹配)
..
匹配不论什么字符(包含新行)
mysql> select "fofo" REGEXP "^f.*"; -> 1(表示匹配)
mysql> select "fonfo" REGEXP "^f.*"; -> 1(表示匹配)
a*
匹配随意0-n多个a(包含空串)
mysql> select "Ban" REGEXP "^Ba*n"; -> 1(表示匹配)
mysql> select "Baaan" REGEXP "^Ba*n"; -> 1(表示匹配)
mysql> select "Bn" REGEXP "^Ba*n"; -> 1(表示匹配)
a+
匹配随意1-n多个a(不包含空串)
mysql> select "Ban" REGEXP "^Ba+n"; -> 1(表示匹配)
mysql> select "Bn" REGEXP "^Ba+n"; -> 0(表示不匹配)
a?
匹配0-1个a
mysql> select "Bn" REGEXP "^Ba?n"; -> 1(表示匹配)
mysql> select "Ban" REGEXP "^Ba?n"; -> 1(表示匹配)
mysql> select "Baan" REGEXP "^Ba?n"; -> 0(表示不匹配)
de|abc
匹配de或abc
mysql> select "pi" REGEXP "pi|apa"; -> 1(表示匹配)
mysql> select "axe" REGEXP "pi|apa"; -> 0(表示不匹配)
mysql> select "apa" REGEXP "pi|apa"; -> 1(表示匹配)
mysql> select "apa" REGEXP "^(pi|apa)$"; -> 1(表示匹配)
mysql> select "pi" REGEXP "^(pi|apa)$"; -> 1(表示匹配)
mysql> select "pix" REGEXP "^(pi|apa)$"; -> 0(表示不匹配)
(abc)*
匹配随意多个(0-n个)abc(包含空串)
mysql> select "pi" REGEXP "^(pi)*$"; -> 1(表示匹配)
mysql> select "pip" REGEXP "^(pi)*$"; -> 0(表示不匹配)
mysql> select "pipi" REGEXP "^(pi)*$"; -> 1(表示匹配)
{1}
{2,3}
这是一个更全面的方法,它能够实现前面好几种保留字的功能
a*
能够写成a{0,}
a+
能够写成a{1,}
a?
能够写成a{0,1}
在{}内仅仅有一个整型參数i,表示字符仅仅能出现i次;在{}内有一个整型參数i,
后面跟一个“,”,表示字符能够出现i次或i次以上;在{}内仅仅有一个整型參数i,
后面跟一个“,”,再跟一个整型參数j,表示字符仅仅能出现i次以上,j次下面
(包含i次和j次)。当中的整型參数必须大于等于0,小于等于 RE_DUP_MAX(默认是25
5)。
假设有两个參数,第二个必须大于等于第一个
[a-dX]
匹配“a”、“b”、“c”、“d”或“X”
[^a-dX]
匹配除“a”、“b”、“c”、“d”、“X”以外的不论什么字符。
“[”、“]”必须成对使用
mysql> select "aXbc" REGEXP "[a-dXYZ]"; -> 1(表示匹配)
mysql> select "aXbc" REGEXP "^[a-dXYZ]$"; -> 0(表示不匹配)
mysql> select "aXbc" REGEXP "^[a-dXYZ]+$"; -> 1(表示匹配)
mysql> select "aXbc" REGEXP "^[^a-dXYZ]+$"; -> 0(表示不匹配)
mysql> select "gheis" REGEXP "^[^a-dXYZ]+$"; -> 1(表示匹配)
mysql> select "gheisa" REGEXP "^[^a-dXYZ]+$"; -> 0(表示不匹配)
------------------------------------------------------------
[[.characters.]]
表示比較元素的顺序。在括号内的字符顺序是唯一的。可是括号中能够包含通配符,
所以他能匹配很多其它的字符。举例来说:正則表達式[[.ch.]]*c匹配chchcc的前五个字符
。
[=character_class=]
表示相等的类,能够取代类中其它相等的元素,包含它自己。比如,假设o和(+)是
一个相等的类的成员,那么[[=o=]]、[[=(+)=]]和[o(+)]是全然等价的。
[:character_class:]
在括号中面,在[:和:]中间是字符类的名字,能够代表属于这个类的全部字符。
字符类的名字有: alnum、digit、punct、alpha、graph、space、blank、lower、uppe
r、cntrl、print和xdigit
mysql> select "justalnums" REGEXP "[[:alnum:]]+"; -> 1(表示匹配)
mysql> select "!!" REGEXP "[[:alnum:]]+"; -> 0(表示不匹配)
[[:<:>
[[:>:]]
分别匹配一个单词开头和结尾的空的字符串,这个单词开头和结尾都不是包含在alnum中
的字符也不能是下划线。
mysql> select "a word a" REGEXP "[[:<:>:]]"; -> 1(表示匹配)
mysql> select "a xword a" REGEXP "[[:<:>:]]"; -> 0(表示不匹配)
mysql> select "weeknights" REGEXP "^(wee|week)(knights|nights)$"; -> 1(表示
匹配)
2.9.5. Select 中使用 IF Statement
mysql> select * from test;
+------+------+------+-------+
| dept | id | sex | name |
+------+------+------+-------+
| 1 | 1 | 0 | wang |
| 2 | 2 | 1 | zhang |
| 3 | 3 | 0 | li |
+------+------+------+-------+
3 rows in set (0.00 sec)
mysql> select dept,id,if(sex=0,'女','男') sex,name from test;
+------+------+-----+-------+
| dept | id | sex | name |
+------+------+-----+-------+
| 1 | 1 | 女 | wang |
| 2 | 2 | 男 | zhang |
| 3 | 3 | 女 | li |
+------+------+-----+-------+
3 rows in set (0.00 sec)
2.9.6. Select中使用CASE Statement
mysql> select dept,id,(case sex when '0' then '女' else '男' end) as sex,name from test;
+------+------+------+-------+
| dept | id | sex | name |
+------+------+------+-------+
| 1 | 1 | 女 | wang |
| 2 | 2 | 男 | zhang |
| 3 | 3 | 女 | li |
+------+------+------+-------+
mysql> select (case dept when '1' then 'no1' when '2' then 'no2' else 'other' end) as dept from test;
+-------+
| dept |
+-------+
| no1 |
| no2 |
| other |
+-------+
3 rows in set (0.00 sec)
注意: 相当于Oracle 中的decode 和case when
在统计报表中非常实用处
2.10. 存储过程和函数
CREATE PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
CREATE FUNCTION sp_name ([func_parameter[,...]])
RETURNS type
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type
func_parameter:
param_name type
type:
Any valid MySQL data type
characteristic:
LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'
routine_body:
Valid SQL procedure statement or statements
样例:创建一个过程hello,显示问候语
mysql> delimiter ;
mysql> drop PROCEDURE if exists hello;
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql>
mysql> CREATE PROCEDURE hello (IN s varchar(20))
-> BEGIN
-> SELECT CONCAT('Hello,',s,'!') as hello;
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call hello('wangyl');
+---------------+
| hello |
+---------------+
| Hello,wangyl! |
+---------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
样例2: 查询t表的记录总数
mysql> delimiter //
mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @a;
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
样例:创建一个函数
mysql> delimiter ;
mysql> drop FUNCTION if exists dateFunction;
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql> CREATE FUNCTION dateFunction (iCase int)
-> RETURNS varchar(50)
-> begin
-> DECLARE iType int;
-> DECLARE sReturn varchar(50);
-> set iType =icase +1;
-> case iType
-> when 1 then select DATE_FORMAT(NOW(),'%Y-%m-%d') into sReturn;
-> when 2 then select DATE_FORMAT(NOW(),'%W %M %Y') into sReturn;
-> else
-> select NOW() into sReturn;
-> end case;
-> return sReturn;
-> end;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> select dateFunction(0);
+-----------------+
| dateFunction(0) |
+-----------------+
| 2005-11-14 |
+-----------------+
1 row in set (0.00 sec)
mysql> select dateFunction(1);
+----------------------+
| dateFunction(1) |
+----------------------+
| Monday November 2005 |
+----------------------+
1 row in set (0.00 sec)
mysql> select dateFunction(2);
+---------------------+
| dateFunction(2) |
+---------------------+
| 2005-11-14 15:05:43 |
+---------------------+
1 row in set (0.00 sec)
2.11. 补充:trigger
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
CREATE TABLE test1(a1 INT);
CREATE TABLE test2(a2 INT);
CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE test4(
a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
b4 INT DEFAULT 0
);
DELIMITER |
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END|
DELIMITER ;
INSERT INTO test3 (a3) VALUES
(NULL), (NULL), (NULL), (NULL), (NULL),
(NULL), (NULL), (NULL), (NULL), (NULL);
INSERT INTO test4 (a4) VALUES
(0), (0), (0), (0), (0), (0), (0), (0), (0), (0);
If you insert the following values into table test1 as shown here:
mysql> INSERT INTO test1 VALUES
-> (1), (3), (1), (7), (1), (8), (4), (4);
Query OK, 8 rows affected (0.01 sec)
Records: 8 Duplicates: 0 Warnings: 0
Then the data in the four tables will be as follows:
mysql> SELECT * FROM test1;
+------+
| a1 |
+------+
| 1 |
| 3 |
| 1 |
| 7 |
| 1 |
| 8 |
| 4 |
| 4 |
+------+
8 rows in set (0.00 sec)
mysql> SELECT * FROM test2;
+------+
| a2 |
+------+
| 1 |
| 3 |
| 1 |
| 7 |
| 1 |
| 8 |
| 4 |
| 4 |
+------+
8 rows in set (0.00 sec)
mysql> SELECT * FROM test3;
+----+
| a3 |
+----+
| 2 |
| 5 |
| 6 |
| 9 |
| 10 |
+----+
5 rows in set (0.00 sec)
mysql> SELECT * FROM test4;
+----+------+
| a4 | b4 |
+----+------+
| 1 | 3 |
| 2 | 0 |
| 3 | 1 |
| 4 | 2 |
| 5 | 0 |
| 6 | 0 |
| 7 | 1 |
| 8 | 1 |
| 9 | 0 |
| 10 | 0 |
+----+------+
10 rows in set (0.00 sec)
2.12. 删除 DELETE Syntax
Single-table syntax:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
Multiple-table syntax:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
tbl_name[.*] [, tbl_name[.*]] ...
FROM table_references
[WHERE where_condition]
Or:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
FROM tbl_name[.*] [, tbl_name[.*]] ...
USING table_references
[WHERE where_condition]
对于删除多个表,不能使用order by 和limit
多表删除语法一: ,仅仅是在删除 在from 前面的表所中匹配的记录
DELETE t1, t2 FROM t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
从t1,t2,t3 表选出要删除的记录,仅仅是删除t1,t2 表中所匹配得这些记录.
多表删除语法二: 在from 列出的表中删除选中的记录.
DELETE FROM t1, t2 USING t1, t2, t3 WHERE t1.id=t2.id AND t2.id=t3.id;
从t1,t2,t3 表选出要删除的记录(使用using ),仅仅是删除t1,t2 表中所匹配得这些记录.
注意: 假设您使用一个别名,您必须使用别名.
DELETE t1 FROM test AS t1, test2 WHERE ...
支持多个数据之间的多表删除,可是在这样的情况下,你必须指定表,而不能使用别名:
DELETE test1.tmp1, test2.tmp2 FROM test1.tmp1, test2.tmp2 WHERE ...
当前,您不能从同样表的子查询中,删除同一表的数据
抛出异常的爱 1 星期前
1016错误:文件无法打开,使用后台修复或者使用phpmyadmin进行修复。
1044错误:数据库用户权限不足,请联系空间商解决
1045错误:数据库server/数据库username/数据库名/数据库password错误,请联系空间商检查帐户。
1054错误:程序文件跟数据库有冲突,请使用正确的程序文件上传上去覆盖。
1146错误:数据表缺失,请恢复备份数据.
1005:创建表失败
1006:创建数据库失败
1007:数据库已存在,创建数据库失败
1008:数据库不存在,删除数据库失败
1009:不能删除数据库文件导致删除数据库失败
1010:不能删除数据文件夹导致删除数据库失败
1011:删除数据库文件失败
1012:不能读取系统表中的记录
1020:记录已被其它用户改动
1021:硬盘剩余空间不足,请加大硬盘可用空间
1022:keyword反复,更改记录失败
1023:关闭时错误发生
1024:读文件错误
1025:更改名字时错误发生
1026:写文件错误
1032:记录不存在
1036:数据表是仅仅读的,不能对它进行改动
1037:系统内存不足,请重新启动数据库或重新启动server
1038:用于排序的内存不足,请增大排序缓冲区
1040:已到达数据库的最大连接数,请加大数据库可用连接数
1041:系统内存不足
1042:无效的主机名
1043:无效连接
1044:当前用户没有訪问数据库的权限
1045:不能连接数据库,username或password错误
1048:字段不能为空
1049:数据库不存在
1050:数据表已存在
1051:数据表不存在
1054:字段不存在
1065:无效的SQL语句,SQL语句为空
1081:不能建立Socket连接
1114:数据表已满,不能容纳不论什么记录
1116:打开的数据表太多
1129:数据库出现异常,请重新启动数据库
1130:连接数据库失败,没有连接数据库的权限
1133:数据库用户不存在
1141:当前用户无权訪问数据库
1142:当前用户无权訪问数据表
1143:当前用户无权訪问数据表中的字段
1146:数据表不存在
1147:没有定义用户对数据表的訪问权限
1149:SQL语句语法错误
1158:网络错误,出现读错误,请检查网络连接状况
1159:网络错误,读超时,请检查网络连接状况
1160:网络错误,出现写错误,请检查网络连接状况
1161:网络错误,写超时,请检查网络连接状况
1062:字段值反复,入库失败
1169:字段值反复,更新记录失败
1177:打开数据表失败
1180:提交事务失败
1181:回滚事务失败
1203:当前用户和数据库建立的连接已到达数据库的最大连接数,请增大可用的数据库连接数或重新启动数据库
1205:加锁超时
1211:当前用户没有创建用户的权限
1216:外键约束检查失败,更新子表记录失败
1217:外键约束检查失败,删除或改动主表记录失败
1226:当前用户使用的资源已超过所同意的资源,请重新启动数据库或重新启动server
1227:权限不足,您无权进行此操作
1235:MySQL版本号过低,不具有本功能
2002:Can't connect to ...通常意味着没有一个MySQLserver运行在系统上或当试图连接mysqldserver时,你正在使用一个错误的套接字文件或TCP/IP端口。
说明:以上仅仅是常见错误号,而且解决的方法也不一定肯定管用!具体请參考MYSQL手冊(各大下载站均有下载)
很多其它具体错误代码
错误名称 / 错误代码 SQL状态
ER_HASHCHK 1000 HY000
ER_NISAMCHK 1001 HY000
ER_NO 1002 HY000
ER_YES 1003 HY000
ER_CANT_CREATE_FILE 1004 HY000
ER_CANT_CREATE_TABLE 1005 HY000
ER_CANT_CREATE_DB 1006 HY000
ER_DB_CREATE_EXISTS 1007 HY000
ER_DB_DROP_EXISTS 1008 HY000
ER_DB_DROP_DELETE 1009 HY000
ER_DB_DROP_RMDIR 1010 HY000
ER_CANT_DELETE_FILE 1011 HY000
ER_CANT_FIND_SYSTEM_REC 1012 HY000
ER_CANT_GET_STAT 1013 HY000
ER_CANT_GET_WD 1014 HY000
ER_CANT_LOCK 1015 HY000
ER_CANT_OPEN_FILE 1016 HY000
ER_FILE_NOT_FOUND 1017 HY000
ER_CANT_READ_DIR 1018 HY000
ER_CANT_SET_WD 1019 HY000
ER_CHECKREAD 1020 HY000
ER_DISK_FULL 1021 HY000
ER_DUP_KEY 1022 23000
ER_ERROR_ON_CLOSE 1023 HY000
ER_ERROR_ON_READ 1024 HY000
ER_ERROR_ON_RENAME 1025 HY000
ER_ERROR_ON_WRITE 1026 HY000
ER_FILE_USED 1027 HY000
ER_FILSORT_ABORT 1028 HY000
ER_FORM_NOT_FOUND 1029 HY000
ER_GET_ERRNO 1030 HY000
ER_ILLEGAL_HA 1031 HY000
ER_KEY_NOT_FOUND 1032 HY000
ER_NOT_FORM_FILE 1033 HY000
ER_NOT_KEYFILE 1034 HY000
ER_OLD_KEYFILE 1035 HY000
ER_OPEN_AS_READONLY 1036 HY000
ER_OUTOFMEMORY 1037 HY001
ER_OUT_OF_SORTMEMORY 1038 HY001
ER_UNEXPECTED_EOF 1039 HY000
ER_CON_COUNT_ERROR 1040 08004
ER_OUT_OF_RESOURCES 1041 08004
ER_BAD_HOST_ERROR 1042 08S01
ER_HANDSHAKE_ERROR 1043 08S01
ER_DBACCESS_DENIED_ERROR 1044 42000
ER_ACCESS_DENIED_ERROR 1045 42000
ER_NO_DB_ERROR 1046 42000
ER_UNKNOWN_COM_ERROR 1047 08S01
ER_BAD_NULL_ERROR 1048 23000
ER_BAD_DB_ERROR 1049 42000
ER_TABLE_EXISTS_ERROR 1050 42S01
ER_BAD_TABLE_ERROR 1051 42S02
ER_NON_UNIQ_ERROR 1052 23000
ER_SERVER_SHUTDOWN 1053 08S01
ER_BAD_FIELD_ERROR 1054 42S22
ER_WRONG_FIELD_WITH_GROUP 1055 42000
ER_WRONG_GROUP_FIELD 1056 42000
ER_WRONG_SUM_SELECT 1057 42000
ER_WRONG_VALUE_COUNT 1058 21S01
ER_TOO_LONG_IDENT 1059 42000
ER_DUP_FIELDNAME 1060 42S21
ER_DUP_KEYNAME 1061 42000
ER_DUP_ENTRY 1062 23000
ER_WRONG_FIELD_SPEC 1063 42000
ER_PARSE_ERROR 1064 42000
ER_EMPTY_QUERY 1065 42000
ER_NONUNIQ_TABLE 1066 42000
ER_INVALID_DEFAULT 1067 42000
ER_MULTIPLE_PRI_KEY 1068 42000
ER_TOO_MANY_KEYS 1069 42000
ER_TOO_MANY_KEY_PARTS 1070 42000
ER_TOO_LONG_KEY 1071 42000
ER_KEY_COLUMN_DOES_NOT_EXITS 1072 42000
ER_BLOB_USED_AS_KEY 1073 42000
ER_TOO_BIG_FIELDLENGTH 1074 42000
ER_WRONG_AUTO_KEY 1075 42000
ER_READY 1076 00000
ER_NORMAL_SHUTDOWN 1077 00000
ER_GOT_SIGNAL 1078 00000
ER_SHUTDOWN_COMPLETE 1079 00000
ER_FORCING_CLOSE 1080 08S01
ER_IPSOCK_ERROR 1081 08S01
ER_NO_SUCH_INDEX 1082 42S12
ER_WRONG_FIELD_TERMINATORS 1083 42000
ER_BLOBS_AND_NO_TERMINATED 1084 42000
ER_TEXTFILE_NOT_READABLE 1085 HY000
ER_FILE_EXISTS_ERROR 1086 HY000
ER_LOAD_INFO 1087 HY000
ER_ALTER_INFO 1088 HY000
ER_WRONG_SUB_KEY 1089 HY000
ER_CANT_REMOVE_ALL_FIELDS 1090 42000
ER_CANT_DROP_FIELD_OR_KEY 1091 42000
ER_INSERT_INFO 1092 HY000
ER_UPDATE_TABLE_USED 1093 HY000
ER_NO_SUCH_THREAD 1094 HY000
ER_KILL_DENIED_ERROR 1095 HY000
ER_NO_TABLES_USED 1096 HY000
ER_TOO_BIG_SET 1097 HY000
ER_NO_UNIQUE_LOGFILE 1098 HY000
ER_TABLE_NOT_LOCKED_FOR_WRITE 1099 HY000
ER_TABLE_NOT_LOCKED 1100 HY000
ER_BLOB_CANT_HAVE_DEFAULT 1101 42000
ER_WRONG_DB_NAME 1102 42000
ER_WRONG_TABLE_NAME 1103 42000
ER_TOO_BIG_SELECT 1104 42000
ER_UNKNOWN_ERROR 1105 HY000
ER_UNKNOWN_PROCEDURE 1106 42000
ER_WRONG_PARAMCOUNT_TO_PROCEDURE 1107 42000
ER_WRONG_PARAMETERS_TO_PROCEDURE 1108 HY000
ER_UNKNOWN_TABLE 1109 42S02
ER_FIELD_SPECIFIED_TWICE 1110 42000
ER_INVALID_GROUP_FUNC_USE 1111 42000
ER_UNSUPPORTED_EXTENSION 1112 42000
ER_TABLE_MUST_HAVE_COLUMNS 1113 42000
ER_RECORD_FILE_FULL 1114 HY000
ER_UNKNOWN_CHARACTER_SET 1115 42000
ER_TOO_MANY_TABLES 1116 HY000
ER_TOO_MANY_FIELDS 1117 HY000
ER_TOO_BIG_ROWSIZE 1118 42000
ER_STACK_OVERRUN 1119 HY000
ER_WRONG_OUTER_JOIN 1120 42000
ER_NULL_COLUMN_IN_INDEX 1121 42000
ER_CANT_FIND_UDF 1122 HY000
ER_CANT_INITIALIZE_UDF 1123 HY000
ER_UDF_NO_PATHS 1124 HY000
ER_UDF_EXISTS 1125 HY000
ER_CANT_OPEN_LIBRARY 1126 HY000
ER_CANT_FIND_DL_ENTRY 1127 HY000
ER_FUNCTION_NOT_DEFINED 1128 HY000
ER_HOST_IS_BLOCKED 1129 HY000
ER_HOST_NOT_PRIVILEGED 1130 HY000
ER_PASSWORD_ANONYMOUS_USER 1131 42000
ER_PASSWORD_NOT_ALLOWED 1132 42000
ER_PASSWORD_NO_MATCH 1133 42000
ER_UPDATE_INFO 1134 HY000
ER_CANT_CREATE_THREAD 1135 HY000
ER_WRONG_VALUE_COUNT_ON_ROW 1136 21S01
ER_CANT_REOPEN_TABLE 1137 HY000
ER_INVALID_USE_OF_NULL 1138 42000
ER_REGEXP_ERROR 1139 42000
ER_MIX_OF_GROUP_FUNC_AND_FIELDS 1140 42000
ER_NONEXISTING_GRANT 1141 42000
ER_TABLEACCESS_DENIED_ERROR 1142 42000
ER_COLUMNACCESS_DENIED_ERROR 1143 42000
ER_ILLEGAL_GRANT_FOR_TABLE 1144 42000
ER_GRANT_WRONG_HOST_OR_USER 1145 42000
ER_NO_SUCH_TABLE 1146 42S02
ER_NONEXISTING_TABLE_GRANT 1147 42000
ER_NOT_ALLOWED_COMMAND 1148 42000
ER_SYNTAX_ERROR 1149 42000
ER_DELAYED_CANT_CHANGE_LOCK 1150 HY000
ER_TOO_MANY_DELAYED_THREADS 1151 HY000
ER_ABORTING_CONNECTION 1152 08S01
ER_NET_PACKET_TOO_LARGE 1153 08S01
ER_NET_READ_ERROR_FROM_PIPE 1154 08S01
ER_NET_FCNTL_ERROR 1155 08S01
ER_NET_PACKETS_OUT_OF_ORDER 1156 08S01
ER_NET_UNCOMPRESS_ERROR 1157 08S01
ER_NET_READ_ERROR 1158 08S01
ER_NET_READ_INTERRUPTED 1159 08S01
ER_NET_ERROR_ON_WRITE 1160 08S01
ER_NET_WRITE_INTERRUPTED 1161 08S01
ER_TOO_LONG_STRING 1162 42000
ER_TABLE_CANT_HANDLE_BLOB 1163 42000
ER_TABLE_CANT_HANDLE_AUTO_INCREMENT 1164 42000
ER_DELAYED_INSERT_TABLE_LOCKED 1165 HY000
ER_WRONG_COLUMN_NAME 1166 42000
ER_WRONG_KEY_COLUMN 1167 42000
ER_WRONG_MRG_TABLE 1168 HY000
ER_DUP_UNIQUE 1169 23000
ER_BLOB_KEY_WITHOUT_LENGTH 1170 42000
ER_PRIMARY_CANT_HAVE_NULL 1171 42000
ER_TOO_MANY_ROWS 1172 42000
ER_REQUIRES_PRIMARY_KEY 1173 42000
ER_NO_RAID_COMPILED 1174 HY000
ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE 1175 HY000
ER_KEY_DOES_NOT_EXITS 1176 HY000
ER_CHECK_NO_SUCH_TABLE 1177 42000
ER_CHECK_NOT_IMPLEMENTED 1178 42000
ER_CANT_DO_THIS_DURING_AN_TRANSACTION 1179 25000
ER_ERROR_DURING_COMMIT 1180 HY000
ER_ERROR_DURING_ROLLBACK 1181 HY000
ER_ERROR_DURING_FLUSH_LOGS 1182 HY000
ER_ERROR_DURING_CHECKPOINT 1183 HY000
ER_NEW_ABORTING_CONNECTION 1184 08S01
ER_DUMP_NOT_IMPLEMENTED 1185 HY000
ER_FLUSH_MASTER_BINLOG_CLOSED 1186 HY000
ER_INDEX_REBUILD 1187 HY000
ER_MASTER 1188 HY000
ER_MASTER_NET_READ 1189 08S01
ER_MASTER_NET_WRITE 1190 08S01
ER_FT_MATCHING_KEY_NOT_FOUND 1191 HY000
ER_LOCK_OR_ACTIVE_TRANSACTION 1192 HY000
ER_UNKNOWN_SYSTEM_VARIABLE 1193 HY000
ER_CRASHED_ON_USAGE 1194 HY000
ER_CRASHED_ON_REPAIR 1195 HY000
ER_WARNING_NOT_COMPLETE_ROLLBACK 1196 HY000
ER_TRANS_CACHE_FULL 1197 HY000
ER_SLAVE_MUST_STOP 1198 HY000
ER_SLAVE_NOT_RUNNING 1199 HY000
ER_BAD_SLAVE 1200 HY000
ER_MASTER_INFO 1201 HY000
ER_SLAVE_THREAD 1202 HY000
ER_TOO_MANY_USER_CONNECTIONS 1203 42000
ER_SET_CONSTANTS_ONLY 1204 HY000
ER_LOCK_WAIT_TIMEOUT 1205 HY000
ER_LOCK_TABLE_FULL 1206 HY000
ER_READ_ONLY_TRANSACTION 1207 25000
ER_DROP_DB_WITH_READ_LOCK 1208 HY000
ER_CREATE_DB_WITH_READ_LOCK 1209 HY000
ER_WRONG_ARGUMENTS 1210 HY000
ER_NO_PERMISSION_TO_CREATE_USER 1211 42000
ER_UNION_TABLES_IN_DIFFERENT_DIR 1212 HY000
ER_LOCK_DEADLOCK 1213 40001
ER_TABLE_CANT_HANDLE_FULLTEXT 1214 HY000
ER_CANNOT_ADD_FOREIGN 1215 HY000
ER_NO_REFERENCED_ROW 1216 23000
ER_ROW_IS_REFERENCED 1217 23000
ER_CONNECT_TO_MASTER 1218 08S01
ER_QUERY_ON_MASTER 1219 HY000
ER_ERROR_WHEN_EXECUTING_COMMAND 1220 HY000
ER_WRONG_USAGE 1221 HY000
ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT 1222 21000
ER_CANT_UPDATE_WITH_READLOCK 1223 HY000
ER_MIXING_NOT_ALLOWED 1224 HY000
ER_DUP_ARGUMENT 1225 HY000
ER_USER_LIMIT_REACHED 1226 42000
ER_SPECIFIC_ACCESS_DENIED_ERROR 1227 HY000
ER_LOCAL_VARIABLE 1228 HY000
ER_GLOBAL_VARIABLE 1229 HY000
ER_NO_DEFAULT 1230 42000
ER_WRONG_VALUE_FOR_VAR 1231 42000
ER_WRONG_TYPE_FOR_VAR 1232 42000
ER_VAR_CANT_BE_READ 1233 HY000
ER_CANT_USE_OPTION_HERE 1234 42000
ER_NOT_SUPPORTED_YET 1235 42000
ER_MASTER_FATAL_ERROR_READING_BINLOG 1236 HY000
ER_WRONG_FK_DEF 1237 42000
ER_KEY_REF_DO_NOT_MATCH_TABLE_REF 1238 HY000
ER_CARDINALITY_COL 1239 21000
ER_SUBSELECT_NO_1_ROW 1240 21000
ER_UNKNOWN_STMT_HANDLER 1241 HY000
ER_CORRUPT_HELP_DB 1242 HY000
ER_CYCLIC_REFERENCE 1243 HY000
ER_AUTO_CONVERT 1244 HY000
ER_ILLEGAL_REFERENCE 1245 42S22
ER_DERIVED_MUST_HAVE_ALIAS 1246 42000
ER_SELECT_REDUCED 1247 01000
ER_TABLENAME_NOT_ALLOWED_HERE 1248 42000
ER_NOT_SUPPORTED_AUTH_MODE 1249 08004
ER_SPATIAL_CANT_HAVE_NULL 1250 42000
ER_COLLATION_CHARSET_MISMATCH 1251 42000
ER_SLAVE_WAS_RUNNING 1252 HY000
ER_SLAVE_WAS_NOT_RUNNING 1253 HY000
ER_TOO_BIG_FOR_UNCOMPRESS 1254 HY000
ER_ZLIB_Z_MEM_ERROR 1255 HY000
ER_ZLIB_Z_BUF_ERROR 1256 HY000
ER_ZLIB_Z_DATA_ERROR 1257 HY000
ER_CUT_VALUE_GROUP_CONCAT 1258 HY000
ER_WARN_TOO_FEW_RECORDS 1259 01000
ER_WARN_TOO_MANY_RECORDS 1260 01000
ER_WARN_NULL_TO_NOTNULL 1261 01000
ER_WARN_DATA_OUT_OF_RANGE 1262 01000
ER_WARN_DATA_TRUNCATED 1263 01000
ER_WARN_USING_OTHER_HANDLER 1264 01000
ER_CANT_AGGREGATE_COLLATIONS 1265 42000
ER_DROP_USER 1266 42000
ER_REVOKE_GRANTS 1267 42000