創建員工信息表t_user
CREATE TABLE t_user(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) ,
password VARCHAR(20) ,
age int ,
phone VARCHAR(20) ,
email VARCHAR(20) ,
is_Delete int
)DEFAULT CHARSET=UTF8;
往員工表中插入數據:
INSERT INTO t_user VALUES(null,'張三','123456',23,'110','11111@qq.com',1),(null,'歷史','123456',23,'110','11111@qq.com',1),(null,'孫悟空','123456',23,'110','11111@qq.com',2),(null,'李白','123456',23,'110','11111@qq.com',2),(null,'八戒','123456',23,'110','11111@qq.com',2);
問題:發現用delete from t_user where id=6;這個語句刪除不了,其它字段名也試過了也不行,如下:
mysql> select * from t_user; +----------+----------------+----------------+-----------+-------------+--------------+-----------------+ | id | username | password | age | phone | email | is_Delete | +----------+----------------+----------------+-----------+-------------+--------------+-----------------+ | 1 | 唐僧 | 123456 | 23 | 110 | 11111@qq.com | 2 | | 2 | 張三 | 123456 | 23 | 110 | 11111@qq.com | 1 | | 3 | 歷史 | 123456 | 23 | 110 | 11111@qq.com | 1 | | 4 | 孫悟空 | 123456 | 23 | 110 | 11111@qq.com | 2 | | 5 | 李白 | 123456 | 23 | 110 | 11111@qq.com | 2 | | 6 | 八戒 | 123456 | 23 | 110 | 11111@qq.com | 2 | +----------+----------------+----------------+-----------+-------------+--------------+-----------------+ 6 rows in set (0.00 sec) mysql> mysql> delete from t_user where id=6; 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 'delete from t_user where id=6' at line 1 mysql> delete from t_user where id='6'; ERROR 1054 (42S22): Unknown column 'id' in 'where clause' mysql> delete form t_user where id=6; 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 't_user where id=6' at line 1 mysql> delete from t_user where id=6; ERROR 1054 (42S22): Unknown column 'id' in 'where clause' mysql> delete from t_user where username='八戒'; ERROR 1054 (42S22): Unknown column 'username' in 'where clause' mysql> delete from t_user where t_user.id=6; ERROR 1054 (42S22): Unknown column 't_user.id' in 'where clause' mysql>
然后發現是表中的字段名有空格,正常的沒空格的如下:
mysql> select * from dept; +----+--------+ | id | name | +----+--------+ | 1 | 神仙 | | 2 | 妖怪 | +----+--------+
字段名出現空格的話,通過字段名進行數據操作的時候要給字段名添加單引號,如下:
mysql> delete from t_user where 'id'=6; Query OK, 0 rows affected, 1 warning (0.07 sec)