referential-integrity

CONSTRAINT to check values from a remotely related table (via join etc.)

若如初见. 提交于 2019-11-27 05:01:30
I would like to add a constraint that will check values from related table. I have 3 tables: CREATE TABLE somethink_usr_rel ( user_id BIGINT NOT NULL, stomethink_id BIGINT NOT NULL ); CREATE TABLE usr ( id BIGINT NOT NULL, role_id BIGINT NOT NULL ); CREATE TABLE role ( id BIGINT NOT NULL, type BIGINT NOT NULL ); (If you want me to put constraint with FK let me know.) I want to add a constraint to somethink_usr_rel that checks type in role ("two tables away"), e.g.: ALTER TABLE somethink_usr_rel ADD CONSTRAINT CH_sm_usr_type_check CHECK (usr.role.type = 'SOME_ENUM'); I tried to do this with

In MySQL, can I defer referential integrity checks until commit

白昼怎懂夜的黑 提交于 2019-11-27 03:55:15
As in this question , I've been reading PoEAA and wondering if it's possible to defer referential integrity checks until commit in MySQL. I've run into this problem when wanting to insert a bunch of products and related products in the same commit. Even within a transaction, I get constraint errors when I try to insert into the related_products join table. If it helps, I'm using PHP PDO for database connections. I'd appreciate any help you could offer. Ross McFarlane Looks like my answer is here ... Like MySQL in general, in an SQL statement that inserts, deletes, or updates many rows, InnoDB

Foreign key vs check constraint for integrity

混江龙づ霸主 提交于 2019-11-27 02:46:25
问题 I am building a system that is a central repository for storing data from a number of other systems. A sync process is required to update the central repository when the other systems data is updated. There will be a sync_action table to identify which system the central repo needs to sync with and the type of sync required. There are set of defined actions that is very unlikely to change. A slimmed down system is below. As I see it I can approach this in two ways: Option 1 ) Have an Action

Why do Rails migrations define foreign keys in the application but not in the database?

落爺英雄遲暮 提交于 2019-11-27 00:42:12
问题 If I define a Customer and Order model in which a Customer "has many" Orders and the Order "belongs to" the Customer , in Rails we talk about Order having a foreign key to the Customer through customer_id but we don't mean that this is enforced in the database. Because Rails does not define this as a database-level constraint, there is a risk of your data's integrity being violated, perhaps outside the application (or inside if you receive simultaneous requests?), unless you enforce the

MySQL foreign key to allow NULL?

不想你离开。 提交于 2019-11-26 14:26:12
问题 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, ... );

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

大城市里の小女人 提交于 2019-11-26 14:24:38
问题 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? 回答1: 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

CONSTRAINT to check values from a remotely related table (via join etc.)

无人久伴 提交于 2019-11-26 09:53:09
问题 I would like to add a constraint that will check values from related table. I have 3 tables: CREATE TABLE somethink_usr_rel ( user_id BIGINT NOT NULL, stomethink_id BIGINT NOT NULL ); CREATE TABLE usr ( id BIGINT NOT NULL, role_id BIGINT NOT NULL ); CREATE TABLE role ( id BIGINT NOT NULL, type BIGINT NOT NULL ); (If you want me to put constraint with FK let me know.) I want to add a constraint to somethink_usr_rel that checks type in role (\"two tables away\"), e.g.: ALTER TABLE somethink_usr

What's wrong with foreign keys?

瘦欲@ 提交于 2019-11-26 01:38:49
问题 I remember hearing Joel Spolsky mention in podcast 014 that he\'d barely ever used a foreign key (if I remember correctly). However, to me they seem pretty vital to avoid duplication and subsequent data integrity problems throughout your database. Do people have some solid reasons as to why (to avoid a discussion in lines with Stack Overflow principles)? Edit: \"I\'ve yet to have a reason to create a foreign key, so this might be my first reason to actually set up one.\" 回答1: Reasons to use

Create unique constraint with null columns

谁说胖子不能爱 提交于 2019-11-26 00:27:06
问题 I have a table with this layout: CREATE TABLE Favorites ( FavoriteId uuid NOT NULL PRIMARY KEY, UserId uuid NOT NULL, RecipeId uuid NOT NULL, MenuId uuid ) I want to create a unique constraint similar to this: ALTER TABLE Favorites ADD CONSTRAINT Favorites_UniqueFavorite UNIQUE(UserId, MenuId, RecipeId); However, this will allow multiple rows with the same (UserId, RecipeId) , if MenuId IS NULL . I want to allow NULL in MenuId to store a favorite that has no associated menu, but I only want