foreign-keys

MySQL: How to determine foreign key relationships programmatically?

Similar to this question but for MySQL.... How can I programmatically determine foreign key references in MySQL (assuming InnoDB)? I can almost get them with: SHOW TABLE STATUS WHERE Name = 'MyTableName'; ...but alas, the comment column which seems to contain some of this info gets truncated so I can't rely on it. There must be som...

Are foreign keys indexed automatically in SQL Server?

Would the following SQL statement automatically create an index on Table1.Table1Column, or must one be explicitly created? Database engine is SQL Server 2000 CREATE TABLE [Table1] ( . . . CONSTRAINT [FK_Table1_Table2] FOREIGN KEY ( [Table1Column] ) REFERENCES [Table2] ( ...

SQL : one foreign key references primary key in one of several tables

I am working on an application that will be used as an extensible framework for other applications. One of the fundamental classes is called Node, and Nodes have Content. The SQL tables look like this: TABLE Node ( NodeId int, .... etc ) TABLE NodeContentRelationship ( NodeId int, ContentType string, ContentId int) It will be up to t...

Disappearing Foreign Keys in phpMyAdmin

I am creating a new table inside mysql and I am trying to add a foreign key constraint to one of the fields. CREATE TABLE `onlineorder` ( `receiptid` varchar(10) NOT NULL default '', `delivereddate` date default NULL, `cid` int(10) NOT NULL, `card#` int(10) default NULL, `expire` date default NULL, PRIMARY KEY (`receiptid`...

LINQ to SQL - Nullable INT in ForeignKey = "Cannot create an association..."

I have a table that has a primary key that's an INT... I have another table, that has a foreignkey relationship to that first table, but it's a NULLABLE INT. This is perfectly ok, and 100% acceptable to SQL... however LINQ to SQL is complaining about mismatched types ("int to Nullable[int]"). Error Message: Cannot create an association...

MySQL terminology "constraints" vs "foreign keys" difference?

I'm looking at the MySQL docs here and trying to sort out the distinction between FOREIGN KEYs and CONSTRAINTs. I thought an FK was a constraint, but the docs seem to talk about them like they're separate things. The syntax for creating an FK is (in part)... [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) R...

MySQL -- create two indexes, only one appears in SHOW CREATE TABLE output

In have a many-to-many linking table and I'm trying to set up two foreign keys on it. I run these two statements: ALTER TABLE address_list_memberships ADD CONSTRAINT fk_address_list_memberships_address_id FOREIGN KEY index_address_id (address_id) REFERENCES addresses (id); ALTER TABLE address_list_memberships ADD CONSTRAINT fk_address_...

Support for foreign key constraint in Rails

In Ruby on Rails, how to add foreign key constraint in migration? ...

MySQL Composite PK with Nullable FKs

First off, let me preface this question by stating that I'm really a pretty terrible data modeler. I know only enough to be dangerous. The table I'm building has four foreign keys, two of which reference the same table. Here's the create statement for that table. CREATE TABLE IF NOT EXISTS `abnr`.`reputation_event_log` ( `id` INT ...

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

I've got a couple django models that look like this: from django.contrib.sites.models import Site class Photo(models.Model): title = models.CharField(max_length=100) site = models.ForeignKey(Site) file = models.ImageField(upload_to=get_site_profile_path) def __unicode__(self): return self.title class Gallery...

Get access to ForeignKey objects at parent save in Django

Hi, I am trying to make a combined image of all images added to a modell in django with inline editing and a ForeignKey. Ive got these models (simplified): class Modell(models.Model): title = models.CharField('beskrivelse', max_length=200) slug = models.SlugField() is_public = models.BooleanField('publisert', default=True)...

Unindexed Foreign Key leads to TM Enqueue Contention

So we've been told that one source of TM Enq contention can be unindexed FK's. My question is which one. I have an INSERT INTO Table_B that is recording TM Enq Wait. It contains a PK that is the parent to other tables and it has columns that are FK constrained to other PKs. So which FKs need indexed. That table's columns or its childr...

Should I use SQL triggers?

Hello, I am implementing a data base design that has a vehicle table, vehicle engine and vehicle gear table with SQL 2005. Each table has an ID that is a SQL identity number, and each engine and gear has a relation with the vehicle ID. So before I create a vehicle I must create an engine and gear. How could I know the vehicle identity...

How to design a database schema to support tagging with categories?

I am trying to so something like Database Design for Tagging, except each of my tags are grouped into categories. For example, let's say I have a database about vehicles. Let's say we actually don't know very much about vehicles, so we can't specify the columns all vehicles will have. Therefore we shall "tag" vehicles with information...

Foreign key not working in MySQL: Why can I INSERT a value that's not in the foreign column?

I've created a table in MySQL: CREATE TABLE actions ( A_id int NOT NULL AUTO_INCREMENT, type ENUM('rate','report','submit','edit','delete') NOT NULL, Q_id int NOT NULL, U_id int NOT NULL, date DATE NOT NULL, time TIME NOT NULL, rate tinyint(1), PRIMARY KEY (A_id), CONSTRAINT fk_Question FOREIGN KEY (Q_id) REFERENCES questions(P_id), CON...

SELECT on a nullable reference

Hello, I have a relationship between two tables, authors and styles. Every author is associated with a style, with the special case where an author doesn't have a style (IS NULL). There's no problem in setting the reference to NULL, but there's a problem doing a query to select the authors and styles. For example, the query: SELECT ...

C# MySqlCommand Multiple Drop Foreign Key problem

I'm hoping someone can help me with this problem. I have a small app to 'patch' our DB as we need to augment the functionality and fix bugs. It reads in the patches which are just files with SQL statements and does some internal housekeeping, querying the DB and applying the patches that haven't been applied. Anyhow, this is the solut...

How to get to Foreign keys in the ADO.NET entity model?

I have 3 tables (and corresponding entities in the entity model) Game: Id - primay key ... other columns Player: Id - primary key ... other columns GamePlayer (a player can participate in many games) GameId --> foreign key from Game PlayerId --> foreign key from Player ... other columns In my code, I have gameId and playerId available...

Django form - in-place edit of data from reverse foreign key join

I have a Person table and Phone table. The Phone table has a foreign key into the Person table, which is an auto-increment ID. Each person can have an arbitrary number of phone numbers. Is there a way for me to create a Django form to enter phone numbers while creating a new Person entry and edit them along with an existing Person ent...

MySQL foreign key to allow NULL?

Hi there, I'm piecing together an image website. The basic schema's pretty simple MySQL, but I'm having some trouble trying to represent possible admin flags associated with an image ("inappropriate", "copyrighted", etc.). My current notion is as follows: tblImages ( imageID INT UNSIGNED NOT NULL AUTO_INCREMENT, ... ); tblImag...