MySQL中INFORMATION_SCHEMA是什么?(1)

在获取自增ID时,我用到了以下语句:

select auto_increment from information_schema.tables where table_name = "表名";

仔细一看,这其实就是一条查询语句,查询了information_schema数据库下的"tables"表,里面 以 table_name  为 "表名"  的一行记录的  auto_increment字段的值

那么information_schema数据库是什么呢?

【INFORMATION_SCHEMA 数据库】 是MySQL自带的,它提供了访问数据库 元数据 的方式。什么是 元数据 呢?元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等。
有些时候用于表述该信息的其他术语包括“数据词典”和“系统目录”。
在MySQL中,把【INFORMATION_SCHEMA】 看作是一个数据库,确切说是信息数据库。其中保存着关于MySQL服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等。
【INFORMATION_SCHEMA 】中,有数个 只读 表。它们实际上是 视图 ,而不是基本表,因此,你将无法看到与之相关的任何文件。

上文说information_schema中有可读表 ! 那我们来看看能不能从里面    auto_increment来!


选择information_schema数据库:

use information_schema;

查询(information_schema数据库里面的)tables表中所有的自增ID

select auto_increment from tables;

OK整个数据库中所有的自增ID已经查询出来,每一行都代表一个数据表!!(有很多null,是因为该表没有自增主键!)


如果我们想要查询指定表的自增ID,可以用下列语句:

select auto_increment from tables where table_name='表名';

当然如果有同名的数据表,查出来的可就不只是一条记录了。可以加上指定数据库的条件。

select auto_increment from tables where table_schema='数据库名' and table_name='表名';

OK大功告成,我想直接修改指定数据表的自增ID,采用以下语句:

update tables set auto_increment = 27 where table_schema='数据库' and table_name='表名';

可是为什么报了如下错误呢:

Error Code: 1044. Access denied for user 'root'@'localhost' to database 'information_schema'

真正修改auto_increment的办法是:

alter table 表名 auto_increment = 数字;

  

原因很简单information_schema是只读表,不能修改!

查看更多information_schema数据库的内容请移步   MySQL中INFORMATION_SCHEMA是什么?(2)

原文地址:https://www.cnblogs.com/drake-guo/p/6099389.html