Mysql Forward engineer errno 150 - mysql

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!

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

error 1215 MYSQL Cannot add foreign key constraint,

I have this code:
CREATE TABLE IF NOT EXISTS `biblioteca`.`ejemplar` (
`idejemplar` INT(11) NOT NULL AUTO_INCREMENT ,
`estado` VARCHAR(45) NOT NULL ,
`comentario` VARCHAR(45) NULL ,
`isbn` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idejemplar`) ,
INDEX `fk_ejemplar_libro1_idx` (`isbn` ASC) ,
CONSTRAINT `fk_ejemplar_libro1`
FOREIGN KEY (`isbn` )
REFERENCES `biblioteca`.`libro` (`isbn` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
and the table that's makes reference is:
CREATE TABLE IF NOT EXISTS `biblioteca`.`libro` (
`isbn` VARCHAR(25) NOT NULL ,
`idcategoria` INT(11) NOT NULL ,
`ideditorial` INT(11) NOT NULL ,
`titulo` VARCHAR(45) NOT NULL ,
`autor` VARCHAR(45) NOT NULL ,
`reseña` VARCHAR(45) NULL ,
PRIMARY KEY (`isbn`) ,
INDEX `fk_libro2_idx` (`idcategoria` ASC) ,
INDEX `fk_libro3_idx` (`ideditorial` ASC) ,
CONSTRAINT `fk_libro2`
FOREIGN KEY (`idcategoria` )
REFERENCES `biblioteca`.`categoria` (`idcategoria` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_libro3`
FOREIGN KEY (`ideditorial` )
REFERENCES `biblioteca`.`editorial` (`ideditorial` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
and i think thats foreign keys are good.
If these are fresh tables with no values, It seems like an error with the columns
one has a varchar(25) the other a var_char(45) make them the same
edit:
CREATE TABLE IF NOT EXISTS `biblioteca`.`libro` (
`isbn` VARCHAR(45) NOT NULL ,
`idcategoria` INT(11) NOT NULL ,
`ideditorial` INT(11) NOT NULL ,
`titulo` VARCHAR(45) NOT NULL ,
`autor` VARCHAR(45) NOT NULL ,
`reseña` VARCHAR(45) NULL ,
PRIMARY KEY (`isbn`) ,
INDEX `fk_libro2_idx` (`idcategoria` ASC) ,
INDEX `fk_libro3_idx` (`ideditorial` ASC) )
ENGINE = INNODB
DEFAULT CHARACTER SET = utf8;
CREATE TABLE IF NOT EXISTS `biblioteca`.`ejemplar` (
`idejemplar` INT(11) NOT NULL AUTO_INCREMENT ,
`estado` VARCHAR(45) NOT NULL ,
`comentario` VARCHAR(45) NULL ,
`isbn` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idejemplar`) ,
INDEX `fk_ejemplar_libro1_idx` (`isbn` ASC) ,
CONSTRAINT `fk_ejemplar_libro1`
FOREIGN KEY (`isbn` )
REFERENCES `biblioteca`.`libro` (`isbn` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = INNODB
DEFAULT CHARACTER SET = utf8;
works no problem for me. Ive taken away the other 2 keys which i dont have schemas for though

Foreign Key equal Null

I have tabel users:
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`users` (
`id` INT NOT NULL AUTO_INCREMENT ,
`firstName` VARCHAR(45) NOT NULL ,
`lastName` VARCHAR(45) NOT NULL ,
`personalId` VARCHAR(11) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`city` VARCHAR(45) NULL ,
`address` VARCHAR(45) NULL ,
`email` VARCHAR(45) NULL ,
`phone` VARCHAR(45) NULL ,
`specialization` VARCHAR(45) NULL ,
`role` VARCHAR(45) NOT NULL ,
`active` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `personalId_UNIQUE` (`personalId` ASC) )
and tabel visits. Tabel visits have two foreign key to the users table
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`visits` (
`id` INT NULL AUTO_INCREMENT ,
`doctorId` INT NOT NULL ,
`patientId` INT NULL,
`dateVisit` DATE NOT NULL ,
`hourVisit` DATE NOT NULL ,
PRIMARY KEY (`id`, `doctorId`, `patientId`) ,
INDEX `fk_visits_Users1_idx` (`doctorId` ASC) ,
INDEX `fk_visits_Users2_idx` (`patientId` ASC) ,
CONSTRAINT `fk_visits_Users1`
FOREIGN KEY (`doctorId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_visits_Users2`
FOREIGN KEY (`patientId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
The second foreign key I set that can be null, because I care about adding an entry to the table visits where it will be empty.
However, when you try to add I get an error
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (eprzychodnia.visits, CONSTRAINT fk_visits_Users2 FOREIGN KEY (patientId) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE NO ACTION)
What can I do to make the key could be null?
In your relation setup change the action on update and delete
The problem is not your foreign key, it's your primary key. You can't have nullable fields in a primary key. The id field by itself might be a better choice.
I'm not a fan of nullable foreign keys. I would add a record to your users table that equates to "not an actual person" and use it as a default. By the way, the hourvisit field might be redundant.
Dana Bruck I do not know if I understand your proposals. But it seems to me that it's something like this:
Table visits
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`visits` (
`id` INT NULL AUTO_INCREMENT ,
`doctorId` INT NOT NULL ,
`patientId` INT NULL,
`dateVisit` DATE NOT NULL ,
`hourVisit` DATE NOT NULL ,
PRIMARY KEY (`id`, `doctorId`, `patientId`))
Table without foreign keys. The fields not related to the table users, but I set these fields when I need and this field may be null.
If the id in visits is auto_increment, why you create your PK with 3 columns ??
Just id is enough. You can create a unique index for doctor and patience ID.
Try this model:
CREATE TABLE IF NOT EXISTS `ePrzychodnia`.`visits` (
`id` INT NOT NULL AUTO_INCREMENT ,
`doctorId` INT NOT NULL ,
`patientId` INT NULL,
`dateVisit` DATE NOT NULL ,
`hourVisit` DATE NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `unique_doctor_pacient` (`doctorId`, `patientId`),
INDEX `fk_visits_Users1_idx` (`doctorId` ASC) ,
INDEX `fk_visits_Users2_idx` (`patientId` ASC) ,
CONSTRAINT `fk_visits_Users1`
FOREIGN KEY (`doctorId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_visits_Users2`
FOREIGN KEY (`patientId` )
REFERENCES `ePrzychodnia`.`users` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)

mySql foreign key name issue

Hi I have designed a database in mysql workbench. When I went to forward engineer I got (errno: 121) because I had foreign keys named the same in multiple tables which I realized is not allowed. I have quite a few junction tables as mostly n:m relationships. I am using composite primary keys (made up of 2 foreign keys) for most of these junction tables. My question is do I have to rename these primary keys to be unique along with the foreign keys?
Appreciate some help please.
(I have not renamed the foreign keys yet in below code)
CREATE TABLE IF NOT EXISTS `mydb`.`tblStudent` (
`StudentID` INT(6) NOT NULL AUTO_INCREMENT ,
`Student_Firstname` VARCHAR(20) NOT NULL ,
`Student_Lastname` VARCHAR(20) NOT NULL ,
`DOB` DATE NOT NULL ,
`Student_Gender` ENUM('Male','Female') NOT NULL ,
`Student_Address1` VARCHAR(40) NOT NULL ,
`Student_Address2` VARCHAR(22) NOT NULL ,
`Student_Address3` VARCHAR(14) NOT NULL ,
`Student_Phonenum` INT(10) NULL ,
`Student_Email` VARCHAR(60) NOT NULL ,
`Student_Password` CHAR(128) NOT NULL ,
`Enrollment_Date` DATE NOT NULL ,
`Graduation_Date` DATE NULL ,
`Student_Picture` VARCHAR(100) NOT NULL ,
`PPSN` VARCHAR(9) NOT NULL ,
PRIMARY KEY (`StudentID`) )
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`tblIllness` (
`IllnessID` CHAR(5) NOT NULL ,
`Illness_Name` VARCHAR(30) NOT NULL ,
PRIMARY KEY (`IllnessID`) )
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `mydb`.`tblStudentIllness` (
`IllnessID` CHAR(5) NOT NULL ,
`StudentID` INT(6) NOT NULL ,
`Doctor_Name` VARCHAR(30) NOT NULL ,
`Doctor_Phonenum` INT(10) NOT NULL ,
`Medication` VARCHAR(40) NOT NULL ,
`Medical_Advice` VARCHAR(250) NOT NULL ,
PRIMARY KEY (`IllnessID`, `StudentID`) ,
INDEX `IllnessID_idx` (`IllnessID` ASC) ,
INDEX `StudentID_idx` (`StudentID` ASC) ,
CONSTRAINT `IllnessID`
FOREIGN KEY (`IllnessID` )
REFERENCES `mydb`.`tblIllness` (`IllnessID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `StudentID`
FOREIGN KEY (`StudentID` )
REFERENCES `mydb`.`tblStudent` (`StudentID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
This compiles fine for me. One solution is to just remove the constraint names and they will be generated automatically for you.
...
CONSTRAINT
FOREIGN KEY (`IllnessID` )
REFERENCES `tblIllness` (`IllnessID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT
FOREIGN KEY (`StudentID` )
REFERENCES `tblStudent` (`StudentID` )
ON DELETE CASCADE
...

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)
);