For my project I am using oracle database where I have to work with 2 different database schema. Consider the following scenario please -
I have a schema A and in it I have a table table_a with a primary key apk
And I have another schema B and in it I have a table table_b with a primary key bpk
If both of these tables are in a same database then I can easily make a primary key - foreign key relationship.
But can I make a primary key - foreign key relation ship (or something like this) between these two columns - A.table_a.apk and B.table_b.pbk.
Thanks in advance.
To create a foreign key that references an object in a different schema, you just need to qualify the object name
ALTER TABLE B.table_b
ADD CONSTRAINT fk_b_a FOREIGN KEY (apk) REFERENCES a.table_a( apk )
This also requires that the user B has sufficient privileges on A.table_a. The user would need to have the REFERENCES privilege and would, presumably, need to have the SELECT privilege on the table as well.
Related
I have 3 tables in PostgreSQL 9.6:
**table1 :** {id( primary key) ,account_id}
**users :** {id( primary key)} INHERITS (common_table);
**channel:** {id( primary key)} INHERITS (common_table);
I want something like this:
FOREIGN KEY (account_id) REFERENCES (users(id) OR channel(id)) ON UPDATE CASCADE ON DELETE CASCADE
What is the best way?
Not possible. This is a known limitation of Postgres inheritance.
Read the chapter Caveats in the manual, which closes with the statement:
There is no good workaround for this case.
Addressing your case exactly.
If you need an FK constraint enforcing referential integrity like this, you have to ditch inheritance. You could instead have table1 as a "master" table holding the complete key space in its id column. And users as well as channel have their respective id as FK to table1.id.
Possibly add a type column to table1, a (redundant) type column in users and channel and use a multicolumn FK constraint, so that rows in users can only reference type "user" in table1, etc.
Your desired FK constraint could then just be:
FOREIGN KEY (account_id) REFERENCES table1(id) ON UPDATE CASCADE ON DELETE CASCADE
Related:
How to have a foreign key pointing to two primary keys?
Foreign keys referring other foreign keys in PostgreSQL
I came across this T-SQL code in a client's database today:
CREATE TABLE dbo.Dates
(
MarketDate date,
PRIMARY KEY (MarketDate),
FOREIGN KEY (MarketDate) REFERENCES dbo.Dates (MarketDate)
)
It is unclear to me what purpose can be served by allowing the primary key column to have a foreign key relationship back to itself. Naturally I have deleted the relationship.
Would this foreign key have any effect at all? Is there ever a use case which would justify using it? If not then why would SQL Server permit such a relationship.
The only reason I can imagine is that the creator of the table wanted to disallow the use of TRUNCATE TABLE statements against the Dates table.
I have set up my tables like so:
Table A:
Id
[...]
Table B:
Id
AId
[...]
I have a foreign key created as
FK_TableB_TableA where primary key table is Table A and its primary key is Id and the foreign key table is TableB and its foreign key is AId...
However, when I update my dbml for linq TableB is defined as an entityref instead of an entityset...
Adding a foreign key should generate a one-to-many relationship correct?
This is very generic but if I need to add more detail please let me know!
Didn't figure out why this was happening however on the dbml there was no association created between the two tables for whatever reason - therefore I just added the association.
Hi i have a database first EF5 model defined.
My user table has a primary key guid UserGUID
and another key field UserID with an auto-incrementing integer.
I have created a table called UserCustomField which has
UserID and I have created a foreign key constraint to UserID in my User Table.
When I update the model from the database all foreign key relationships to primary keys are generated but none to Key fields. Ignoring the potential point about using the guid through all my tables.....
A/ shouldn't EF add this relationship?
B/ how can i manually add it?
Cheers
Tim
A: No.
B: You cannot.
EF is able to use relations only when they point to primary key so either change User table to use UserID as primary key and remove UserGuid or change UserCustomField table to point UserID to UserGuid in the User table.
The reason why it doesn't work is that your database requires UserID in User table to be marked as unique (that means unique constraint) and EF doesn't support unique constraints yet.
I have an oracle db which has no Foreign Keys, all table relations are handled by the software. For example, table Customer with columns Customer.customercode, Customer.Name where customercode is the primary key and table Order with columns Order.ordercode (PK), Order.customercode where customercode has no foreign key constraint. So far the application handles all transactions and takes care of all the table relations so that the data are consistent. I need to change this to a proper relational DB implementation, so I need to modify Order.customercode to be a FK from table Customer. Any sqlplus statement to do this without losing my data?
In Oracle, creating a foreign key would never lose any data, but it will fail if the data doesn't correspond to the new constraint. Assuming your data is OK, you can use an alter table statement:
ALTER TABLE order
ADD CONSTRAINT order_customer_fk FOREIGN KEY (customercode)
REFERENCES customer(customercode)