4

The first table was created with no problems:

mysql > CREATE TABLE nodes(
     -> id varchar(10) PRIMARY KEY,
     -> user text,
     -> uid varchar(10),
     -> version tinyint,
     -> changeset smallint,
     -> timestamp timestamp
     -> );

It is when I tried to create the second table that MySQL is outputting an error:

mysql > CREATE TABLE node_tags(
     -> id varchar(10),
     -> key text,
     -> value text,
     -> type text,
     -> CONSTRAINT pk_node_tag PRIMARY KEY (id, key),
     -> CONSTRAINT fk_node_tags_id FOREIGN KEY (id)
     ->  REFERENCES nodes (id)
     -> );

ERROR 1064 (42000): 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 ' value text, type text, CONSTRAINT pk_node_tag PRIMARY KEY (id, key), CONSTRAINT' at line 3

3 Answers 3

4

This succeeds. Key is a reserved word, so it needs back-ticks.

Also, a text in an index limits it size as to how much of it can be indexed. It is influenced by your character set in force.

So the following runs thru:

drop table if exists nodes;
CREATE TABLE nodes
(   id varchar(10) PRIMARY KEY,
    user text,
    uid varchar(10),
    version tinyint,
    changeset smallint,
    timestamp timestamp
)engine=innodb;

drop table if exists node_tags;
CREATE TABLE node_tags
(   id varchar(10) not null,
    `key` text,
    value text,
    type text,
    CONSTRAINT pk_node_tag PRIMARY KEY (id, `key`(255)),
    CONSTRAINT fk_node_tags_id FOREIGN KEY (id) REFERENCES nodes (id)
)engine=innodb;

I would highly suggest not having a TEXT in a primary key anyway.

Sign up to request clarification or add additional context in comments.

2 Comments

Great, this went through. Thank you for your help.
Your welcome. I am sorry I can't recommend something about the key as I know little about it. You know best with your data. Remember it is going to be way-wide so if you use it as a foreign key you may run into performance problems (certainly compared to a thin INT)
0

Reserved keywords such as 'key, value, timestamp etc' shouldn't be preferred while creating columns. This induces bugs and isn't scalable. As far as this is concerned, using backticks key will solve the issue here.

Comments

0

because key is the reserved word in MySQL. please refer to this document to see list of the reserved words: https://dev.mysql.com/doc/refman/5.5/en/keywords.html

Comments

Your Answer

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