foreign-keys

SQL Server foreign key to multiple tables

倾然丶 夕夏残阳落幕 提交于 2019-12-17 20:47:51
问题 I have the following database schema: members_company1(id, name, ...); members_company2(id, name, ...); profiles(memberid, membertypeid, ...); membertypes(id, name, ...) [ { id : 1, name : 'company1', ... }, { id : 2, name : 'company2', ... } ]; So each profile belongs to a certain member either from company1 or company2 depending on membertypeid value members_company1 ————————— members_company2 ———————————————— ———————————————— id ——————————> memberid <——————————— id name membertypeid name /

What is the meaning of self referencing foreign key?

浪尽此生 提交于 2019-12-17 20:46:58
问题 I went over a legacy database and found a couple of foreign keys that reference a column to itself. The referenced column is the primary key column. ALTER TABLE [SchemaName].[TableName] WITH CHECK ADD CONSTRAINT [FK_TableName_TableName] FOREIGN KEY([Id]) REFERENCES [SchemaName].[TableName] ([Id]) What is the meaning of it? 回答1: ALTER TABLE [SchemaName].[TableName] WITH CHECK ADD CONSTRAINT [FK_TableName_TableName] FOREIGN KEY([Id]) REFERENCES [SchemaName].[TableName] ([Id]) This foreign key

SQL join following foreign key: statically check that LHS is key-preserved

不问归期 提交于 2019-12-17 19:58:32
问题 Often you join two tables following their foreign key, so that the row in the RHS table will always be found. Adding the join does not affect the number of rows affected by the query. For example create table a (x int not null primary key) create table b (x int not null primary key, y int not null) alter table a add foreign key (x) references b (x) Now, assuming you set up some data in these two tables, you can get a certain number of rows from a: select x from a Adding a join to b following

Problem adding Foreign Key using Alter Table with existing MYSQL Database - can't add it! Help!

 ̄綄美尐妖づ 提交于 2019-12-17 19:38:49
问题 I have a production database where I have renamed several column's that are foreign keys. Obviously mysql makes this a real pain to do in my experience. My solution was to drop all the indexes and foreign keys, rename the id columns, and then re-add the indexes and foreign keys. This works great on mysql 5.1 on windows for the development database. I went to run my migration script on my debian server, which is also using mysql 5.1, and it gives the following error: mysql> ALTER TABLE

In what scenarios do I need foreign keys AND navigation properties in entity framework

不问归期 提交于 2019-12-17 19:16:24
问题 My Order class has: public int CustomerId { get; set; } public Customer Customer { get; set; } Do I really need both properties to make a relation working? I am not using disconnected entities, I am using code first approach. 回答1: According to Julia Lerman's book: Programming Entity Framework: DbContext, the difference lies at the difficulty of updating the navigation property. In page 85, She suggests "If there is one thing you can do to make your life easier in N-Tier scenarios, it’s to

Why is the foreign key part of the primary key in an identifying relationship?

断了今生、忘了曾经 提交于 2019-12-17 18:39:29
问题 I'm trying to understand a concept rather than fixing a piece of code that won't work. I'll take a general example of a form (parent table) and a form field (child table). Logically , this would be an identifying relationship, since a form field cannot exist without a form. This would make me think that in order to translate the logical relationship into the technical relationship, a simple NOT NULL for the form_id field in the form_field table would suffice. (See the left part of above

SQLite foreign key examples

若如初见. 提交于 2019-12-17 17:38:20
问题 I am not an expert in sql / sqlite.. suppose we have two tables: CREATE TABLE child ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, ); CREATE TABLE MyTableB( dog TEXT, FOREIGN KEY(dogList) REFERENCES child(id) ); how will the INSERT? is correct my createTable operations? I would like to have: a child can have more than one dog a dog can have more children EDIT What if I wanted all the children and for each child a list of dogs associated with that child? 回答1: Many-To-Many In order to

One to Many MySQL [duplicate]

久未见 提交于 2019-12-17 16:37:50
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: MySQL Relationships I am trying to create a one to many relationship in MySQL with foreign keys. Two tables, user and location . Each user can have many location s, but each location can have only one user . How do I configure this? I am using HeidiSQL if that helps, though I can input code as well. 回答1: MySQL does not know, nor does it need to know if a relationship is 1-1, or 1-many. No SQL supports many

Django model with 2 foreign keys from the same table

旧时模样 提交于 2019-12-17 15:27:47
问题 I wanted a Django model with 2 foreign keys from the same table. It's an event table which has 2 columns for employees: the 'actor' and the 'receiver'. But I get this error: Error: One or more models did not validate: tasks.task: Intermediary model TaskEvent has more than one foreign key to Employee, which is ambiguous and is not permitted. Is there a better way to model this? Thanks I think I'm going to add a TaskEvent_to_Employee table. There will be two records in it, one for each of the

SQL DROP TABLE foreign key constraint

血红的双手。 提交于 2019-12-17 14:59:48
问题 If I want to delete all the tables in my database like this, will it take care of the foreign key constraint? If not, how do I take care of that first? GO IF OBJECT_ID('dbo.[Course]','U') IS NOT NULL DROP TABLE dbo.[Course] GO IF OBJECT_ID('dbo.[Student]','U') IS NOT NULL DROP TABLE dbo.[Student] 回答1: No, this will not drop your table if there are indeed foreign keys referencing it. To get all foreign key relationships referencing your table, you could use this SQL (if you're on SQL Server