What is the difference between foreign key constraint & referential integrity constraint - database

This might be a silly question pardon me if.
I recently faced this question:
Consider the relations R1(A,B,C) and R2(B,P,Q), where A,B,C,P,Q are sets
of attributes. The values of B in R1 must mandatorily exist in R2. This is
an example of
a foreign key constraint
logical data independence
a referential integrity constraint
a domain constraint
Answer: a referential integrity constraint
I cannot distinguish between referential integrity constraint & foreign key constraint

Why we are preferring Referential Integrity Constraint:
According to the reference[1], the referential integrity constraint is the state of a database in which all values of all foreign keys are valid.
In your example, the line “ The values of B in R1 must mandatorily exist in R2” indicates--the child table(R2) contains all values of B from the parent table(R1), means all values of B in R2 are valid -- resulting in Referential integrity constraint.
Again why we are not using foreign key constraint:
According to the reference[2], it is not necessary to always have foreign keys to ensure referential integrity constraints. There are other ways of ensuring the integrity constraint.
Back to your example, there is no mention about foreign keys or primary keys. The question only indicates the reference between two tables. Hence, it would be better to choose the referential integrity constraint instead of foreign key constraint.
References:
https://www.toolbox.com/tech/big-data/question/difference-between-foreign-key-and-refrential-integrity-constraint-091703/
https://www.mssqltips.com/sqlservertip/4242/sql-server-referential-integrity-without-foreign-keys/

Related

What is the difference between participation constraint and referential integrity constraint?

Apologies in advance if this is a silly question. I recently faced this question while revising for databases, so I wish to check if my understanding is correct.
I understand that participation constraint is referring to the number of instances of an entity that can participate in a relationship. (E.g. Total and Partial).
Referential integrity is also a constraint that is related to relationships in a table, stating that a foreign key must reference an existing primary key of another entity.
Based on what I have read, I feel that referential integrity constraint is more about ensuring data integrity with regards to foreign keys, while participation constraint is more about just describing the relationships between two entities.
However, I keep having the feeling that Total Participation and Referential Integrity have a huge similarity. For example, let's say we have two tables
Customer(CustID, CustName)
Order(OrderID, CustID, OrderDate).
As CustID in the Order table must match a valid CustID in the Customer table, there is a referential integrity constraint here. Am I right to say that Order is also totally participating with Customer in a relationship?

Is it necessary to define on a FOREIGN KEY the same constraints that are defined on the PRIMARY KEY (parent table)?

Let's say I create a table Clients. I define a primary key and a set of constraints, such as:
NOT NULL
Length > 5
UPPERCASE
and so on..
Now, I create another table, with a foreign key to Clients primary key.
Should I create the same CONSTRAINTS for the foreign key?
If I don't it wouldn't matter, since the value won't exist on the primary table in the first place:
Example: I don't create the constraints on the foreign key, and I try to add a value which length is lower than 5 characters, and is lowercase... The database will not find that value on the parent table, hence the value will not be recorded, so what is the point of setting the same set of constraints on the foreign table?
At least you should keep the Foreign key column as not null. Otherwise, you can have many NULL values coming into the child table.
Again, as #Dale Burrell, mentioned, PRIMARY KEY should be system generated, to enforce uniqueness. If you are going to create clustered index on the primary key column, it should be narrow, incrementing, not null, unique value for getting good performance.
You don't need the length or uppercase constraints on the foreign key. They'll be "implicitly checked" by the foreign key constraint (as you say, because the referenced data cannot exist).
But for nullability, it's a choice. In SQL Server, if one or more columns in the referencing table of a foreign key are NULL, the constraint isn't enforced.
So here, the question should instead be - are there rows in the other table which should validly not reference a row in the Clients table?
Others have advised that the PK should be system generated. Whilst I agree that it's often useful to do so, don't forget to also enforce the constraints on the real data. E.g. even if this column doesn't end up being your PK, maybe it needs a unique constraint on it to ensure that you don't end up with duplicates in your data.

Does a foreign key need to reference a primary key or candidate key?

I am having two conflicting definitions of foreign key.
From Wikipedia
the foreign key is defined in a second table, but it refers to the primary key in the first table.
From my lecture notes:
Foreign key does not have to match a primary key but must match a candidate key in some relation
Which is which? Does a foreign key need to reference a primary key or candidate key?
In the relational model of data, a foreign key must reference a candidate key.
In almost all SQL dbms, a foreign key must reference a candidate key.
In MySQL, a foreign key can reference just about anything.
Additionally, MySQL requires that the referenced columns be indexed for performance reasons. However, the system does not enforce a requirement that the referenced columns be UNIQUE or be declared NOT NULL.
Emphasis added.
This is a Bad Thing, IMHO.
The rule that a FK must reference a "primary" key was imposed by the older versions of the SQL standard. That rule has been relaxed by now (and it is now permitted for a FK to reference any candidate key), but it may be the case that some products still go by the old rule, just as it may be the case that some textbooks or other sources have not yet been updated to reflect the new state of affairs.
I don't know precisely when the rule was relaxed in the standard, but imo it must certainly have been no later than 2003.
A foreign key must refer to an unique key (a primary key is unique), because if it doesn't, it cans be the reference of 2 lines, and it's just impossible for a foreign key. Then you can have your primary key, but an unique key who is not a primary key and do a foreign key on it. And your unique key must be NOT NULL
Foreign keys have to be linked to candidate keys because, if join between tables was made using foreign key, the FK linked to an attribute that was not a candidate key could result in multiple values being retrieved where only one value should be retrieved.
Practically, the foreign key has nothing to do with the primary key tag of another table, if it points to a unique column (not necessarily a primary key) of another table then too, it would be a foreign key. So, a correct definition of the foreign key would be:
Foreign keys are the columns of a table that points to the candidate key of another table.

Foreign keys referencing superkeys

As far as I know, foreign keys can reference candidate keys of tables. Can a compound foreign key also reference a superkey of a table (in case overdefinition is required)? If possible I am interested in how it could be done in mysql workbench.

Foreign Key Useful in SQLite?

I have two tables 'Elements' and 'Lists'
Lists has a primary key and a list name.
Elements has data pertaining to an individual entry in the list.
Elements needs a column that holds which list the element is in.
I've read about SQL's foreign key constraint and figure that is the best way to link the tables, but I'm using SQLite which doesn't enforce the foerign key constraint.
Is there a point to declaring the foreign key constraint if there is no enforcement?
It's always good to do, even if your database doesn't enforce the constraint (old MySQL, for instance). The reasoning for this, is that someday, someone will try reading your schema (perhaps even yourself).
If you can't use the new version, you can still declare the constraint and enforce it with triggers. In either case, I wouldn't omit the notation. It's far too helpful.
Nowadays sqlite enforces foreign keys, download the new release.
A foreign key is a field (or fields)
that points to the primary key of
another table. The purpose of the
foreign key is to ensure referential
integrity of the data. In other words,
only values that are supposed to
appear in the database are permitted.
It only enforces the "business rule". If you require this from the business side, then yes, it is required.
Indexing will not be affected.
You can still create indexes as requred.
Have a look at Foreign Key
and
Wikipedia Foreign key

Resources