mysql create table script fails - error 150 - mysql

I forward engineered a script from a model I created in WorkBench, but the script keeps failing. It fails on the second create table:
CREATE TABLE IF NOT EXISTS `myDatabase`.`UserProfile` (
`ProfileId` INT NOT NULL AUTO_INCREMENT ,
`FirstName` VARCHAR( 45 ) NULL ,
`LastName` VARCHAR( 45 ) NULL ,
`Gender` CHAR( 1 ) NULL ,
`DOB` DATETIME NULL ,
`HairColor` VARCHAR( 20 ) NULL DEFAULT 'No Answer',
`EyeColor` VARCHAR( 20 ) NULL DEFAULT 'No Answer',
`Height` VARCHAR( 10 ) NULL DEFAULT 'No Answer',
`Weight` VARCHAR( 45 ) NULL DEFAULT 'Average',
`UserId` VARCHAR( 45 ) NOT NULL ,
PRIMARY KEY ( `ProfileId` ) ,
CONSTRAINT `FK_User_Profile` FOREIGN KEY ( `UserId` ) REFERENCES `OurAgreement`.`UserAccount` (
`UserId`
) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB;
I believe error 150 is a foreign key issue. The table associated with this FK is create first in the script, so it exists before this constraint is attempted. Here is the DDL for that table:
CREATE TABLE IF NOT EXISTS `mydatabase`.`UserAccount` (
`UserId` INT NOT NULL AUTO_INCREMENT ,
`Login` VARCHAR(20) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
`Password` CHAR(64) NOT NULL ,
`Email` VARCHAR(45) NULL ,
PRIMARY KEY (`UserId`))
ENGINE = InnoDB;
Any ideas what is going on?
EDIT============================
table with 2 FKs:
CREATE TABLE IF NOT EXISTS `MyDatabase`.`Answer` (
`AnswerId` INT NOT NULL ,
`QuestionId` INT NOT NULL ,
`ProfileId` INT NOT NULL ,
PRIMARY KEY ( `AnswerId` ) ,
INDEX `ProfileId_idx` ( `ProfileId` ASC ) ,
INDEX `QuestionId_idx` ( `QuestionId` ASC ) ,
CONSTRAINT `FK_Question_Answer` FOREIGN KEY ( `QuestionId` ) REFERENCES `MyDatabase`.`Question` (
`QuestionId`
) ON DELETE NO ACTION ON UPDATE CASCADE ,
CONSTRAINT `FK_Answer_Profile` FOREIGN KEY ( `ProfileId` ) REFERENCES `MyDatabase`.`UserProfile` (
`ProfileId`
) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE = INNODB;

If
`UserId` INT NOT NULL AUTO_INCREMENT
is in your original table, you need exact same syntax in derived table:
`UserId` VARCHAR( 45 ) NOT NULL
should be
`UserId` INT NOT NULL
Also, you need to index the field UserId in both tables. Since it is PRIMARY in one, so no need there. In another add:
INDEX `uid`( `UserId` )

In
CREATE TABLE IF NOT EXISTS `myDatabase`.`UserProfile`
you reference table in database OurAgreement:
REFERENCES `OurAgreement`.`UserAccount`
But the table UserAccount you create earlier is in mydatabase:
CREATE TABLE IF NOT EXISTS `mydatabase`.`UserAccount`

Related

Mysql error 150 with foreigns keys

I've got this
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
`subject` VARCHAR(200) NOT NULL ,
`body` TEXT NOT NULL ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id_msg`) ,
INDEX fk_msg_messages_user (`id_user_from` ASC) ,
INDEX fk_msg_messages_msg_messages_users (`id_msg` ASC) ,
CONSTRAINT `fk_msg_messages_user`
FOREIGN KEY (`id_user_from` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_msg_messages_msg_messages_users`
FOREIGN KEY (`id_msg` )
REFERENCES `beta`.`msg_messages_users` (`id_msg` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
*And this is my error. These are my user and msg_messages_users tables too. I've reviewed all my tables and i cant see my error. Im working on MysqlWorbench and it generate this erroneous sintax.*
Error 1005: Can't create table 'beta.msg_messages' (errno: 150).
CREATE TABLE IF NOT EXISTS `beta`.`user` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`id`) ,
ENGINE = InnoDB
AUTO_INCREMENT = 87
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
`id` BIGINT UNSIGNED NOT NULL ,
`id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
`id_msg` BIGINT(20) UNSIGNED NOT NULL ,
`status` TINYINT NOT NULL DEFAULT 0 ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
CONSTRAINT `fk_msg_messages_users_user`
FOREIGN KEY (`id_usr_to` )
REFERENCES `beta`.`user` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Im a dumb... i was declaring a foreign key in the wrong table..
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT ,
`id_user_from` BIGINT(20) UNSIGNED NOT NULL ,
`subject` VARCHAR(200) NOT NULL ,
`body` TEXT NOT NULL ,
`date` DATETIME NOT NULL ,
PRIMARY KEY (`id_msg`) ,
INDEX fk_msg_messages_user (`id_user_from` ASC) ,
CONSTRAINT `fk_msg_messages_user`
FOREIGN KEY (`id_user_from` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `beta`.`msg_messages_users` (
`id_msg` BIGINT(20) UNSIGNED NOT NULL ,
`id_usr_to` BIGINT(20) UNSIGNED NOT NULL ,
`status` TINYINT NOT NULL DEFAULT 0 ,
`date` DATETIME NOT NULL ,
INDEX id_msg (`id_msg` ASC) ,
INDEX fk_msg_messages_users_msg_messages (`id_msg` ASC) ,
INDEX fk_msg_messages_users_user (`id_usr_to` ASC) ,
CONSTRAINT `fk_msg_messages_users_msg_messages`
FOREIGN KEY (`id_msg` )
REFERENCES `beta`.`msg_messages` (`id_msg` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_msg_messages_users_user`
FOREIGN KEY (`id_usr_to` )
REFERENCES `beta`.`user` (`id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
thanks all

Mysql Forward engineer errno 150

I'm modeling my db shema using MySQL Workbench CE EER modeler and now I'm stuck with mysql errno 150.
My sql code:
CREATE TABLE `myschema`.`Clients` (
`phone` VARCHAR(15) NOT NULL ,
`surname` VARCHAR(30) NOT NULL ,
`name` VARCHAR(30) NOT NULL ,
`middleName` VARCHAR(30) NULL ,
`discountCardNumber` BIGINT NULL ,
PRIMARY KEY (`phone`) ,
UNIQUE INDEX `discountCardNumber_UNIQUE` (`discountCardNumber` ASC) ,
CONSTRAINT `fk_Clients_DiscountCards1`
FOREIGN KEY (`discountCardNumber` )
REFERENCES `myschema`.`DiscountCards` (`cardNumber` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE `myschema`.`OrderStatuses` (
`statusID` INT NOT NULL AUTO_INCREMENT ,
`statusTitle` VARCHAR(45) NOT NULL ,
`statusDescription` VARCHAR(150) NULL ,
PRIMARY KEY (`statusID`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `myschema`.`WorkstationUsers` (
`userID` INT NOT NULL AUTO_INCREMENT ,
`login` VARCHAR(45) NOT NULL ,
`pass` VARCHAR(45) NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
`surname` VARCHAR(45) NOT NULL ,
`middleName` VARCHAR(45) NULL ,
PRIMARY KEY (`userID`) )
ENGINE = InnoDB;
CREATE TABLE `myschema`.`Orders` (
`orderID` BIGINT NOT NULL AUTO_INCREMENT ,
`registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`clientPhone` VARCHAR(15) NOT NULL ,
`shipmentAddress` VARCHAR(150) NOT NULL ,
`orderStatus` INT NOT NULL ,
`registrator` INT NOT NULL ,
PRIMARY KEY (`orderID`) ,
INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
CONSTRAINT `fk_Orders_Clients`
FOREIGN KEY (`clientPhone` )
REFERENCES `myschema`.`Clients` (`phone` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_OrderStatuses1`
FOREIGN KEY (`orderStatus` )
REFERENCES `myschema`.`OrderStatuses` (`statusID` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_WorkstationUsers1`
FOREIGN KEY (`registrator` )
REFERENCES `myschema`.`WorkstationUsers` (`userID` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
It fails in last statement (CREATE TABLE Orders).
I already checked:
Types of refferencing tables are equals
Indexes on all columns exists
Engine is InnoDB
Thanks for any help! Have a good day!
P.S. sorry for possible duplicate. I really can't find any problem in my code.
Some of fields are defined as NOT NULL, but you defined 'ON DELETE' action as 'SET NULL'.
Make these fields nullabe -
CREATE TABLE `myschema`.`Orders` (
`orderID` BIGINT NOT NULL AUTO_INCREMENT ,
`registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`clientPhone` VARCHAR(15) NULL, -- NOT NULL ,
`shipmentAddress` VARCHAR(150) NOT NULL ,
`orderStatus` INT NULL, -- NOT NULL ,
`registrator` INT NULL, -- NOT NULL ,
PRIMARY KEY (`orderID`) ,
INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
CONSTRAINT `fk_Orders_Clients`
FOREIGN KEY (`clientPhone` )
REFERENCES `myschema`.`Clients` (`phone` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_OrderStatuses1`
FOREIGN KEY (`orderStatus` )
REFERENCES `myschema`.`OrderStatuses` (`statusID` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_WorkstationUsers1`
FOREIGN KEY (`registrator` )
REFERENCES `myschema`.`WorkstationUsers` (`userID` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
In my case, the problem was solved by adding
DEFAULT CHARACTER SET = utf8
at the end of each create table
regards!

MySQL : able to create a table through MySQL command line, but not able to create via XAMPP

I ran the following query in MySQL command line and the tables were created. But when I ran in XAMPP, I got an error "1005 - Can't create table 'zoneboard.work' (errno: 150)"
My query is :
Create Table user (
id_user INT (50)NOT NULL AUTO_INCREMENT ,
email VARCHAR( 64 ) NOT NULL ,
username VARCHAR( 16 ) NOT NULL ,
password VARCHAR( 32 ) NOT NULL ,
PRIMARY KEY ( id_user )
) ENGINE = InnoDB
CREATE TABLE work (
id_user INT( 50 ) NOT NULL AUTO_INCREMENT ,
task VARCHAR( 50 ) NOT NULL ,
comments VARCHAR( 100 ) NOT NULL ,
assignee VARCHAR( 16 ) NOT NULL ,
priority VARCHAR( 50 ) NOT NULL ,
status VARCHAR( 50 ) NOT NULL ,
dataum1 VARCHAR( 50 ) NOT NULL ,
dataum2 VARCHAR( 50 ) NOT NULL ,
PRIMARY KEY ( id_user ),
FOREIGN KEY(assignee) REFERENCES user(username)
) ENGINE = InnoDB
According to MySQL Documentation : If you are creating a table, it must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.
Therefore, try adding an index on your foreign key field username as the following:
CREATE INDEX username_index ON 'user'(username)
You should refer to the primary key of the user table. Integer is faster and it is more secure to use the primary key instead of a none unique value. Imagine you have 2 or more rows with the same username.
CREATE TABLE `work` (
`id_user` int(50) NOT NULL AUTO_INCREMENT,
`task` varchar(50) NOT NULL,
`comments` varchar(100) NOT NULL,
`assignee` int(50) NOT NULL,
`priority` varchar(50) NOT NULL,
`status` varchar(50) NOT NULL,
`dataum1` varchar(50) NOT NULL,
`dataum2` varchar(50) NOT NULL,
PRIMARY KEY (`id_user`),
KEY `assignee` (`assignee`),
CONSTRAINT `assignee` FOREIGN KEY (`assignee`) REFERENCES `user` (`id_user`)
) ENGINE=InnoDB;
CREATE TABLE USER (
id_user INT (50)NOT NULL AUTO_INCREMENT ,
email VARCHAR( 64 ) NOT NULL ,
username VARCHAR( 16 ) NOT NULL ,
PASSWORD VARCHAR( 32 ) NOT NULL ,
PRIMARY KEY ( id_user, username ),
INDEX(username)
) ENGINE = INNODB;
Parent table-user should have an index column(Here username should be indexed).
Refer: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

MySQL code schema generating code that does nothing?

I'm using MySQL to design a database, I'm exporting the code for generating the database by using the 'SQL Create Script' option on the workbench. However, when I run the code, and use SHOW TABLES I get null, which probably means no table was created in the database. This is the code that was generated (its long but since it was auto generated the error is probably from a setting I had when exporting it.)
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL';
CREATE SCHEMA IF NOT EXISTS `travel_agency` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `travel_agency` ;
-- -----------------------------------------------------
-- Table `travel_agency`.`Region`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Region` (
`region_name` VARCHAR(45) NOT NULL ,
`languages` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`region_name`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Country`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Country` (
`country_name` VARCHAR(15) NOT NULL ,
`Region_region_name` VARCHAR(45) NOT NULL ,
`currency` CHAR(20) NOT NULL ,
PRIMARY KEY (`country_name`, `Region_region_name`) ,
INDEX `fk_Country_Region1` (`Region_region_name` ASC) ,
CONSTRAINT `fk_Country_Region1`
FOREIGN KEY (`Region_region_name` )
REFERENCES `travel_agency`.`Region` (`region_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Transport`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Transport` (
`Country_country_name` VARCHAR(15) NOT NULL ,
`cost_estimate` DECIMAL(8,2) NOT NULL ,
`transport_deals` BLOB NOT NULL ,
`transport_rating` CHAR(1) NOT NULL ,
PRIMARY KEY (`Country_country_name`) ,
CONSTRAINT `fk_Transport_Country`
FOREIGN KEY (`Country_country_name` )
REFERENCES `travel_agency`.`Country` (`country_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Resort`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Resort` (
`resort_name` VARCHAR(50) NOT NULL ,
`city` VARCHAR(50) NOT NULL ,
`Country_country_name` VARCHAR(15) NOT NULL ,
`resort_type` CHAR(20) NOT NULL ,
INDEX `fk_Resort_Country1` (`Country_country_name` ASC) ,
PRIMARY KEY (`resort_name`, `city`) ,
CONSTRAINT `fk_Resort_Country1`
FOREIGN KEY (`Country_country_name` )
REFERENCES `travel_agency`.`Country` (`country_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Hotel`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Hotel` (
`hotel_name` CHAR NOT NULL ,
`rating` CHAR(1) NOT NULL ,
`address1` CHAR(50) NOT NULL ,
`address2` CHAR(50) NOT NULL ,
`postcode` CHAR(10) NOT NULL ,
`telephone` CHAR(20) NOT NULL ,
`Resort_resort_name` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`hotel_name`) ,
INDEX `fk_Hotel_Resort1` (`Resort_resort_name` ASC) ,
CONSTRAINT `fk_Hotel_Resort1`
FOREIGN KEY (`Resort_resort_name` )
REFERENCES `travel_agency`.`Resort` (`resort_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Room`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Room` (
`room_num` INT NOT NULL ,
`room_type` CHAR(10) NOT NULL ,
`Hotel_hotel_name` CHAR NOT NULL ,
`minibar` BIT NOT NULL ,
`tv` BIT NOT NULL ,
`smoking_permitted` BIT NOT NULL ,
INDEX `fk_Room_Hotel1` (`Hotel_hotel_name` ASC) ,
PRIMARY KEY (`room_num`, `room_type`) ,
CONSTRAINT `fk_Room_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Guest`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Guest` (
`guest_id` INT NOT NULL ,
`firstname` VARCHAR(20) NOT NULL ,
`surname` VARCHAR(20) NOT NULL ,
`mobilephone` CHAR(20) NOT NULL ,
PRIMARY KEY (`guest_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Bookings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Bookings` (
`Guest_guest_id` INT NOT NULL ,
`Hotel_hotel_name` CHAR NOT NULL ,
`guest_count` INT NOT NULL ,
PRIMARY KEY (`Guest_guest_id`, `Hotel_hotel_name`) ,
INDEX `fk_Booking_Guest1` (`Guest_guest_id` ASC) ,
INDEX `fk_Booking_Hotel1` (`Hotel_hotel_name` ASC) ,
CONSTRAINT `fk_Booking_Guest1`
FOREIGN KEY (`Guest_guest_id` )
REFERENCES `travel_agency`.`Guest` (`guest_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Booking_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`HotelFacilities`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`HotelFacilities` (
`Hotel_hotel_name` CHAR NOT NULL ,
`internet_access` BIT NOT NULL ,
`hotel_restaurant` BIT NOT NULL ,
`games_room` BIT NOT NULL ,
`bar` BIT NOT NULL ,
`evening_shows` BIT NOT NULL ,
`massage_parlour` BIT NOT NULL ,
`misc_details` BLOB NOT NULL ,
PRIMARY KEY (`Hotel_hotel_name`) ,
CONSTRAINT `fk_HotelFacilities_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Attractions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Attractions` (
`Region_region_name` VARCHAR(45) NOT NULL ,
`dirt_mountains` BIT NOT NULL ,
`beaches` BIT NOT NULL ,
`casinos` BIT NOT NULL ,
`safari` BIT NOT NULL ,
`snow_mountains` BIT NOT NULL ,
`misc_details` BLOB NOT NULL ,
PRIMARY KEY (`Region_region_name`) ,
CONSTRAINT `fk_Attractions_Region1`
FOREIGN KEY (`Region_region_name` )
REFERENCES `travel_agency`.`Region` (`region_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Rate`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Rate` (
`Hotel_hotel_name` CHAR NOT NULL ,
`Room_room_num` INT NOT NULL ,
`Room_room_type` CHAR(10) NOT NULL ,
`first_quarter` DECIMAL(20,2) NOT NULL ,
`second_quarter` DECIMAL(20,2) NOT NULL ,
`third_quarter` DECIMAL(20,2) NOT NULL ,
`fourth_quarter` DECIMAL(20,2) NOT NULL ,
`discount_information` BLOB NOT NULL ,
PRIMARY KEY (`Hotel_hotel_name`, `Room_room_num`, `Room_room_type`) ,
INDEX `fk_Rate_Room1` (`Room_room_num` ASC, `Room_room_type` ASC) ,
CONSTRAINT `fk_Rate_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Rate_Room1`
FOREIGN KEY (`Room_room_num` , `Room_room_type` )
REFERENCES `travel_agency`.`Room` (`room_num` , `room_type` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
I'm using sql command line client and putting the code but it doesn't work.
Its because you're using command line client as your database. That just makes your pc function as a database but doesn't exactly have all features a professional database would have. And MySQL workbench is designed to export code for databases more professional.
Since you're using command line client I'm assuming this is either practice or homework of some sort? Either way, this is the way the code would work in Command line client. Try this.
CREATE TABLE Region
(
region_name VARCHAR(45) NOT NULL ,
languages VARCHAR(100) NOT NULL ,
PRIMARY KEY (region_name)
);
CREATE TABLE Country
(
country_name VARCHAR(15) NOT NULL ,
Region_region_name VARCHAR(45) NOT NULL ,
currency CHAR(20) NOT NULL ,
PRIMARY KEY (country_name, Region_region_name) ,
CONSTRAINT fk_Country_Region1
FOREIGN KEY (Region_region_name )
REFERENCES Region (region_name )
);
CREATE TABLE Transport (
Country_country_name VARCHAR(15) NOT NULL ,
cost_estimate DECIMAL(8,2) NOT NULL ,
transport_deals CHAR(100) NULL ,
transport_rating CHAR(20) NOT NULL ,
PRIMARY KEY (Country_country_name) ,
CONSTRAINT fk_Transport_Country
FOREIGN KEY (Country_country_name)
REFERENCES Country (country_name )
);
CREATE TABLE Resort (
resort_name VARCHAR(50) NOT NULL ,
Country_country_name VARCHAR(15) NOT NULL ,
resort_type CHAR(20) NOT NULL ,
PRIMARY KEY (resort_name) ,
CONSTRAINT fk_Resort_Country1
FOREIGN KEY (Country_country_name)
REFERENCES Country (country_name)
);
CREATE TABLE Hotel (
hotel_name CHAR(20) NOT NULL ,
rating CHAR NOT NULL ,
address1 CHAR(50) NOT NULL ,
address2 CHAR(50) NOT NULL ,
postcode CHAR(10) NOT NULL ,
telephone CHAR(20) NOT NULL ,
Resort_resort_name VARCHAR(50) NOT NULL ,
PRIMARY KEY (hotel_name) ,
CONSTRAINT fk_Hotel_Resort1
FOREIGN KEY (Resort_resort_name)
REFERENCES Resort (resort_name)
);
CREATE TABLE Room (
room_num INT NOT NULL ,
room_type CHAR(10) NOT NULL ,
Hotel_hotel_name CHAR(20) NOT NULL ,
minibar BOOLEAN NOT NULL ,
tv BOOLEAN NOT NULL ,
smoking_permitted BOOLEAN NOT NULL ,
PRIMARY KEY (room_num) ,
CONSTRAINT fk_Room_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE Guest (
guest_id INT NOT NULL ,
firstname VARCHAR(20) NOT NULL ,
surname VARCHAR(20) NOT NULL ,
mobilephone CHAR(20) NOT NULL ,
PRIMARY KEY (guest_id)
);
CREATE TABLE Bookings (
Guest_guest_id INT NOT NULL ,
Hotel_hotel_name CHAR(20) NOT NULL ,
guest_count INT NOT NULL ,
PRIMARY KEY (Guest_guest_id, Hotel_hotel_name) ,
CONSTRAINT fk_Booking_Guest1
FOREIGN KEY (Guest_guest_id)
REFERENCES Guest (guest_id),
CONSTRAINT fk_Booking_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE HotelFacilities (
Hotel_hotel_name CHAR(20) NOT NULL ,
internet_access BOOLEAN NOT NULL ,
hotel_restaurant BOOLEAN NOT NULL ,
games_room BOOLEAN NOT NULL ,
bar BOOLEAN NOT NULL ,
evening_shows BOOLEAN NOT NULL ,
massage_parlour BOOLEAN NOT NULL ,
misc_details CHAR(100) NULL ,
PRIMARY KEY (Hotel_hotel_name) ,
CONSTRAINT fk_HotelFacilities_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE Attractions (
Region_region_name VARCHAR(45) NOT NULL ,
dirt_mountains BOOLEAN NOT NULL ,
beaches BOOLEAN NOT NULL ,
casinos BOOLEAN NOT NULL ,
safari BOOLEAN NOT NULL ,
snow_mountains BOOLEAN NOT NULL ,
misc_details CHAR(100) NULL ,
PRIMARY KEY (Region_region_name) ,
CONSTRAINT fk_Attractions_Region1
FOREIGN KEY (Region_region_name )
REFERENCES Region (region_name )
);
CREATE TABLE Rate (
Hotel_hotel_name CHAR(20) NOT NULL ,
Room_room_num INT NOT NULL ,
first_quarter DECIMAL(10,2) NOT NULL ,
second_quarter DECIMAL(10,2) NOT NULL ,
third_quarter DECIMAL(10,2) NOT NULL ,
fourth_quarter DECIMAL(10,2) NOT NULL ,
discount_information CHAR(100) NULL ,
PRIMARY KEY (Hotel_hotel_name, Room_room_num) ,
CONSTRAINT fk_Rate_Hotel1
FOREIGN KEY (Hotel_hotel_name )
REFERENCES Hotel (hotel_name ),
CONSTRAINT fk_Rate_Room1
FOREIGN KEY (Room_room_num)
REFERENCES Room (room_num)
);

sql error errno: 121

CREATE TABLE `users` (
`UID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NOT NULL ,
`password` VARCHAR(100) NULL ,
`name` VARCHAR(100) NULL ,
`gender` BIT NULL ,
`email` VARCHAR(255) NULL ,
`phone` VARCHAR(30) NOT NULL ,
`verified` BIT NOT NULL DEFAULT 0 ,
`time_zone` INT NULL ,
`time_register` DATETIME NULL ,
`time_active` DATETIME NULL ,
PRIMARY KEY (`UID`) ,
UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
INDEX `verified_INDEX` (`verified` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `companies`
-- -----------------------------------------------------
CREATE TABLE `companies` (
`CID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`address` VARCHAR(100) NULL ,
`email` VARCHAR(255) NULL ,
`phone` VARCHAR(30) NULL ,
`link` TEXT NULL ,
`image_small` TEXT NULL ,
`image_large` TEXT NULL ,
`yahoo` VARCHAR(100) NULL ,
`linkin` VARCHAR(100) NULL ,
`twitter` VARCHAR(20) NULL ,
`description` TEXT NULL ,
`shoutout` VARCHAR(140) NULL ,
`verified` BIT NOT NULL DEFAULT 0 ,
PRIMARY KEY (`CID`) ,
INDEX `name_INDEX` (`name` ASC) ,
INDEX `verified_INDEX` (`verified` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `products`
-- -----------------------------------------------------
CREATE TABLE `products` (
`PID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`CID` INT UNSIGNED NOT NULL ,
`name` VARCHAR(100) NULL ,
`description` TEXT NULL ,
`image_small` TEXT NULL ,
`image_large` TEXT NULL ,
`tag` VARCHAR(45) NULL ,
`price` DECIMAL(11,2) NULL ,
PRIMARY KEY (`PID`) ,
INDEX `tag_INDEX` (`tag` ASC) ,
INDEX `company.cid_FK` (`CID` ASC) ,
CONSTRAINT `company.cid_FK`
FOREIGN KEY (`CID` )
REFERENCES `companies` (`CID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
-- -----------------------------------------------------
-- Table `users_companies`
-- -----------------------------------------------------
CREATE TABLE `users_companies` (
`UID` INT UNSIGNED NOT NULL ,
`CID` INT UNSIGNED NOT NULL ,
`role` INT UNSIGNED NULL ,
PRIMARY KEY (`CID`, `UID`) ,
INDEX `users.uid_FK` (`UID` ASC) ,
INDEX `company.cid_FK` (`CID` ASC) ,
CONSTRAINT `users.uid_FK`
FOREIGN KEY (`UID` )
REFERENCES `users` (`UID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `company.cid_FK`
FOREIGN KEY (`CID` )
REFERENCES `companies` (`CID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
i got error
#1005 - Can't create table 'wew.users_companies' (errno: 121) (Details...)
can anyone tell me which one is the problem ?
You have two constraints called company.cid_FK. Rename one.
For errors like this, you can find more information with 'perror'. i.e.
shell $ perror 121
MySQL error code 121: Duplicate key on write or update
Win32 error code 121: The semaphore timeout period has expired.
I got the same error MySQL error code 121: Duplicate key on write or update.
although I deleted the table from my database to recreated again but I could not so I recreated the table yet with different constrain.
I found that the old constrain is still there.