mysql logging table change history - mysql

I want to log the changes history of tables in a single table
Eg:
table a(col1,col2,col3)
table b(col1,col2)
table history(tablename,olddata,newdata,modifiedDate,modifiedBy)
if i execute an update statement, for eg
update a set col1=2,col2=4,col3=5
then it should log on the history table as
a|col1=1,col2=2,col3=3| col1=2,col2=4,col3=5|13/6/2012|username

Related

Delete from a specific row to the last one

I have a table (cars) that has 26500 rows. Is it possible to delete from the row number 10001 through the end?
in InnoDB Tables
If you are deleting many rows from a large table, you may exceed the lock table size for an InnoDB table. To avoid this problem, or simply to minimize the time that the table remains locked, the following strategy (which does not use DELETE at all) might be helpful:
Step 1: Select the rows not to be deleted into an empty table that has the same structure as the original table:
INSERT INTO `cars_copy` SELECT * FROM `cars` LIMIT 10000 ;
Step 2: Use RENAME TABLE to atomically move the original table out of the way and rename the copy to the original name:
RENAME TABLE `cars` TO `cars_old`, `cars_copy` TO `cars` ;
Step 3: Drop the original table:
DROP TABLE `cars_old`;
No other sessions can access the tables involved while RENAME TABLE executes, so the rename operation is not subject to concurrency problems.
When your Rows are labelled with an ID you can just do this:
DELETE FROM cars WHERE ID > 10000

why mysql innodb could update data when alter table structure?

When I add a new column to a table, at the same time I update the table date before the alter table transaction finished, but the update data task succeeds, why?
Why does mysql innodb engine don't lock the table when altering table structure? If locking the table, why could I update the table data?
Conditions:
My table data is too large, about 16000000 records.
mysql version:5.7.15;
Certain ALTERs do not require locking the table; some don't even modify any part of the data. If you would like to show us the ALTER and provide the MySQL version number, we can be more specific.

Alter datatypes in the table -mysql

I need to alter the huge table around 200GB (INNODB).
As i seen many blogs, there is suggestion for creating a new structure, copying the table content to new table and rename the tables.
As given below
CREATE TABLE WorkingTableNew LIKE WorkingTable;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT SQL_NO_CACHE * FROM WorkingTable;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
But if do above steps, How could we handle the newly inserted data into original table. Since many process updating and inserting the records into original table.
I have one master and 8 slaves

MySQL renaming and create table at the same time

I need to rename MySQL table and create a new MySQL table at the same time.
There is critical live table with large number of records. master_table is always inserted records from scripts.
Need to backup the master table and create a another master table with same name at the same time.
General SQL is is like this.
RENAME TABLE master_table TO backup_table;
Create table master_table (id,value) values ('1','5000');
Is there a possibility to record missing data during the execution of above queries?
Any way to avoid missing record? Lock the master table, etc...
What I do is the following. It results in no downtime, no data loss, and nearly instantaneous execution.
CREATE TABLE mytable_new LIKE mytable;
...possibly update the AUTO_INCREMENT of the new table...
RENAME TABLE mytable TO mytable_old, mytable_new TO mytable;
By renaming both tables in one statement, they are swapped atomically. There is no chance for any data to be written "in between" while there is no table to receive the write. If you don't do this atomically, some writes may fail.
RENAME TABLE is virtually instantaneous, no matter how large the table. You don't have to wait for data to be copied.
If the table has an auto-increment primary key, I like to make sure the new table starts with an id value greater than the current id in the old table. Do this before swapping the table names.
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='mydatabase' AND TABLE_NAME='mytable';
I like to add some comfortable margin to that value. You want to make sure that the id values inserted to the old table won't exceed the value you queried from INFORMATION_SCHEMA.
Change the new table to use this new value for its next auto-increment:
ALTER TABLE mytable_new AUTO_INCREMENT=<increased value>;
Then promptly execute the RENAME TABLE to swap them. As soon as new rows are inserted to the new, empty table, it will use id values starting with the increased auto-increment value, which should still be greater than the last id inserted into the old table, if you did these steps promptly.
Instead of renaming the master_backup table and recreating it, you could
just create a backup_table with the data from the master_table for the first backup run.
CREATE TABLE backup_table AS
SELECT * FROM master_table;
If you must add a primary key to the backup table then run this just once, that is for the first backup:
ALTER TABLE backup_table ADD CONSTRAINT pk_backup_table PRIMARY KEY(id);
For future backups do:
INSERT INTO backup_table
SELECT * FROM master_table;
Then you can delete all the data in the backup_table found in the master_table like:
DELETE FROM master_table A JOIN
backup_table B ON A.id=B.id;
Then you can add data to the master_table with this query:
INSERT INTO master_table (`value`) VALUES ('5000'); -- I assume the id field is auto_incrementable
I think this should work perfectly even without locking the master table, and with no missing executions.

What is the difference between DROP and DELETE for tables?

Which one will delete all data from the table, and which one will remove the table from the database?
What is the right thing to do if I don't want this table in my database any more?
Should I drop or delete?
DROP command deleting the table and its structure from the data base.
DROP TABLE tbl_user;
DELETE command used for deleting the records from the table,and it removing the table space which is allocated by the data base, and returns number of rows deleted.
DELETE FROM tbl_user WHERE id = 1;
TRUNCATE command is also delete the records but it doesn't delete the table space which is created by the data base, and does not return number of deleted rows.
TRUNCATE TABLE tbl_user;
DROP is used to remove tables (and databases).
DELETE is used to delete rows from tables.
Maybe are you talking about TRUNCATE and DELETE ?
TRUNCATE TABLE users;
is equivalent (logically) to
DELETE FROM users;
This will erase all data in table users. If you want to delete the whole table structure you should write:
DROP TABLE users;
But, DELETE is DML command while TRUNCATE and DROP are DDL commands. There is also some other differences in different RDBMS. More info - here
And another useful link: Difference between TRUNCATE, DELETE and DROP commands
drop removes the contents and the table (but not user permissions on the table). This is what you want if you want to completely remove the table from your schema.
delete selectively (or not) removes rows from a table. It does not alter the table structure.
There's no such thing as DELETE TABLE. You should use DROP TABLE to delete your table.
The DROP TABLE statement is used to delete a table.
DROP TABLE table_name
The DELETE statement is used to delete data(records) in a table.
DELETE FROM table_name WHERE some_column=some_value;
So, as you want to delete table from database then you will go for DROP Command
DELETE is used to delete one or several rows from the table. DROP TABLE would remove the entire table from the database, so if you want to remove the table, you should use DROP TABLE.
DROP TABLE reference
Delete will update the log, while drop does not.
Delete is used to remove the rows, while drop is used to remove the tables and DB.
delete
- removes the rows from the table - where clause can be used to delete specific rows.
- If where is not specified, then all the rows in the table will be removed
- rollback can be done.
without using where condition
DELETE * FROM empl;
using where condition
DELETE FROM empl WHERE job = 'Manager';
truncate
- removes all the rows from the table.
- rollback cannot be done.
SQL> TRUNCATE TABLE empl;
drop
- removes a table from the database.
- rows, indexes and privileges will also be removed.
- rollback cannot be done.
SQL> DROP TABLE empl;
Delete remove all data from a specific table and drop remove whole database and also remove specific table from database.
If you don't want the table in database any more then you should drop the table.