foreign-keys

Example of when you should use a foreign key that points to a candidate key, not a primary key?

别等时光非礼了梦想. 提交于 2019-12-04 19:47:28
From my reading, I understand what makes a good primary key, what a foreign key is and what a candidate key is. I've read in several different books and sources that: A foreign key must point to a candidate key (or primary) A foreign key almost always points to a primary key The authors of the sources always say something along the lines of, "while foreign keys can point at a candidate key (not primary) they seem to". Are there any examples of why you might choose a candidate key and not the primary key? Thank you Primary keys (PKs) have no role in relational theory. (Eg integrity or

How to index all foreign keys in MS SQL?

本秂侑毒 提交于 2019-12-04 19:20:53
We have large MS SQL database with many tables and foreign keys. We need to index all foreign keys in our tables due to performances and we are trying to avoid do it manually. Is there any way to MS SQL do it automatically or with some tools? MS SQL server used is MS SQL Server 2008 R2. Thanks a lot. I'd steer you away from trying to do this automatically and encourage you to examine your system (Google missing index DMVs (SQL 2005 or later) for a good start) and create appropriate indexes instead. If you try to do this automatically, you could potentially introduce issues like: Creating

Only one key from composite primary key as foreign key

余生长醉 提交于 2019-12-04 19:20:18
In this database, key1 & key2 make up the composite primary key of table4 , but i'm able to add a foreign key to table3 that comprise just key1 . Why MySQL allows this? Does the above database design make no sense at all? TL;DR It's not standard SQL. MySQL documentation itself advises not doing it. In SQL a FOREIGN KEY declaration must reference a column list declared PRIMARY KEY or UNIQUE NOT NULL. (PRIMARY KEY creates a UNIQUE NOT NULL constraint.) (The constraint has to be explicit, even though any set of NOT NULL columns containing a set that is UNIQUE has to be unique.) The MySQL

How to make two foreign keys to same model unique together?

寵の児 提交于 2019-12-04 19:09:33
Let's say I have a relationship class such as: class Friendship(models.Model): person1 = models.ForeignKey(Person, related_name='person1') person2 = models.ForeignKey(Person, related_name='person2') so I want to make this object unique for a pair of Person s. If I simply do unique_together = (("person1", "person2"),) then I can end up with two Friendship objects where FS1.person1 = A, FS1.person2 = B FS2.person1 = B, FS2.person2 = A I do not want this. I want a unique friendship object between two people. So how can I ensure that there is -at most- one Friendship object for any pair of Person

Entity Framework - Reverse Foreign Key Check

风格不统一 提交于 2019-12-04 18:50:20
We have a system that allows Administrators to build out new content types within the system, including foreign key linkages to other tables. The Admin can then rebuild the database, at which point it creates the tables and all the necessary relationships, then rebuilds the EDMX and recompiles everything. Works like a champ (I didn't write it or I might know the answer to this). One drawback that we have is when a user goes to delete a record that may be linked to by an item in another table. This throws an error due to referential integrity. I'm trapping this, of course, but all I can provide

Foreign keys must be Index in mySQL?

烂漫一生 提交于 2019-12-04 18:31:44
问题 I've just created my first mySQL table on my own (other than using Joomla, Wordpress, etc.) and I am MS SQL developer for years but normally I can easily create a foreign key in MS SQL but I came across a difficulty or lack of knowledge here. Here is my tables : users user_id int primary auto_increment username varchar(20) password varchar(20) posts post_id in primary auto_increment title varchar(100) message text user_id int When I try to add a foreign key to users which refers to posts-

yii null foreign key

孤者浪人 提交于 2019-12-04 18:13:59
I have such structure of db http://pikucha.ru/icFsc (I can't add pictures here) Some addresses my not have a metro (other tables have the same problem, for example "user" may not have an address, but there is constraint in "user" table) If I add a record in mysql it's ok. If I do the same thing using yii I get an error Cannot add or update a child row: a foreign key constraint fails ( address , CONSTRAINT fk_Address_Area1 FOREIGN KEY ( area_id ) REFERENCES area ( id ) ON UPDATE NO ACTION) So, what's the problem in? Rafay Zia Mir This error can occur due to some reasons as mentioned below 1.

Django Model Inheritance And Foreign Keys

三世轮回 提交于 2019-12-04 18:06:11
问题 Basically, I have a model where I've created a superclass that many other classes share, and then each of those classes has some unique features that differ from each other. Let's say class A is the superclass, and class B, C, and D are children of that class. Both class B and class C can have multiples of class D, however I've seen that it's best to put the foreign key relationship in class D, which then refers to its parent class. Now in other languages, I could simply say it has a

mysql alter int column to bigint with foreign keys

吃可爱长大的小学妹 提交于 2019-12-04 17:28:30
问题 I want to change the datatype of some primary-key columns in my database from INT to BIGINT. The following definition is a toy-example to illustrate the problem: CREATE TABLE IF NOT EXISTS `owner` ( `id` int(11) NOT NULL AUTO_INCREMENT, `thing_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `thing_id` (`thing_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; DROP TABLE IF EXISTS `thing`; CREATE TABLE IF NOT EXISTS `thing` ( `id` int(11) NOT NULL AUTO_INCREMENT

Django blog reply system

久未见 提交于 2019-12-04 17:25:45
i'm trying to build a mini reply system, based on the user's posts on a mini blog. Every post has a link named reply. if one presses reply, the reply form appears, and one edits the reply, and submits the form.The problem is that i don't know how to take the id of the post i want to reply to. In the view, if i use as a parameter one number (as an id of the blog post),it inserts the reply to the database. But how can i do it by not hardcoding? The view is: def save_reply(request): if request.method == 'POST': form = ReplyForm(request.POST) if form.is_valid(): new_obj = form.save(commit=False)