I have 100 tables in my MYSQL and I use the following query to find all the tables which has the specific Column:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%Kiddle_id%';
I want to delete the user by passing the value of the Kiddle_id, so as from users table, i should be able to delete him and from al other tables which has the Kiddle_id as column and verifying its value, i should be able to delete his/her contents from other tables
I am confused how do i do it, I do not have cascade option enabled,
Related
I have an issue with InnoDB tables. all table status are showing in use.
I am trying to open table but it is giving error "Table 'mysqlbackup.aadhars' doesn't exist in engine"
please help me
Maybe the table belongs to different schema (and not mysqlbackup) I would recommend checking all the table names (along with schemas) and see which schema aadhars table belongs to, e.g.:
SELECT *
FROM information_schema.tables
WHERE table_schema = 'mysqlbackup';
The above query will return the names of all the tables present in mysqlbackup schema. If you want to see which schema aadhars belongs to, try the following query:
SELECT *
FROM information_schema.tables
WHERE table_name = 'aadhars';
I am tracking history of changes to rows in a table that is filled with a trigger on update of another table. It tracks the revision history of the main table.
Often, my users, out of habit, will hit the SAVE button even though they have not changed anything in the record, and the system will still record a copy of that row as a revision in the history table, despite the fact that nothing has changed.
Lets say I have the tables with columns like this (although mine have about 40+ cols):
Main Data:
id, name, phone, task, dob, timestamp, note, drivername, student, doctor, userid
On Update of Main Data, insert into history:
revisionid, revisiontime, id, name, phone, task, dob, timestamp, note, drivername, student, doctor, userid
The solutions to find duplicate records presented in this site and on other sites all will work well, if I wanted to list out the columns by hand.
The problem is that there are many many columns, and that I often add columns and don't want to rewrite this query every time.
When the user saves, often only the timestamp will change. What I want to do is keep only the revisions where values have changed (ignoring the revisionid and revisiontime which always change).
In the query, I dont want to list any other column names besides the columns which i want to ignore. Is it possible?
Pseudo code:
DELETE [rows, except one] FROM historytable WHERE [all columns match values] EXCEPT [these few columns which can still be different and be deleted]
Here are a few reference questions:
Deleting duplicate rows from a table
How to check for duplicates in mysql table over multiple columns
MySQL remove duplicates from big database quick
No, it isn't possible to delete duplicates from a table without specifying the columns.
The only way I know of to use a SQL statement to trim a table of dups without specifying an explicit column list is to do the following. Create a new copy with only distinct records:
create table T_UNIQUES as select distinct * from T;
You'd have to create a new table, rename the old one and then rename the new one into place. This is sometimes done on data warehouses when a DELETE operation is too slow. However, this doesn't ignore any timestamp columns, so it may not be adequate.
The only way I know to write a prune your history table with something automatic and extensible is to extract the columns from the data dictionary (INFORMATION_SCHEMA). This only automates it, but doesn't avoid specifying the columns in question.
My approach would be to fix the trigger. It sounds broken / inadequate; I would rewrite it to do an "UPSERT" instead of a blind INSERT.
My thought process is as following..
List all the column names (with an exclusion list)
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='db'
AND TABLE_NAME='table'
AND COLUMN_NAME NOT IN ('columnToIgnore')
Store the names as rows in a temporary table
CREATE TEMPORARY TABLE IF NOT EXISTS columnNames AS (step1);
Fetch all records from temporary table 'columnNames' and store in a variable.
SELECT GROUP_CONCAT(COLUMN_NAME) into #cols FROM columnNames;
Prepare the final statement, list all the redundant rows. (I used SELECT for checking)
SET #sql = CONCAT('SELECT CONCAT_WS(" ",',#cols,') AS allColumns FROM targetTable GROUP BY allcolumns');
To sum up,
CREATE TEMPORARY TABLE IF NOT EXISTS columnNames AS (SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='dbName'
AND `TABLE_NAME`='tableName'
AND `COLUMN_NAME` NOT IN ('columnNameToIgnore'));
SELECT GROUP_CONCAT(COLUMN_NAME) into #cols FROM columnNames;
SET #sql = CONCAT('SELECT CONCAT_WS(" ",',#cols,') AS allColumns FROM targetTable GROUP BY allcolumns');
PREPARE stmt FROM #sql;
EXECUTE stmt;
Who says we can't use chainsaw to slice a bread ;)
I am having two mysql databases. One is NEW and other one is OLD.
I require the list of different tables between two databases. That is my old database is having the list of 155 tables and my new database is having the list of 165 tables.
How can I get the name of the TEN differnt tables ?
Is there any tool to do that or we can able to do via query ?
Any help will be thankful...
Thanks in advance..
Have you tried:
SHOW TABLES IN database;
or using information schema:
select table_schema,
table_name from information_schema.tables
where table_name = ???;
EDIT AS PER OP'S COMMENT:
The INFORMATION_SCHEMA database is made up of temporary tables using the MEMORY storage engine.. All tables in the INFORMATION_SCHEMA database are stored directly in memory as MEMORY storage engine tables. They are totally internal to MySQL, so the .frm mechanisms are handled in mysqld. In my answer, I first showed the table layout of INFORMATION_SCHEMA.TABLES. It is a temporary table in memory. It is manipulated using storage engine protocols. Thus, when mysqld is shutdown, all information_schema tables are dropped. When mysqld is started, all information_schema tables are created as TEMPORARY tables and repopulated with metadata for every table in the mysql instance.
For e.g. If your run following two commands you will see all the databases in your mysql metadata.
show databases;
use information_schema; show tables;
Here you are specifying a table_schema to get the table names.
SELECT table_name from
information_schema.tables WHERE table_schema = 'mydb';
With a join: assuming one database name is db1, other db2
SELECT table_name from
db1.tables x
inner join
db2.tables
on x.table_name = y.table_name
;
I think you should query on database information_schema. It's a table which contains all meta data of all database.
Query something like:
SELECT * FROM `TABLES` T1
LEFT JOIN `TABLES` T2
ON T1.`TABLE_NAME` = T2.`TABLE_NAME`
WHERE T1.`TABLE_SCHEMA`='xxx'
AND T2.`TABLE_SCHEMA`='yyy'
AND T1.TABLE_CATALOG IS NULL
You can do this by querying the INFORMATION_SCHEMA (a database which contains information of other databases in the server like table names, column names, primary key columns, primary key names, indexes, etc.) like this:
-- this gives the table names that are in the new table but not in the old table
select newTable.TABLE_NAME
from TABLES newTable
where newTable.TABLE_SCHEMA='NEW' and newTable.TABLE_NAME not in
(
select oldTable.TABLE_NAME
from TABLES oldTable
where oldTable.TABLE_SCHEMA='OLD'
)
I am using MySQL and it is not a relational database (foreign keys constraints) so when I perform a delete on a record I want to check whether it is used in any other table, if so, don't delete.
I assume I would have to perform a database-wide search on all tables except it's own. I keep each records id uniform throughout the database.
Example:
Assets
id | date_created | type_id
History
asset_id | date_recorded | store_id
I found a script to find all the table that have the records id:
SELECT
DISTINCT TABLE_NAME,
TABLE_NAME.COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
COLUMN_NAME IN ('desander_id')
AND
TABLE_SCHEMA='emp'
But I get an error on the TABLE_NAME.COLUMN_NAME part where it says COLUMN_NAME is unknown. Is there a way I can do this? Am I doing this the right way?
your from table is INFORMATION_SCHEMA.COLUMNS and you are selecting TABLE_NAME.COLUMN_NAME which not possible in MySQL. It should be like table_name.column_name. try this:
SELECT DISTINCT COLUMNS.TABLE_NAME,
COLUMNS.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('desander_id') AND
TABLE_SCHEMA='emp';
I am trying to get a list of tables and their number of rows. I have been using this query:
SELECT TABLE_NAME, TABLE_ROWS
FROM INFORMATION_SCHEMA WHERE TABLE_SCHEMA = 'myDatabase'
I am finding this sometimes returns null. What I would like to do is catch this probably doing something similar to
IFNULL ( TABLE_ROWS, SELECT COUNT(*) FROM ????)
Only I'm not sure what I should enter for ????
how to make it dynamic based on the TABLE_NAME column?.
Edit: Additional information: I found that the 'tables' not displaying are actually views.
Might as well do
SHOW TABLE STATUS FROM YOUR_DATABASE;
It always return a column named as Rows which is number of rows in that table.
From the documentation:
The TABLE_ROWS column is NULL if the table is in the INFORMATION_SCHEMA database.
So, you will propably not need it.