4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SELECTはDMLに含まれるか? - 結論:多くのケースで含まれる

Last updated at Posted at 2025-02-28

ネタ記事です。

とある記事を読んでふと気になって X にポストしたら、いくつか情報をもらったので、追加の情報を加えて忘れないように書いておきます。

1. DBMS のドキュメントベースの情報

1-1. Oracle の場合

含まれる。

データ操作言語(DML)文は、既存スキーマ・オブジェクトのデータにアクセスし、操作します。次の文は、現在のトランザクションを暗黙的にコミットしません。次に、データ操作言語文を示します。

CALL
DELETE
EXPLAIN PLAN
INSERT
LOCK TABLE
MERGE
SELECT
UPDATE

SELECT文は、DML文の制限された形式であり、データベース内のデータへのアクセスのみが可能です。アクセスしたデータを操作してから問合せの結果を戻すことはできますが、データベースに格納されたデータを操作することはできません。

ただし、パラレル処理においてはクエリと DML は明確に区別するとのこと。確かに ALTER SESSEION ENABLE PARALLEL {QUERY|DML|DDL} は明確に使い分けている。

1-2. MS SQL Server の場合

含まれる。

データ操作言語
データ操作言語 (DML) は、データベースに格納される情報に影響します。 データベースの行を挿入、更新、変更するには、以下のステートメントを使います。

BULK INSERT
DELETE
INSERT
SELECT
UPDATE
MERGE

1-3. MySQL の場合

含まれる?

DML
Data manipulation language, a set of SQL statements for performing INSERT, UPDATE, and DELETE operations. The SELECT statement is sometimes considered as a DML statement, because the SELECT ... FOR UPDATE form is subject to the same considerations for locking as INSERT, UPDATE, and DELETE.

DML statements for an InnoDB table operate in the context of a transaction, so their effects can be committed or rolled back as a single unit.

SELECT ... FOR UPDATE はロックの考慮が必要なため、DML とみなされる場合がある。逆に言うと、FOR UPDATE ではない SELECT は DML ではない?

ちなみに、ロックを考え出すと、SQL Server の READ_COMMITTED_SNAPSHOT = OFF の場合の SELECT の扱いもどうなのかと疑問が湧いてくる。

1-4. PostgreSQL の場合

ドキュメントの "6. Data Manipulation" には SELECT に関する記述はないので、含まれない?

Chapter 6. Data Manipulation
Table of Contents

6.1. Inserting Data
6.2. Updating Data
6.3. Deleting Data
6.4. Returning Data from Modified Rows

"26. High Availability, Load Balancing, and Replication" にも以下のような記載があり、SELECT は含まれていない。(文脈上の問題な気もする)

Data Manipulation Language (DML): INSERT, UPDATE, DELETE, MERGE, COPY FROM, TRUNCATE.
Note that there are no allowed actions that result in a trigger being executed during recovery.
This restriction applies even to temporary tables, because table rows cannot be read or written
without assigning a transaction ID, which is currently not possible in a hot standby environment.

TRUNCATE は DML に含まれるのかという更なる疑問が…

"Acronyms" にも。(Such as は例示なので、含んでいないことを意味しているわけではないが)

DML
Data Manipulation Language14, SQL commands such as INSERT, UPDATE, DELETE

1-5. Snowflake

含まれないっぽい。

General DML

Commands for inserting, deleting, updating, and merging data in Snowflake tables:

  • INSERT
  • INSERT (multi-table)
  • MERGE
  • UPDATE
  • DELETE
  • TRUNCATE TABLE

2. SQL 標準

無料では読めない SQL 標準においては含んでいるらしい。(そういえば SELECT * INTO あったなぁ…)

Note:
As per SQL standard select is in DML list.
They mentioned that select is limited form of DML means select is DQL and select * into is DML.
Data manipulation can be,

  1. Data aggregation
  2. Data Grouping
  3. Data Ordering like ascending or descending
  4. Performing mathematical operations
  5. Data conversion or type conversion
  6. Pattern matching with regex
  7. Condition retrieving of data
  8. Working with CTE

Other than the above list, there are many other operations which can be included in the DML list.

3. まとめ

ドキュメント上では、SELECT は DML に含めることが多いようです。ただし、コミュニケーションをとる際に疑問があれば確認しましょう。

あと、単に SELECT といっても、以下を含むので、ちょっと話がややこしくなりますね。

  • SELECT ... FOR UPDATE
  • SELECT * INTO ...
  • (SQL Server における READ_COMMITTED_SNAPSHOT = OFF 時の SELECT

あと、次は TRUNCATE TABLE は DML か DDL か?という疑問が…

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?