Right syntax for creating a foreign key - mysql

I use MySql 5.5.8 and I can't find out how should I write which field need to be a foreign key refererece. For example:
CREATE TABLE IF NOT EXISTS `answers` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT ,
`answer` text COLLATE utf8_polish_ci NOT NULL ,
`user` tinyint( 4 ) DEFAULT NULL ,
`created` timestamp NULL DEFAULT CURRENT_TIMESTAMP ,
`html_template_id` int( 11 ) NOT NULL ,
PRIMARY KEY ( `id` ) ,
CONSTRAINT html_template_id FOREIGN KEY `html_template_id` REFERENCES `html_templates` ( `id` )
) ENGINE = InnoDB DEFAULT CHARSET = utf8 COLLATE = utf8_polish_ci AUTO_INCREMENT =1;
And I get an error message:
#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 'REFERENCES `html_templates`(`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=' at line 10

use
CONSTRAINT html_template_id FOREIGN KEY (`html_template_id`) REFERENCES `html_templates` ( `id` )
i.e. the foreign key field should be in brackets

Related

MySQL database import error #1064

I have a SQL database that I want to import using phplyadmin but I am getting this error.
CREATE TABLE `wp_commentmeta` (
`meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` VARCHAR( 255 ) DEFAULT NULL ,
`meta_value` LONGTEXT,
PRIMARY KEY ( `meta_id` ) ,
KEY `comment_id` ( `comment_id` ) ,
KEY `meta_key` ( `meta_key` ( 191 ) )
) ENGINE = Aria AUTO_INCREMENT =3843 DEFAULT CHARSET = utf8
PAGE_CHECKSUM =1 DELAY_KEY_WRITE =1 TRANSACTIONAL =1;
MySQL said: Documentation
#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 'PAGE_CHECKSUM=1 DELAY_KEY_WRITE=1 TRANSACTIONAL=1' at line 9
If you are not using MariaDB, change ENGINE=Aria to ENGINE=MyISAM(or ENGINE=InnoDB) and remove PAGE_CHECKSUM=1 and TRANSACTIONAL =1;
Find an example here

mysql syntax error: 'USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 AUTOINCREMENT='

Im getting this error:
CREATE TABLE `pdc5l_usergroups` (
`id` int( 10 ) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Clave primaria',
`parent_id` int( 10 ) unsigned NOT NULL DEFAULT '0' COMMENT 'ID Lista de referencia adyacente',
`lft` int( 11 ) NOT NULL DEFAULT '0' COMMENT 'Anidadas conjunto lft.',
`rgt` int( 11 ) NOT NULL DEFAULT '0' COMMENT 'Anidadas conjunto rgt.',
`title` varchar( 100 ) NOT NULL DEFAULT '',
PRIMARY KEY ( `id` ) ,
UNIQUE KEY `idx_usergroup_parent_title_lookup` ( `parent_id` , `title` ) ,
KEY `idx_usergroup_title_lookup` ( `title` ) ,
KEY `idx_usergroup_adjacency_lookup` ( `parent_id` ) ,
KEY `idx_usergroup_nested_set_lookup` ( `lft` , `rgt` ) USING BTREE
) ENGINE = MYISAM AUTO_INCREMENT =9 DEFAULT CHARSET = utf8AUTOINCREMENT =9;
MySQL said: Documentation
#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 'USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 AUTOINCREMENT=' at line 11
I have tried these:
) ENGINE = MYISAM AUTO_INCREMENT =9 DEFAULT CHARSET = utf8 AUTO_INCREMENT =9;
) ENGINE = MYISAM AUTOINCREMENT =9 DEFAULT CHARSET = utf8 AUTOINCREMENT =9;
) ENGINE = MYISAM AUTOINCREMENT =9 DEFAULT CHARSET = utf8 AUTO_INCREMENT =9;
but I still get the error.
phpmyadmin says this: MySQL client version: 4.1.22
This is a mysql version problem. You can see the issue in that bug:
http://bugs.mysql.com/bug.php?id=25162
Before MySQL 5.0.60, this option can be given only before the ON
tbl_name clause. Use of the option in this position is deprecated as
of 5.0.60 and support for it there will be removed in a future MySQL
release. If an index_type option is given in both the earlier and
later positions, the final option applies.
TYPE type_name is recognized as a synonym for USING type_name.
However, USING is the preferred form.
For more details see here: http://www.dbforums.com/mysql/1617755-using-btree.html

Error 1064 - MySQL Database Creation

So I'm a complete novice with MySQL and I'm trying to follow a tutorial that will allow me to sort out an Ajax + JQuery page for my database.
However, running this code in PHPMyAdmin:
CREATE TABLE IF NOT EXISTS 'add_delete_record' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'content' text NOT NULL,
PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
produces the error:
SQL query:
CREATE TABLE IF NOT EXISTS 'add_delete_record'(
'id'INT( 11 ) NOT NULL AUTO_INCREMENT , 'content'TEXT NOT NULL ,
PRIMARY KEY ( 'id' ) ) ENGINE = INNODB DEFAULT CHARSET = latin1
AUTO_INCREMENT =1 MySQL said:
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 ''add_delete_record' ( 'id' int(11) NOT NULL AUTO_INCREMENT,
'content' text' at line 1
Being a novice I have absolutely no clue what is wrong, apart from the fact that there is a problem with the syntax somewhere? Thanks to anyone who can help!
Use backticks instead. Single quotes represent string literals.
CREATE TABLE IF NOT EXISTS `add_delete_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
use backticks
CREATE TABLE IF NOT EXISTS `add_delete_record` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Create table throws an error

Hi I am trying to create a table using SQL in Mysql but I keep getting an error.Here is my code:
USE e-commerce
CREATE TABLE `categories` (
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR( 30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
When I try to run this in phpmyadmin SQL console I get this 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 'CREATE TABLE `categories` ( `id` SMALLINT NOT NULL AUTO_INCREMENT, `category` ' at line 2
What am I doing wrong?
Add semicolon after use
USE e-commerce;
CREATE TABLE `categories` (
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR( 30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Missing semi-colon
USE e-commerce;
CREATE TABLE `categories` (
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR( 30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
In your code you have 2 queries. You should always end query with a semicolon.
So try the following
USE e-commerce;
CREATE TABLE `categories` (
`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR( 30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

MYSQL Error #1064 Error in SQL Syntax

I keep getting this error:
MySQL said: Documentation
#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 'USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=13' at line 11
with this with this query:
SQL query:
CREATE TABLE IF NOT EXISTS `jml_usergroups` (
`id` int( 10 ) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
`parent_id` int( 10 ) unsigned NOT NULL DEFAULT '0' COMMENT 'Adjacency List Reference Id',
`lft` int( 11 ) NOT NULL DEFAULT '0' COMMENT 'Nested set lft.',
`rgt` int( 11 ) NOT NULL DEFAULT '0' COMMENT 'Nested set rgt.',
`title` varchar( 100 ) NOT NULL DEFAULT '',
PRIMARY KEY ( `id` ) ,
UNIQUE KEY `idx_usergroup_parent_title_lookup` ( `parent_id` , `title` ) ,
KEY `idx_usergroup_title_lookup` ( `title` ) ,
KEY `idx_usergroup_adjacency_lookup` ( `parent_id` ) ,
KEY `idx_usergroup_nested_set_lookup` ( `lft` , `rgt` ) USING BTREE
) ENGINE = MYISAM DEFAULT CHARSET = utf8 AUTO_INCREMENT =13;
Any idea what the problem is? these errors are like the thorns on a rose
The syntax is ok, your problem is probably that you're trying to run it on a MySQL version earlier than 5.1, which does not have USING BTREE.