Im having to create and use a database for a class project. I developed the database and application locally using mysql workbench. It needs to be hosted on another service called myphpmyadmin. the problem is when I create my tables using the mysql execution line in that service I get the following error.
Error
SQL query:
CREATE TABLE IF NOT EXISTS `artists_residence` (
`artists_id` INT( 11 ) ,
`city_id` INT( 11 ) ,
PRIMARY KEY ( `artists_id` , `city_id` ) ,
CONSTRAINT FOREIGN KEY ( `artists_id` ) REFERENCES `artists` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE ,
CONSTRAINT FOREIGN KEY ( `city_id` ) REFERENCES `city` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB CHARSET = Latin1;
MySQL said: Documentation
#1005 - Can't create table 'fondellb-db.artists_residence' (errno: 150)
Here is the entire file of mySql i'm trying to execute.
CREATE TABLE IF NOT EXISTS `artists` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`fname` VARCHAR(255) NOT NULL,
`lname` VARCHAR(255) NOT NULL,
`year_of_birth` DATE NOT NULL,
`year_of_death` DATE NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `country` (
`country_id` INT(11) NOT NULL,
`name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`country_id`))
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `city` (
`id` INT(11) NOT NULL,
`name` VARCHAR(255) NOT NULL,
`fk_country_id` INT(11),
PRIMARY KEY (`id`, `fk_country_id`),
CONSTRAINT `country_id`
FOREIGN KEY (`fk_country_id`)
REFERENCES `country` (`country_id`)
ON DELETE RESTRICT
ON UPDATE RESTRICT)
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `Medium` (
`id` INT(11) NOT NULL,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `artists_medium` (
`artists_id` INT(11),
`medium_id` INT(11),
PRIMARY KEY (`artists_id`, `medium_id`),
CONSTRAINT
FOREIGN KEY (`artists_id`)
REFERENCES `artists` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT
FOREIGN KEY (`medium_id`)
REFERENCES `Medium` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB CHARSET = Latin1;
CREATE TABLE IF NOT EXISTS `artists_residence` (
`artists_id` INT(11),
`city_id` INT(11),
PRIMARY KEY (`artists_id`, `city_id`),
CONSTRAINT
FOREIGN KEY (`artists_id`)
REFERENCES `artists` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT
FOREIGN KEY (`city_id`)
REFERENCES `city` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB CHARSET = Latin1;
I cant figure out what is failing in the final query. All the other queries are successful and the tables were created. Ive fiddled with that query in all sorts of variations and nothing seems to solve the error. Ive removed the explicit indexes, Ive changed the order of the constraints, checked all my type comparisons. This is a file that mysql exported based on the model i designed so I'm not sure why now i'm receiving this error. Please do not refer me to a reference to the error code or to another post that also receives this error code, I know what can cause the error but im wondering what is causing it here.
In this case - you're creating the following foreign key
CONSTRAINT FOREIGN KEY ( `city_id` ) REFERENCES `city` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE
while city table does not have an id primary key, but a composite primary key.
Obviously, the only one column of the 2 cannot guarantee uniqueness of the value hence mysql restricts you from creating a such FK.
So you must either change the city's PK or the artists_residence's FK.
Related
I'm trying to set a foreign key relationship between two existing table but for some reason which I'm missing I keep getting this error 1215.
The relevant fields of the two tables are:
CREATE TABLE `approvals` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_contract` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`lang` varchar(8) NOT NULL,
`request_ts` datetime NOT NULL,
`version` int(11) NOT NULL,
-- more nullable columns here with no indexing/relationships
PRIMARY KEY (`id`),
KEY `approval_id_user` (`id_user`),
KEY `version` (`version`,`lang`,`id_contract`),
CONSTRAINT `approval_id_user` FOREIGN KEY (`id_user`)
REFERENCES `users` (`iduser`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1079 DEFAULT CHARSET=utf8
CREATE TABLE `version` (
`idversion` int(11) NOT NULL,
`idcontract` int(11) NOT NULL,
`language` varchar(8) NOT NULL,
PRIMARY KEY (`idversion`,`idcontract`,`language`),
KEY `fk_version_contracts_idx` (`idcontract`),
CONSTRAINT `fk_kidversion_contracts` FOREIGN KEY (`idcontract`)
REFERENCES `contracts` (`id_contract`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8
What have I tried
First thing I checked is the persistency:
SELECT * FROM approvals a
WHERE NOT EXISTS (
SELECT 1 FROM version v
WHERE v.idcontract = a.id_contract AND v.language = a.lang
AND v.idversion = a.version);
which resulted in 0 rows returned out of 701, ok my db should be good from the data pov.
So I checked the collation and for some reason the version table was latin1 vs utf8_generic_ci, ok so I restored the version table to utf8 with this.
ALTER TABLE `kidversion`
COLLATE = utf8_general_ci;
-- and for good measure
ALTER TABLE `kidversion`
CHANGE COLUMN `language` `language` VARCHAR(8)
CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NOT NULL;
But at this point I'm still getting an error while trying to create the foreign key:
ALTER TABLE `approvals`
ADD CONSTRAINT `approval_ibfk_1`
FOREIGN KEY (`version` , `lang` , `id_contract`)
REFERENCES `version` (`idversion` , `language` , `idcontract`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
And that results in a 1215 in every step I made. Surely I'm missing something.. can anyone help me out with this?
FOREIGN KEY (version , lang , id_contract) REFERENCES version (idversion , language , idcontract) AND PRIMARY KEY (idversion,idcontract,language) don't match fields have to be in the same order as well as same type.
'In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order.' - https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
I have a table files and a table users and staff and client.
The file table contain a row made_by a row published_byand a row about_client pointing to a staff, a staff and a client respectively.
One user must be either a staff or a client.
One file can have multiple author (made_by row), but only one user can publish it (published_by), and it can only be about one client (about_client).
First, I put 3 foreign key on files:
CONSTRAINT `made_by_fk` FOREIGN KEY (`made_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `published_by_fk` FOREIGN KEY (`published_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `about_client_fk` FOREIGN KEY (`about_client`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
but after a little research, I found out that I should put the foreign key on staff / client, and here come the problem, the fk are OPTIONAL. A staff is not obligatory an author of a file, or the client maybe don't have any file attached to it.
I am a bit lost.
DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
`id` INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,
`made_by` INT NOT NULL,
`published_by` INT NOT NULL,
`about_client` INT NOT NULL,
`creation_date` DATETIME NOT NULL,
`modification_date` DATETIME NULL
ON UPDATE CURRENT_TIMESTAMP,
`path` VARCHAR(255) NOT NULL,
`title` VARCHAR(100) NOT NULL UNIQUE,
`category` INT NOT NULL,
`type` VARCHAR(30) NOT NULL,
`size` INT NOT NULL,
CONSTRAINT `made_by_fk` FOREIGN KEY (`made_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `published_by_fk` FOREIGN KEY (`published_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `about_client_fk` FOREIGN KEY (`about_client`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
ENGINE = InnoDB
COLLATE = utf8_unicode_ci;
DROP TABLE IF EXISTS `staff`;
CREATE TABLE IF NOT EXISTS `staff` (
`id` INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,
`job` INT NOT NULL,
`password` VARCHAR(255) NOT NULL
)
ENGINE = InnoDB
COLLATE = utf8_unicode_ci;
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
`id` INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,
`name` VARCHAR(100) NOT NULL,
`surname` VARCHAR(100) NOT NULL,
`email` VARCHAR(50) NOT NULL UNIQUE
)
ENGINE = InnoDB
COLLATE = utf8_unicode_ci;
DROP TABLE IF EXISTS `clients`;
CREATE TABLE IF NOT EXISTS `clients` (
`id` INT NOT NULL AUTO_INCREMENT
PRIMARY KEY,
`phone` VARCHAR(30) NOT NULL,
`age` INT NOT NULL,
`date_of_birth` DATE NOT NULL,
`security_number` VARCHAR(99) NOT NULL UNIQUE
)
ENGINE = InnoDB
COLLATE = utf8_unicode_ci;
If there can be multiple made_by, it can't be a column in the file table, since that can only contain one value.
You need another table, file_made_by that represents the many-to-many relationship between files and authors.
CREATE TABLE file_made_by (
file_id INT NOT NULL,
author_id INT NOT NULL,
PRIMARY KEY (file_id, author_id),
FOREIGN KEY (file_id) REFERENCES files (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (author_id) REFERENCES users (id) ON DELETE CASCADE ON UPDATE CASCADE
);
The clients and staff tables should also include a user_id column that's a foreign key to users.id.
If about_client has to be about a client, not staff, then it should be a foreign key to clients.id rather than users.id.
And if only staff are allowed to be authors, file_made_by.author_id should be a FK to staff.id rather than users.id. The same with files.published_by.
Error
I have a working site in one server. I decided to move it to another company. I exported the database with phpmyadmin and uploaded in the new server. Everytime I try to import the database I receive this error:
SQL query:
--
-- Constraints for table `seller_cart`
--
ALTER TABLE `seller_cart`
ADD CONSTRAINT `seller_cart_ibfk_1`
FOREIGN KEY ( `subscription` )
REFERENCES `subscription` ( `id` ) ,
ADD CONSTRAINT `seller_cart_ibfk_2`
FOREIGN KEY ( `user` )
REFERENCES `users` ( `user_id` )
ON DELETE CASCADE
ON UPDATE CASCADE ,
ADD CONSTRAINT `seller_cart_ibfk_3`
FOREIGN KEY ( `featured_item` )
REFERENCES `featured_item` ( `item` )
ON DELETE CASCADE
ON UPDATE CASCADE ;
MySQL said: Documentation
#1005 - Can't create table 'project123.#sql-12050_1d' (errno: 150) (Details...)
HERE'S THE STRUCTURE OF seller cart:
--
-- Table structure for table `seller_cart`
--
CREATE TABLE IF NOT EXISTS `seller_cart` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user` bigint(20) NOT NULL,
`subscription` bigint(20) DEFAULT NULL,
`description` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`item_listings` text COLLATE utf8_unicode_ci,
`featured_item` bigint(20) DEFAULT NULL,
`quantity` int(2) NOT NULL,
`price` float NOT NULL,
PRIMARY KEY (`id`),
KEY `user` (`user`),
KEY `subscription` (`subscription`),
KEY `featured_item` (`featured_item`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
What could be the problem? I just exported and imported the samedatabase. Even tried on a localhost and the same problem.
This error is thrown when you are trying to refer a column which is not indexed.
Check if
subscription ( id )
users ( user_id )
featured_item ( item )
have valid indexes defined. If not, define them and run your ALTER ... statement.
Refer To: Rules for Foreign Key Constraints:
InnoDB and FOREIGN KEY Constraints
I have three tables: categories, languages and categories_languages. Categories_languages is many to many table which links together categories and languages. I would like to update a foregin key value in table languages but it throws me error #1451 - Cannot delete or update a parent row: a foreign key constraint fails!
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(20) NOT NULL,
`modified` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `languages` (
`id` char(2) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `categories_languages` (
`id` int(11) unsigned NOT NULL auto_increment,
`category_id` int(11) unsigned NOT NULL,
`language_id` char(2) NOT NULL,
`translation` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_category_id_language_id` (`category_id`,`language_id`),
KEY `fk_language_id` (`language_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
ALTER TABLE `categories_languages`
ADD CONSTRAINT `categories_languages_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `categories_languages_ibfk_2` FOREIGN KEY (`language_id`) REFERENCES `languages` (`id`) ON DELETE CASCADE;
The error is clear to me, but how can I update a key value in this case? I tried adding ON UPDATA CASCADE:
ALTER TABLE `categories_languages`
ADD CONSTRAINT `categories_languages_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `categories_languages_ibfk_2` FOREIGN KEY (`language_id`) REFERENCES `languages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
but that also fails with message: MySQL said: Documentation #1005 - Can't create table './db_dodo/#sql-c2f_80e6f.frm' (errno: 121)
You can temporarily suspend foreign key checking:
SET foreign_key_checks = 0;
UPDATE languages SET id='xyz' WHERE id='abc';
UPDATE categories_languages SET language_id='xyz' WHERE language_id='abc';
SET foreign_key_checks = 1;
EDIT: As for the foreign key problem: is the data stored on a local or a remote file system? errno 121 is EREMOTEIO (Remote I/O error). Perhaps there are permission problems on the target file system or it doesn't support the # character in filenames?
If you are looking for a temp solution you can also change the ON UPDATE action to CASCADE and modify your ids
I have such a problem. I'm using EF1 with VS2008 SP1 & MySQL with MySQL Connector/Net 6.3.4
My database schema looks like this:
CREATE TABLE IF NOT EXISTS `credential` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `user_credential` (
`user_id` int(11) NOT NULL,
`credential_id` int(11) NOT NULL,
KEY `credential_id` (`credential_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `user_credential`
ADD CONSTRAINT `user_credential_ibfk_2` FOREIGN KEY (`credential_id`) REFERENCES `credential` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `user_credential_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE;
While I'm trying to execute the folowing code I have exception that I cannot understand
var entities = new studyEntities();
var user = new User { Name = "test" };
var credential = new Credential { Name = "admin" };
entities.AddToCredentialSet(credential);
entities.AddToUserSet(user);
entities.SaveChanges();
user.Credentials.Add(credential);
entities.SaveChanges(); // He I have a strange exception thrown
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 '(SELECT
`user_credential`.`credential_id`,
`user_credential`.`user_' at line 1
What does it mean? How can I see the whole query to seek the problem in it? or maybe what I'm doing wrong?
I had the same error because I had no primary key in the middle table.
This fixed it for me:
CREATE TABLE IF NOT EXISTS `user_credential` (
`user_id` int(11) NOT NULL,
`credential_id` int(11) NOT NULL,
PRIMARY KEY(`user_id`, `credential_id`)
KEY `user_id` (`user_id`)
CONSTRAINT .... FOREIGN KEY....
CONSTRAINT .... FOREIGN KEY....
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Looks like a bug in Connector /NET.
We have made a test, this code succeeded in dotConnect fro MySQL.
Add a primary key to the many-to-many table.
So if your table is
CREATE TABLE If Not Exists SubsidiaryMapping
(
CompanyId bigint NOT NULL,
SubsidiaryId bigint NOT NULL,
-- Add Primary Key composing exisiting keys
-- Following fixed issue for me
PRIMARY KEY(CompanyId , SubsidiaryId ),
CONSTRAINT .... FOREIGN KEY....
CONSTRAINT .... FOREIGN KEY....
);