学习SQL:INFORMATION_SCHEMA数据库

The best way how to explain what the INFORMATION_SCHEMA database is would be – “This is the database about databases. It’s used to store details of other databases on the server”. What does that mean, how we can use it, and what we can do with this data is the topic of today’s article.

解释什么是INFORMATION_SCHEMA数据库的最佳方法是-“这是有关数据库的数据库。 它用于在服务器上存储其他数据库的详细信息。” 这是什么意思,我们如何使用它,以及我们如何使用这些数据是今天文章的主题。

该模型 (The Model)

As always, we’ll start with the data model first. This is the same model we’ve used so far in this series, so I won’t describe it again.

与往常一样,我们将首先从数据模型开始。 这是我们到目前为止在本系列中使用过的相同模型,因此我不再赘述。

学习SQL:INFORMATION_SCHEMA数据库_第1张图片

Still, one thing is interesting. Today we won’t be interested in the data stored in tables, but rather how this model is described in the INFORMATION_SCHEMA database.

尽管如此,有一件事很有趣。 今天,我们将不再对表中存储的数据感兴趣,而是对如何在INFORMATION_SCHEMA数据库中描述此模型感兴趣。

什么是INFORMATION_SCHEMA数据库? (What is the INFORMATION_SCHEMA Database?)

The INFORMATION_SCHEMA database is an ANSI standard set of views we can find in SQL Server, but also MySQL. Other database systems also have either exactly such or similar database implemented. It provides the read-only access to details related to databases and their objects (tables, constraints, procedures, views…) stored on the server.

INFORMATION_SCHEMA数据库是ANSI标准视图集,我们可以在SQL Server和MySQL中找到这些视图。 其他数据库系统也具有完全相同或相似的数据库。 它提供对与存储在服务器上的数据库及其对象(表,约束,过程,视图等)相关的详细信息的只读访问。

You could easily use this data to:

您可以轻松地将此数据用于:

  • Check what’s on the server and/or in the database

    检查服务器和/或数据库中的内容
  • Check if everything is as expected (e.g. compared to the last time you’ve performed this check)

    检查一切是否符合预期(例如,与您上次执行此检查的时间相比)
  • Automate processes and build some complex code (e.g. code generators – we’ll talk about this later)

    自动化流程并构建一些复杂的代码(例如,代码生成器–我们稍后将讨论)

Therefore, this database could prove to be very useful in some cases, especially if you’re in the DBA role

因此,在某些情况下,尤其是当您担任DBA角色时,该数据库可能会非常有用。

列出所有数据库 (Listing All Databases)

Maybe the first logical thing to do is to list all databases which are currently on our server. We can do it in a few ways. While these two are not directly related to the usage of the INFORMATION_SCHEMA and are SQL Server-specific, let’s look at them first.

也许要做的第一件事是列出我们服务器上当前的所有数据库。 我们可以通过几种方式做到这一点。 虽然这两个与​​INFORMATION_SCHEMA的使用并不直接相关,并且是特定于SQL Server的,但让我们先来看一下它们。

SELECT * FROM sys.databases;
EXEC sp_databases;

学习SQL:INFORMATION_SCHEMA数据库_第2张图片

You can easily notice that the first query returns many more details (several columns outside this pic) than the second query. It uses a SQL Server-specific sys object. While this works great, it’s very specific, so I’ll go into detail in a separate article. The second statement is the execution of the

你可能感兴趣的:(数据库,python,mysql,java,sql)