1、登陆退出
登陆
mysql -u root -p
退出
exit
2、检索操作
(1)查看所有数据库
【1】
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
【2】从元数据中查询所有数据库
mysql> select schema_name from information_schema.schemata;
(2) 查看限定数据库
(2、1)全名:
mysql> show databases like 'mysql';
+------------------+
| Database (mysql) |
+------------------+
| mysql |
+------------------+
1 row in set (0.00 sec)
(2、2)特定字符结尾:【%: 表示任何字符出现任意次数 (可以是0次)】
mysql> show databases like '%sql';
+-----------------+
| Database (%sql) |
+-----------------+
| mysql |
+-----------------+
1 row in set (0.00 sec)
(2、3)特定字符开头:
mysql> show databases like 'my%';
+----------------+
| Database (my%) |
+----------------+
| mysql |
+----------------+
1 row in set (0.00 sec)
(2、4)限定字符个数按规定排列【_表示只匹配单个字符,不能多也不能少】
mysql> show databases like '_y_ql';
+------------------+
| Database (_y_ql) |
+------------------+
| mysql |
+------------------+
1 row in set (0.00 sec)
选定数据库:
use db_name
(3)查看表
【1】查询当前使用use后连接的数据库db_name的表。【使用from db_name可以不先使用use选定数据库】
show tables 【from db_name】;
【2】从表元数据中查询【数据库为db_name】所有表名
mysql> select table_name from information_schema.tables 【where table_schema='db_name'】;
【3】返回所有数据
mysql> select * from information_schema.tables where table_schema='mysql';
举栗子:
(3、1)不使用use选定数据库查看表
mysql> show tables from mysql;
mysql> show tables from mysql like 'h%';
mysql> select table_name from information_schema.tables ;
mysql> select table_name from information_schema.tables where table_schema='mysql';
mysql> select table_name from information_schema.tables where table_schema='mysql' and table_name like '%b';
mysql> select table_name from information_schema.tables where table_schema='mysql' and table_name like '_s_r';
(3、2)查看表前先use选定数据库:
mysql> use mysql;
Database changed
mysql> show tables;
查看所有表mysql> show tables;
(4)查询字段
【1】SHOW 【FULL】 COLUMNS FROM db_name.table_name
mysql> show columns from mysql.db;
【2】使用前使用use选定数据库
mysql> SHOW 【full】 COLUMNS FROM db;
【3】从列元数据中查询【条件为数据库为mysql,表名为db的】字段名
mysql> select column_name from information_schema.columns where table_schema='mysql' and table_name='db';
【4】查看表结构
DESCRIBE 【DESC 】 table_name
(5)查询字段内容
【1】select column_name from db_name.table_name
mysql> select Db from mysql.db;
【2】mysql> select column_name from db_mane;【使用前用use选定数据库】
mysql> select DB from db;