简体   繁体   English

这种MySQL语法有什么问题?

[英]What is wrong with this MySQL syntax?

INSERT INTO homework_MarcWed30-2011 ('Teacher', 'Class', 'Period', 'Assn') 
                              VALUES ('a', 'a', 'a', 'a')

i get the following error: 我收到以下错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '-2011 (Teacher varchar(30), Class varchar(30), Period varchar(30), Assn varchar(' at line 1 检查与您的MySQL服务器版本相对应的手册以获取在'-2011附近使用的正确语法(Teacher varchar(30),Class varchar(30),Period varchar(30),Assn varchar('在第1行)

what is going on? 到底是怎么回事?

on a side note, the create table statement does not work for me: 另外,create table语句对我不起作用:

mysql_query("CREATE TABLE homework_MarcWed30-2011 (Teacher varchar(30), Class varchar(30), Period varchar(30), Assn varchar(400))","mysql_connect('#####', '#####', '#####')") OR die(mysql_error());

use 采用

`homework_MarcWed30-2011`   

in table name and use ` for quoting column name 在表名中,并使用`引用列名

INSERT INTO homework_MarcWed30-2011 (`Teacher`, `Class`, `Period`, `Assn`) 
                              VALUES ('a', 'a', 'a', 'a')

An identifier may be quoted or unquoted. 标识符可以带引号或不带引号。 If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. 如果标识符包含特殊字符或为保留字,则在引用标识符时必须将其引用。 The set of alphanumeric characters from the current character set, “_”, and “$” are not special. 当前字符集中的字母数字字符集“ _”和“ $”并不特殊。

http://dev.mysql.com/doc/refman/5.0/en/identifiers.html http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

Try this query 试试这个查询

INSERT INTO `homework_marcwed30-2011` (`Teacher` ,`Class` ,`Period` ,`Assn`) VALUES ('a', 'a', 'a', 'a');

Variation , without field quotes 变体 ,不带引号

INSERT INTO `homework_marcwed30-2011` (Teacher ,Class ,Period ,Assn)
VALUES ('a', 'a', 'a', 'a')

Variation , without field defination 变化 ,无场限制

INSERT INTO `homework_marcwed30-2011` VALUES ('a', 'a', 'a', 'a')

For the table creation part 对于表创建部分

mysql_query("CREATE TABLE `homework_MarcWed30-2011` (Teacher varchar(30) NOT NULL, Class varchar(30) NOT NULL, Period varchar(30) NOT NULL, Assn varchar(400) NOT NULL);","mysql_connect('#####', '#####', '#####')") OR die(mysql_error());

Your error is that you didn't define whether to allow Null or not. 您的错误是您没有定义是否允许Null。

Column names should be out of quotes they can be surrounded by back quote `` but not with single or double quote. 列名应该用引号引起来,它们可以用反引号``括起来,但不能用单引号或双引号引起来。

 INSERT INTO `homework_MarcWed30-2011` (`Teacher`, `Class`, `Period`, `Assn`)
 VALUES ('a', 'a', 'a', 'a')

if there is - in table name you can still use by surrounding name in back quotes ``. 如果在表名中有-,您仍然可以在反引号``周围使用名字。

First of all, I don't think you can have "-" within a table name. 首先,我认为表名称中不能包含“-”。 Not certain on that one , but that is what it is suggesting based on the near '-2011' comment. 不确定那一项,但这是基于近乎“ -2011”评论的建议。 (or as stated above, surround it via backticks)... that's the better option here!) (或如上所述,通过反引号将其包围)...这是更好的选择!)

Secondly, the second one will die because you have a PHP syntax error: 其次,第二个将死亡,因为您有一个PHP语法错误:

mysql_query(""...", "mysql_connect(...)") OR die(msyql_error());

The first parameter has two beginning quotes: meaning a string has started and ended. 第一个参数有两个开头的引号:表示字符串已开始和结束。 Remove one. 删除一个。

And without looking up the documentation, I believe the second parameter requires a resource, if I remember correctly. 而且,如果我没有记错的话,我相信不用看文档,第二个参数就需要资源。 You are sending it a string. 您正在发送一个字符串。 try removing the quotes. 尝试删除引号。

You can't use a dash in the table name as a bare string. 您不能在表格名称中使用破折号作为裸字符串。 If instead you used underscore it will work fine: 相反,如果您使用下划线,它将可以正常工作:

mysql> CREATE TABLE homework_MarcWed30_2011 (Teacher varchar(30), Class varchar(30), Period varchar(30), Assn varchar(400));
Query OK, 0 rows affected (0.09 sec)

in the insert statement you can't use quotes for the column names and if you are inserting all the columns you don't need to list them at all: 在插入语句中,您不能在列名中使用引号,并且如果要插入所有列,则根本不需要列出它们:

mysql> INSERT INTO homework_MarcWed30_2011 VALUES ('a', 'a', 'a', 'a');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO homework_MarcWed30_2011 (Teacher, Class, Period, Assn) VALUES ('b', 'b', 'b', 'b')

Too much answers and not a single right one 答案太多而不是一个正确的答案

a backtick will do the magic 倒钩会做魔术

INSERT INTO `homework_MarcWed30-2011` (`Teacher`, `Class`, `Period`, `Assn`) 
                          VALUES ('a', 'a', 'a', 'a')

However, as a general rule, give your identifiers less fancy yet sensible names 但是,一般来说,给您的标识符加上一些花哨但又明智的名称

CREATE TABLE homework (
  teacher varchar(30), 
  class varchar(30), 
  period varchar(30),
  assn text
)

keeping them alphanumeric lowercase will save you TONS of trouble 保留字母数字小写将为您节省许多麻烦

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM