foreign-keys

Bidirectional foreign key constraint

余生长醉 提交于 2019-11-29 10:57:26
I'm thinking of designing a database schema similar to the following: Person ( PersonID int primary key, PrimaryAddressID int not null, ... ) Address ( AddressID int primary key, PersonID int not null, ... ) Person.PrimaryAddressID and Address.PersonID would be foreign keys to the corresponding tables. The obvious problem is that it's impossible to insert anything into either table. Is there any way to design a working schema that enforces every Person having a primary address? "I believe this is impossible. You cannot create an Address Record until you know the ID of the person and you cannot

Can someone explain MySQL foreign keys

风格不统一 提交于 2019-11-29 10:06:45
问题 I know what they are my question is, how do you link them or are they automatically linked when you have identical names in different tables. Here is an example: Say I have an [orders] table and a [customer] table. Each row in the [orders] table has a customer_id number which is associated with the customer_id in the [customer] table. So how do I get the customer information by referencing the order? What would be the sql query? 回答1: ... how do you link them or are they automatically linked

Tools for discovering de facto foreign keys in databases? [closed]

强颜欢笑 提交于 2019-11-29 09:26:57
问题 A good way to quickly survey the information in a database is to apply a tool that automatically creates a database diagram of all tables and all relationships between them. In my experience, such tools use foreign keys as the relationships, which most of the databases I try them do not contain. Sure, they satisfy constraints corresponding to foreign keys, but do not enforce them. And I'll end up with a 'diagram' consisting of a bunch of unrelated tables. So what I'm looking for is software

MySQL table with a varchar column as foreign key

帅比萌擦擦* 提交于 2019-11-29 09:16:28
I am trying to create a table with a varchar column as foreign key but MySql gives me an error while creating the table. My query is like this: CREATE TABLE network_classes ( id TINYINT(1) UNSIGNED NOT NULL AUTO_INCREMENT, category VARCHAR(80) NOT NULL, PRIMARY KEY(id), KEY `key_1` (`id`,`category`) ) ENGINE=InnoDB; CREATE TABLE networks ( id TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, category VARCHAR(80) NOT NULL, director_id TINYINT(3) UNSIGNED NULL, director_name VARCHAR(100) NULL, description VARCHAR(1000) NULL, last_modified TIMESTAMP NULL DEFAULT CURRENT

Laravel migration self referencing foreign key issue

北城以北 提交于 2019-11-29 08:38:53
问题 Hi I have a problem to create a table using migration schema builder. The problem occure with table with self referencing foreign key. Here is the code which produce error: Schema::create('cb_category', function($table) { $table->integer('id')->primary()->unique()->unsigned(); $table->integer('domain_id')->unsigned(); $table->foreign('domain_id')->references('id')->on('cb_domain'); $table->integer('parent_id')->nullable(); $table->foreign('parent_id')->references('id')->on('cb_category')-

In django, how to limit choices of a foreignfield based on another field in the same model?

删除回忆录丶 提交于 2019-11-29 08:12:53
问题 I have these models (I have limited the number of fields to just those needed) class unit(models.Model): name = models.CharField(max_length=200) class project(models.Model): name = models.CharField(max_length=200) class location(address): project = models.ForeignKey(project) class project_unit(models.Model): project = models.ForeignKey(project) unit = models.ForeignKey(unit) class location_unit(models.Model): project = models.ForeignKey(project) #Limit the selection of locations based on

Entity Framework 0..1 to 0 relation

ε祈祈猫儿з 提交于 2019-11-29 07:52:31
class First { [Key] public int Id { get; set; } } class Second { [Key] public int Id { get; set; } public int? First_Id { get; set; } [ForeignKey("First_Id")] public First First { get; set; } } public class SecondMapping : EntityTypeConfiguration<Second> { public SecondMapping () : base() { this.HasOptional(s => s.First) .With ... ??? } } Second may have a reference to First. But First never has a reference to Second. Is it possible to apply this mapping with Entity Framework 4.1? EDIT: Previously, that was my solution: this.HasOptional(s => s.First) .WithOptionalDependent()

MySQL non primary foreign key

人走茶凉 提交于 2019-11-29 07:41:51
I'm a bit of a newbie and I can't get my head around primary keys as foreign keys. To me, foreign keys are meant to connect two rows of a table together. Therefore, it would make logical sense to use the, for example, username of the user table as a foreign key in the picture table. This means that the picture in that row belongs to the specified user. However, it appears that general practice favors using meaningless numbers as primary IDs. Furthermore the foreign key must/should refer to the primary key. What if I don't know the primary key, but I know another unique column, in this case

Many-to-Many Relationships in MySQL

梦想与她 提交于 2019-11-29 07:39:53
I've been reading up on foreign keys and joins recently, and have been pleasantly surprised that many of the basic concepts are things I'm already putting into practice. For example, with one project I'm currently working on, I'm organizing word lists, and have a table for the sets, like so: `words` Table `word_id` `headword` `category_id` `categories` Table `category_id` `category_name` Now, generally speaking this would be a one-to-many relationship, with several words being placed under a single category with the foreign key category_id . Let's assume for a moment, however, that a user

EF4.1 Code First : How to disable delete cascade for a relationship without navigation property in dependent entity

为君一笑 提交于 2019-11-29 07:33:03
问题 Let's say I have these two very basic entities: public class ParentEntity { public int Id; public virtual ICollection<ChildEntity> Childrens; } public class ChildEntity { public int Id; public int ParentEntityId; // Foreign Key public virtual ParentEntity parent; // [NOTWANTED] } For some reasons, I don't want the ChildEntity to hold a reference back to his parent. I just want it to keep the ParentEntity id but nothing more. Up until now, no problem, I just delete the [NOTWANTED] line, and