I have a two tables. values inside them are not identical, but most of the strcuture is the same, one of the tables has some extra fields in them. in a simplified form I have something like this:
|table_1| |table_2|
id id
name name
telephone telephone
email email
address
language
I would like to copy table_2 structore over to table_1 and set address and language as NULL. To do so, I need to explicitly set them to null which is not very good, because my real table is a mess (over 30 columns). I have only 4-5 new fields though, is there a way to just compare structure between two tables and see the difference? I would just add the new fields myself then.
Specify the needed connections to MySQL servers in the Connection fields. Select the databases you want to compare in the Database fields. Note, if you want to compare tables in the same database, you may just select Source connection and database and then just click the Copy source settings to target button.
To compare database definitions. On the Tools menu, select SQL Server, and then click New Schema Comparison. Alternatively, right-click the TradeDev project in Solution Explorer, and select Schema Compare. The Schema Compare window opens, and Visual Studio automatically assigns it a name such as SqlSchemaCompare1 .
Using joins to compare columns by priority among the table. For example, left join returns all values from the first table and null value for the not-matched records from the second table. Similarly, we can use right join, inner join, full join and self join as per our requirements.
The following (untested) SQL should give you a list of columns in both tables.
The columns in_table_1 and in_table_2 will contain 'Yes' if the column exists in that table.
select column_name
,max(case when table_name = 'table_1' then 'Yes' end) as in_table_1
,max(case when table_name = 'table_2' then 'Yes' end) as in_table_2
from information_schema.columns
where table_name in('table_1', 'table_2')
and table_schema = 'your_database'
group
by column_name
order
by column_name;
You could add having count(*) = 1
to only return the columns that aren't in both tables.
You probably want to add datatypes as well. Have a look at the the INFORMATION_SCHEMA
To see the differences in two tables in two different databases
SELECT column_name,ordinal_position,data_type,column_type FROM
(
SELECT
column_name,ordinal_position,
data_type,column_type,COUNT(1) rowcount
FROM information_schema.columns
WHERE
(
(table_schema='db1' AND table_name='tb1') OR
(table_schema='db2' AND table_name='tb2')
)
AND table_name IN ('product_today','product_yesterday')
GROUP BY
column_name,ordinal_position,
data_type,column_type
HAVING COUNT(1)=1
) A;
original answer is given by / at https://dba.stackexchange.com/a/75651/193007
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With