CREATE TABLE items (
    id serial primary key,
    author_id int references users,
    body text,
    created timestamp default (current_timestamp at time zone 'utc')
);

Why?

link|improve this question

60% accept rate
1  
Probably better off on Stackoverflow. – Chris Sep 1 '10 at 17:39
1  
Simple - Wrong syntax for MySQL. – John Gardeniers Sep 1 '10 at 20:14
feedback

2 Answers

up vote 1 down vote accepted

MySQL uses a slightly different syntax, so a straight copy / paste will not work, here's an example of what the create statement will look like for MySQL:

CREATE TABLE  `test`.`item` (
  `id` int(4) NOT NULL auto_increment,
  `author_id` int(11) default NULL,
  `body` text,
  `created` datetime default NULL,
  PRIMARY KEY  (`id`),
CONSTRAINT `FK_author` FOREIGN KEY (`author_id`) REFERENCES `users` (`author_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Hope this helps.

--schiz

link|improve this answer
I got a #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 'CONSTRAINT FK_author FOREIGN KEY (author_id) REFERENCES users (author_id' at line 7 – Delirium tremens Sep 1 '10 at 17:29
whoops, you're correct, I was typing it directly with no access to mysql, updated it with the correctly formatted CREATE now. – schizix Sep 1 '10 at 17:41
No. You don't need to quote column names in MySQL. You optionally can, but that's not a requirement. You also don't need to (though you can) include the database name. Check the docs: dev.mysql.com/doc/refman/5.1/en/create-table.html – Bill Weiss Sep 1 '10 at 18:14
feedback

Because you can't really copypasta SQL between two different engines and expect it to work. ISTR that MySQL has no idea what a Serial field is.

The syntax between database engines is vastly different.

link|improve this answer
change copypasta to copy/paste or something I believe. :-) – Chris Sep 1 '10 at 17:39
Someone must be hungry. :-) "copypasta to mouth, IMMEDIATE". :-) – Brian Knoblauch Sep 1 '10 at 17:54
I'm always hungry.. – Tom O'Connor Sep 2 '10 at 12:48
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.