语法
SHOW DATABASES
语法:
USE 数据库名
SHOW TABLES
实例:
mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
语法
DESCRIBE [数据库名.]表名
实例
mysql> describe user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(32) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) unsigned | NO | | 0 | |
| plugin | char(64) | NO | | mysql_native_password | |
| authentication_string | text | YES | | NULL | |
| password_expired | enum('N','Y') | NO | | N | |
| password_last_changed | timestamp | YES | | NULL | |
| password_lifetime | smallint(5) unsigned | YES | | NULL | |
| account_locked | enum('N','Y') | NO | | N | |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)
1、DDL语句可用于创建数据库对象,如库、表、索引等
2、使用DDL语句新建库、表
语法
创建数据库 CREATE DATABASE数据库名
创建数据表 CREATE TABLE表名(字段定义.....
使用DDL语句删除库、表
DROP TABLE [数据库名.]表名
DROP DATABASE数据库名
mysql> drop table auth.users;
Query OK, 0 rows affected (0.01 sec)
mysql> drop database auth;
Query OK, 0 rows affected (0.00 sec)
1、DML语句用于对表中的数据进行管理
2、包括的操作
1、向数据表中插入新的数据记录
INSERT INTO表名(字段1,字段2, ....)
VALUES(字段1的值,字段2的值,.....)
顺序一致数量相同用逗号进行分割
mysql> use auth;
mysq|l> insert into users(user_ name,user_ passwd)
values('zhangsan'PASSWORD('123456'));
Query OK, 1 row affected (0.01 sec)
mysq|> use auth;
mysql> insert into users values('lisi' ,PASSWORD('654321'));
Query OK, 1 row affected (0.00 sec)
2、修改、更新数据表中的数据记录
UPDATE 表名 SET 字段名1=值1 [字段名2=值2] WHERE 条件表达式
mysql> update auth.users set user passwd=PASSWORD(") where
user_ name="lisi;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
my
mysql> update mysql.user set authentication_string=PASSWORD('123457') where user='root'; //通过直接修改数据库的方式实现修改数据库管理员密码
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
DELETE FROM 表名 WHERE 条件表达式
mysq|> delete from auth.users where user name='lisi';
Query OK, 1 row affected (0.00 sec)
4、不带where条件的语句表示删除表中所有记录
mysql> delete from auth.users;
Query oK, 1 row affected (0.00 sec)
不带where子句时需谨慎操作
1、DQL是数据查询语句,只有SELECT
2、用于从数据表中查找符合条件的数据记录
3、查询时可不指定条件
where——指条件
SELECT 字段名1,字段名2.... FROM 表名
mysq|l> select * from auth.users;
DELETE FROM tablename //两者的新值初始ID不同
TRUNCATE TABLE tablename
mysql> truncate table tmp;
临时建立的表,用于保存一些临时数据,不会长期存在,连接断开,临时表被删除,括号内约束条件前也要加逗号
mysq|> CREATE TEMPORARY TABLE 'mytmp' (
'id' int(10) NOT NULL AUTO_ INCREMENT,
'NAME' varchar(32) CHARACTER SET utf8 COLLATE utf8_bin
NOT NULL,
'level' int(10) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> create table test like mytmp, //复制生成表结构, 无数据
mysql> insert into test select * from mytmp; //把数据全部导入到新表中
mysql> show create table mytmp\G
mysql> create table test(.....);
mysql> insert into test select * from mytmp;
1、DCL语句设置用户权限(用户不存在时,则新建用户)
GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [ IDENTIFIED BY '密码' ]
mysql> GRANT select ON auth." TO xiaqi'@locahost IDENTIFIED BY '123456'; //允许小七登录查询数据库的表
Query OK, 0 rows affected (0.00 sec)
2、查看用户的权限
语法
SHOW GRANTS FOR 用户名@来源地址
例子
mysql> SHOW GRANTS FOR 'dbuser'@'192. 168.4.19';
3、撤销用户的权限
REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址
例子
mysql> REVOKE all ON auth.* FROM 'xiaoqi'@'localhost';