foreign-keys

MySQL 5.5 foreign key constraint fails when foreign key exists

北慕城南 提交于 2019-11-27 09:17:49
Just installed MySQL 5.5 on mac os x 10.6 and am having a strange issue on many tables. Below is an example. Inserting a row fails with a foreign key constraint when it shouldn't. The foreign key it references does exist. Any ideas? mysql> show create table Language; +----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

MySQL foreign key to allow NULL?

可紊 提交于 2019-11-27 08:59:57
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, ... ); tblImageFlags ( imageFlagID INT UNSIGNED NOT NULL AUTO_INCREMENT, imageID INT UNSIGNED NOT NULL, flagTypeID INT UNSIGNED NOT NULL, resolutionTypeID INT UNSIGNED NOT NULL, ... ); luResolutionTypes ( resolutionTypeID INT UNSIGNED NOT NULL AUTO_INCREMENT, resolutionType VARCHAR(63) NOT NULL,

Delete parent if it's not referenced by any other child

我怕爱的太早我们不能终老 提交于 2019-11-27 08:58:20
I have an example situation: parent table has a column named id , referenced in child table as a foreign key. When deleting a child row, how to delete the parent as well if it's not referenced by any other child? In PostgreSQL 9.1 or later you can do this with a single statement using a data-modifying CTE . This is generally less error prone. It minimizes the time frame between the two DELETEs in which a race conditions could lead to surprising results with concurrent operations: WITH del_child AS ( DELETE FROM child WHERE child_id = 1 RETURNING parent_id, child_id ) DELETE FROM parent p USING

Create association on non-primary key fields with Entity Framework 4.1 Fluent API

别说谁变了你拦得住时间么 提交于 2019-11-27 08:54:35
We are using EF 4.1 and the fluent API to get data from a legacy database (that we are not permitted to change). We are having a problem creating a relationship between two tables where the related columns are not primary and foreign keys. With the classes below, how would we configure the one-to-many relationship between Report and RunStat such that Report.RunStats would return all of the RunStat entities where the ReportCode fields are equal? public class Report { [Key] public int ReportKey { get; set; } public string Name { get; set; } public int ReportCode { get; set; } // Can we associate

How do you enforce foreign key constraints in SQLite through Java?

前提是你 提交于 2019-11-27 08:54:32
It appears that SQLite does not enforce foreign keys by default. I'm using sqlitejdbc-v056.jar and I've read that using PRAGMA foreign_keys = ON; will turn on foreign key constraints, and that this needs to be turned on in a per-connection basis. My question is: what Java statements do I need to execute to turn on this command? I've tried: connection.createStatement().execute("PRAGMA foreign_keys = ON"); and Properties properties = new Properties(); properties.setProperty("PRAGMA foreign_keys", "ON"); connection = DriverManager.getConnection("jdbc:sqlite:test.db", properties); and connection =

SQL Server: Self-reference FK, trigger instead of ON DELETE CASCADE

断了今生、忘了曾经 提交于 2019-11-27 07:59:26
问题 I need to perform an ON DELETE CASCADE on my table named CATEGORY, which has the following columls CAT_ID (BIGINT) NAME (VARCHAR) PARENT_CAT_ID (BIGINT) PARENT_CAT_ID is a FK on CAT_ID. Obviously, the lovely SQL Server does not let me use ON DELETE CASCADE claiming circular or multiple paths to deletion. A solution that I see often proposed is triggers. I made the following trigger: USE [ma] GO /****** Object: Trigger [dbo].[TRG_DELETE_CHILD_CATEGORIES] Script Date: 11/23/2009 16:47:59 ******

PostgreSQL array of elements that each are a foreign key

岁酱吖の 提交于 2019-11-27 07:12:46
I am attempting to create a DB for my app and one thing I'd like to find the best way of doing is creating a one-to-many relationship between my Users and Items tables. I know I can make a third table, ReviewedItems , and have the columns be a User id and an Item id, but I'd like to know if it's possible to make a column in Users , let's say reviewedItems , which is an integer array containing foreign keys to Items that the User has reviewed. If PostgreSQL can do this, please let me know! If not, I'll just go down my third table route. No, this is not possible. PostgreSQL is a relational DBMS,

How to correctly add Foreign Key constraints to SQLite DB using SQLAlchemy [duplicate]

二次信任 提交于 2019-11-27 06:58:23
问题 This question already has an answer here: Sqlite / SQLAlchemy: how to enforce Foreign Keys? 7 answers I'm very new to SQLAlchemy and I'm trying to figure it out. Please have in mind the following test setup: class Nine(Base): __tablename__ = 'nine' __table_args__ = (sqlalchemy.sql.schema.UniqueConstraint('nine_b', name='uq_nine_b'), ) nine_a = sqlalchemy.Column(sqlalchemy.dialects.sqlite.INTEGER(), primary_key=True, autoincrement=False, nullable=False) nine_b = sqlalchemy.Column(sqlalchemy

Hibernate unidirectional one to many association - why is a join table better?

ぐ巨炮叔叔 提交于 2019-11-27 06:53:55
In this document (scroll down to the Unidirectional section): http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-mapping-association-collections it says that a unidirectional one-to-many association with a join table is much preferred to just using a foreign key column in the owned entity. My question is, why is it much preferred? Consider the situation where the owned entity type can also be owned by another parent entity type. Do you put foreign key references in the owned table to both parent tables? What if you have three parent types? It just doesn't scale

Should you make a self-referencing table column a foreign key?

时光毁灭记忆、已成空白 提交于 2019-11-27 05:51:06
问题 For example to create a hierarchy of categories you use a column 'parent_id', which points to another category in the same table. Should this be a foreign key? What would the dis/advantages be? 回答1: Yes. Ensures that you don't have an orphan (entry with no parent), and depending on usage, if you define a cascading delete, when a parent is deleted, all its children will also be deleted. Disadvantage would be a slight performance hit just like any other foreign key. 回答2: Yes, you should. If you