foreign-key-relationship

Foreign key referencing a 2 columns primary key in SQL Server

风流意气都作罢 提交于 2019-11-27 01:15:32
问题 This question is pretty much similar to this one, but for SQL Server 2005 : I have 2 tables in my database: --'#' denotes the primary key [Libraries] #ID #Application Name 1 MyApp Title 1 2 MyApp Title 2 [Content] #ID Application LibraryID Content 10 MyApp 1 xxx 11 MyApp 1 yyy (the database is obviously much more complex and having this double key makes sense) Each library is identified by its unique ID and Application name. I'm trying to ensure that each content is properly referencing an

Foreign key relationship with composite primary keys in SQL Server 2005

别说谁变了你拦得住时间么 提交于 2019-11-27 01:14:06
问题 I have two tables Table1( FileID, BundledFileID, Domain) and Table2( FileID, FileType, FileName) In Table2 FileID and FileType are the composite primary key. I want to create a foreign key relationship from Table1.FileID to Table2 . Is it possible to do this? 回答1: Since Table2 has a composite primary key (FileID, FileType) , then any reference to it must also include both columns . ALTER TABLE dbo.Table1 ADD CONSTRAINT FK_Table1_Table2 FOREIGN KEY(FileID, FileType) REFERENCES Table2(FileID,

Defining multiple Foreign Key for the Same table in Entity Framework Code First

帅比萌擦擦* 提交于 2019-11-27 00:14:43
I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as above an extra column is created named City_ID in the Student table after migration. Id there a mistake or how to define these FKs? Thanks in advance. Student: public class Student { public int ID { get; set; } public string Name { get; set; } public string Surname { get; set; } public int BirthCityID { get; set; } public int LivingCityID { get; set

How to Implement Referential Integrity in Subtypes

痞子三分冷 提交于 2019-11-26 22:36:09
I have the following tables in a relational database: [Sensor] LocationId [PK / FK -> Location] SensorNo [PK] [AnalogSensor] LocationId [PK/FK -> Sensor] SensorNo [PK/FK -> Sensor] UpperLimit LowerLimit [SwitchSensor] LocationId [PK/FK -> Sensor] SensorNo [PK/FK -> Sensor] OnTimeLimit [Reading] LocationId [PK/FK -> Sensor] SensorNo [PK/FK -> Sensor] ReadingDtm [PK] [ReadingSwitch] LocationId [PK/FK -> Reading] SensorNo [PK/FK -> Reading] ReadingDtm [PK/FK -> Reading] Switch [ReadingValue] LocationId [PK/FK -> Reading] SensorNo [PK/FK -> Reading] ReadingDtm [PK/FK -> Reading] Value [Alert]

The better way to pass the foreign_key value to the Rails controller

六月ゝ 毕业季﹏ 提交于 2019-11-26 22:25:21
问题 It's been almost a week since I've began to dig deeper in forms , associations , hashes , symbols... But it seems I cannot solve the puzzle without your help . I am working on a project for displaying different galleries content . The basic idea is when the user sees the names of galleries (names are links ) to be able to click on chosen one. Then all the images ,that belong to this gallery , are displayed . On the bottom there should be a link "Add image in this gallery" . My models : class

Entity framework code-first null foreign key

你离开我真会死。 提交于 2019-11-26 21:57:38
I have a User < Country model. A user belongs to a country, but may not belong to any (null foreign key). How do I set this up? When I try to insert a user with a null country, it tells me that it cannot be null. The model is as follows: public class User{ public int CountryId { get; set; } public Country Country { get; set; } } public class Country{ public List<User> Users {get; set;} public int CountryId {get; set;} } Error: A foreign key value cannot be inserted because a corresponding primary key value does not exist. [ Foreign key constraint name = Country_Users ]"} You must make your

Does Rails need database-level constraints?

て烟熏妆下的殇ゞ 提交于 2019-11-26 20:01:12
问题 I have the same problem as in the following post. So I am wondering, why doesn't Rails support generating foreign keys by default? Isn't it necessary? Or are we supposed to do it manually? 回答1: Database constraints aren't required any more than wearing seat-belts are required in your car. You can drive around all you like and everything will work great until a problem arrives. The seat-belt (constraints) keep you (the data) safe. So it's highly recommended that you create constraints to

POSTGRESQL Foreign Key Referencing Primary Keys of two Different Tables

て烟熏妆下的殇ゞ 提交于 2019-11-26 19:30:24
问题 I have two tables Books and Audiobooks, both of which have ISBN as their primary keys. I have a table writtenby that has an isbn attribute that has a foreign key constraint to Books and Audiobooks ISBN. The issue that comes up when I insert into writtenby is that postgresql wants the ISBN I insert into writtenby to be in both Books and Audiobooks. It makes sense to me to have a table writtenby that stores authors and the books/audiobooks they have written, however this does not translate to a

Any example of a necessary nullable foreign key?

限于喜欢 提交于 2019-11-26 16:07:59
问题 Customers customer_id Orders order_id customer_id fk If I have two tables and define a foreign key on customer_id in the Orders table, by allowing it to be null I am saying that I can have an order that does not have a customer associated with it. As such, the notion of a nullable foreign key seems at odds with the purpose of a foreign key, which is to enforce this constraint. Is there a simple example of a situation in which a nullable foreign key would be necessary? Or an argument in favor

Multiple column foreign key contraints

寵の児 提交于 2019-11-26 14:47:03
问题 I want to setup table constraints for the following scenario and I’m not sure how to do it or if it’s even possible in SQL Server 2005. I have three tables A,B,C. C is a child of B. B will have a optional foreign key(may be null) referencing A. For performance reasons I also want table C to have the same foreign key reference to table A. The constraint on table C should be that C must reference its parent (B) and also have the same foreign key reference to A as its parent. Anyone have any