foreign-keys

How to determine cardinality of foreign key using mysql

风流意气都作罢 提交于 2019-12-25 20:57:15
问题 I am stuck in a problem where i have to find cardinality of a relationship between tables using mysql. Following this post MySQL: How to determine foreign key relationships programmatically? I have found all tables related to my table and the columns which are foreign key. Now i also want to find the cardinality of relationship i.e. one-to-one, one-to-many or many-to-many. Any ideas or snippets would be highly appreciated 回答1: Let us assume that table A has a foreign key f which refers to the

How to determine cardinality of foreign key using mysql

时光毁灭记忆、已成空白 提交于 2019-12-25 20:56:10
问题 I am stuck in a problem where i have to find cardinality of a relationship between tables using mysql. Following this post MySQL: How to determine foreign key relationships programmatically? I have found all tables related to my table and the columns which are foreign key. Now i also want to find the cardinality of relationship i.e. one-to-one, one-to-many or many-to-many. Any ideas or snippets would be highly appreciated 回答1: Let us assume that table A has a foreign key f which refers to the

MySQL: INSERT blocked by an UPDATE of the foreign key referenced row

别等时光非礼了梦想. 提交于 2019-12-25 18:48:45
问题 Let me start my question with an SQL example... Here is the table setup, Create table X and Y. With y.x refers to x.id. Insert a row into X (id=1) START TRANSACTION; CREATE TABLE `x` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `value` INT(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=INNODB; CREATE TABLE `y` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `x_id` INT(11) NOT NULL, `value` INT(11) NOT NULL, PRIMARY KEY (`id`), CONSTRAINT `fk_x` FOREIGN KEY (`x_id`) REFERENCES `x` (`id`) ) ENGINE=INNODB; INSERT

MySQL foreign key using more than one field to reference to a primary key from another table

旧巷老猫 提交于 2019-12-25 17:45:03
问题 I want to reference 2 of my columns in one table to a primary key from another table. Here is how it looks like in the db structure: Users uid (INT) name (VARCHAR) 1 John Doe 2 Jane Doe SystemProcesses uid (INT) name (VARCHAR) 1 Hitman 2 Cron Logs uid (INT) modelType (VARCHAR) modelUID (INT) Action 1 Users 2 Jane Doe did this 2 Users 1 John Doe did that 3 SystemProcesses 1 Hitman just killed John Doe How do I reference modelType and modelUID in Logs table to those Users and SystemProcesses?

Foreign keys when cascades aren't needed

霸气de小男生 提交于 2019-12-25 16:59:38
问题 If I don't need to use cascade / restrict and similar constraints in a field which would logically be a foreign key , do I have any reason to explicitly declare it as a foreign key , other than aesthetics? Wouldn't it actually decrease performance, since it has to test for integrity? edit: to clarify, I don't need it since: I won't edit nor delete those values anyway, so I don't need to do cascade and similar checks Before calling INSERT , I'll check anyway if the target key exists, so I don

Not able to display random images below product image in Ruby on rails app?

可紊 提交于 2019-12-25 16:09:20
问题 I want to be able to show 3 random product images below a product image, so far I've inserted this code below the product image as seen in the views/products/show.html.erb views/products/show.html.erb <div class="col-xs-12 col-sm-6 center-block" > <%= image_tag @product.image.url(:medium), class: "img-responsive" %> #This is the Product image# ##EDITED ## #Below is the codesnippet for three products to appear## <% @products.each do |product| %> <div class="col-sm-2 center-block " > <%= link

The INSERT statement conflicted with the FOREIGN KEY constraint “FK_tKosikZbozi_tKosik”

耗尽温柔 提交于 2019-12-25 15:55:53
问题 i have eshop with tZbozi (table of products), tKosik (table of shopping cart), and middle table, so that is N:M. In my PC everything works fine, but when I put it on the server, i got "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tKosikZbozi_tKosik". The conflict occurred in database "sumenergocz1", table "dbo.tKosik", column 'idKosik'. The statement has been terminated." Here is my ADD method: // Check if exist row of this product in this cart table bool exist = false;

Migrate from one django model to two models referenced with a foreign key

流过昼夜 提交于 2019-12-25 09:42:20
问题 I need to outsource some of the attribues in the following Django model: class TextResult(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) text = models.ForeignKey(Text) # following fields will be in the referenced model wpm = models.FloatField(default=0.0) accuracy = models.FloatField(default=1.0, validators=[MinValueValidator(0.0), MaxValueValidator(1.0)]) to a model, that references to the specific data: class TextResult(models.Model): user = models.ForeignKey

Composite Foreign Key from multiple related tables

徘徊边缘 提交于 2019-12-25 08:49:46
问题 Just beginning to learn about SQL and had a question I couldn't figure out. I have a setup based on the following tables and their primary keys, the columns with the same name between tables are constrained by foreign keys: Company: CompanyId Division: CompanyId DivisionId Resource: CompanyId ResourceId DivisionResource : CompanyId DivisionId ResourceId DivisionResource is used to create a many to many relation between division and resource and constrain them so that divisions can only be

How to add foreign key to MySQL table?

北城以北 提交于 2019-12-25 07:29:37
问题 I use MySQL with InnoDB engine. I double-checked type of columns. But always have: Error Code: 1215. Cannot add foreign key constraint I tried: ALTER TABLE `mail`.`boxes` ADD CONSTRAINT FK_id FOREIGN KEY (id) REFERENCES `mail`.`users` (id) ON UPDATE NO ACTION ON DELETE NO ACTION; and ALTER TABLE `mail`.`boxes` ADD FOREIGN KEY (id) REFERENCES `mail`.`users` (id) Nothing works((( Please, help, what I am doing wrong (except choosing MySQL :-) )? 回答1: If table contains data then you are not able