How to delete all but one record in all tables in Mysql? - mysql

I need to give my customer some sample data of each table in the dbase. I have duplicated my dbase, and now in the duplicate dbase I want to delete all records in all tables, except for 1 record. It doesn't matter which record remains. Then I will do an export for them without a million records.
What is the query I need to run to do this? Each table can have different primary keys.
I am using mysql 5.7.34 command line.

Related

How do I stop autonumber in a table spiralling out of control?

I'm using an append query to import records from table 1 to table 2. Table 2 has an autonumber field that I use to generate a primary key. I have a composite key on 4 fields in table 2 that stops duplicate records being imported from table 1. When I run the append query it says it's going to add 200k records but then only imports the 5k records that are new. All fine except that the autonumber in table 2 jumps to start 195k higher for the new records.
I've looked at playing around with joins to get the append query to only pull the new 5k records from table 1 in the first place but the only combinations I can see all seem to include all records from one of the tables. What's the simplest way of avoiding the autonumber spiraling out of control?
Import to a (temp) table having your composite key but no AutoNumber.
Copy the ~5K records from that table to your Table2.
Or:
Set the ID not as AutoNumber.
After every import, update the (empty) IDs manually.

how to structure large table and its transactions in database?

I have two big tables for example:
'tbl_items' and 'tbl_items_transactions'
First table keeping some items metadata which may have 20 (varchar) columns with millions rows... and second table keeping each transaction of first table.
for example if a user insert new record to tbl_items then automatically a new record will be adding to tbl_items_transactions with same data plus date, username and transaction type to keep each row history.
so in the above scenario two tables have same columns but tbl_items_transactions have 3 extra columns date, username, transaction_type to keep each tbl_items history
now assume we have 1000 users that wants to Insert, Update, Delete tbl_items records with a web application. so these two tables scale very soon (maybe billion rows in tbl_items_transactions)
I have tried MySQL, MariaDB, PostgreSQL... they are very good but when table scale and millions rows inserted they are slow when run some select queries on tbl_items_transactions... but sometimes PostgreSQL is faster than MySQL or MariaDB
now I think I'm doing wrong things... If you was me... do you use MariaDB or PostgreSQL or somthing like that and structure your database like what I did?
Your setup is wrong.
You should not duplicate the columns from tbl_items in tbl_items_transactions, rather you should have a foreign key in the latter table pointing to the former.
That way data integrity is preserved, and tbl_items_transactions will be much smaller. This technique is called normalization.
To speed up queries when the table get large, define indexes on them that match the WHERE and JOIN conditions.

SSIS- Update few columns of a row for which the primary key already exists

The following is an example to better explain my scenario. My database table has following columns
Column -1: Operating_ID (which is the primary key)
Column -2: Name
Column -3: Phone
Column -4: Address
Column -5: Start Date
Column -6: End Date
The values for the columns 1,2,3,4 come from an extract and this extract is pushed to the database daily using SSIS data flow task
The values for the columns 5 and 6 are user inputted from a web applicxation and saved to the database.
Now in SSIS process instead of throwing violation of primary key error, i need to update Columns 2,3,4 if the primary key i.e column 1 already exists.
First i considered replace but that deletes the user inputted data columns 4,5.
I would like to keep the data in columns 4,5 and update columns 2,3,4 when column 1 already exists.
Do a LOOKUP for Operating_ID. Change that lookup from "FAIL ON NOT FOUND" to "REDIRECT ROWS TO NO MATCH"
If match not found, go to INSERT
If match found, go to UPDATE. You can run OLAP commands to update, but if it is a large set of data, you are better off putting into a table, and doing an UPDATE with a JOIN
This is what I would do. I would put all the data in a staging table. Then I woudl use a data flow to insert the new records and the source of that dataflow would be the staging table with a not exists clause referencing the prod table.
Then I would use an Execute SQL task in the control flow to update the data for existing rows.

How to remove duplicate entries from database which as over 10000 records with no id field

I have database like the following with 10K rows. How to delete duplicate if all fields are same. I don't want to search for any specific company. Is there a way to search and find any multiple entries with all same fields get deleted. Thanks
This command adds a unique key, and drops all rows that generate errors (due to the unique key). This removes duplicates.
ALTER IGNORE TABLE table ADD UNIQUE KEY idx1(title);
Note: This command may not work for InnoDB tables for some versions of MySQL. See this post for a workaround. (Thanks to "an anonymous user" for this information.)
OR
Simply creates a new table without duplicates. Sometimes this is actually faster and easier than trying to delete all the offending rows. Just create a new table, insert the unique rows (I used min(id) for the id of the resulting row), rename the two tables, and (once you are satisfied that everything worked correctly) drop the original table
This below query used to find the duplicate entry using all fields:
Select * from Table group by company_name,city,state,country having count(*)>1;

How to automatically delete old records from a database table?

There is a database table for logging purpose only so old records are useless. Is there an easy way to automatically delete old records and keep most recent records only (say, records created in last 7 days, or, the latest 1 million records). The table uses MyISAM engine and have a timestamp column but do not have primary key (for fast insertion).
Set up a cron job to run a query to do so.
U can put your Logic in Insert Trigger to delete your old records