How to get a list of MySQL views? - mysql

I'm looking for a way to list all views in a database.
Initially I found and tried an answer on the MySQL forums:
SELECT table_name
FROM information_schema.views
WHERE information_schema.views.table_schema LIKE 'view%';
How ever this doesn't work, returning an empty set. (I know they're in there!)
These also fail:
mysql> use information_schema;
Database changed
mysql> select * from views;
ERROR 1102 (42000): Incorrect database name 'mysql.bak'
mysql> select * from tables;
ERROR 1102 (42000): Incorrect database name 'mysql.bak'
Why isn't this working?

SHOW FULL TABLES IN database_name WHERE TABLE_TYPE LIKE 'VIEW';
MySQL query to find all views in a database

Here's a way to find all the views in every database on your instance:
SELECT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.tables
WHERE TABLE_TYPE LIKE 'VIEW';

To complement about to get more info about a specific view
Even with the two valid answers
SHOW FULL TABLES IN your_db_name WHERE TABLE_TYPE LIKE 'VIEW';
SELECT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_TYPE LIKE 'VIEW' AND TABLE_SCHEMA LIKE 'your_db_name';
You can apply the following (I think is better):
SELECT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.VIEWS
WHERE TABLE_SCHEMA LIKE 'your_db_name';
is better work directly with information_schema.VIEWS (observe now is VIEWS and not TABLES anymore), thus you can retrieve more data, use DESC VIEWS for more details:
+----------------------+---------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(64) | YES | | NULL | |
| TABLE_SCHEMA | varchar(64) | YES | | NULL | |
| TABLE_NAME | varchar(64) | YES | | NULL | |
| VIEW_DEFINITION | longtext | YES | | NULL | |
| CHECK_OPTION | enum('NONE','LOCAL','CASCADED') | YES | | NULL | |
| IS_UPDATABLE | enum('NO','YES') | YES | | NULL | |
| DEFINER | varchar(93) | YES | | NULL | |
| SECURITY_TYPE | varchar(7) | YES | | NULL | |
| CHARACTER_SET_CLIENT | varchar(64) | NO | | NULL | |
| COLLATION_CONNECTION | varchar(64) | NO | | NULL | |
+----------------------+---------------------------------+------+-----+---------+-------+
For example observe the VIEW_DEFINITION field, thus you can use in action:
SELECT TABLE_SCHEMA, TABLE_NAME, VIEW_DEFINITION
FROM information_schema.VIEWS
WHERE TABLE_SCHEMA LIKE 'your_db_name';
Of course you have more fields available for your consideration.

select * FROM information_schema.views\G;

This will work.
USE INFORMATION_SCHEMA;
SELECT TABLE_SCHEMA, TABLE_NAME
FROM information_schema.tables
WHERE TABLE_TYPE LIKE 'VIEW';

Try moving that mysql.bak directory out of /var/lib/mysql to say /root/ or something. It seems like mysql is finding that and it may be causing that ERROR 1102 (42000): Incorrect database name 'mysql.bak' error.

The error your seeing is probably due to a non-MySQL created directory in MySQL's data directory. MySQL maps the database structure pretty directly onto the file system, databases are mapped to directories and tables are files in those directories.
The name of the non-working database looks suspiciously like someone has copied the mysql database directory to a backup at some point and left it in MySQL's data directory. This isn't a problem as long as you don't try and use the database for anything. Unfortunately the information schema scans all of the databases it finds and finds that this one isn't a real database and gets upset.
The solution is to find the mysql.bak directory on the hard disk and move it well away from MySQL.

If you created any view in Mysql databases then you can simply see it as you see your all tables in your particular database.
write:
--mysql> SHOW TABLES;
you will see list of tables and views of your database.

Another way to find all View:
SELECT DISTINCT table_name FROM information_schema.TABLES WHERE table_type = 'VIEW'

Related

MySQL command output too wide in command-line client [duplicate]

This question already has answers here:
How to best display in Terminal a MySQL SELECT returning too many fields?
(12 answers)
Closed 3 years ago.
I am using mysql command line client in terminal emulator lxterminal in Ubuntu. When I run the following command:
mysql> select * from routines where routine_name = "simpleproc";
The output is a mess:
But if I copy and paste it here, the output shows me a nice table:
mysql> select * from routines where routine_name = "simpleproc";
+---------------+-----------------+----------------+--------------+--------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+----------------+--------------+--------------------------------------------------------+---------------+-------------------+-----------------+------------------+-----------------+----------+---------------+---------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------+----------------+----------------------+----------------------+--------------------+
| SPECIFIC_NAME | ROUTINE_CATALOG | ROUTINE_SCHEMA | ROUTINE_NAME | ROUTINE_TYPE | DATA_TYPE | CHARACTER_MAXIMUM_LENGTH | CHARACTER_OCTET_LENGTH | NUMERIC_PRECISION | NUMERIC_SCALE | DATETIME_PRECISION | CHARACTER_SET_NAME | COLLATION_NAME | DTD_IDENTIFIER | ROUTINE_BODY | ROUTINE_DEFINITION | EXTERNAL_NAME | EXTERNAL_LANGUAGE | PARAMETER_STYLE | IS_DETERMINISTIC | SQL_DATA_ACCESS | SQL_PATH | SECURITY_TYPE | CREATED | LAST_ALTERED | SQL_MODE | ROUTINE_COMMENT | DEFINER | CHARACTER_SET_CLIENT | COLLATION_CONNECTION | DATABASE_COLLATION |
+---------------+-----------------+----------------+--------------+--------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+----------------+--------------+--------------------------------------------------------+---------------+-------------------+-----------------+------------------+-----------------+----------+---------------+---------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------+----------------+----------------------+----------------------+--------------------+
| simpleproc | def | test | simpleproc | PROCEDURE | | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | SQL | BEGIN
SELECT COUNT(*) INTO param1 FROM CUSTOMERS1;
END | NULL | NULL | SQL | NO | CONTAINS SQL | NULL | DEFINER | 2018-01-12 15:18:20 | 2018-01-12 15:18:20 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | | root#localhost | utf8 | utf8_general_ci | latin1_swedish_ci |
+---------------+-----------------+----------------+--------------+--------------+-----------+--------------------------+------------------------+-------------------+---------------+--------------------+--------------------+----------------+----------------+--------------+--------------------------------------------------------+---------------+-------------------+-----------------+------------------+-----------------+----------+---------------+---------------------+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------+-----------------+----------------+----------------------+----------------------+--------------------+
1 row in set (0.01 sec)
I wonder if it is possible to view the output in a terminal emulator as a nice table like this one?
Using mysql's ego command
From mysql's help command:
ego          (\G) Send command to mysql server, display result vertically.
So by appending a \G to your select, you can get a very clean vertical output:
mysql> select * from routines where routine_name = "simpleproc" \G
Using a pager
You can tell MySQL to use the less pager with its -S option that chops wide lines and gives you an output that you can scroll with the arrow keys:
mysql> pager less -S
Thus, next time you run a command with a wide output, MySQL will let you browse the output with the less pager:
mysql> select * from routines where routine_name = "simpleproc";
If you're done with the pager and want to go back to the regular output on stdout, use this:
mysql> nopager
You can try also adjusting the font size of the terminal but displaying the output vertically should be clear if all doesn't.
use the /G option to run the query i.e
mysql> select * from routines where routine_name = "simpleproc" \G
If you are running on Ubuntu you can use the bash shell, it looks nice and not messed up like this.

Select from information_schema tables very slow

Query for * from information_schema.tables is very slow.
Innodb_stats_on_metadata is off, and select table_name from tables is fast, just selecting more fields is very slow (12 minutes!)
mysql> select * from tables limit 1;
+---------------+--------------------+----------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | ENGINE | VERSION | ROW_FORMAT | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | MAX_DATA_LENGTH | INDEX_LENGTH | DATA_FREE | AUTO_INCREMENT | CREATE_TIME | UPDATE_TIME | CHECK_TIME | TABLE_COLLATION | CHECKSUM | CREATE_OPTIONS | TABLE_COMMENT |
+---------------+--------------------+----------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| def | information_schema | CHARACTER_SETS | SYSTEM VIEW | MEMORY | 10 | Fixed | NULL | 384 | 0 | 32869632 | 0 | 0 | NULL | 2016-12-19 23:55:46 | NULL | NULL | utf8_general_ci | NULL | max_rows=87381 | |
+---------------+--------------------+----------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
1 row in set (**12 min 27.02 sec**)
Additional information:
mysql> select count(*) from tables;
+----------+
| count(*) |
+----------+
| 194196 |
+----------+
1 row in set (0.57 sec)
mysql> show global variables like '%innodb%metada%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_stats_on_metadata | OFF |
+--------------------------+-------+
1 row in set (0.00 sec)
Selecting more columns means the server has to do more work -- interrogating the storage engines for all of the tables in all of the schemas to obtain what you requested.
The tables in information_schema are not real tables. They are server internals, exposed via an SQL interface, in some cases allowing you to query information the server doesn't store and must calculate or gather because you asked. The server code knows what columns you ask for, and only gathers that information.
LIMIT 1 doesn't help, because information_schema doesn't handle LIMIT as you would expect -- the entire table is rendered in memory before the first row is returned and the rest are discarded.
Even in 5.7, the information about the tables is scattered in files on disk. Reading 200K files takes a lot of time.
That is one reason why 200K tables is not a good design. Other reasons have to do with caching -- there are practical limits on such.
You will see variations on timings of I_S queries because of caching.
Advice: Re-think your schema design.
8.0 Stores all that info in an InnoDB table, so it will be enormously faster.

How to get names of mysql tables, columns and their types?

To improve the ease of use, I want my mysql connection wrapper class to automatically determine the type of command properties. I'm assuming the information I need is available though the connector api but I've no clue how, is there a command to get the names and types of columns?
For table name:
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'your database name'
Or
SHOW TABLES IN db_name
For Columns:
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
Choose fields as per your requirements.
Many ways, many columns, pick and choose:
SELECT column_name, data_type,is_nullable,character_maximum_length
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'course' and TABLE_SCHEMA='so_gibberish';
+-------------+-----------+-------------+--------------------------+
| column_name | data_type | is_nullable | character_maximum_length |
+-------------+-----------+-------------+--------------------------+
| course_id | varchar | NO | 8 |
| title | varchar | YES | 50 |
| dept_name | varchar | YES | 20 |
| credits | decimal | YES | NULL |
+-------------+-----------+-------------+--------------------------+

phpmyAdmin gives error while database and tables look good in mySQL CLI

I changed a name of the table from within phpMyAdmin, and immediately it crapped. after that when I try to connect using phpMyAdmin (/phpMyAdmin/index.php) I get error in log:
[Wed Aug 08 14:18:58 2012] [error] Query call failed: Table 'mydb.mychangedtbl' doesn't exist (1146)
mychangedtbl is the table whose name was changed. this issue is only in phpMyAdmin, I am able to access the database and tables find from CLI. I restarted mySQL, but that did not fix. Seems like something is stuck for phpMyAdmin. I restarted browser also but that didnt help either.
when i rename this particular table back to what it was using command line, myphphAmin works fine again. here is the structure of this table:
mysql> DESCRIBE mychangedtbl;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| userid | char(6) | NO | PRI | NULL | |
| userpass | varchar(40) | NO | | NULL | |
| userlevel | char(3) | NO | | o | |
| userpcip | varchar(45) | NO | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
column userpass has Collation = asci_bin which it does not show in above output, other columns are ascii_general_ci
pl advice.
ty.
Rajeev
this was due to the reason that, apache was using the same table to do mysql authentication. i changed apache config and restart. that let me change table name. all good again.

How can I access the table comment from a mysql table?

How can I get just the table comment from a mysql table? I tried the following, but they didn't work for various reasons. I want to figure out how to get just the string 'my comment' (ideally via perl =)
Any help?
-- Abbreviated output for convenience.
SHOW TABLE STATUS WHERE Name="foo"
+------+--------+---------+------------+------+----------------+---------------+
| Name | Engine | Version | Row_format | Rows | Create_options | Comment |
+------+--------+---------+------------+------+----------------+---------------+
| foo | MyISAM | 10 | Fixed | 0 | | my comment |
+------+--------+---------+------------+------+----------------+---------------+
and
SHOW CREATE TABLE foo;
+-------+------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------+
| fooo | CREATE TABLE `fooo` (`id` int(11) NOT NULL PRIMARY KEY) COMMENT='my comment' |
+-------+------------------------------------------------------------------------------+
Based on the answer by OMG Ponies, but using INFORMATION_SCHEMA.TABLES instead of INFORMATION_SCHEMA.COLUMNS. When looking around on the web, all I could find was info on the columns' comments, but never on the table's. This is how to get a table's comment.
SELECT table_comment
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema='my_cool_database'
AND table_name='user_skill';
+--------------------------+
| table_comment |
+--------------------------+
| my awesome comment |
+--------------------------+
If you don't want to have both database name and table name in the query, you can use :
SHOW TABLE STATUS WHERE Name='table_name';
and then pick up the "Comment" key of the result (you have to use an associative function like mysqli_fetch_assoc() in php).