I searched through the prior posts but didn't quite find the answer, so here it goes ...
I have this table
CREATE TABLE `leags`
(
`lid` int(10) NOT NULL AUTO_INCREMENT,
`lName` varchar(255) NOT NULL,
`description` varchar(255) DEFAULT NULL,
`links` varchar(255) DEFAULT NULL,
`cid` int(10) NOT NULL,
PRIMARY KEY (`lid`),
KEY `index2` (`lid`,`lName`),
KEY `index3` (`lid`,`lName`,`cid`),
KEY `cid_idx` (`cid`),
CONSTRAINT `cid` FOREIGN KEY (`cid`) REFERENCES `cats` (`cid`)
)
ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='Leagues'$$
I have another table that has the above PK as a foreign key.
CREATE TABLE `tams`
(
`tId` INT(10) NOT NULL ,
`tName` VARCHAR(255) NOT NULL ,
`lid` INT(10) NULL ,
`url` TEXT NULL ,
PRIMARY KEY (`tId`) ,
INDEX `index2` (`tId` ASC, `tName` ASC) ,
INDEX `index3` (`tId` ASC, `tName` ASC, `lid` ASC) ,
INDEX `lid_idx` (`lid` ASC) ,
CONSTRAINT `lid`
FOREIGN KEY (`lid` )
REFERENCES leags` (`lid` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The creation of this table works fine, but I run into a problem creating the below table.
CREATE TABLE IF NOT EXISTS spSched
(
`spscId` INT(10) NOT NULL AUTO_INCREMENT ,
`startDate` DATE NOT NULL ,
`endDate` DATE NULL ,
`startTime` TIME NOT NULL ,
`endTime` TIME NULL ,
`htId` INT(10) NULL ,
`type` VARCHAR(45) NULL ,
`atId` INT(10) NULL ,
`leagId` INT(10) NULL ,
PRIMARY KEY (`spscId`) ,
UNIQUE INDEX `spscId_UNIQUE` (`spscId` ASC) ,
INDEX `htId_idx` (`htId` ASC) ,
INDEX `atId_idx` (`atId` ASC) ,
INDEX `lid_idx` (`leagId` ASC) ,
CONSTRAINT `tId`
FOREIGN KEY (`htId` )
REFERENCES tams (`tId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `atId`
FOREIGN KEY (`atId` )
REFERENCES tams` (`tId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `lId`
FOREIGN KEY (`leagId` )
REFERENCES leags (`lid` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
If I remove the lId as a FK and the constraint from the above sql, the table creates just fine. I can't figure out where the duplicate key is in here. All help is much appreciated.
Thanks.
lId you already have a constraint with this name in table tams lid.
I suspect your system is not case sensitive.
http://richardfearn.wordpress.com/2007/03/12/case-insensitive-constraint-names-in-mysql/
internally, InnoDB stores foreign key constraint names as
case-insensitive, with the database name prepended. So although
onedatabase.b_ibfk_1 is treated as different to
anotherdatabase.b_ibfk_1, Photos.b_ibfk_1 and photos.b_ibfk_1 are
treated as the same.
Just pick another name for that key.
Related
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
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
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)
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
...
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!