Keine Cache-Version

Caching deaktiviert Standardeinstellung für diese Seite:aktiviert (code DEF204)
Wenn die Anzeige zu langsam ist, können Sie den Benutzermodus deaktivieren, um die zwischengespeicherte Version anzuzeigen.

Rechercher dans le manuel MySQL

12.20.4 Detection of Functional Dependence

The following discussion provides several examples of the ways in which MySQL detects functional dependencies. The examples use this notation:

  1. {X} -> {Y}

Understand this as X uniquely determines Y, which also means that Y is functionally dependent on X.

The examples use the world database, which can be downloaded from https://dev.mysql.com/doc/index-other.html. You can find details on how to install the database on the same page.

Functional Dependencies Derived from Keys

The following query selects, for each country, a count of spoken languages:

  1. SELECT co.Name, COUNT(*)
  2. FROM countrylanguage cl, country co
  3. WHERE cl.CountryCode = co.Code
  4. GROUP BY co.Code;

co.Code is a primary key of co, so all columns of co are functionally dependent on it, as expressed using this notation:

  1. {co.Code} -> {co.*}

Thus, co.name is functionally dependent on GROUP BY columns and the query is valid.

A UNIQUE index over a NOT NULL column could be used instead of a primary key and the same functional dependence would apply. (This is not true for a UNIQUE index that permits NULL values because it permits multiple NULL values and in that case uniqueness is lost.)

Functional Dependencies Derived from Multiple-Column Keys and from Equalities

This query selects, for each country, a list of all spoken languages and how many people speak them:

  1. SELECT co.Name, cl.Language,
  2. cl.Percentage * co.Population / 100.0 AS SpokenBy
  3. FROM countrylanguage cl, country co
  4. WHERE cl.CountryCode = co.Code
  5. GROUP BY cl.CountryCode, cl.Language;

The pair (cl.CountryCode, cl.Language) is a two-column composite primary key of cl, so that column pair uniquely determines all columns of cl:

  1. {cl.CountryCode, cl.Language} -> {cl.*}

Moreover, because of the equality in the WHERE clause:

  1. {cl.CountryCode} -> {co.Code}

And, because co.Code is primary key of co:

  1. {co.Code} -> {co.*}

Uniquely determines relationships are transitive, therefore:

  1. {cl.CountryCode, cl.Language} -> {cl.*,co.*}

As a result, the query is valid.

As with the previous example, a UNIQUE key over NOT NULL columns could be used instead of a primary key.

An INNER JOIN condition can be used instead of WHERE. The same functional dependencies apply:

  1. SELECT co.Name, cl.Language,
  2. cl.Percentage * co.Population/100.0 AS SpokenBy
  3. FROM countrylanguage cl INNER JOIN country co
  4. ON cl.CountryCode = co.Code
  5. GROUP BY cl.CountryCode, cl.Language;

Inhaltsverzeichnis Haut

Functional Dependency Special Cases

Whereas an equality test in a WHERE condition or INNER JOIN condition is symmetric, an equality test in an outer join condition is not, because tables play different roles.

Assume that referential integrity has been accidentally broken and there exists a row of countrylanguage without a corresponding row in country. Consider the same query as in the previous example, but with a LEFT JOIN:

  1. SELECT co.Name, cl.Language,
  2. cl.Percentage * co.Population/100.0 AS SpokenBy
  3. FROM countrylanguage cl LEFT JOIN country co
  4. ON cl.CountryCode = co.Code
  5. GROUP BY cl.CountryCode, cl.Language;

For a given value of cl.CountryCode, the value of co.Code in the join result is either found in a matching row (determined by cl.CountryCode) or is NULL-complemented if there is no match (also determined by cl.CountryCode). In each case, this relationship applies:

  1. {cl.CountryCode} -> {co.Code}

cl.CountryCode is itself functionally dependent on {cl.CountryCode, cl.Language} which is a primary key.

If in the join result co.Code is NULL-complemented, co.Name is as well. If co.Code is not NULL-complemented, then because co.Code is a primary key, it determines co.Name. Therefore, in all cases:

  1. {co.Code} -> {co.Name}

Which yields:

  1. {cl.CountryCode, cl.Language} -> {cl.*,co.*}

As a result, the query is valid.

However, suppose that the tables are swapped, as in this query:

  1. SELECT co.Name, cl.Language,
  2. cl.Percentage * co.Population/100.0 AS SpokenBy
  3. FROM country co LEFT JOIN countrylanguage cl
  4. ON cl.CountryCode = co.Code
  5. GROUP BY cl.CountryCode, cl.Language;

Now this relationship does not apply:

  1. {cl.CountryCode, cl.Language} -> {cl.*,co.*}

Indeed, all NULL-complemented rows made for cl will be put into a single group (they have both GROUP BY columns equal to NULL), and inside this group the value of co.Name can vary. The query is invalid and MySQL rejects it.

Functional dependence in outer joins is thus linked to whether determinant columns belong to the left or right side of the LEFT JOIN. Determination of functional dependence becomes more complex if there are nested outer joins or the join condition does not consist entirely of equality comparisons.

Inhaltsverzeichnis Haut

Functional Dependencies and Views

Suppose that a view on countries produces their code, their name in uppercase, and how many different official languages they have:

  1. CREATE VIEW Country2 AS
  2. SELECT co.Code, UPPER(co.Name) AS UpperName,
  3. COUNT(cl.Language) AS OfficialLanguages
  4. FROM country AS co JOIN countrylanguage AS cl
  5. ON cl.CountryCode = co.Code
  6. WHERE cl.isOfficial = 'T'
  7. GROUP BY co.Code;

This definition is valid because:

  1. {co.Code} -> {co.*}

In the view result, the first selected column is co.Code, which is also the group column and thus determines all other selected expressions:

  1. {Country2.Code} -> {Country2.*}

MySQL understands this and uses this information, as described following.

This query displays countries, how many different official languages they have, and how many cities they have, by joining the view with the city table:

  1. SELECT co2.Code, co2.UpperName, co2.OfficialLanguages,
  2. COUNT(*) AS Cities
  3. FROM country2 AS co2 JOIN city ci
  4. ON ci.CountryCode = co2.Code
  5. GROUP BY co2.Code;

This query is valid because, as seen previously:

  1. {co2.Code} -> {co2.*}

MySQL is able to discover a functional dependency in the result of a view and use that to validate a query which uses the view. The same would be true if country2 were a derived table (or common table expression), as in:

  1. SELECT co2.Code, co2.UpperName, co2.OfficialLanguages,
  2. COUNT(*) AS Cities
  3. (
  4.  SELECT co.Code, UPPER(co.Name) AS UpperName,
  5.  COUNT(cl.Language) AS OfficialLanguages
  6.  FROM country AS co JOIN countrylanguage AS cl
  7.  ON cl.CountryCode=co.Code
  8.  WHERE cl.isOfficial='T'
  9.  GROUP BY co.Code
  10. ) AS co2
  11. JOIN city ci ON ci.CountryCode = co2.Code
  12. GROUP BY co2.Code;

Inhaltsverzeichnis Haut

Combinations of Functional Dependencies

MySQL is able to combine all of the preceding types of functional dependencies (key based, equality based, view based) to validate more complex queries.


Suchen Sie im MySQL-Handbuch

Deutsche Übersetzung

Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.

Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.

Vielen Dank im Voraus.

Dokument erstellt 26/06/2006, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/mysql-rf-group-by-functional-dependence.html

Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.

Referenzen

  1. Zeigen Sie - html-Dokument Sprache des Dokuments:en Manuel MySQL : https://dev.mysql.com/

Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.

Inhaltsverzeichnis Haut