2

When I run query below on phpMyAdmin and Sequel, I got the following error

#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 'id Int UNSIGNED NOT NULL AUTO_INCREMENT, login Char(128) NOT NULL, passw' at line 3.

I can't figure out the error on this query. Mysql is running on 5.5.25 version.

CREATE TABLE user
(
  id Int UNSIGNED NOT NULL AUTO_INCREMENT,
  login Char(128) NOT NULL,
  password Char(128) NOT NULL,
  emailaddress Char(128),
  alternateemailaddress Char(128),
  officephone Char(20),
  officefax Char(20),
  mobilephone Char(20),
  client Int UNSIGNED,
  facility Int UNSIGNED,
  user_status Int UNSIGNED NOT NULL DEFAULT 0,
  valid_from Datetime NOT NULL,
  valid_to Datetime NOT NULL,
  last_login Datetime,
  last_login_from Char(48),
  modified_by Int NOT NULL,
  modified_time Datetime,

  PRIMARY KEY (id),
  FOREIGN KEY client REFERENCES client (id) ON DELETE CASCADE,
  FOREIGN KEY facility REFERENCES facility (id) ON DELETE CASCADE
 
 ) ENGINE = InnoDB
  AUTO_INCREMENT = 0
;
2
  • 1
    Same query but no errors in mysql :) Commented Apr 15, 2013 at 4:26
  • @TheUnlucky i think the error is cause by copying the query. there might some hidden characters or spaces. I tried copying the query from this post, but it still return an error, different error on this time on references. it is good now when i remove the foreign key. thanks for confirming the query. Commented Apr 15, 2013 at 4:30

2 Answers 2

1

You should use backsticks when writing names of tables and fields to avoid MySQL parse it as reserved keywords. You should also use parentheses wrapping your foreign keys names. Working query should look like this:

CREATE TABLE `user`
(
  `id` Int UNSIGNED NOT NULL AUTO_INCREMENT,
  `login` Char(128) NOT NULL,
  `password` Char(128) NOT NULL,
  `emailaddress` Char(128),
  `alternateemailaddress` Char(128),
  `officephone` Char(20),
  `officefax` Char(20),
  `mobilephone` Char(20),
  `client` Int UNSIGNED,
  `facility` Int UNSIGNED,
  `user_status` Int UNSIGNED NOT NULL DEFAULT 0,
  `valid_from` Datetime NOT NULL,
  `valid_to` Datetime NOT NULL,
  `last_login` Datetime,
  `last_login_from` Char(48),
  `modified_by` Int NOT NULL,
  `modified_time` Datetime,

  PRIMARY KEY (`id`),
  FOREIGN KEY (`client`) REFERENCES `client` (`id`) ON DELETE CASCADE,
  FOREIGN KEY (`facility`) REFERENCES `facility` (`id`) ON DELETE CASCADE

 ) ENGINE = InnoDB
  AUTO_INCREMENT = 0
;
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the suggestions. i did delete the table i created to check if the query works with the references. when i copy and execute your query, I got this error "Can't create table 'portal.user' (errno: 150)."
is it alright to add foreign key when the reference table is not yet created?
I think that your id field in client and facility doesn't have UNSIGNED attribute. Try to remove UNSIGNED from client and facility fields in my variant of your create query and you'll see that it will work.
back ticks are not necessary for this query - see my sqlfiddle proving it.
I don't say that backsticks are necessary but it should be used everywhere in field and table names to avoid some problems with reserved words. Mainly, problem was in wrong type provided by fields in referenced tables.
1

This works for me.
See my successful SQLFiddle

I suspect you have some odd whitespace characters in your input, perhaps tabs.

1 Comment

The query part is empty, but the schema (the table create) builds OK.

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.