検索

如何创建MySQL5的视图

Jun 07, 2016 pm 04:04 PM
creater作成する基本どうやってビュー文法

基本语法: CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [( column_list )] AS select_statement [WITH [CASCADED | LOCAL] CHECK OPTION] This statement creates a new view, or replaces an existing one if the

<STRONG>基本语法:</STRONG>
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    VIEW <EM class=replaceable><CODE>view_name</CODE></EM> [(<EM class=replaceable><CODE>column_list</CODE></EM>)]
    AS <EM class=replaceable><CODE>select_statement</CODE></EM>
    [WITH [CASCADED | LOCAL] CHECK OPTION]

This statement creates a new view, or replaces an existing one if the <font face="新宋体">OR REPLACE</font> clause is given. The <font face="新宋体">select_statement</font> is a <font face="新宋体">SELECT</font> statement that provides the definition of the view. The statement can select from base tables or other views.

This statement requires the <font face="新宋体">CREATE VIEW</font> privilege for the view, and some privilege for each column selected by the <font face="新宋体">SELECT</font> statement. For columns used elsewhere in the <font face="新宋体">SELECT</font> statement you must have the <font face="新宋体">SELECT</font> privilege. If the <font face="新宋体">OR REPLACE</font> clause is present, you must also have the <font face="新宋体">DELETE</font> privilege for the view.

A view belongs to a database. By default, a new view is created in the current database. To create the view explicitly in a given database, specify the name as <font face="新宋体">db_name.view_name</font> when you create it.

mysql> <STRONG class=userinput><CODE>CREATE VIEW test.v AS SELECT * FROM t;</CODE></STRONG>

Tables and views share the same namespace within a database, so a database cannot contain a table and a view that have the same name.

Views must have unique column names with no duplicates, just like base tables. By default, the names of the columns retrieved by the <font face="新宋体">SELECT</font> statement are used for the view column names. To define explicit names for the view columns, the optional <font face="新宋体">column_list</font> clause can be given as a list of comma-separated identifiers. The number of names in <font face="新宋体">column_list</font> must be the same as the number of columns retrieved by the <font face="新宋体">SELECT</font> statement.

Columns retrieved by the <font face="新宋体">SELECT</font> statement can be simple references to table columns. They can also be expressions that use functions, constant values, operators, and so forth.

Unqualified table or view names in the <font face="新宋体">SELECT</font> statement are interpreted with respect to the default database. A view can refer to tables or views in other databases by qualifying the table or view name with the proper database name.

A view can be created from many kinds of <font face="新宋体">SELECT</font> statements. It can refer to base tables or other views. It can use joins, <font face="新宋体">UNION</font>, and subqueries. The <font face="新宋体">SELECT</font> need not even refer to any tables. The following example defines a view that selects two columns from another table, as well as an expression calculated from those columns:

mysql> <STRONG class=userinput><CODE>CREATE TABLE t (qty INT, price INT);</CODE></STRONG>
mysql> <STRONG class=userinput><CODE>INSERT INTO t VALUES(3, 50);</CODE></STRONG>
mysql> <STRONG class=userinput><CODE>CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;</CODE></STRONG>
mysql> <STRONG class=userinput><CODE>SELECT * FROM v;</CODE></STRONG>
+------+-------+-------+
| qty  | price | value |
+------+-------+-------+
|    3 |    50 |   150 |
+------+-------+-------+

A view definition is subject to the following restrictions:

  • The <font face="新宋体">SELECT</font> statement cannot contain a subquery in the <font face="新宋体">FROM</font> clause.

  • The <font face="新宋体">SELECT</font> statement cannot refer to system or user variables.

  • The <font face="新宋体">SELECT</font> statement cannot refer to prepared statement parameters.

  • Within a stored routine, the definition cannot refer to routine parameters or local variables.

  • Any table or view referred to in the definition must exist. However, after a view has been created, it is possible to drop a table or view that the definition refers to. To check a view definition for problems of this kind, use the <font face="新宋体">CHECK TABLE</font> statement.

  • The definition cannot refer to a <font face="新宋体">TEMPORARY</font> table, and you cannot create a <font face="新宋体">TEMPORARY</font> view.

  • The tables named in the view definition must already exist.

  • You cannot associate a trigger with a view.

<font face="新宋体">ORDER BY</font> is allowed in a view definition, but it is ignored if you select from a view using a statement that has its own <font face="新宋体">ORDER BY</font>.

For other options or clauses in the definition, they are added to the options or clauses of the statement that references the view, but the effect is undefined. For example, if a view definition includes a <font face="新宋体">LIMIT</font> clause, and you select from the view using a statement that has its own <font face="新宋体">LIMIT</font> clause, it is undefined which limit applies. This same principle applies to options such as <font face="新宋体">ALL</font>, <font face="新宋体">DISTINCT</font>, or <font face="新宋体">SQL_SMALL_RESULT</font> that follow the <font face="新宋体">SELECT</font> keyword, and to clauses such as <font face="新宋体">INTO</font>, <font face="新宋体">FOR UPDATE</font>, <font face="新宋体">LOCK IN SHARE MODE</font>, and <font face="新宋体">PROCEDURE</font>.

If you create a view and then change the query processing environment by changing system variables, that may affect the results you get from the view:

mysql> <STRONG class=userinput><CODE>CREATE VIEW v AS SELECT CHARSET(CHAR(65)), COLLATION(CHAR(65));</CODE></STRONG>
Query OK, 0 rows affected (0.00 sec)

mysql> <STRONG class=userinput><CODE>SET NAMES 'latin1';</CODE></STRONG>
Query OK, 0 rows affected (0.00 sec)

mysql> <STRONG class=userinput><CODE>SELECT * FROM v;</CODE></STRONG>
+-------------------+---------------------+
| CHARSET(CHAR(65)) | COLLATION(CHAR(65)) |
+-------------------+---------------------+
| latin1            | latin1_swedish_ci   |
+-------------------+---------------------+
1 row in set (0.00 sec)

mysql> <STRONG class=userinput><CODE>SET NAMES 'utf8';</CODE></STRONG>
Query OK, 0 rows affected (0.00 sec)

mysql> <STRONG class=userinput><CODE>SELECT * FROM v;</CODE></STRONG>
+-------------------+---------------------+
| CHARSET(CHAR(65)) | COLLATION(CHAR(65)) |
+-------------------+---------------------+
| utf8              | utf8_general_ci     |
+-------------------+---------------------+
1 row in set (0.00 sec)

The optional <font face="新宋体">ALGORITHM</font> clause is a MySQL extension to standard SQL. <font face="新宋体">ALGORITHM</font> takes three values: <font face="新宋体">MERGE</font>, <font face="新宋体">TEMPTABLE</font>, or <font face="新宋体">UNDEFINED</font>. The default algorithm is <font face="新宋体">UNDEFINED</font> if no <font face="新宋体">ALGORITHM</font> clause is present. The algorithm affects how MySQL processes the view.

For <font face="新宋体">MERGE</font>, the text of a statement that refers to the view and the view definition are merged such that parts of the view definition replace corresponding parts of the statement.

For <font face="新宋体">TEMPTABLE</font>, the results from the view are retrieved into a temporary table, which then is used to execute the statement.

For <font face="新宋体">UNDEFINED</font>, MySQL chooses which algorithm to use. It prefers <font face="新宋体">MERGE</font> over <font face="新宋体">TEMPTABLE</font> if possible, because <font face="新宋体">MERGE</font> is usually more efficient and because a view cannot be updatable if a temporary table is used.

A reason to choose <font face="新宋体">TEMPTABLE</font> explicitly is that locks can be released on underlying tables after the temporary table has been created and before it is used to finish processing the statement. This might result in quicker lock release than the <font face="新宋体">MERGE</font> algorithm so that other clients that use the view are not blocked as long.

A view algorithm can be <font face="新宋体">UNDEFINED</font> three ways:

  • No <font face="新宋体">ALGORITHM</font> clause is present in the <font face="新宋体">CREATE VIEW</font> statement.

  • The <font face="新宋体">CREATE VIEW</font> statement has an explicit <font face="新宋体">ALGORITHM = UNDEFINED</font> clause.

  • <font face="新宋体">ALGORITHM = MERGE</font> is specified for a view that can be processed only with a temporary table. In this case, MySQL generates a warning and sets the algorithm to <font face="新宋体">UNDEFINED</font>.

As mentioned earlier, <font face="新宋体">MERGE</font> is handled by merging corresponding parts of a view definition into the statement that refers to the view. The following examples briefly illustrate how the <font face="新宋体">MERGE</font> algorithm works. The examples assume that there is a view <font face="新宋体">v_merge</font> that has this definition:

CREATE ALGORITHM = MERGE VIEW v_merge (vc1, vc2) AS
SELECT c1, c2 FROM t WHERE c3 > 100;

Example 1: Suppose that we issue this statement:

SELECT * FROM v_merge;

MySQL handles the statement as follows:

  • <font face="新宋体">v_merge</font> becomes <font face="新宋体">t</font>

  • <font face="新宋体">*</font> becomes <font face="新宋体">vc1, vc2</font>, which corresponds to <font face="新宋体">c1, c2</font>

  • The view <font face="新宋体">WHERE</font> clause is added

The resulting statement to be executed becomes:

SELECT c1, c2 FROM t WHERE c3 > 100;

Example 2: Suppose that we issue this statement:

SELECT * FROM v_merge WHERE vc1 < 100;

This statement is handled similarly to the previous one, except that <font face="新宋体">vc1 </font> becomes <font face="新宋体">c1 </font> and the view <font face="新宋体">WHERE</font> clause is added to the statement <font face="新宋体">WHERE</font> clause using an <font face="新宋体">AND</font> connective (and parentheses are added to make sure the parts of the clause are executed with correct precedence). The resulting statement to be executed becomes:

SELECT c1, c2 FROM t WHERE (c3 > 100) AND (c1 < 100);

Effectively, the statement to be executed has a <font face="新宋体">WHERE</font> clause of this form:

WHERE (select WHERE) AND (view WHERE)

The <font face="新宋体">MERGE</font> algorithm requires a one-to relationship between the rows in the view and the rows in the underlying table. If this relationship does not hold, a temporary table must be used instead. Lack of a one-to-one relationship occurs if the view contains any of a number of constructs:

  • Aggregate functions (<font face="新宋体">SUM()</font>, <font face="新宋体">MIN()</font>, <font face="新宋体">MAX()</font>, <font face="新宋体">COUNT()</font>, and so forth)

  • <font face="新宋体">DISTINCT</font>

  • <font face="新宋体">GROUP BY</font>

  • <font face="新宋体">HAVING</font>

  • <font face="新宋体">UNION</font> or <font face="新宋体">UNION ALL</font>

  • Refers only to literal values (in this case, there is no underlying table)

Some views are updatable. That is, you can use them in statements such as <font face="新宋体">UPDATE</font>, <font face="新宋体">DELETE</font>, or <font face="新宋体">INSERT</font> to update the contents of the underlying table. For a view to be updatable, there must be a one-to relationship between the rows in the view and the rows in the underlying table. There are also certain other constructs that make a view non-updatable. To be more specific, a view is not updatable if it contains any of the following:

  • Aggregate functions (<font face="新宋体">SUM()</font>, <font face="新宋体">MIN()</font>, <font face="新宋体">MAX()</font>, <font face="新宋体">COUNT()</font>, and so forth)

  • <font face="新宋体">DISTINCT</font>

  • <font face="新宋体">GROUP BY</font>

  • <font face="新宋体">HAVING</font>

  • <font face="新宋体">UNION</font> or <font face="新宋体">UNION ALL</font>

  • Subquery in the select list

  • Join

  • Non-updatable view in the <font face="新宋体">FROM</font> clause

  • A subquery in the <font face="新宋体">WHERE</font> clause that refers to a table in the <font face="新宋体">FROM</font> clause

  • Refers only to literal values (in this case, there is no underlying table to update)

  • <font face="新宋体">ALGORITHM = TEMPTABLE</font> (use of a temporary table always makes a view non-updatable)

With respect to insertability (being updatable with <font face="新宋体">INSERT</font> statements), an updatable view is insertable if it also satisfies these additional requirements for the view columns:

  • There must be no duplicate view column names.

  • The view must contain all columns in the base table that do not have a default value.

  • The view columns must be simple column references and not derived columns. A derived column is one that is not a simple column reference but is derived from an expression. These are examples of derived columns:

    3.14159
    col1 + 3
    UPPER(col2)
    col3 / col4
    (<EM class=replaceable><CODE>subquery</CODE></EM>)
    

A view that has a mix of simple column references and derived columns is not insertable, but it can be updatable if you update only those columns that are not derived. Consider this view:

CREATE VIEW v AS SELECT col1, 1 AS col2 FROM t;

This view is not insertable because <font face="新宋体">col2</font> is derived from an expression. But it is updatable if the update does not try to update <font face="新宋体">col2</font>. This update is allowable:

UPDATE v SET col1 = 0;

This update is not allowable because it attempts to update a derived column:

UPDATE v SET col2 = 0;

It is sometimes possible for a multiple-table view to be updatable, assuming that it can be processed with the <font face="新宋体">MERGE</font> algorithm. For this to work, the view must use an inner join (not an outer join or a <font face="新宋体">UNION</font>). Also, only a single table in the view definition can be updated, so the <font face="新宋体">SET</font> clause must name only columns from one of the tables in the view. Views that use <font face="新宋体">UNION ALL</font> are disallowed even though they might be theoretically updatable, because the implementation uses temporary tables to process them.

For a multiple-table updatable view, <font face="新宋体">INSERT</font> can work if it inserts into a single table. <font face="新宋体">DELETE</font> is not supported.

The <font face="新宋体">WITH CHECK OPTION</font> clause can be given for an updatable view to prevent inserts or updates to rows except those for which the <font face="新宋体">WHERE</font> clause in the <font face="新宋体">select_statement</font> is true.

In a <font face="新宋体">WITH CHECK OPTION</font> clause for an updatable view, the <font face="新宋体">LOCAL</font> and <font face="新宋体">CASCADED</font> keywords determine the scope of check testing when the view is defined in terms of another view. <font face="新宋体">LOCAL</font> keyword restricts the <font face="新宋体">CHECK OPTION</font> only to the view being defined. <font face="新宋体">CASCADED</font> causes the checks for underlying views to be evaluated as well. When neither keyword is given, the default is <font face="新宋体">CASCADED</font>. Consider the definitions for the following table and set of views:

mysql> <STRONG class=userinput><CODE>CREATE TABLE t1 (a INT);</CODE></STRONG>
mysql> <STRONG class=userinput><CODE>CREATE VIEW v1 AS SELECT * FROM t1 WHERE a < 2</CODE></STRONG>
    -> <STRONG class=userinput><CODE>WITH CHECK OPTION;</CODE></STRONG>
mysql> <STRONG class=userinput><CODE>CREATE VIEW v2 AS SELECT * FROM v1 WHERE a > 0</CODE></STRONG>
    -> <STRONG class=userinput><CODE>WITH LOCAL CHECK OPTION;</CODE></STRONG>
mysql> <STRONG class=userinput><CODE>CREATE VIEW v3 AS SELECT * FROM v1 WHERE a > 0</CODE></STRONG>
    -> <STRONG class=userinput><CODE>WITH CASCADED CHECK OPTION;</CODE></STRONG>

Here the <font face="新宋体">v2</font> and <font face="新宋体">v3</font> views are defined in terms of another view, <font face="新宋体">v1</font>. <font face="新宋体">v2</font> has a <font face="新宋体">LOCAL</font> check option, so inserts are tested only against the <font face="新宋体">v2</font> check. <font face="新宋体">v3</font> has a <font face="新宋体">CASCADED</font> check option, so inserts are tested not only against its own check, but against those of underlying views. The following statements illustrate these differences:

ql> INSERT INTO v2 VALUES (2);
Query OK, 1 row affected (0.00 sec)
mysql> <STRONG class=userinput><CODE>INSERT INTO v3 VALUES (2);</CODE></STRONG>
ERROR 1369 (HY000): CHECK OPTION failed 'test.v3'

The updatability of views may be affected by the value of the <font face="新宋体">updatable_views_with_limit</font> system variable. (完)


声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
MySQLの場所:データベースとプログラミングMySQLの場所:データベースとプログラミングApr 13, 2025 am 12:18 AM

データベースとプログラミングにおけるMySQLの位置は非常に重要です。これは、さまざまなアプリケーションシナリオで広く使用されているオープンソースのリレーショナルデータベース管理システムです。 1)MySQLは、効率的なデータストレージ、組織、および検索機能を提供し、Web、モバイル、およびエンタープライズレベルのシステムをサポートします。 2)クライアントサーバーアーキテクチャを使用し、複数のストレージエンジンとインデックスの最適化をサポートします。 3)基本的な使用には、テーブルの作成とデータの挿入が含まれ、高度な使用法にはマルチテーブル結合と複雑なクエリが含まれます。 4)SQL構文エラーやパフォーマンスの問題などのよくある質問は、説明コマンドとスロークエリログを介してデバッグできます。 5)パフォーマンス最適化方法には、インデックスの合理的な使用、最適化されたクエリ、およびキャッシュの使用が含まれます。ベストプラクティスには、トランザクションと準備された星の使用が含まれます

MySQL:中小企業から大企業までMySQL:中小企業から大企業までApr 13, 2025 am 12:17 AM

MySQLは、中小企業に適しています。 1)中小企業は、顧客情報の保存など、基本的なデータ管理にMySQLを使用できます。 2)大企業はMySQLを使用して、大規模なデータと複雑なビジネスロジックを処理して、クエリのパフォーマンスとトランザクション処理を最適化できます。

Phantomの読み取りとは何ですか?Innodbはどのようにそれらを防ぐ(次のキーロック)?Phantomの読み取りとは何ですか?Innodbはどのようにそれらを防ぐ(次のキーロック)?Apr 13, 2025 am 12:16 AM

INNODBは、次のキーロックメカニズムを通じてファントムの読み取りを効果的に防止します。 1)Next-KeyLockingは、Row LockとGap Lockを組み合わせてレコードとギャップをロックして、新しいレコードが挿入されないようにします。 2)実際のアプリケーションでは、クエリを最適化して分離レベルを調整することにより、ロック競争を削減し、並行性パフォーマンスを改善できます。

mysql:プログラミング言語ではありませんが...mysql:プログラミング言語ではありませんが...Apr 13, 2025 am 12:03 AM

MySQLはプログラミング言語ではありませんが、そのクエリ言語SQLにはプログラミング言語の特性があります。1。SQLは条件付き判断、ループ、可変操作をサポートします。 2。ストアドプロシージャ、トリガー、機能を通じて、ユーザーはデータベースで複雑な論理操作を実行できます。

MySQL:世界で最も人気のあるデータベースの紹介MySQL:世界で最も人気のあるデータベースの紹介Apr 12, 2025 am 12:18 AM

MySQLはオープンソースのリレーショナルデータベース管理システムであり、主にデータを迅速かつ確実に保存および取得するために使用されます。その実用的な原則には、クライアントリクエスト、クエリ解像度、クエリの実行、返品結果が含まれます。使用法の例には、テーブルの作成、データの挿入とクエリ、および参加操作などの高度な機能が含まれます。一般的なエラーには、SQL構文、データ型、およびアクセス許可、および最適化の提案には、インデックスの使用、最適化されたクエリ、およびテーブルの分割が含まれます。

MySQLの重要性:データストレージと管理MySQLの重要性:データストレージと管理Apr 12, 2025 am 12:18 AM

MySQLは、データストレージ、管理、クエリ、セキュリティに適したオープンソースのリレーショナルデータベース管理システムです。 1.さまざまなオペレーティングシステムをサポートし、Webアプリケーションやその他のフィールドで広く使用されています。 2。クライアントサーバーアーキテクチャとさまざまなストレージエンジンを通じて、MySQLはデータを効率的に処理します。 3.基本的な使用には、データベースとテーブルの作成、挿入、クエリ、データの更新が含まれます。 4.高度な使用には、複雑なクエリとストアドプロシージャが含まれます。 5.一般的なエラーは、説明ステートメントを介してデバッグできます。 6.パフォーマンスの最適化には、インデックスの合理的な使用と最適化されたクエリステートメントが含まれます。

なぜMySQLを使用するのですか?利点と利点なぜMySQLを使用するのですか?利点と利点Apr 12, 2025 am 12:17 AM

MySQLは、そのパフォーマンス、信頼性、使いやすさ、コミュニティサポートに選択されています。 1.MYSQLは、複数のデータ型と高度なクエリ操作をサポートし、効率的なデータストレージおよび検索機能を提供します。 2.クライアントサーバーアーキテクチャと複数のストレージエンジンを採用して、トランザクションとクエリの最適化をサポートします。 3.使いやすく、さまざまなオペレーティングシステムとプログラミング言語をサポートしています。 4.強力なコミュニティサポートを提供し、豊富なリソースとソリューションを提供します。

InnoDBロックメカニズム(共有ロック、排他的ロック、意図ロック、レコードロック、ギャップロック、次のキーロック)を説明します。InnoDBロックメカニズム(共有ロック、排他的ロック、意図ロック、レコードロック、ギャップロック、次のキーロック)を説明します。Apr 12, 2025 am 12:16 AM

INNODBのロックメカニズムには、共有ロック、排他的ロック、意図ロック、レコードロック、ギャップロック、次のキーロックが含まれます。 1.共有ロックにより、トランザクションは他のトランザクションが読み取らないようにデータを読み取ることができます。 2.排他的ロックは、他のトランザクションがデータの読み取りと変更を防ぎます。 3.意図ロックは、ロック効率を最適化します。 4。ロックロックインデックスのレコードを記録します。 5。ギャップロックロックインデックス記録ギャップ。 6.次のキーロックは、データの一貫性を確保するためのレコードロックとギャップロックの組み合わせです。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

EditPlus 中国語クラック版

EditPlus 中国語クラック版

サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SublimeText3 Linux 新バージョン

SublimeText3 Linux 新バージョン

SublimeText3 Linux 最新バージョン

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール