Question

I am trying to learn MySQL for one simple application. While I am trying to create a table with foreign key, I am getting the following error.

mysql> create table User_Activity (SLNO INT NOT NULL AUTO_INCREMENT, 
DATEOFTASK TIMESTAMP default NOW(), 
TASKNAME VARCHAR(30) NOT NULL, 
TASKACTION VARCHAR(30) NOT NULL, 
BACKUP VARCHAR(5) NOT NULL, 
TASKSTATUS VARCHAR(15) NOT NULL, 
HANDLEDBY VARCHAR(30) NOT NULL SET utf8 DEFAULT NULL ,
 PRIMARY KEY (SLNO), 
FOREIGN KEY (HANDLEDBY) REFERENCES User_Access(NAME));

And error is ( Even with out SET utf8 DEFAULT NULL I am getting error)

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 'SET utf8 DEFAULT NULL , PRIMARY KEY (SLNO), FOREIGN KEY (HANDLEDBY) REFERENCES U' at line 1

Error without SET utf8 DEFAULT NULL :

mysql> create table User_Activity (SLNO INT NOT NULL AUTO_INCREMENT,
DATEOFTASK TIMESTAMP default NOW(), 
TASKNAME VARCHAR(30) NOT NULL,
TASKACTION VARCHAR(30) NOT NULL,
 BACKUP VARCHAR(5) NOT NULL,
TASKSTATUS VARCHAR(15) NOT NULL, 
HANDLEDBY VARCHAR(30), 
PRIMARY KEY (SLNO), FOREIGN KEY (HANDLEDBY) REFERENCES User_Access(NAME));

ERROR 1005 (HY000): Can't create table 'activity.User_Activity' (errno: 150)

The Description of User_Access table is,

mysql> DESC User_Access;
+-------------+-------------+------+-----+---------+----------------+
| Field       | Type        | Null | Key | Default | Extra          |
+-------------+-------------+------+-----+---------+----------------+
| SLNO        | int(11)     | NO   | PRI | NULL    | auto_increment |
| NAME        | varchar(30) | NO   |     | NULL    |                |
| PASSWORD    | varchar(30) | NO   |     | NULL    |                |
| DESIGNATION | varchar(30) | NO   |     | NULL    |                |
+-------------+-------------+------+-----+---------+----------------+
4 rows in set (0.07 sec)

EDIT 1:

mysql> create table User_Activity (SLNO INT NOT NULL AUTO_INCREMENT, DATEOFTASK TIMESTAMP default NOW(), TASKNAME VARCHAR(30) NOT NULL, TASKACTION VARCHAR(30) NOT NULL, BACKUP VARCHAR(5) NOT NULL, TASKSTATUS VARCHAR(15) NOT NULL, HANDLEDBY VARCHAR(30) NOT NULL CHARACTER SET utf8 DEFAULT NULL , PRIMARY KEY (SLNO), FOREIGN KEY (HANDLEDBY) REFERENCES User_Access(NAME));

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 'CHARACTER SET utf8 DEFAULT NULL , PRIMARY KEY (SLNO), FOREIGN KEY (HANDLEDBY) RE' at line 1 mysql>

Yes User_Access table is Inno_DB only , here is the output

mysql> SHOW CREATE TABLE User_Access;
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table       | Create Table                                                                                                                                                                                                                                                  |
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| User_Access | CREATE TABLE `User_Access` (
  `SLNO` int(11) NOT NULL AUTO_INCREMENT,
  `NAME` varchar(30) NOT NULL,
  `PASSWORD` varchar(30) NOT NULL,
  `DESIGNATION` varchar(30) NOT NULL,
  PRIMARY KEY (`SLNO`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 |
+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.05 sec)
Was it helpful?

Solution 3

Finally from a IRC person at #mysql I have found the solution. I am giving it here to help others.

He told me that , what ever I am taking for primary key or it should have index field. so I did like adding index to NAME field in table Use_Activity with

mysql> alter table User_Access add key(NAME);
Query OK, 0 rows affected (0.30 sec)
Records: 0  Duplicates: 0  Warnings: 0

and then the 2nd query executed fine.

mysql> create table User_Activity (SLNO INT NOT NULL AUTO_INCREMENT, DATEOFTASK TIMESTAMP default NOW(), TASKNAME VARCHAR(30) NOT NULL, TASKACTION VARCHAR(30) NOT NULL, BACKUP VARCHAR(5) NOT NULL, TASKSTATUS VARCHAR(15) NOT NULL, HANDLEDBY VARCHAR(30) CHARACTER SET latin1 NOT NULL , PRIMARY KEY (SLNO), FOREIGN KEY (HANDLEDBY) REFERENCES User_Access(NAME));

Query OK, 0 rows affected (0.21 sec)

OTHER TIPS

Foreign keys work for the InnoDB storage engine, not MyISAM.

There needs to be an index (or KEY) defined on the NAME column in User_Access (the column(s) referenced by the foreign key.)

The datatype of the foreign key column must be identical to the datatype of the referenced columns.

The normative foreign key pattern is to reference the PRIMARY KEY in the parent table; but you can also reference any indexed column. (Normally, we'd expect it to be a UNIQUE KEY in the parent table, but MySQL extends that to any index, including non-unique.)

You must use CHARACTER SET instead of just SET.

edit: Another important syntax requirement is that the CHARACTER SET utf8 syntax must come before NOT NULL.

...
HANDLEDBY VARCHAR(30) CHARACTER SET utf8 NOT NULL, /* can't use NULL as default */
...

The foreign key will work if the character set of the column is the same as that of the referenced column User_acces(NAME).

Other requirements must also be satisfied, e.g. the referenced column must be in a key, and the table must be InnoDB, etc. See a complete list of conditions here: https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top