How to keep foreign tables imported via foreign data wrapper in Postgres up to date with underlying table? - database

I am wondering how foreign data in Postgres are updated. I understand that the FDW makes a connection to the remote database to access the tables, and the foreign tables don't take up physical memory in the database. It is the equivalent of making a select statement to the the underlying tables.
I have some tables in Database A that I have imported as foreign tables into database B. I am using the foreign Tables in Database B to create materialized views.
I am running these commands in Database B:
IMPORT FOREIGN SCHEMA completed LIMIT TO (tb1,tbl2)
FROM SERVER db_data_pipeline
INTO public;
COMMIT;
CREATE MV1 AS SELECT * FROM TB1;
My tables in database A are updated through a drop and recreate process. I have two questions regarding how foreign data are synchronized:
Since my tables in database A are updated through a drop and recreate, are the foreign tables in Database B also updated automatically? is there a scenario where the foreign data becomes 'stale'? Would I have to re-import foreign tables to keep them up to date?
I've read that the foreign table definition does not change when the underlying table changes. For example, a column added in the underlying table will not be reflected in the foreign table. However, since my underlying table is never updated directly and dropped and recreated instead, do I have to worry about this issue?

Ad 1:
When you query tb1 in database B, PostgreSQL composes a query, connects to database A and runs the query. If the table in database A has been dropped and re-created, this query will access the new table, since it goes by the table name. There is no need to drop and re-create the foreign tables. The data in the foreign tables will never be stale, but the data in the materialized views can of course become stale.
Ad 2:
Yes, you have to worry about that. A change in the table definition on database A will not change the foreign table in database B. So if you add a bew column in database A, it will not show up in the foreign table in database B. If you drop a column in the table in database A, querying the foreign table may cause an error, since the query could access the missing column. You have to modify or re-create the foreign table to deal with metadata changes.

Related

How to copy one database to another database in sql server when tables and constraints already exist in the target database?

I had created identical databases in different environments: Dev and QA. While doing the development, I have changed a few tables in the Dev database. How do I change the QA database to make it again identical to the Dev database in terms of tables (and constraints)?
I checked the below link:
[Copy one database to another database
Steps in the above link did not directly work because the tables and constraints already existed in the second database. I did modification in the sql file after the steps
I followed the below steps:
Right-click on the database you want to copy
Choose 'Tasks' > 'Generate scripts'
'Select specific database objects' and Check 'Tables'
Click on Next. Again click on Next.
This exports .sql file to the path shown while following the above steps.
I edited the script file and changed the database name to the QA database name (at the top of the script).
After this added the below line above every create table statement as the table exist.
DROP TABLE IF EXISTS tablename;
On running the query, I get an error message saying
Could not drop object tablename because it is referenced by a FOREIGN
KEY constraint.
How do I change the second database to make it identical to the first database in terms of tables (and constraints)?
Thank You
Well, the most straight forward solution would be to drop all constraints first. You could add a drop constraint per constraint above your drop table lines, though that may be tedious.
An answer to this question has a script that drops every constraint in a database and table. You could omit the table name param in the where.
But, since you're destroying everything in the database, it might be easiest to delete and recreate the database. Then you wouldn't need to add the drop table statements to the create script from dev.
In your scripts, separate tables creation, from insertion of records, for later.
Example (before):
create table one ...
insert into table one ...
create table two ...
insert into table two ..
create table three ...
insert into table three ..
Example (after)
create table one ...
create table two ...
create table three ...
insert into table one ...
insert into table two ..
insert into table three ..
If you have foreign constraints, check that the destination tables (primary table or master table), are created and filled first, than the source tables (secondary table or slave table).
Example (before):
create table one
(int onekey primary key, int threekey foreing key)
create table three ...
(int threekey primary key)
insert into table one ...
insert into table three ..
Example (after):
create table three ...
(int threekey primary key)
create table one
(int onekey primary key, int threekey foreing key)
insert into table three ..
insert into table one ...
And, finally, if you use automatic of self generated keys, turn off before table insertion, and turn on back, after table insertion, because the DB server may assign new keys to destination tables, and source tables, may expect the previous keys.

table included in merge replication has all its data disposed when execute

I have merge replication between two databases, "db1" as a publisher and "db2"
as a subscriber.
Just adding one row to "table1" included in db2 and then right clicking on data then execute I got all its data disposed. Even if I try to copy its data manually from table1 in the published I got a message that table1 is being upgraded by the publisher and I cannot insert.
The table's primary key is foreign key in other tables.
How is the content permitted to delete?

Cannot create a relation between two tables with three primary keys

I recently used Microsoft SQL Server Migration Assistant for Oracle to convert an Oracle database to a SQL Server database through a two-pass approach.
There is are two tables, BILL_INFO and BILL_INFO_DETAIL, that are supposed to have a master-detail relation through composite PK. However, when I try to create that relation, I get this error:
'BILL_INFO' table saved successfully 'BILL_INFO_DETAIL' table
- Unable to create relationship 'FK_BILL_INFO_DETAIL_BILL_INFO'. The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
"FK_BILL_INFO_DETAIL_BILL_INFO". The conflict occurred in database
"MyDatabase", table "dbo.BILL_INFO".
The database is plagued with bad data. So I did a basic search in the detail table to find BILL_NUMBER, PAY_MODE_ID, and CASHIER_ID that may not exist in master (one by one) and found two records when searching on BILL_NUMBER. I fixed them and also verified that PAY_MODE_ID and CASHIER_ID were in order.
Still, I cannot create the relation. Same error. Now I wonder if the Tuple is invalid between tables. How do I find a composite key that exists only in details table?
You could check for non-existing values using:
SELECT bill_number, pay_mode_id, cashier_id
FROM Bill_Info_Detail
EXCEPT
SELECT bill_number, pay_mode_id, cashier_id
FROM Bill_Info;
-- and then fix missing data
When using composite key, you need to check all columns as tuple.

How to insert Master/Detail record in SQL Server?

I have three tables: Employee, Emp_Address and Emp_AddressDetail.
Employee table is master and Emp_Address is detail.
Emp_Address is master and Emp_AddressDetail is detail.
I want to copy all rows from a table to another table.
How can I do it?
If I understand your question correct you want to insert data from one table to another? If so, you should have a look at the INTO statement. http://msdn.microsoft.com/en-us/library/ms188029.aspx
SELECT * INTO dbo.OneTable FROM Production.AnotherTable
If the tables have foreign keys defined, and those foreign key definitions need to be set on the new database, than you need to take a look at the data diagrams, and identify any tables that do not have foreign key columns. In your case the copy order will be Employee, Emp_Address, Emp_AddressDetail
If you do not have explicit SQL Server maintained foreign keys, or if the foreign keys are not set on the target database, you can just copy the data in any order you like.
Note that it's entirely possible to paint yourself into a corner, e.g. if there was field in the Employee table called PrimaryAddress that would be a foreign key to the Emp_Address table.

Connecting the tables in a database

I have created a simple database in SQL Server Express which consists of three tables: Inventory, Customers, Orders.
I try to connect them in db diagram forcing the primary keys of Inventory and Customers (CarID and CustID) as foreign keys to Orders. However, when I try to save the diagram, I receive an error that does not allow me to save the diagram and link the tables.
The error indicates:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
"FK_Orders_Inventory". The conflict occurred in database "AutoLot",
table "dbo.Inventory", column 'CarID'.
FK_Orders_Inventory is the connection between Inventory and Orders. What could be a potential cause of the error?
The data currently in the table is probably not conforming to the constraints you have defined.
Make sure the data is consistent with the constraints before adding them.
In this case, one of the foreign keys you are defining fails because the column you are defining it on (in the Inventory table) contains values that do not exist on the referenced column (CarId) in the foreign table.
You have a CarID value in the child table that does not exist in the parent table.

Resources