getting mysql column from the command line - mysql

I am accessing mysql from cmd, and I want to get all rows from a specific table, but I can't find the exact command to do so.
Example:
SHOW DATABSES #shows all available databases;
CONNECT TEST_DB #I connect to test_db
SHOW TABLES #shows all tables.
What Is missing is how to only see the columns from a specific table.
if I do SELECT * FROM table_test it displays all results instead of just the columns

You may be looking for:
DESCRIBE table_test;
which is same as
EXPLAIN table_test;
See reference in MySQL - 13.8.1 DESCRIBE Syntax which links to MySQL - 13.8.1 EXPLAIN Syntax.

Select a database:
USE YOUR_DB;
To show your tables
SHOW TABLES;
To show columns and rows of a table
DESC YOUR_TABLE;
access a specific field
SELECT YOUR_COLUMN FROM YOUR_TABLE;

Related

Exclude tables in show tables like statement in MySQL

In MySQL database I have some tables which end with _history.
Now I want to select tables that are like _history.
I have done like below.
show tables like '%_history`
Now I got the desired result.
Now in this result I got some tables which start with temp. Ex: temp_102_history.
Is there a way to exclude the tables that start with temp in the show tables like '%_history statement.
Select table_name
from information_schema.tables
Where table_name like '%_history'
and table_name not like 'temp%'
and table_schema='your database'
You can use information_schema database for this.
Try this
SHOW TABLES
WHERE tables_in_test NOT LIKE 'temp_%'
AND tables_in_test LIKE '%_history'
Replace test with your DB name.

how to duplicate all the databases with limited rows in the tables

How can I duplicate my databases with limited number of rows in the tables.
Basically the duplicated db must have the same properties of original database but limited rows in the tables.
Try this, first create a similar table using
CREATE TABLE tbl_name_duplicate LIKE tlb_name;
then insert limited number of records into it using
INSERT INTO tbl_name_duplicate(SELECT * FROM tlb_name LIMIT 10);
to insert 10 records
Another approach, is to use the --where option in the mysqldump, so you could create something similar to a SQL query:
SELECT * FROM table_name WHERE id > (SELECT MAX(id) FROM table_name) - 10
re-written for the mysqldump (but you'll have to dump each table at a time, not the whole database):
mysqldump [options] --where="id > (SELECT MAX(id) FROM table_name) - 10" | mysql --host=host --user=user --password=password some_database
More information at MySQL Reference Guide.

how to row wise manipulate the data from a "show databases" query result set

Suppose I have a database data1 which gives me this:
show tables;
table1
table2
table3
Now instead of individually executing "select * from each table" i want to create a procedure which goes through each database shown in "show databases;" resultset, and then executes select * from each table of that database. I thought of using cursors which would scroll down the resultset, hold each database name in a variable and then execute select statement on each table of that database traversing in the same way. Can someone kindly help me out with how to use cursors in this case, as i am only aware of using cursors for SELECT and UPDATE statements.
btw i use MYSQL.
I'll refrain from asking why you would do this. Here is a general strategy in pseudocode;
[words in parentheses are (SQL commands and tables) which will run on your mySQL server.]
Connect to your mySQL server with your favourite tool/programming language and:
-- (USE information_schema;)
for db in (select distinct table_schema from tables;)
do:
for table in (select table_name from tables where table_schema='$db';)
do:
select field,column,attribute from $table;
done
done
Good luck!
you can get the query from infromation_Schema instead of 'show datbases'

SHOW COLUMNS alias in MySQL

I want to make an alias(as in bash) for the following MySQL query:
SHOW COLUMNS FROM table WHERE Field != 'col_name' AND Field != 'col_name';
I read something about views but it seems that I need a SELECT query to use them.
I want to type only something like: showcols in the MySQL prompt and in the background the above query to be executed, is that possible?
PS: I cannot use DESCRIBE because of the length of some enum fields in the table.
You can replace the show columns with a select from the information_schema database.
SELECT column_name FROM INFORMATION_SCHEMA.`columns`
WHERE column_name not IN ('col1','col2');
Now you can create a view based on this select:
CREATE VIEW as SELECT ......

Existence confirmation method of the table of MySQL

I want to confirm whether there is a certain table.
When create a table, there is an SQL sentence such as DROP TABLE IF EXISTS xxx_tb.
Will there be the method that can identify the existence of the table by SQL likewise?
Use INFORMATION_SCHEMA:
select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'MyTable';
Should be portable across most databases.
You want the SHOW TABLES command of MySQL:
SHOW TABLES LIKE 'xxx_tb';
Or indeed, you can just do a query like
SELECT COUNT(*) FROM tbl WHERE 1=0
Which will give an error (see documentation for exact error code, or try it) if the table doesn't exist, but succeed with no results if it does.