I'm having a relation problem with my database:
When I execute the code on my database manager in order to test it, it draws me an error:
"THERE IS NO UNIQUE CONSTRAINT MATCHING GIVEN KEYS FOR REFERENCED
TABLE compras"
https://dbdiagram.io/d/634e9b02470941019587dd03 << Link to the database ER diagram
Since the table accounts can have many compras and an compras can have many tickets they must be relationed, I'm trying to see how can I solve that constraint problem but I couldn't find the problem until now.
Well, I've found the error, I've put the foreign key in the wrong table it was inverse than I've done, fixed!
Updated ER diagram: https://dbdiagram.io/d/634e9b02470941019587dd03
Related
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.
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.
From some mystical reason I starter the database design with the inbuilt Database Diagrams GUI designer (Server Management Studio), actually I only did the first 2 tables (users and product) there rest were done using query commands.
It turns out that at the end there’s something I didn’t expect between:
users (table)
product(table)
I’ve created a foreign key column (“users_id”) in the “product” table pointing to the “users” table (column “users_id”).
Instead of having a one to many relation It seem to be a one to one relation?
Users table is referencing the product table and I don’t want this.
What is the problem?
edit: 4-sep-2014 10:48
I've droped the FK_product_TO_users constraint and created a new one, but still the results are the same.
ALTER TABLE product
DROP CONSTRAINT FK_product_TO_users
GO
ALTER TABLE product
ADD
CONSTRAINT FK_product_TO_users
FOREIGN KEY (users_id)
REFERENCES users (users_id)
edit: 4-sep-2014 12:51
I've rebuilt the database, by using just Queries with no GUI help in the table design. The problem related to FK_product_TO_users was fixed, still I don't know why.
It comes out that after the resolution the same issue is present in two other tables with 2 FK relations.
Besides this, inputting data in those tables seems to work fine.
I'm wondering if this is just a bug of the GUI in the Database Diagram?
This is really interesting one.
You can do one thing: just delete the key FK_product_to_users and rebuild the key.
You do NOT need to delete users_id from product table.
I was looking for a basic SQL diagram for an e-commerce site and I came across this example: http://addons.oscommerce.com/info/3853/page,23.
There are a lots of tables with composite primary keys but they seem to be constraint with just one foreign key.
This is a capture of one the tables in subject:
products_options_values_to_products_options and products_options_values
schema: http://sdrv.ms/1bIMx52 (compositkey.jpg).
I was trying to constrain the columns products_options_values_id with products_options_values_id
I'm using SQL Server 2012, and I've tried to constrain the composite keys with one fk, clearly SQL Server Management Studio gave me an error, stating:
The columns in table 'products_options_values(products)' do not match an existing primary key or UNIQUE constraint.
or
Both sides of a relationship must have the same number of columns
I obviously agree with the 2 statements, but how come in the diagram the constraint seems possible?
I'd like to replicate the whole database scheme but I would like to understand this point first.
Luther
edit: as pointed out by Allan S. Hansen, I've applied an "unique key" to the primary key and despite what I knew, it's working.
This is a snippet of the mssql diagram:
In context to my comment; this is completely possible in SQL Server:
As long as there's a unique index on column One, Three, then they those two together can be used as FK.
Where to use such a pattern, I'm not sure - because if PK_One can be defined by just One, Three, then I wouldn't think Two should be in that key, but well .... I've not given it much thought and I'm sure something can be thought up.
That's also why you received the error message:
The columns in table 'products_options_values(products)' do not match
an existing primary key or UNIQUE constraint.
Remember, Primary Keys and Unique indexes aren't the same.
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.