1

when i try to insert this code i get an mysql error syntax

CREATE TABLE 'data'
(
'id' int primary key auto_increment,
'data' varchar(50),
'weight' int(2),
)

#1064 - 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 ''data'

what is the error?!

thanks

3 Answers 3

4

You have an extra comma after the weight line:

'weight' int(2),
               ^--- here

as well, you don't enclose field names in quotes, so the correct syntax for the whole thing is:

CREATE TABLE data (
    id int primary key auto_increment,
    data varchar(50),
    weight int(2)
);
Sign up to request clarification or add additional context in comments.

6 Comments

+1 for the comma and taking out the single-quotes. -1 for not recommending backticking field names.
why? backticking only encourages using reserved words in the first place. unless you have an absolutely critical reason to use one, they should be avoided, therefore backticks should be avoided as well.
@MarcB: In the real world, you choose field names based on what the data represents, not whether something might be some obscure MySQL keyword. Getting into the habit of using backticks means that this is never an issue and you can carry on creating expressive, self-explanatory tables without wasting your time on implementation crap.
sure, and I"ll go write some code like function function() { echo "this is stupid"; } because reserved words are just there so the language designers can be proud of themselves. They're reserved for a reason, and being given a tool to bypass the restriction doesn't mean you SHOULD bypass it.
In practice, it is still a good idea to avoid reserved words, even if idealistically there is a way to not care.
|
1

You have an erroneous trailing comma, and the way to delimit fieldnames is with the backtick, not single-quote.

CREATE TABLE `data` (
    `id`     INT PRIMARY KEY AUTO_INCREMENT,
    `data`   VARCHAR(50),
    `weight` INT(2)
);

Comments

0

You're using the wrong type of quotes. MySQL's literal-name quoting uses a backtick, not an on ordinary apostrophe. In fact, you don't really need to quote those names at all.

1 Comment

No, but it's good practice to avoid [potentially quiet] bugs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.