简体   繁体   中英

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; 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

what is going on?

on a side note, the create table statement does not work for me:

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

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.

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. (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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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