Error on creating FOREIGN KEY - mysql

CREATE TABLE IF NOT EXISTS `servergraph_server` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`ip` VARCHAR(16) NOT NULL,
`port` SMALLINT UNSIGNED NOT NULL
)
CREATE TABLE IF NOT EXISTS `servergraph_data` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`serverid` INT NOT NULL,
`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
`fps` SMALLINT UNSIGNED NOT NULL,
`replay` TINYINT UNSIGNED NOT NULL,
`dropped_packets` INT UNSIGNED NOT NULL,
`online` TINYINT UNSIGNED NOT NULL,
`clients0` TINYINT UNSIGNED NOT NULL,
`clients1` TINYINT UNSIGNED NOT NULL,
`clients2` TINYINT UNSIGNED NOT NULL,
`clients3` TINYINT UNSIGNED NOT NULL,
FOREIGN KEY (`serverid`) REFERENCES `servergraph_server` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
This are the tables I'd like to create.
But I am always getting an error on the FOREIGN KEY line.
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 'FOREIG' at line 1
I am using this version of MySQL: 5.5.35-0+wheezy1
What is wrong?
Thanks, floube
SOLUTION:
ALTER TABLE `servergraph_data` ADD CONSTRAINT `fk_server` FOREIGN KEY (`serverid`) REFERENCES `servergraph_server` (`id`)
instead of the FOREIGN KEY line in CREATE TABLE servergraph_data

Place semi colons after the create table statements:
CREATE TABLE IF NOT EXISTS `servergraph_server` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`ip` VARCHAR(16) NOT NULL,
`port` SMALLINT UNSIGNED NOT NULL
);
CREATE TABLE IF NOT EXISTS `servergraph_data` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`serverid` INT NOT NULL,
`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
`fps` SMALLINT UNSIGNED NOT NULL,
`replay` TINYINT UNSIGNED NOT NULL,
`dropped_packets` INT UNSIGNED NOT NULL,
`online` TINYINT UNSIGNED NOT NULL,
`clients0` TINYINT UNSIGNED NOT NULL,
`clients1` TINYINT UNSIGNED NOT NULL,
`clients2` TINYINT UNSIGNED NOT NULL,
`clients3` TINYINT UNSIGNED NOT NULL,
FOREIGN KEY (`serverid`) REFERENCES `servergraph_server` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
);

Related

Can't Create table in Mysql

I'm creating a database of greatest Movies, and i'm getting error:
ERROR 1005 (HY000) at line 19: Can't create table 'Greatest_Movies.genre' (errno: 150)
I have checked this Post:
Error Code: 1005. Can't create table '...' (errno: 150) and tried lot of things but with no succes.
This is what actually have:
DROP DATABASE `Greatest_Movies`;
CREATE DATABASE `Greatest_Movies`;
USE `Greatest_Movies` ;
CREATE TABLE IF NOT EXISTS `movie` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`film` VARCHAR(255) NOT NULL,
`director` VARCHAR(255) NOT NULL,
`release_year` VARCHAR(255) NOT NULL,
`oscars` TINYINT NULL,
`IMDB_link` VARCHAR(255) NOT NULL,
`film_page` VARCHAR(255) NOT NULL,
`country` VARCHAR(255) NOT NULL,
`genre` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `genre` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`id`) REFERENCES `movie`(`genre`))
ENGINE = InnoDB;
What i'm missing?
Regards
This script work and tested, try it:
DROP DATABASE `Greatest_Movies`;
CREATE DATABASE `Greatest_Movies`;
USE `Greatest_Movies` ;
DROP TABLE IF EXISTS `genre`;
CREATE TABLE `genre` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `movie`;
CREATE TABLE `movie` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`film` varchar(255) NOT NULL,
`director` varchar(255) NOT NULL,
`release_year` varchar(255) NOT NULL,
`oscars` tinyint(4) DEFAULT NULL,
`IMDB_link` varchar(255) NOT NULL,
`film_page` varchar(255) NOT NULL,
`country` varchar(255) NOT NULL,
`genre` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `genre_fk` (`genre`),
CONSTRAINT `genre_fk` FOREIGN KEY (`genre`) REFERENCES `genre` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think you need to remove the comma from the end of this line:
`genre` TINYINT UNSIGNED NOT NULL,
And also after:
`name` VARCHAR(255) NOT NULL,
As #Solarflare said on the comments, I did this changes:
DROP DATABASE `Greatest_Movies`;
CREATE DATABASE `Greatest_Movies`;
USE `Greatest_Movies`;
CREATE TABLE IF NOT EXISTS `genre` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `movie` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`film` VARCHAR(255) NOT NULL,
`director` VARCHAR(255) NOT NULL,
`release_year` VARCHAR(255) NOT NULL,
`oscars` TINYINT NULL,
`IMDB_link` VARCHAR(255) NOT NULL,
`film_page` VARCHAR(255) NOT NULL,
`country` VARCHAR(255) NOT NULL,
`genre` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`genre`) REFERENCES `genre`(`id`))
ENGINE = InnoDB;

MySQL error while trying to "translate" MsSQL to MySQL

I am trying to create two tables on a MySQL database, with the same characteristics of its MsSQL version but i get a syntax error. Any suggestions please?
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT ((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
);
CREATE TABLE locations
(
`lat` DECIMAL (10, 6) DEFAULT ((0)) NOT NULL,
`lon` DECIMAL (10, 6) DEFAULT ((0)) NOT NULL,
`dt` DATETIME NULL,
`id` INT auto_increment NOT NULL,
`owner` INT NULL
);
EDIT: The error i get is
SQL query:
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT ((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
)
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 '((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (' at line 7
Try this.
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT 1 NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE locations
(
`lat` DECIMAL (10, 6) DEFAULT 0 NOT NULL,
`lon` DECIMAL (10, 6) DEFAULT 0 NOT NULL,
`dt` DATETIME DEFAULT NULL,
`id` INT AUTO_INCREMENT NOT NULL,
`owner` INT ,
PRIMARY KEY (`id`)
);
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT 1 NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
)

Cannot add foreign key SQL fiddle

I'm having issues with the foriegn key in my table. If any can point out the error, please do, I have no idea what it is.
CREATE TABLE IF NOT EXISTS `user_account` (
`accountID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL,
`typeID` int(10) UNSIGNED NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`accountID`),
FOREIGN KEY (`typeID`) REFERENCES account_type(`typeID`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `account_type`(
`typeID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeName` VARCHAR (20) NOT NULL,
`description` VARCHAR (255),
PRIMARY KEY (`typeID`),
) ENGINE = InnoDB;
As far as I'm concerned, my datatypes are fine.
Thanks for the help.
Just do them in the opposite order after you fix the TYPO extra comma in the first table below.
CREATE TABLE IF NOT EXISTS `account_type`(
`typeID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`typeName` VARCHAR (20) NOT NULL,
`description` VARCHAR (255),
PRIMARY KEY (`typeID`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `user_account` (
`accountID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(20) NOT NULL,
`typeID` int(10) UNSIGNED NOT NULL,
`email` VARCHAR(100) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`accountID`),
FOREIGN KEY (`typeID`) REFERENCES account_type(`typeID`)
) ENGINE = InnoDB;

Not able to create a Foreign Constraint in Mysql

Here is the script I am trying to run
CREATE TABLE IF NOT EXISTS `store` (
`store_id` INT NOT NULL AUTO_INCREMENT,
`store_name` VARCHAR(1024) NOT NULL,
`store_user` INT NOT NULL,
`store_address` INT NOT NULL,
`store_type` INT NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
PRIMARY KEY (`store_id`)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `store_address` (
`address_id` INT NOT NULL AUTO_INCREMENT,
`address_line_1` VARCHAR(1024) NOT NULL,
`address_line_2` VARCHAR(1024) NOT NULL,
`address_line_3` VARCHAR(1024) NULL,
`city` VARCHAR(45) NOT NULL,
`locality` VARCHAR(100) NOT NULL,
`pincode` CHAR(6) NOT NULL,
`latitude` DECIMAL(8,6) NULL,
`longitude` DECIMAL(9,6) NULL,
`state` VARCHAR(45) NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
PRIMARY KEY (`address_id`),
CONSTRAINT `FK_STR_STR_ADR`
FOREIGN KEY (`address_id`)
REFERENCES `store` (`store_address`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I get this error - Error Code: 1215. Cannot add foreign key constraint
No clue what is wrong with the DDL
Two issues:
1. use unsigned int for the primary keys (Edit: this is a good idea but was not the problem)
2. the store.store address must be a key in the store table in order to use it in a foreign key constraint in the store_address table
CREATE TABLE IF NOT EXISTS `store` (
`store_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`store_name` VARCHAR(1024) NOT NULL,
`store_user` INT NOT NULL,
`store_address` int(10) unsigned NOT NULL DEFAULT 0,
`store_type` INT NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
PRIMARY KEY (`store_id`),
KEY (`store_address`)
) ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `store_address` (
`address_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`address_line_1` VARCHAR(1024) NOT NULL,
`address_line_2` VARCHAR(1024) NOT NULL,
`address_line_3` VARCHAR(1024) NULL,
`city` VARCHAR(45) NOT NULL,
`locality` VARCHAR(100) NOT NULL,
`pincode` CHAR(6) NOT NULL,
`latitude` DECIMAL(8,6) NULL,
`longitude` DECIMAL(9,6) NULL,
`state` VARCHAR(45) NOT NULL,
`created_date` DATETIME NOT NULL,
`updated_date` DATETIME NOT NULL,
PRIMARY KEY (`address_id`),
CONSTRAINT `FK_STR_STR_ADR` FOREIGN KEY (`address_id`) REFERENCES `store` (`store_address`)
)ENGINE = InnoDB;

Errors Adding Foreign Keys (MySQL) (Error Code 1215)

I am trying to add a foreign key between two different sets of tables, the first set is customer and shopping_cart. I tried looking at the other posts regarding this error but I couldn't get it to work after looking at them.
CREATE TABLE `usale`.`customer` (
CREATE TABLE `usale`.`customer` (
`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`)),
INDEX `fk_customer_student1_idx` (`student_student_id` ASC,`student_school_id` ASC),
INDEX `fk_customer_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
CONSTRAINT `fk_customer_student1`
FOREIGN KEY (`student_student_id`)
REFERENCES `usale`.`student` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart` (`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;`customer_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(45) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`email` VARCHAR(50) NULL DEFAULT NULL,
`active` TINYINT(1) NOT NULL DEFAULT TRUE,
`create_date` DATETIME NOT NULL,
`last_update` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
`student_student_id` BIGINT NOT NULL,
`student_school_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
PRIMARY KEY (`customer_id`, `student_student_id`, `student_school_id`))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE customer ADD CONSTRAINT `fk_customer_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I'm getting the error: "ERROR: Cannot add foreign key constraint
Error Code: 1215"
For the second set of tables they are named shopping_cart and shopping_cart_item
CREATE TABLE IF NOT EXISTS `usale`.`shopping_cart_item` (
`shopping_cart_item_id` BIGINT NOT NULL,
`shopping_cart_cart_id` TINYINT UNSIGNED NOT NULL,
`inventory_item_inventory_item_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_student_id` BIGINT NOT NULL,
`inventory_item_inventory_type` VARCHAR(45) NOT NULL,
`inventory_item_student_student_id` BIGINT NOT NULL,
`inventory_item_student_customer_id` BIGINT NOT NULL,
`inventory_item_student_school_id` BIGINT NOT NULL,
`inventory_item_edition_edition_id` BIGINT UNSIGNED NOT NULL,
`inventory_item_edition_author_id` BIGINT NOT NULL,
PRIMARY KEY (`shopping_cart_item_id`, `shopping_cart_cart_id`, `inventory_item_inventory_item_id`, `inventory_item_student_id`, `inventory_item_inventory_type`, `inventory_item_student_student_id`, `inventory_item_student_customer_id`, `inventory_item_student_school_id`, `inventory_item_edition_edition_id`, `inventory_item_edition_author_id`),
INDEX `fk_shopping_cart_items_shopping_cart1_idx` (`shopping_cart_cart_id` ASC),
INDEX `fk_shopping_cart_item_inventory_item1_idx` (`inventory_item_inventory_item_id` ASC, `inventory_item_student_id` ASC, `inventory_item_inventory_type` ASC, `inventory_item_student_student_id` ASC, `inventory_item_student_customer_id` ASC, `inventory_item_student_school_id` ASC, `inventory_item_edition_edition_id` ASC, `inventory_item_edition_author_id` ASC))
CREATE TABLE IF NOT EXISTS `frankapp`.`shopping_cart` (
`cart_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`cart_total` DECIMAL(12,2) NULL,
PRIMARY KEY (`cart_id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
ALTER TABLE shopping_cart_item ADD CONSTRAINT `fk_shopping_cart_items_shopping_cart1`
FOREIGN KEY (`shopping_cart_cart_id`)
REFERENCES `usale`.`shopping_cart`(`cart_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Once again I am getting error 1215: "ERROR: Cannot add foreign key constraint
Error Code: 1215"