Posts

Showing posts with the label MySQL

MySQL search value methods

If you want to find a specific value anywhere in a MySQL database (any column, any table), there are a few common approaches—ranging from quick-and-dirty to more systematic. 1️⃣ Search a known table but any column If you know the table but not the column: SELECT * FROM your_table WHERE col1 = 'value' OR col2 = 'value' OR col3 = 'value'; ⚠️ This requires you to list columns manually. 2️⃣ Search all columns of all tables (automatic way) This is the practical solution when you have no idea where the value lives. Step 1: Generate search queries using INFORMATION_SCHEMA SELECT CONCAT( 'SELECT "', TABLE_NAME, '" AS table_name, "', COLUMN_NAME, '" AS column_name FROM ', TABLE_SCHEMA, '.', TABLE_NAME, ' WHERE ', COLUMN_NAME, ' = ''your_value'';' ) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'your_database'; This generates SQL statem...

Find column in MySQL

SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('columnA','ColumnB') AND TABLE_SCHEMA='YourDatabase';

Find table in MySQL

show tables like '%part_of_table_name%'

Capitalize first letter in SQL

concat(ucase(left( column_name , 1)), substring( column_name , 2))

Set auto increment value in MySQL

ALTER TABLE table_name AUTO_INCREMENT = value ;

Start/Stop MySQL Server on CentOS

Stop MySQL Server # /etc/init.d/mysqld stop Start MySQL Server # /etc/init.d/mysqld start Restart MySQL Server # /etc/init.d/mysqld restart

Show column names in alphabetical order

SELECT c . column_name FROM INFORMATION_SCHEMA . COLUMNS c WHERE c . table_name = 'tbl_name' -- AND c.table_schema = 'db_name' ORDER BY c . column_name

How to see all foreign keys reference to a table column?

select   TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where   REFERENCED_TABLE_NAME = 'table_name'

Count number of distinct occurrences

select column_name, count(*) from table_name group by column_name order by count(*) desc;

Find specific column in table

select table_name from information_schema.columns where column_name in ('column1','column2') and table_schema='yourdatabase';