Currently IMPORT FOREIGN SCHEMA does not seem to support IF NOT EXISTS so how to solve this case? If I have a script that runs on every reboot for example I would get
ERROR: relation "xxx" already exists
Is there a way to overcome this?
Use the EXCEPT clause to exclude those tables that are already there:
IMPORT FOREIGN SCHEMA xy EXCEPT (xxx, yyy, zzz) FROM ...
Alternatively, delete all foreign tables first. Creating them again shouldn't take too long.
Related
I'm trying to introduce a foreign key with ON DELETE SET NULL into a database with a complex schema.
The script I'm using has this command (names changed):
ALTER TABLE a
ADD CONSTRAINT FK_a_b
FOREIGN KEY (b_id)
REFERENCES b (b_id)
ON DELETE SET NULL;
When I run this script I get the following error message:
Introducing FOREIGN KEY constraint 'FK_a_b' on table 'b' may cause
cycles or multiple cascade paths.
I can't see what the multiple paths are, is there some way I can get SQL Server to list them?
You can check dependencies as outlined here:
https://msdn.microsoft.com/en-us/library/ms190624.aspx
When you use the T-SQL as shown, you get only one level, but if you use SSMS, it will cascade down multiple levels.
There's no built-in functionality just for that purpose. I suppose that with recursive CTE you could write a query on sys.foreign_keys and couple of other system views, that would list cascade paths.
But simplest solution is to do that with Database Diagrams. Start by adding the table that's not letting you add the constraint, then right click and select "Add Related Tables". Then select all the tables and add related tables again, and so on. To keep the diagram clean, in the process unselect or remove tables that you know are not part of the problem.
After you're done, you'll be able to see cycles/multi cascades: they will be chains of 1-N (direcion matters) or 1-1 relations starting and ending on the same table, or leading from one table to another through different paths.
You could use code similar to below to exlude tables that don't have any FK constraints with update/delete actions other than NO_ACTION (0). Building on that you could also exlude tables with only one FK constraint of this type.
select
object_name(parent_object_id),
*
from sys.foreign_keys fk
where 1=2
or delete_referential_action <> 0
or update_referential_action <> 0
I know it may be time-consuming especially for big databases, and you have to know your db well, but that's my best shot for now :).
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 have one filemaker database for my own use that I update regularly from a second filemaker database (which is active everyday).
I have a simple script that imports all data from active database into my own database. Both database are identical - just differ in records.
The problem I have is for tables which are used as portals. This data, when imported, shows the correct number of rows but only as duplicates of the last row.
This is my import script:
http://farm9.staticflickr.com/8165/7351115686_d7efbac90e_b.jpg
And this is the original table (left) and the table on my database after import (right):
http://farm8.staticflickr.com/7229/7351115740_c6677dfee5_b.jpg
Has totally thrown me - what am I doing wrong?
any help will be hugely appreciated,
Best
Steve
It looks like you're matching based on the foreign key, not the primary key. when you use the foreign it's matching as it iterates thru the lines and the last one is the one that stays.
I'm attempiting to use cx_OracleTool's CopyData.py script to copy data between two tables on separate Oracle schemas/instances:
http://cx-oracletools.sourceforge.net/cx_OracleTools.html
When I run it against my tables, I get the error:
No primary or unique constraint found on table.
I don't know much about Oracle, to be honest, but from what I can tell the tables don't seem to have any PK constraint or anything like that defined.
The merits of this aside, I think it's simply been setup that way for expediency, and it's unlikely to change anytime nearterm.
Is there any way to get copyData.py to run in this scenario without a PK constraint?
Cheers,
Victor
The issue is that CopyData checks to see if the row exists in the destination table, and it can't do that without a unique key.
If it is acceptable to insert all rows and not update changed ones, use the --no-check-exists option. According to the code this will bypass the primary key check.
Otherwise, use the --key-columns=COLS option to manually specify the columns to be used as the unique key. This will also bypass the primary key check.
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.