Oracle (ORA-02270) : no matching unique or primary key for this column-list error: when referencing a materialized view - database

I am getting the ORA-2270 error when I try to create some Oracle tables.
The error is quite simple from what I have read so far:
It happens when the columns I reference in the foreign key do not
match a primary key or unique constraint on the parent table.
The problem is that my tables have several foreign keys, and I am trying to reference a PK column on a materialized view (because I do not have the chance of getting access to the real table, only the MV).
How can I then create my tables and reference them to the correct PKs in the materialized view given that MVs does not have PKs?
And generally talking... What is the common procedure once this problem occurs?
Thanks in advance.

Related

Symmetricds error Unable to reload rows for missing foreign key data for table 'ir_model_fields'

I'm using symmetricds and i have this problem Unable to reload rows for missing foreign key data for table 'ir_model_fields'
Make sure you're syncing all tables that are referenced through foreign keys.

oracle get tables that reference given primary key value

In out DB we have a table called "AGENCY" with AGENCY_ID as primary key (PK). There are also about 30 tables that use AGENCY_ID as a foreign key (FK) which references PK in "AGENCY" table.
Is there a way to count how many times a specific PK value AGENCY_ID (i.e. 1004) is referenced as FK in all of the 30 "referencing" tables without checking each individual linked table.
Referential Integrity exception gets thrown when you try to delete row with PK referenced in another table, so I assume there must be a way to check if references in other tables exist.
I tried looking at all_constraints and all_tab_columns tables, but they do not solve the problem. Any ideas how to solve it? Thanks
I don't think any data dictionary view is available for achieving your goal but you should need to create procedure or function to achieve this goal. Without this you cannot get result of PK value AGENCY_ID (i.e. 1004) referenced or not if referenced then how many times.

Entity Framework won't resolve PK-FK relationship with composite primary key?

I'm trying to set up an EDM on an existing SQL Server infrastructure, and came across a problem.
The EDM will not resolve a PK-FK relationship to a composite foreign key.
My DB table structure looks something like this (names changed to protect the innocent):
I have a PERSONS table containing an INT column called PerID (PK)
I have an OFFICE table containing an INT column called OffID (PK)
I am tying these tables together using a table called OFFICEPERSONS, creating a many-to-many relationship between PERSONS and OFFICE. This table has two INT columns, PerID and OffID, which together form a composite primary key.
I have a table called OFFICELOCATION that contains two INT columns, LocID and OffID. These two columns comprise a composite primary key. Additionally, OffID is also a FK to the OFFICE table.
Finally, I have a table called OFFICEPERSONSLOCATION. This table has three INT columns: PerID, OffID, and LocID. All three columns comprise a composite primary key. LocID and OffID provide a FK relationship to OFFICELOCATION, and OffID and PerID provide a FK relationship to OFFICEPERSONS.
With me so far? Hopefully, I haven't lost you yet. When all is said and done, my structure looks like this:
This structure works great in SQL Server. In EDM? Not so much. It will NOT allow me to construct the relation between OFFICEPERSONSLOCATION and OFFICEPERSONS. I get the following error:
Error 6037: Foreign key constraint 'FK_OFFICEPERSONSLOCATION_OFFICEPERSONS' has been omitted from the storage model. Column 'OffID' of table 'Model.Store.OFFICEPERSONSLOCATION' is a foreign key participating in multiple relationships. A one-to-one Entity Model will not validate since data inconsistency is possible.
Huh? Data inconsistency?!? How?
How do I get my entity framework to recognize this?
I agree that it is the entity framework's problem, and the problem is stupid. Even if you have the UPDATE CASCADE to "no action", it is not like you could create an inconsistency, but no, it claims that you can somehow.
In any case, in this situation, if you are willing to use surrogate keys instead of composite keys, you can get around this, because the only place to change the ID reference is in the main table.
In this case, OffID could be "inconsistent", but by using ID's in the OFFICEPERSONS and OFFICELOCATIONS tables (and therefore reference in OFFICEPERSONSLOCATION), you are forced to have the OffId managed in its primary table.

ForeignKey and fetching performance

Let's consider the following scenario: I've a "master table" with a "detail" table. The detail table have just a foreign key pointing to the primary one ( not a primary key ). This is the schema that NHibernate generates for me when I map a simple bag. My question is, does the FK itself on the detail table suffices to have queryes without full scan? In other world, having or not having the FK defined on the detail table, does change the performance? I guess yes, but I don't know if I'm right, or where to find a source to explain it.
In both cases (FK or no FK) you'll have to create an index on the FK field in the details table to prevent a table scan. In sql server, when creating an FK constraint an index is not created automatically.
See MSDN, "Indexing FOREIGN KEY Constraints".

How can we make foreign key among tables after inserting data in those tables?

I have created tables in sql server. And i have also inserted data/rows in that tables.
Now i want to make relationship among them means i want to create foreign key constraints among them, is it possible ?
Whenever i try to create relationship among table a problem is occured. "Saving changes is permitted, The changes you made required table to re-created and dropped"
Please suggest me what should i do to make relationship(foreign key) among them ?
My Child table design is this
this is my parent table:-
please now right what alter query i should write..?
You can try this link
"Error message when you try to save a table in SQL Server 2008: "Saving changes is not permitted"
Another solution is below.
I think the problem is because of a feature when using the GUI. If you have a look at this link it shows you how to work round it. It is a feature which prevents you from dropping and recreating the table which is what SSMS does in the background when you click ok.
The code provided by the previous posted is the best way to do this.
You could do this with a script like this:
alter table ChildTable
add constraint FK_ChildTable_ColumnName foreign key (ColumnName) references ParentTable(PrimaryKeyColumnName)
[Edit] If I read your description correctly the script would be:
alter table emp
add constraint FK_emp_salary foreign key(salary) references testing(roll)
You can only add foreign constraints that aren't violated by existing data. You may also have to add suitable indices first, although this depends on the DBMS. In any case, first make sure your existing data is compatible with the foreign keys you want to introduce. In other words, if you were to create the foreign key first, and then insert the data, you should not produce any foreign key violations.

Resources