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';
Related
I have the following problem that I'm trying to find the best solution for. Let's say I have a view such as the following:
CREATE VIEW myView AS (
SELECT
country_code,
other_column,
COUNT(1) as cnt
FROM mytable
JOIN otherDatabase.otherTable ON (id)
GROUP BY 1,2 ORDER BY 1 LIMIT 1
)
What would be the fastest way to resolve the field names and types of the view? For example, on the above I am looking to get something along the lines of:
{
country_code: VARCHAR,
other_column: BOOL,
cnt: INT
}
The first approach is just to run the query (with a limit, if necessary) and then get the types of the result-set from the driver. The downside of this is what if the query takes 50 minutes to resolve?
The second approach I thought of is to 'follow' the columns to get their types and then do some parsing to resolve any expressions/literals/etc. This would involve a lot of code but would be orders of magnitude faster than the above. However, the potential downside of this is we may have access to the view but not have access to a table (possibly in another database on the server) that contains the column type, so it's possible we might not be able to resolve all field names.
What would be the best way to resolve the types of a view? Note I have tagged this as MySQL, but I'm also wondering if there's a more generic way to resolve types or if it's something that is non-standard and more needs to be done on a per-database basis?
Update: I believe the correct answer is just to run a DESCRIBE myView, and that would give me the column names and types without running the query?
In the current version of MySQL at least, INFORMATION_SCHEMA.COLUMNS holds metadata for views as well as base tables:
mysql> create table mytable (id serial primary key, x int);
Query OK, 0 rows affected (0.01 sec)
mysql> create view v as select * from mytable;
mysql> select column_name, data_type from information_schema.columns where table_name='v';
+-------------+-----------+
| COLUMN_NAME | DATA_TYPE |
+-------------+-----------+
| id | bigint |
| x | int |
+-------------+-----------+
A related issue...
SHOW CREATE TABLE myView;
or
SHOW CREATE VIEW myView;
will fully qualify all the columns.
(When writing a JOIN, it is wise to always qualify the column names.)
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,
I'm running a quick query to make sure that all the foreign keys in my table are referencing a field that is indexed.
My code is as follows:
select table_name, column_name, index_name from statistics where table_name in (select referenced_table_name from key_column_usage where table_name='table' and table_schema='schema') and column_name in (select referenced_column_name from key_column_usage where table_name='table' and table_schema='schema') order by table_name;
I'm running this on multiple environments, but for some reason I'm receiving duplicate results on one particular environment. The tables are all set up the same, however, so I'm not sure what is causing this behavior.
Any suggestions?
In that mysql server you probably have 2 databases (schemas) with the same structure or you have multi-column foreign keys. Include the table_schema field in the select list of the outer query to confirm.
Use a multi-column in operator or inner join instead of multiple single column in operators in the where clause of your query.
I have an inno-db table with one primary key which is the auto-increment column. I am using the following query to find the next auto-increment id:
SELECT Auto_increment FROM information_schema.tables WHERE table_name='mytable';
However, when I view the results, I get two rows returned with two values (1352, 123841). I've tried resetting the auto-increment value and even deleted the rows below 2000. How can reset the table to return a single result?
You just might have two databases in the MySQL Instance with the same table name.
SELECT table_schema, Auto_increment
FROM information_schema.tables WHERE table_name='mytable';
If you are asking about the database you are standing in, run this
SELECT Auto_increment
FROM information_schema.tables
WHERE table_name='mytable'
AND table_schema=DATABASE();
Give it a Try !!!
You probably have two tables in your db. Try to check the schema and you will find the reason:-
SELECT table_schema, Auto_increment
FROM information_schema.tables
WHERE table_name='mytable';
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.