SQL delete and dependency - sql-server

Anyone know of a tool or script to delete data from a table and have it also deleted foreign key related rows?
This database is massive with lots of relationships. We are looking for an automated approach.
Cascade delete is not enabled. Many tables have multiple foreign key constraints.
Thanks!
p.s. I have seen many SO posts that all suggest enabling cascade delete or how to do this with very simple one or two table related entities. We have a very complex large table with many relationships.
Tried these and they did not work as expected. We still saw referential constraint errors.
https://social.technet.microsoft.com/wiki/contents/articles/2958.script-to-create-all-foreign-keys.aspx
https://blog.sqlauthority.com/2014/12/02/sql-server-how-to-disable-and-enable-all-constraint-for-table-and-database/

I was also looking for the same solution. I think this might work for you.
check out the Deleting Specific SQL Server Records with Foreign Keys portion on this page there you will get a script which can be use to print all the delete query which you need to fire to remove all the depended record from your database. And to execute that query's replace print with exec and your all depend rows will be removed from your complex database :)

Related

How to Add Foreign key (FK) on Legacy database that has no FK but only Query join

I have an old database that is live in production, and this database does not have a Foreign key and without documentation.
The relation between tables is done directly in stored procedure using query with inner join.
the question is
how do I add Foreign key in the tables that has relations in this database.
I hope I don't have to analyze one by one stored procedures to get these table relationship. and I don't know the sequence of store procedures that need to be analyzed
Please advise.
thank you
this time I analyze stored procedures and record the relationships between the tabs but this method is frustrating.
I look for other ways to get relationships between tables other than analyzing one by one stored procedures.

How to turn off foreign key constraints permanently on SQL database?

Running EXEC sp_msforeachtable #command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL" will disable Foreign keys on existing tables.
What if the tables and insert data queries that enforce foreign key constraints run after this query,?
I am encountering this issue during build automation and What I am ideally look for is a permanent switch to disable all constraints on the database (i can do that since the database is created as a part of build process).
NOTE: See the 5 steps mentioned towards the last to get an idea of the issue faced during build automation
I have created a build step before processing the scripts to disable all existing foreign key constraints. The next step would be package and run all release sql scripts that may contain tables created, data inserted. The earlier build step to disable constraints have no clue about forth coming database tables and insert scripts which will enforce foreign key constraints after running the data insert, failing my build process.
Is there a way i am set a flag in the database to stop checking for foreign keys?
Adding some more context to what i am doing specifically.Automating build using bamboo and following steps are performed on a high level
locate last available deployed db schema
build a database using the schema generated script (no master data copied).
disable all foreign keys (unable to disable FK for tables yet to be created in next step)
merge all release specific db scripts(may contain new db and insert scripts)
apply other transformations like running codegeneration, script compare, delta finding etc.
Step 3 is the challenge.
Note: This is automating a legacy system with 300ish master datables and data, since Codesmith tools are used, schema changes has to be detected and auto generated code has to be checked against last deployed schema. Since the master data is so huge, keeping a reference db with data for build purposes is out of the question hence the referential integrity constraint issue will be more prominent.
The only thing I can think of is to create a DDL trigger which listens for constraints' creation and, if any are detected, drops them. However, I'm not sure this approach is viable if a constraint is created as a part of the create table statement. You should test it thoroughly before using.
Personally, however, I usually solve this by properly ordering the sequence in which the data is inserted. It's much safer, not prohibitively difficult and, last but not least, always possible to do.
Your basic problem is that your database migrations that are creating your database are running in the wrong order. Adjust the order of tables and data insertion so that only data that references already existing data, is inserted at any one time
Turning all the constraints off, loading data, and turning them all back on at the start and end of each script that does DB data alterations, is also an option, but you should separate your scripts that do schema changes from your scripts that do data loading and run all the schema changes first

PostgreSql propagate changes across databases

I'm a newbie to databases and i'm facing what seems a simple problem.
i have and old database db_a contains a table table_a and i want to use this table in a new database db_b.
i found out that Referential integrity across databases in PostgreSql is not a good practice. So the solution was to copy the table table_a to db_b and use referential integrity.
So far so good!
The problem now is: i want to always update the new table in db_b with any changes in the old table in db_a. So the two tables will remain similar all over the time.
What is the best solution for such a classic issue?
The classic solution would be to use multiple schemas (as in create schema...) instead of multiple databases. Foreign key references, including on update cascade and on delete cascade just work. You can alter default privileges for each schema if you need to.

Foreign key constraint unavailable in DDL Script generated by enterprise architect

Modeled numerous tables for a database. Each table has got its primary key and Foreign Key(s). But when i'm generating DDL scripts for the tables, foreign key constraints are unavailable even if model diagram shows constraints it has.
As its solution i've already tried out all those check marks available we could use while generating DDL.
What i believe i must have left any other configuration or followed incorrect sequence of steps for the same.
Possibles solutions will be welcomed. Else i will have to add constraints manually which shouldn't be the standard if you're using a dedicated tool.
See the images for more clarification

Transfer several joined tables from one database to another using SSIS?

I have several tables in my database A which are interconnected via foreign keys and contain values. These values need to be transfered to another database B, all dependencies must be preserved, but the actual (numeric) values of primary and foreign keys are, of course, of no importance.
What would be the easiest way to fulfill this task using SSIS?
Here are the approaches I tried but with no much success:
I implemented a really very sophisticated view with flattened data and a lot of redundancy in the data and bumped into the problem how to split the data from this flattened view into several tables connected via foreign keys. This might be a solution, but I would personally prefer to avoid the data flatenning step if possible.
I tried to copy the tables one-to-one using NOCHECK options to lift up the constraint checks and to perform insertion into PK and FK fields. This, however, confines my transfer to a complete new import, I cannot just "add" some new data to existing set of data that would be nice.
Any other suggestions?
Integration Services has a Control Flow called Transfer Database Task and Transfer SQL Server Objects Task exclusive for what you need.
Here is a tutorial for what you need LINK.

Resources