Mirror mysql table schema and keeping it in sync - mysql

So I have this problem where I need to create an exact copy of another table (schema, indexes) but not the data. We need to temporarily store data before we move it to the table it mirrors.
I can create a mirror table like CREATE TABLE foo_mirror LIKE foo; But I want to automatically update the schema when the schema of the table foo changes'.
I have considered temporary tables as well instead of mirror tables but we will have 2 different processes copying data to the mirror table foo_mirror and copying data to the actual table foo. So I can't use a single session.
So I can compare the schema using the following queries:
-- for_mirror schema
SELECT DISTINCT *
FROM `information_schema`.`columns`
WHERE `TABLE_NAME` LIKE 'foo_mirror'
-- foo schema
SELECT DISTINCT *
FROM `information_schema`.`columns`
WHERE `TABLE_NAME` LIKE 'foo'
I could write some code that will apply updates in a job, or perhaps a trigger. I'm wondering if there is a better way to keep tables in sync or even a tool where I don't need to manage this myself.
I also see a potential problem e.g. Changing the type for a column may fail if data hasn't been updated correctly in the mirror table (string to integer - "one" -> 1).

Related

Skipping or Ignoring Temp tables when using mysqldump

Wondering if there is a way to skip / ignore all temp tables using mysqldump. In our instance, these tables are prefixed as tmp{guid}.
These temp tables have a very short lifespan, they are used for building some sort of reports in its parent application. Lifetime may be up to 1 minute.
EDIT:
It has been suggested that I use the ignore-tables parameter, unfortunately this doesn't provide a way for me to specify a wildcard as the table name (tmp*).
You are not talking about tables from CREATE TEMPORARY TABLE ..., correct? Instead, you are talking about a set of tables with a particular naming convention?
Instead of trying to do it with table names, do it with a DATABASE:
CREATE TABLE TempTables;
CREATE TABLE TempTables.abcd (...);
And reference them via the db name:
INSERT INTO TempTables.abcd ...
SELECT ... FROM TempTables.abcd JOIN ...
Then use the suitable parameters on mysqldump to avoid that oneDATABASE` (or pick all the other databases to dump).

Is there any way to import database table schema only

I have two databases. Now I'm trying to import all table schema only from first database to second database. I already exported all table structure from first database using phpmyadmin. But I can't import the table structures in the second database. phpmyadmin says
#1050 - Table 'XXXXXX' already exists
How can I import only the table structure correctly?
Note: Both databases had same table and all table had same structure. I have changed some table structures in the first database that I can't remember right now. Now I need to merge both table structure only. Also both database contains different data set and I can't afford any data loss from both databases.
Before executing any command I would recommend taking full database backup, otherwise you may lost a few days of sleep.
Using ALTER command
Use ALTER command to modify table structure. Here's sql that adds age not nullable age field to users table.
ALTER TABLE users ADD age int(11) not null
Re-creating table
I wouldn't recommend this method because you'll have data loss. Drop old table then create with new schema.
DROP TABLE mytable;
CREATE TABLE mytable (
...
);
Or if you want to keep data you can:
Duplicate or rename table to different name.
Create a new table with new schema.
Copy data from old table: INSERT INTO newtable SELECT * FROM oldtable
Renaming tables might cause relationship issues. I would recommend using ALTER command as much as possible. You can also take a look at scheme migration(aka: code first migration, database migration).
The main Issue is merging the tables. To identify the differences between the two tables I use a small software called SQL Power Architect.

How to clone SQL table with index

I have a problem with a mysql table which I'd like to clone and give another name. When I try the
CREATE TABLE data1 AS SELECT * FROM data;
It gets stuck in execution, and never actually do anything, and I have to abort.
The thing is that my table "data" has some extra commands in the sql source like
CREATE INDEX keywords ON data (keywords);
It has a few of these, and I suspect that this is what's causing the issue, since I have been able to clone other tables without these extra commmands. I'm new in sql, so I have no idea how to overcome this problem. Anyone care to help?
Use is create table like syntax. to create a table with the same structure and after this you can copy the data.
https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html
a second solution is to use mysqldump to extract the table structur including the data, rename the table in dump file and Reimport the it.
-- To copy table with constraints
CREATE TABLE new_table_name LIKE old_table_name;
-- To copy without constraints
CREATE TABLE new_table_name
(SELECT * FROM old_table_name WHERE 0);
source

Could someone help me understand and convert this MySql statement into Postgresql?

so the code I am working on has this statement executed by PHP (note:This is taken from the PostgreSQL log file so it doesn't include any PHP stuff):
CREATE temporary table IF NOT EXIST temp tablename(id int primary key,
shared int default 0) replace select 1, userid as id
from tablefoo where sharedid = 1337
I don't quite understand what's going on here exactly, I know what a temporary table is, and I can quite accurately guestimate what IF NOT EXIST does, but what is replace doing here? I know replace is like insert but it replaces stuff as well, but in this case, nothing is specified for it to replace with, so does it just replace something with nothing and why the Select 1, I know that pretty much just tells you if your table has rows or something, but what is the point of using it here?
After some research, I found that IF NOT EXIST and replace do not exist in PostgreSQL. Most online sources suggest that SQL functions be used to replace them.
Should I use an SQL function to emulate IF NOT EXIST? If so, what would I write (sorry, I am pretty new to SQL) or should I just use a PHP function.
What about replace?
Sorry for the trouble, thanks for your time, oh and if you guys aren't busy or anything, you could also tell me about how to emulate "ignore", my current solution involves arbitrarily removing it.
Many uses in MySQL for temporary tables can be replaced in PostgreSQL with common table expressions or ordinary subselects.
WITH someCTE AS (
SELECT
...
) SELECT/UPDATE/DELETE ... WHERE sometable.column = someCTE.another_column;
Look into CREATE TABLE documentation. Temporary tables are just as name suggests not permanent:
If specified, the table is created as a temporary table. Temporary
tables are automatically dropped at the end of a session, or
optionally at the end of the current transaction (see ON COMMIT
below). Existing permanent tables with the same name are not visible
to the current session while the temporary table exists, unless they
are referenced with schema-qualified names. Any indexes created on a
temporary table are automatically temporary as well.
In particular temp tables are stored in diffrent (non-public) schema, e.g.:
=> Create Temporary Table someTempTable (value integer);
CREATE TABLE
=> \dt someTempTable
List of relations
Schema | Name | Type | Owner
-----------+---------------+-------+----------
pg_temp_2 | sometemptable | table | postgres
(1 row)
PostgreSQL doesn't have IF NOT EXISTS like in MySQL's CREATE TABLE, so you can't use it. If you want to create some table you need to firstly drop existing one (if it exists). Fortunately you could use SQL command DROP TABLE IF EXISTS to handle this:
=> Drop Table If Exists someTempTable;
DROP TABLE
=> Drop Table If Exists someTempTable;
NOTICE: table "sometemptable" does not exist, skipping
DROP TABLE

Mysql Query - Copy resultset from one database to another

I have a large db that I am chopping into smaller databases based on time intervals. This will reduce query time dramatically. In a query can I copy a resultset from one database to another with an identical schema?
Basically a select followed by an update conducted in the same code block?
Thanks,
slothishtype
Copying data from one database into another should be almost as simple as #slotishtype describes except you'll need to qualify it with the OTHER database you want it replicated into.
create table OtherDatabase.Student Select * from FirstDatabase.student
However, as you mention about copying same schema, that is something else. If you want all your R/I rules, triggers, etc, you may have to dump the database schema from your first (where it has all the create tables, indexes, etc) and run in a new database. However, that might post an issue where you have auto-incrementing columns. You can't write to a read-only auto-increment column -- the database controls that. However, if such case existed, you would have to just make those columns as integer datatypes (or similar) and do a
insert into OtherDatabase.Student ( field1, field2, etc )
select field1, field2, etc from FirstDatabase.student
If it is not necessary to add it to a new database, this will do fine:
CREATE TABLE student1 SELECT * FROM student
EDIT: for the record: This will not coopy over indices etc.
This, however, will:
CREATE TABLE student_new LIKE student;
INSERT recipes_new SELECT * FROM student;
slotishtype