Is there a way to get the insert statements for a table via some query in MySql?
for ex: if the table name is Cards,which has 5 rows, i need to get the insert statements for that particular table.
Thanks.
You can do a "mysqldump" which will backup the database to a file. That file will include all off the insert statements with data.
Enjoy!
Doug's right. You can use mysqldump. But you can refine it a bit and only write out the scripts for the tables that you want. This explains how.
Related
I want to add some 1000 records into my table for creating a database. Inserting each record manually is not at all practical. Is there a proper way to do this?
In MySQL you can insert multiple rows with a single insert statement.
insert into table values (data-row-1), (data-row-2), (data-row-3)
If you run a mysqldump on your database, you will see that this is what the output does.
The insert is then run as a single "transaction", so it's much, much faster than running 1000 individual inserts
can anybody help me with this please?
I want to insert data in table in Teradata SQL Assistant, but insert only that doesn't exist in the table.
I have an ideal how to do it in Microsoft SQL Server but not in teradata as i'm new to it.
Any help would be highly appreciated.
Thank you
This very much depends on your situation. Can you elaborate a little more by answering the following questions? I can then give you my best idea of what to do.
Are you inserting into an existing table using a select statement from other Teradata tables, or from a file?
Does the table you are inserting into have a UPI (unique primary index)? You can check this by running the following code:
show table database.table;
where "table" is the name of your table and "database" is the database where your table is stored.
The end of the resultset should say either "unique primary index" or simply "primary index."
No matter what the answers are, you will most likely need to use an insert statement, insert-select statement, or a merge statement in the end. Teradata SQL Assistant has an example of all of these in the Query Builder. You can bring it up by pressing F2.
If I understood your question, You want to insert some data to a table. A the issue is you only need to insert those rows in the table for-example table A, which does not exist is this table A. The solution depends on your source.
If source is another Teradata table that is table B, then you could use a join between Table A and Table B so detect which are not already there.
If source is some other database or a file, than I would suggest to insert data to a temporary table and then use the same logic as given in point-1.
Br,
Hassam
I have two MySQL database k_db1 and k_db2 on a single server.
In k_db1, I have k_db1.table1 and k_db1.table2.
In k_db2, I have k_db2.table3 and k_db2.table4.
I want to create a third database k_db3 where I copy/paste tables of others databases.
It will result in k_db3.db1-table1, k_db3.db1-table2, k_db3.db2-table3, k_db3.db2-table4. I want to transfer data, indexes etc... and I don't want to delete k_db1 and k_db2 tables in the process. It must duplicate datas.
Do you know a way to do this just with SQL command?
Thanks in advance for your help.
You can try something like this:
DROP TABLE IF EXISTS k_db3.db1_table_1;
CREATE TABLE k_db3.db1_table_1 AS
SELECT * FROM db1.table_1;
Then you can recreate the indexes on the new table via ALTER TABLE statements.
Also I would avoid using - in table names.
CONTEXT:
we have big databases with loads of tables. Most of them (99%) are using innodb.
we want to have a daily process that monitors which table has been modified. As they use innodb the value of Update_time from
SHOW table STATUS from information_schema;
is null.
For that reason we want to create a daily procedure that will store the checksum (and other stuffs for that matters) of each table somewhere (preferably another table). On that, we will do different checks.
PROBLEM:
I'm trying to use
checksum table from db_schema.table_name;
which returns a resultset-table with 2 columns: "table","checksum".
It gives me the value I want but I'm not able to use it in a select or insert statement.
I tried a lot of things like:
select `checksum` from (checksum table from db_schema.table_name);
or other similar queries. But I'm not able to extract the data from the resultset.
Is there a way I can do that?
Thanks in advance for your help.
EDIT: in the end what I want is to build a more complex resultset having different informations in it (table schema, table name, count, checksum, datetime:now()...)
Then I'll use this resultset to compare with the values of yesterday and draw my own statistics. That's why I want to get the checksum from that resultset.
There is no possibility to save the result of CHECKSUM TABLE directly using SQL. Neither can you use prepared statements or cursors in stored procedures to use the checksum result.
You best make a script around it, or download some popular tools doing it for you.
For MyISAM tables using the CHECKSUM=1 table argument, you can simply use INFORMATION_SCHEMA like this:
SELECT TABLE_NAME,CHECKSUM FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'test' AND ENGINE='MyISAM'
AND CHECKSUM IS NOT NULL;
I'm trying come up with the best method of synchronizing particular rows of 2 different database tables. So, for example there's 2 product tables in different databases as such...
Origin Database
product{
merchant_id,
product_id,
... additional fields
}
Destination Database
product{
merchant_id
product_id
... additional fields
}
So, the database schema is the same for both. However I'm looking to select records with a particular merchant_id, remove all records from the destination table that have that merchant_id and replace those records with records from the origin database of the same merchant_id.
My first thought was using mysqldump, parsing out the create table statements, and only running the Insert Statements. Seems like a pain though. So I was wondering if there is a better technique to do this.
I would think mysql has some method of creating INSERT statements as output from a SELECT statement, so you can define how to insert specific record information into a new db.
Any help would be appreciated, thank you much.
phpMyAdmin has part of this capability: You can run a query and then export the results of that query into a file containing CREATE statements.
Update: And mysqldump has it too: Link
mysqldump -u username -p --where="id='merchant_id'" databasename
In regards to replacing merchant IDs, that part I don't entirely understand yet. You may be better off doing a manual search+replace on them. Can you make a real life example of two such records?