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.
Related
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 :)
How do I store data that is shared between databases?
Suppose a database for a contact management system. Each user is given a separate database. User can store his/her contacts' education information.
Currently there's a table called School in every database where the name of every school in the country is stored. School table is referenced as a FK by Contact table.
School table gets updated every year or so, as new schools get added or existing schools change names.
As the school information is common across all user databases, moving it into a separate common database seems to be a better idea. but when it's moved to a separate database, you can not create a FK constraint between School and Contact.
What is the best practice for this kind of situation?
(p.s. I'm using SQL Server if that is relevant)
Things to consider
Database is a unit of backup/restore.
It may not be possible to restore two databases to the same point in time.
Foreign keys are not supported across databases.
Hence, I would suggest managing the School -- and any other common table -- in one reference DB and then replicating those tables to other DBs.
Just straight out of the box, foreign key constraints aren't going to help you. You could look into replicating the individual schools table.
Based on the fact that you won't query tables with the SchoolID column very often I'll asume that inserts/updates to these tables will be really rare... In this case you could create a constraint on the table in which you need the FKs that checks for the existence of such SchoolID in the Schools table.
Note that every insert/update to the table with the SchoolID column will literally perform a query to another DB so, distance between databases, the way they connect to each other and many other factors may impact the performance of the insert/update statements.
Still, if they're on the same server and you have your indexes and primary keys all set up, the query should be fairly fast.
I am new to databases. I want to know which table stores all the constraint info for SQL Server, like the USER_CONSTRAINTS & USER_CONS_COLUMNS views in Oracle.
How can I find the constraints for SQL Server?
look first at the information schema views:
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
is probably the one you want but there are a couple of others that might hellp you get information you want.
Phil Factor recently wrote up a lot of examples:Exploring foreign key relationships with system views
In SQL Server the data dictionary is a set of database tables used to
store information about a database’s definition. One can use these
data dictionaries to check the constraints on an already existing
table and to change them(if possible).
USER_CONSTRAINTS Data Dictionary: This data dictionary contains
information about each constraint used in a database along with
constraint specific information.
I've been tasked with revisiting a database schema we designed and use internally for various ticketing and reporting systems. Currently there exists about 40 tables in one Oracle database schema supporting perhaps six webapps.
However, there's one unifying relationship amongst them all: a rooms table describing the room. Room name, purpose and other data are thrown into a shared table for each app. My initial idea was to pull each of these applications into a separate database, and perform joins between a given database and the room database. But I've discovered this solution prevents foreign key constraints in SQL Server 2005. It seems silly to duplicate one table for each app and keep those multiple copies synchronized.
Should I just leave everything in one large DB, or is there something else I can do separate the tables without losing FK constraints?
The only way to achieve built-in referential integrity is to have the table inside the database in which it is referenced. You might be able to achieve the equivalent of referential integrity using triggers but it would likely be deathly slow.
You might be able to use SQL Server replication, in it's "Transactional replication" mode/form. http://msdn.microsoft.com/en-us/library/ms151176.aspx
if all the apps truly use and depend on the rooms - then keep them all in one DB.
you can still set privilege on the tables properly, and manage the data sets in the non overlapping areas normally -
is there any task you imagine you will not be able to perform when things are together?
I plan on updating some table names by create a synonym of the old name and renaming the table to what I want it to be. Can replication properly reference a synonym?
Also as a side question, is there an easy way to see if a specific table is actually being replicated? (via a query perhaps)
I don't think so. Replication works by reading the log and there are no log records generated for a synonym. As to your question about finding out which tables are replicated, a query on sysarticles in the table should get you where you want to go. HTH.