0

I've been staring at this for ages and i cannot figure out what is wrong with this code

CREATE TABLE `faults` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Reported_by` varchar(30) NOT NULL,
`Company` varchar(20) NOT NULL,
`Reporters_email` varchar(100) NOT NULL,
`Department` varchar(20) NOT NULL,
`Error_Detail` text,
`user_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
CONSTRAINT `faults_fk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);

the fault is on the last line

this is the error it gives me which isn't very helpful:

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 '' at line 11

can anybody spot my mistake??

3
  • 7
    no closing bracket? CREATE TABLE faults (... Commented Dec 16, 2013 at 16:04
  • Prob the needed parenthesis REFERENCES 'users' ('id')); Commented Dec 16, 2013 at 16:06
  • @ldrumm that's an answer, not a comment ;-). Commented Dec 16, 2013 at 16:07

2 Answers 2

1

Sometimes, a little formatting helps find your mistake (missing closing parenthesis). I would also recommend being more consistent with your column naming. Don't mix capitalization with lower case and underscores. I would suggest camel case.

CREATE TABLE `faults` 
  ( 
     `id`              INT(11) NOT NULL auto_increment, 
     `reported_by`     VARCHAR(30) NOT NULL, 
     `company`         VARCHAR(20) NOT NULL, 
     `reporters_email` VARCHAR(100) NOT NULL, 
     `department`      VARCHAR(20) NOT NULL, 
     `error_detail`    TEXT, 
     `user_id`         INT(11) NOT NULL, 
     PRIMARY KEY (`id`), 
     KEY `user_id` (`user_id`), 
     CONSTRAINT `faults_fk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) 
  ); <-- missing
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks its always something so simple, and got rid of the caps thanks for advice on caps
1

It appears you have missed a closing bracket. CREATE TABLE faults ( is not closed before your terminating semicolon on line 11 as MySQL is complaining about.

Add the closing bracket and you should be fine: REFERENCES 'users' ('id'));

To more easily notice errors such as these, you should use a text editor that supports bracket and brace matching. All good programmer's editors will do this.

5 Comments

@MikeSmithDev according to whom? They are synonymous as far as I can tell.
@MikeSmithDev Wikipedia would disagree with you.
@ldrumm I see. I've always associated with [ but guess I'm wrong. Learn something every day.
The square bracket [ can be called 'bracket', but on its own, the word bracket is synonymous with both mathematical parentheses as well as square brackets used for indices in most prgramming languages. I agree that in the context of my answer this seems a bit flowery, so I'll edit my post.
i usually use n++ when editing but was using phpmyadmin and also working off putty as was meant to be a quick edit.

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.