Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [referential-integrity]

Facilities provided by a database management system to ensure consistency within the data.

0
votes
2answers
33 views

How to model very flexible referencing relationships between many tables?

I have various different entity types in my database. For purposes of example, consider an IMDB database: Movie Person Actor Director Translation and 20 more tables My application enables free-form ...
1
vote
0answers
20 views

Redundant column to maintain data integrity

Consider the following example: Syllabus -id -course_id FOREIGN KEY (course_id) REFERENCES Course(id) SyllabusItem -id -syllabus_id FOREIGN KEY (syllabus_id) REFERENCES Syllabus(id) ...
-1
votes
1answer
27 views

Keeping reference in an array format

I have a design with an orders table and products table. Each order can have many products, so 1 order N products. The default design would be to have something like this: orders table order_id, ...
2
votes
2answers
61 views

Enforcing existence of records while avoiding circular references

I'm having issues designing a satisfactory solution for the following problem. There are two main entities: Customer User A customer can have many users associated to it. A user can be associated ...
5
votes
3answers
69 views

Constraining data based on related data in a different table. Should this be done on the database level or the application level?

I am working on a database that contains our customer information as well as information about our vendors. Part of that includes matching our customers with our vendors, the account numbers those ...
0
votes
1answer
69 views

FK to table with non-unique values via a on commit refreshing materialized view?

I would like to create a FK to a table without unique values. So I cannot create a simple FK. [to clarify, the parent table has duplicates - dont ask why, not in my control ] I tried creating a ...
8
votes
2answers
3k views

Constraint - one boolean row is true, all other rows false

I have a column: standard BOOLEAN NOT NULL I would like to enforce one row True, and all others False. The are no FK's or anything else depending on this constraint. I know I can accomplish it with ...
2
votes
2answers
377 views

Conditional table reference

I have a table tree: +------+-------+ | id | name | +------+-------+ | 0 | tree1 | | 1 | tree2 | | 2 | tree3 | | 3 | tree4 | +------+-------+ A table pen: +------+------+ | id | ...
3
votes
0answers
106 views

Orphan records after delete statement in stored procedure

I have a database where some of the tables have parent --> child --> grandchild relationships through foreign keys as shown below. CREATE TABLE Parent ( ParentID INT UNSIGNED PRIMARY KEY ...
2
votes
1answer
53 views

Create foreign key constraint on indirect data

I am trying to enforce relational integrity across some database design containing sensor data. The relevant parts of the database: CREATE TABLE logger ( id INTEGER PRIMARY KEY ); CREATE TABLE ...
1
vote
1answer
21 views

Modelling a second level table relationship

I have three tables in my Database: Customers ---------- Id Name Addresses ---------- Id Description CustomerId Orders ---------- Id Description CustomerId DeliveryAddressId I'd like to make ...
1
vote
3answers
123 views

Determinism inside an INSTEAD OF trigger

In reference to my prior question about a preferred design to avoid circular or multiple update path referential integrity cascading updates and deletes via ON UPDATE CASCADE and ON DELETE CASCADE, I'...
5
votes
2answers
498 views

Preferred design to avoid circular/multiple update paths

Consider the following design: CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL ...
1
vote
4answers
156 views

Referential integrity when two FK columns need to match themselves in the same table?

It's possible my design is wrong, or there's simply a better way. I'll use a very simple example: --------- dbo.Book---------- | | | BookID int identity (1,1) | ...
1
vote
1answer
131 views

How do I set up primary and foreign keys referencing two different tables?

I need to have two primary keys also to be the foreign keys in the same table. These two keys are referenced in different tables. I keep getting error messages that are unclear to me. My code is as ...
1
vote
1answer
55 views

Is there another source for untrusted foreign keys beside FK disable and BULK inserts?

This question and its answer explain how bulk inserts will render FK as untrusted, as bulk inserts are not completely checked (only PK and UNIQUEs) . I also know that temporarily disable of a FK may ...
8
votes
2answers
992 views

Is having relationships between separate databases bad practice?

I am working with a client that has multiple databases. There are several master level databases that have relationships back to them from instance level databases (application specific DBs). The ...
3
votes
2answers
2k views

the REFERENCES privilege is only about creating a foreign key constraint? Practical use cases?

Today I learned about GRANT REFERENCES. In years of SQL admin and dev work I never heard about it and never had issues with it. quote from MySQL 5.7 Reference Manual / GRANT Syntax REFERENCES ...
1
vote
2answers
525 views

Deleting would-be orphans when creating FK in Postgres

I am working on a script that replicates schema and data. I first create all the structure except for foreign keys after reading it from the source's system tables, then copy the data, then create the ...
0
votes
1answer
195 views

Check integrity between servers

I have three Database servers, running PostgreSQL 9.2: master01 (Own data center USA) slave01 (Own data center USA) slave02 (AWS Australia) Both slaves use streaming replication + ...
9
votes
4answers
23k views

DELETE statement conflicted with the REFERENCE constraint

My situation looks like this: Table STOCK_ARTICLES: ID *[PK]* OTHER_DB_ID ITEM_NAME Table LOCATION: ID *[PK]* LOCATION_NAME Table WORK_PLACE: ID *[PK]* WORKPLACE_NAME Table INVENTORY_ITEMS: ID ...
0
votes
1answer
111 views

Simple way to list order of referential integrity in Postgres

Is there a clean, simple, and preferably prepackaged (ready function?) way to list the order of referential integrity in a Postgres database, rather than trying to read the system tables to find that ...
0
votes
1answer
233 views

How do triggers and check constraints relate to maitaining database rules

I was looking at a tutorial on triggers for SQLite. The tutorial said that a use of triggers could be for data validation. The following code was given as an example: CREATE TRIGGER ...
1
vote
1answer
481 views

Make a boolean column unique per group

I want to ensure consistency and make sure only one phone line is the default for a house. Schema: PhoneLine ID | isDefault | HouseID I want to add a trigger so that when you add/update a row, and ...
1
vote
2answers
132 views

DB Design. Tracking history without losing referential integrity

To make it simple, let’s say we have these 2 tables: CREATE TABLE `tbl_companies` ( `id` int(11) NOT NULL, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE `tbl_workers` ( `...
2
votes
1answer
176 views

Optional Parent-Child relationship

I have a problem modeling a relationship whereby the child may or may not be related to a parent. For example. You have a Band database with a Tour(parent) table, and a Gig/Concert(child) table. Now, ...
2
votes
0answers
75 views

Visualize the effects of a cascading delete or update?

In MySQL, I'm working with a database that has integrity constraints on foreign keys (nothing overly special here). Is there a method to somehow visualize the effects of a cascading delete or update? ...
1
vote
0answers
52 views

Can I create a referential integrity constraint in SQL Server that prevents time overlaps? [duplicate]

It is trivial to create a Constraint on uniqueness for an integer value, but I am wondering if it is possible to create a constraint on overlap/intersection of a range of values. For example, my ...
4
votes
1answer
5k views

Foreign key for array column [duplicate]

I have a table: CREATE TABLE methods ( method_id serial PRIMARY KEY, method_name varchar(100) ); I now want to create a table with the following columns: CREATE TABLE experiments ( ...
3
votes
3answers
1k views

SELECT + INSERT or INSERT alone

I have an application where several devices send data every second to my server. On the database I have a table with the devices and another table with the data sent every second. The first time a ...
1
vote
0answers
2k views

How do I disable constraint checking during schema migration?

How do I disable FOREIGN KEY checking for the time of database schema migration? Currently I found engine-based commands like: PostgreSQL: SET CONSTRAINTS ALL DEFERRED; MSSQL: EXEC sp_msforeachtable ...
3
votes
1answer
36 views

Integrity Issues in this structure?

Imagine the situation. There are three tables, Student table[PK Student ID] Enrollment Table [PK EnrollID, FK Student ID] Fees Paid [PK Fee ID, FK EnrollID, FK StudentID] Edit: Fee_Paid Table ...
1
vote
0answers
400 views

Constraint across multiple tables

I'm modeling a system where with vendors, each of which sells some overlapping subset of products available in the universe. Products (T-shirt) come in various variants (Small, Medium, Large) with ...
4
votes
2answers
2k views

“Merge” two rows in a Postgres table, with foreign keys

I am keeping a database of books I've read, using the following two tables in PostgreSQL: CREATE TABLE authors ( id SERIAL PRIMARY KEY, name text ); CREATE TABLE books ( id SERIAL ...
2
votes
0answers
66 views

Any drawbacks to referencing multiple tables from a single column?

I'm working on a system with the following entities: Projects Sections Activities There is one important constraint that needs to be enforced: Activities are always part of one project. Activities ...
2
votes
1answer
54 views

Finding a good DB design for my project

I've posted this on stackOverflow but this might be a more suitable place. I'm really struggling to find a good DB design for my project using SQL server. I've already implemented a few models which ...
22
votes
3answers
4k views

Constraint to enforce “at least one” or “exactly one” in a database

Say we have users and each user can have multiple email addresses CREATE TABLE emails ( user_id integer, email_address text, is_active boolean ) Some sample rows user_id | email_address ...
1
vote
1answer
58 views

How can i enforce data integrity in this setup? [duplicate]

I have these tables: |-------------tblSchool -------------| | + ID | | + Name | | | | ...
3
votes
1answer
290 views

What is the correct way to map relationships between structurally similar comments tables and commentable entities?

I'm working on the usual comments table in postgres that has a one-to-many relationship with a commentable entity. In other words, one comment belongs to a post, and a post can have many comments. ...
2
votes
0answers
54 views

SQL Server 2005 Datawarehouse DB - Integrity failure caused by NonClustered indexes

Last year I've been working with a data warehouse database (SQL Server 2005) loaded on a daily basis during the nights. Overall database performance is acceptable. However, there is a "known error" ...
1
vote
2answers
79 views

Referencing a single table from all tables in the database

At current, I have around 130 tables in my database, anticipating a growth of circa 200 tables in total. Now I have several tables (well, most of them, actually) that contain properties like ...
10
votes
3answers
45k views

Cascade primary key update to all referencing foreign keys

Is it possible to update a primary key column value with cascading the update among all the foreign keys referencing it ? # EDIT 1: When I run followinq query select * from sys.foreign_keys where ...
1
vote
0answers
72 views

Enforce referential integrity for FriendRequest table that has UserA and UserB

I have a table called User that has UserName as a primary key. I want to establish a relationship with enforced referential integrity between User and FriendRequest. FriendRequest has the following: ...
0
votes
1answer
2k views

Foreign keys can't be added because they exist but they don't exist!

I have a conundrum. I am re-engineering a sqlserver 2005 database, and in the course of doing so have attempted to correct a simple lookup table flaw between tblUsers, tblUserRoles, and tblRoles (...
2
votes
1answer
3k views

ON DELETE CASCADE not working

Trying to implement a simple user follower system in MySQL, using a junction table: CREATE TABLE users ( id int NOT NULL AUTO_INCREMENT, email varchar(255) NOT NULL, username varchar(25) ...
1
vote
1answer
42 views

is there some way to give me the order in which inserts should be made so all FK constraint are met? [closed]

I have a mysql 5.6 schema with about 60 tables. I need to write some code (not sure if it will be Sql or java) to populate the data. The schema has lots of FK etc. Is there some good way (I am ...
4
votes
2answers
1k views

Foreign key without referenced columns specification

I noticed in the SQL Server documentation that the list of referenced columns is not a required parameter of a foreign key constraint: <column_constraint> ::= [ CONSTRAINT constraint_name ]...
4
votes
1answer
2k views

Delete rows while updating references

Suppose I have a person table with an id and some other columns. Other tables reference the person table using ON UPDATE CASCADE. for instance: CREATE TABLE person (id int PRIMARY KEY); CREATE TABLE ...
1
vote
3answers
174 views

Best approach for Legal/Natural can have many Orders

I'm working on this model: where as you may see one NaturalPerson can have many Order and the same for LegalPerson the only difference between NaturalPerson and LegalPerson are a few so in that case ...
4
votes
5answers
646 views

How do I prove that the act of removing foreign keys doesn't corrupt existing data?

TL;DR: Prove that in practice, the execution of the alter table table_name drop foreign key constraint_name statement does not corrupt existing data. The important consideration is the execution of ...