Steve
Steve

Reputation: 53

cannot find error in mysql code

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

Upvotes: 0

Views: 147

Answers (2)

user2363448
user2363448

Reputation:

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.

Upvotes: 1

Kermit
Kermit

Reputation: 34055

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

Upvotes: 1

Related Questions