foreign-keys

How do i delete a foreign key constraint programmatically in Microsoft Access

How do i delete a foreign key constraint programmatically in Microsoft Access, preferable using SQL. For starters i don't know how to find the name of the foreign key. I connect to Access from a Java application using the JDBC-ODBC bridge. I want to execute the SQL from my Java application. I can see the relationship in Access, in the ...

Foreign keys not working in link table in Linq2Sql

I have 3 tables Subcontract, Company, and a link table CompanyToSubcontract. The link table contains the Subcontract_id and the Company_id. The foreign keys were set-up in SQL and when I drug them into my dbml the one-to-many relationship arrows showed up and everything looked fine. However, when coding, it's as if the relationship is...

Linq to SQL datacontext not updating for foreign key relationships

I'm writing a database test against a repository that uses L2S. In my database I have a Manifest entity and an AllocatedTransaction entity. The AllocatedTransaction entity has a foreign key to the Manifest's id. The DDL looks something like this: Manifest: Id - int - identity AllocateTransaction: Id - int - identity Quantity - int M...

How to move data between multiple database's table while maintaining foreign-key relationships/referential integrity?

I'm trying to figure out the best way to move/merge a couple tables of worth of data from multiple databases into one. I have a schema similar to the following: CREATE TABLE Products( ProductID int IDENTITY(1,1) NOT NULL, Name varchar(250) NOT NULL, Description varchar(1000) NOT NULL, ImageID int NULL ) CREATE TABLE ...

MySQL: A foreign key referencing two parents

Have an interesting situation: I have a foreign key from a.name (child table) to b.name (parent table). b.name does not have a unique constraint on it, so it may contain duplicate values. If I put the name 'bob' in b.name twice, and then put it in a.name, I can no longer delete or update either instance of 'bob' in table b. In both cas...

Foreign key constraint (complex?)

I have a table that manages virtual download folders for a site. CREATE TABLE `folder` ( # id = PK `id` int(10) unsigned NOT NULL auto_increment, # folderId = self referencing FK to id `folderId` int(10) unsigned default NULL, # siteId = FK to id in site table `siteId` int(10) unsigned NOT NULL ) Ideally I like siteId to r...

Multiple-reference foreign keys in table definition?

Summary How do I make it easy for non-programmers to write a query such as the following? select table_name.* , foreign_table_1.name , foreign_table_2.name from table_name left outer join foreign_table foreign_table_1 on foreign_table_1.id = foreign_1_id left outer join foreign_table foreign_table_2 on foreign_table_2.id = fo...

Accessing foreign key value (int) in Entity Framework

Hi there, I just spent the last 3-4 hours trying to retrieve a foreign key value using linq to entities and a stored procedure. Any advice is much appreciated. public JsonResult GetEvents(double? start, double? end) { AnoEntities _dbAno = new AnoEntities(); var events = _dbAno.Events_GetByDateRange(fromDate, toDate...

What is the equivalent effect to Truncating a table, when the table is referenced by a foreign-key

Straight out of the MSDN docs for Sql Server 2005: You cannot use TRUNCATE TABLE on tables that: Are referenced by a FOREIGN KEY constraint. Participate in an indexed view. Are published by using transactional replication or merge replication. I want the effect of a TRUNCATE (specifically the fact that it resets IDENTITY ...

Django: Can the value of ForeignKey be None?

I have a model called SimplePage in which I have this line: category = models.ForeignKey('Category', related_name='items', blank=True, null=True) I assumed this will allow me to have SimplePage instances that do not have a Category. But for some reason, when I try to create a SimplePage in the Admin with ...

need help in primary key and foreign key

Hi all, I need help in auto populating the primary key values in foreign key table while inserting data in foreign key table. For Example: I have created table: create table Patient ( PatientId int IDENTITY(1,1) primary key, FirstName varchar(50), SurName varchar(50), Gender char(20), ) Say 5 rows are there in this Patient Table: Say...

Fixing Foreign Key issues with a legacy database

Tools: SQL2000/5/8, .NET 3.5, C# I have come across an application that has two tables. In “code” the tables look like this: TABLE1 (1——N) TABLE2 So Table1 (T1) has an Id: IdT1 and Table2 (T2) has its id (IdT2) and a foreign key t2.IdT1 This 1.N relationship is code enforced to some degree. There were no FKs in the DB whatsoever. (Wh...

MySQL: Can I constraint column values in one table to values in a column in another table, by DB design only?

Example: Table "persons", Column "surname" may only contain values predefined in Table "names", Column "surnames", which would contain a collection of surnames acceptable for the purpose. Can I achieve this by design (i.e. without involving any validation code)? On a MyISAM table? No? On InnoDB? Thank you. ...

SQL Server: View to retain the foreign keys of the table?

I have a table "Scholars", that has many foreign keys to look-up tables such as Courses and Colleges. When I create a view on that table to return a subset of the Scholars (eg those who are still living), the view doesn't seem to have the same foreign keys as the table. Am I quite new to SQL Server and not sure if I am doing something w...

Do multiple foreign keys make sense?

can it make sense for one table to have multiple foreign keys? Suppose I have three tables, Table A, Table B and Table C. If I think of the tables as objects (and they are mapped to objects in my code), then both Tables A and B have a many to one relationship with Table C. I.e. Table/object A and B can each have many instances of C. ...

EF4: There is no property with name 'CountryId' defined in type referred by Role 'Customer'.

Within my database (SQL2008), I have a customer table and a country table (among others) and there is a foreign key relationship defined in the database between these tables based upon "Country.Id -> Customer.CountryId". I have created an EF model using VS2010 RC and built this model from the database. When generating the model, I selec...

What is a proper naming convention for MySQL FKs?

Being that they must be unique, what should I name FK's in a MySQL DB? ...

Exclusive Or ForeignKey in Django admin interface

In Django is there a way to force admin users to choose to fill one of a few ForeignKeys and not more than one? I have a model something like : class URL(models.Model): ... links = models.URLField(_('Google Links'),verify_exists=True,unique=True) project = models.ForeignKey(Project,blank=True,null=True) category = model...

Force InnoDB to recheck foreign keys on a table/tables?

I have a set of InnoDB tables that I periodically need to maintain by removing some rows and inserting others. Several of the tables have foreign key constraints referencing other tables, so this means that the table loading order is important. To insert the new rows without worrying about the order of the tables, I use: SET FOREIGN...

MySQL, foreign key constraint does not make any difference

I have a database, with several tables. The "main" table, called contacts, stores information about contacts, each having an id, a name etc... I also have like, 20 other tables, each representing a certain function, like staff, media ... Each of these function-tables, references the contact table, with a foreign key. I've recently ad...