CREATE TABLE endereco (
endereco_id int(11) NOT NULL AUTO_INCREMENT,
cliente_id int(11) DEFAULT NULL,
identificacao varchar(255) DEFAULT NULL,
tipo_endereco tinyint(4) DEFAULT NULL,
data_cadastro datetime DEFAULT NULL,
unidade_origem tinyint(3) unsigned NOT NULL DEFAULT 0,
logradouro varchar(255) DEFAULT NULL,
numero varchar(60) DEFAULT NULL,
complemento varchar(60) DEFAULT NULL,
bairro varchar(60) DEFAULT NULL,
municipio varchar(60) DEFAULT NULL,
uf char(2) DEFAULT NULL,
cep varchar(9) DEFAULT NULL,
cod_cidade_ibge varchar(7) DEFAULT NULL,
pessoa_responsavel varchar(255) DEFAULT NULL,
referencia varchar(255) DEFAULT NULL,
codigo_obra varchar(20) DEFAULT NULL,
endereco_ativo enum('True','False') NOT NULL DEFAULT 'True',
end_envio_boleto enum('True','False') NOT NULL DEFAULT 'False',
uuid_endereco binary(16) DEFAULT NULL,
PRIMARY KEY (endereco_id) USING BTREE,
UNIQUE KEY uuid (uuid_endereco) USING BTREE,
KEY cliente_id (cliente_id) USING BTREE,
KEY end_ativo (endereco_ativo) USING BTREE,
KEY end_env_boleto (end_envio_boleto) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
I have the "endereco" table on 2 separate schemas (centauro001 and centauro005) on a MariaDB database. Most addresses are equal (same endereco_id and same cliente_id), but I want to find out which new addresses where included for the same cliente_id
Devise a way to get a list of addresses at each schema that need to be included in the other schema
Assistant
To synchronize the endereco tables between the centauro001 and centauro005 schemas in your MariaDB database, you can identify the new addresses in each schema that need to be added to the other. This can be accomplished using LEFT JOIN operations to find records present in one schema but absent in the other.
Assumptions:
Both schemas (centauro001 and centauro005) are accessible from the same MariaDB server.
The combination of endereco_id and cliente_id uniquely identifies each address. If uuid_endereco is a better unique identifier in your context, you can adjust the queries accordingly.
Step-by-Step Guide:
Find Addresses in centauro001 Not Present in centauro005:
This query retrieves all addresses that exist in centauro001.endereco but are missing in centauro005.endereco based on endereco_id and cliente_id.
sql
SELECT e1.*
FROM centauro001.endereco e1
LEFT JOIN centauro005.endereco e2
ON e1.endereco_id = e2.endereco_id
AND e1.cliente_id = e2.cliente_id
WHERE e2.endereco_id IS NULL;
Find Addresses in centauro005 Not Present in centauro001:
Similarly, this query retrieves all addresses that exist in centauro005.endereco but are missing in centauro001.endereco.
sql
SELECT e2.*
FROM centauro005.endereco e2
LEFT JOIN centauro001.endereco e1
ON e2.endereco_id = e1.endereco_id
AND e2.cliente_id = e1.cliente_id
WHERE e1.endereco_id IS NULL;
Optionally, Use NOT EXISTS for the Same Purpose:
Alternatively, you can use the NOT EXISTS clause to achieve the same results. This approach can sometimes offer better performance, especially with large datasets.
Addresses in centauro001 not in centauro005:
sql
SELECT e1.*
FROM centauro001.endereco e1
WHERE NOT EXISTS (
SELECT 1
FROM centauro005.endereco e2
WHERE e1.endereco_id = e2.endereco_id
AND e1.cliente_id = e2.cliente_id
);
Addresses in centauro005 not in centauro001:
sql
SELECT e2.*
FROM centauro005.endereco e2
WHERE NOT EXISTS (
SELECT 1
FROM centauro001.endereco e1
WHERE e2.endereco_id = e1.endereco_id
AND e2.cliente_id = e1.cliente_id
);
Review and Synchronize:
After executing these queries, you will have two result sets:
Addresses present in centauro001 but missing in centauro005.
Addresses present in centauro005 but missing in centauro001.
You can then decide how to synchronize the tables, such as inserting the missing records from one schema into the other.
Example: Inserting Missing Addresses from centauro001 to centauro005
If you determine that addresses from centauro001 should be added to centauro005, you can use an INSERT statement combined with a LEFT JOIN to perform this action.
sql
INSERT INTO centauro005.endereco (
cliente_id,
identificacao,
tipo_endereco,
data_cadastro,
unidade_origem,
logradouro,
numero,
complemento,
bairro,
municipio,
uf,
cep,
cod_cidade_ibge,
pessoa_responsavel,
referencia,
codigo_obra,
endereco_ativo,
end_envio_boleto,
uuid_endereco
)
SELECT
e1.cliente_id,
e1.identificacao,
e1.tipo_endereco,
e1.data_cadastro,
e1.unidade_origem,
e1.logradouro,
e1.numero,
e1.complemento,
e1.bairro,
e1.municipio,
e1.uf,
e1.cep,
e1.cod_cidade_ibge,
e1.pessoa_responsavel,
e1.referencia,
e1.codigo_obra,
e1.endereco_ativo,
e1.end_envio_boleto,
e1.uuid_endereco
FROM centauro001.endereco e1
LEFT JOIN centauro005.endereco e2
ON e1.endereco_id = e2.endereco_id
AND e1.cliente_id = e2.cliente_id
WHERE e2.endereco_id IS NULL;
Note: Before performing insertions or any data modifications, always ensure you have proper backups and have tested queries in a safe environment to prevent any unintended data loss or corruption.
Automating the Process:
If this synchronization needs to occur regularly, consider creating a stored procedure or a scheduled event within MariaDB to automate the comparison and synchronization process.
Additional Considerations:
Data Consistency: Ensure that other related tables maintain referential integrity when inserting new addresses.
Conflict Resolution: Decide how to handle potential conflicts, such as differing data for the same endereco_id and cliente_id across schemas.
Performance: For large tables, ensure that appropriate indexes are in place to optimize the performance of JOIN and WHERE clauses.
By following these steps, you can effectively identify and synchronize new addresses between your centauro001 and centauro005 schemas in MariaDB.