您的位置:首页 > 其它

You can't specify target table X for update in FROM clause

2016-03-01 16:56 501 查看
      mysql中遇到的问题,

mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)。 例如下面这个sql:

delete from tbl where id in

(

        select max(id) from tbl a where EXISTS

        (

            select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1

        )

        group by tac

)

改写成下面就行了:

delete from tbl where id in

(

    select a.id from

    (

        select max(id) id from tbl a where EXISTS

        (

            select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1

        )

        group by tac

    ) a

)

也就是说将select出的结果再通过中间表select一遍,这样就规避了错误。注意,这个问题只出现于mysql,mssql和oracle不会出现此问题。

字段长度限制的问题:

      

在MySQL建表时,遇到一个奇怪的现象:

root@localhost : test 10:30:54>CREATE TABLE tb_test (
-> recordid varchar(32) NOT NULL,
-> areaShow varchar(10000) DEFAULT NULL,
-> areaShow1 varchar(10000) DEFAULT NULL,
-> areaShow2 varchar(10000) DEFAULT NULL,
-> PRIMARY KEY (recordid)
-> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs
报错

root@localhost : test 10:31:01>CREATE TABLE tb_test (
-> recordid varchar(32) NOT NULL,
-> areaShow varchar(30000) DEFAULT NULL,
-> areaShow1 varchar(30000) DEFAULT NULL,
-> areaShow2 varchar(30000) DEFAULT NULL,
-> PRIMARY KEY (recordid)
-> ) ENGINE=INNODB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected, 3 warnings (0.26 sec)
可以建立只是类型被转换了。

root@localhost : test 10:31:14>show warnings;
+-------+------+----------------------------------------------------+
| Level | Code | Message                                            |
+-------+------+----------------------------------------------------+
| Note  | 1246 | Converting column 'areaShow' from VARCHAR to TEXT  |
| Note  | 1246 | Converting column 'areaShow1' from VARCHAR to TEXT |
| Note  | 1246 | Converting column 'areaShow2' from VARCHAR to TEXT |
+-------+------+----------------------------------------------------+
3 rows in set (0.00 sec)


疑问:

为什么字段小(10000)的反而报错,而大(30000)的则可以建立。为什么小的不能直接转换呢?

解决:

这里多感谢orczhou的帮助,原来MySQL在建表的时候有个限制:MySQL要求一个行的定义长度不能超过65535。具体的原因可以看:

http://dev.mysql.com/doc/refman/5.1/en/silent-column-changes.html

(1)单个字段如果大于65535,则转换为TEXT 。

(2)单行最大限制为65535,这里不包括TEXT、BLOB。

按照上面总结的限制,来解释出现的现象:

第一个情况是:
单个字段长度:varchar(10000) ,字节数:10000*3(utf8)+(1 or 2) = 30000 ,小于65535,可以建立。
单行记录长度:varchar(10000)*3,字节数:30000*3(utf8)+(1 or 2) = 90000,大于65535,不能建立,所以报错:

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs


第二个情况是:
单个字段长度:varchar(30000) ,字节数:30000*3+(1 or 2) = 90000 , 大于65535,需要转换成TEXT,才可以建立。所以报warnings。
单行记录长度:varchar(30000)*3,因为每个字段都被转换成了TEXT,而TEXT没有限制,所以可以建立表。

root@localhost : test 10:31:14>show warnings;
+-------+------+----------------------------------------------------+
| Level | Code | Message                                            |
+-------+------+----------------------------------------------------+
| Note  | 1246 | Converting column 'areaShow' from VARCHAR to TEXT  |
| Note  | 1246 | Converting column 'areaShow1' from VARCHAR to TEXT |
| Note  | 1246 | Converting column 'areaShow2' from VARCHAR to TEXT |
+-------+------+----------------------------------------------------+


用了这么久的MySQL,这个基本的建表限制都还不知道,惭愧啊。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: