关于SQL|DDL, DML, DCL and TCL 命令

本文详细解析了SQL语言的四大分类:数据定义语言(DDL)、数据操作语言(DML)、数据控制语言(DCL)及事务控制语言(TCL),并列举了各类语言中的常见命令及其用途。

目前网上讲解五花八门,这里援引一下Wiki和某公网的总结

援引Wiki(不贴地址了)

1.A data definition language or data description language (DDL) is a syntax similar to a computer programming language for defining data structures, especially database schemas.

2.A data manipulation language (DML) is a computer programming language used for adding (inserting), deleting, and modifying (updating) data in a database. A DML is often a sublanguage of a broader database language such as SQL, with the DML comprising some of the operators in the language.[1] Read-only selecting of data is sometimes distinguished as being part of a separate data query language (DQL), but it is closely related and sometimes also considered a component of a DML; some operators may perform both selecting (reading) and writing.

3.A data control language (DCL) is a syntax similar to a computer programming language used to control access to data stored in a database (Authorization). In particular, it is a component of Structured Query Language (SQL).

 

援引网站:https://www.geeksforgeeks.org/sql-ddl-dml-dcl-tcl-commands/

Structured Query Language(SQL) as we all know is the database language by the use of which we can perform certain operations on the existing database and also we can use this language to create a database. SQL uses certain commands like Create, Drop, Insert etc. to carry out the required tasks.

These SQL commands are mainly categorized into four categories as discussed below:

 

  1. DDL(Data Definition Language) : DDL or Data Definition Language actually consists of the SQL commands that can be used to define the database schema. It simply deals with descriptions of the database schema and is used to create and modify the structure of database objects in database.

    Examples of DDL commands:

    • CREATE – is used to create the database or its objects (like table, index, function, views, store procedure and triggers).
    • DROP – is used to delete objects from the database.
    • ALTER-is used to alter the structure of the database.
    • TRUNCATE–is used to remove all records from a table, including all spaces allocated for the records are removed.
    • COMMENT –is used to add comments to the data dictionary.
    • RENAME –is used to rename an object existing in the database.
  2. DML(Data Manipulation Language) : The SQL commands that deals with the manipulation of data present in database belong to DML or Data Manipulation Language and this includes most of the SQL statements.

    Examples of DML:

    • SELECT – is used to retrieve data from the a database.
    • INSERT – is used to insert data into a table.
    • UPDATE – is used to update existing data within a table.
    • DELETE – is used to delete records from a database table.
  3. DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE which mainly deals with the rights, permissions and other controls of the database system.

    Examples of DCL commands:

    • GRANT-gives user’s access privileges to database.
    • REVOKE-withdraw user’s access privileges given by using the GRANT command.
  4. TCL(transaction Control Language) : TCL commands deals with the transaction within the database.

    Examples of TCL commands:

    • COMMIT– commits a Transaction.
    • ROLLBACK– rollbacks a transaction in case of any error occurs.
    • SAVEPOINT–sets a savepoint within a transaction.
    • SET TRANSACTION–specify characteristics for the transaction.

This article is contributed by Dimpy Varshni. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

<think>好的,我现在需要帮助用户理解数据库中DDLDMLDCL、DQL和TCL的定义及其区别。首先,我应该回顾用户提供的引用内容,确保准确涵盖每个术语。用户提到五个类别,其中TCL在引用中也有部分涉及,比如引用5提到了DDL操作是隐性提交的,不能回滚,这可能和TCL相关。 首先,我需要逐个定义每个术语,并引用正确的来源。比如,DDL是数据定义语言,涉及创建、修改和删除数据库对象,如CREATE、ALTER、DROP,这在引用1、2、3、4、5中都有提到。DML是数据操作语言,包括INSERT、UPDATE、DELETE,引用1、3、4、5都提到。DCL是数据控制语言,如GRANT和REVOKE,引用1、3、5。DQL是数据查询语言,主要是SELECT,引用1、3、4。TCL可能涉及事务控制,如COMMIT和ROLLBACK,引用2和5提到TCL,但需要确认用户提供的引用是否有足够信息。 接下来要区分它们的区别。根据引用4,DDLDML的主要区别在于DDL操作数据库对象结构,而DML操作表内数据。DCL负责权限,DQL负责查询。TCL处理事务,如提交和回滚。需要注意的是,用户提到的引用中,引用2和5提到了TCL,但可能信息较少,需要结合一般数据库知识补充。 然后,考虑如何结构化回答。按每个术语的定义和常见语句列表,然后对比它们的区别。可能需要表格来清晰展示。同时,用户要求生成相关问题,所以最后要添加相关问题部分。 需要检查是否有遗漏,比如TCL是否被正确涵盖。根据引用5,TCL可能未被详细讨论,但用户的问题明确提到TCL,所以需要补充其定义,即使引用信息有限,可能需要结合外部知识,但根据指示应尽可能使用提供的引用。引用5提到DDL操作是隐性提交,不能回滚,这可能与TCL有关,但需要明确TCL的定义,如COMMIT、ROLLBACK、SAVEPOINT,这可能不在引用中,但用户的问题需要回答,所以可能需要适当说明。 最后,确保回答符合格式要求:使用中文,数学公式正确,引用标识正确,相关问题生成。需要确认所有引用标记正确对应,例如引用[^1]、[^2]等是否正确关联到用户提供的引用内容。</think>### 数据库中DDLDMLDCL、DQL、TCL的定义与区别 #### 1. **DDL(数据定义语言)** 用于定义数据库结构和对象(如表、索引、视图等)。 - **常见语句**:`CREATE`(创建)、`ALTER`(修改)、`DROP`(删除)、`TRUNCATE`(清空表)[^1][^2][^5]。 - **特点**: - 操作对象是数据库结构,而非数据本身。 - DDL语句执行后会自动提交(隐性提交),无法回滚[^5][^4]。 #### 2. **DML(数据操作语言)** 用于对数据库中的数据进行增删改操作。 - **常见语句**:`INSERT`(插入)、`UPDATE`(更新)、`DELETE`(删除)[^1][^3][^4]。 - **特点**: - 操作对象是表中的数据,不影响表结构。 - 需要显式提交(如`COMMIT`)或回滚(如`ROLLBACK`)[^4][^5]。 #### 3. **DQL(数据查询语言)** 用于从数据库中检索数据。 - **核心语句**:`SELECT`(查询)[^1][^4]。 - **特点**: - 仅用于数据查询,不修改数据或结构。 - 支持复杂操作,如聚合函数、分组、排序等[^5]。 #### 4. **DCL(数据控制语言)** 用于管理数据库访问权限和安全控制。 - **常见语句**:`GRANT`(授权)、`REVOKE`(撤销权限)[^1][^3]。 - **特点**: - 控制用户对数据库对象的操作权限。 - 通常由数据库管理员(DBA)使用[^3][^4]。 #### 5. **TCL(事务控制语言)** 用于管理数据库事务的提交与回滚。 - **常见语句**:`COMMIT`(提交事务)、`ROLLBACK`(回滚事务)、`SAVEPOINT`(设置保存点)[^2][^5]。 - **特点**: - 与事务逻辑相关,确保数据一致性。 - DML操作需要配合TCL实现事务的最终生效或撤销[^5]。 --- ### **核心区别对比** | 类别 | 作用范围 | 常见语句 | 是否可回滚 | 操作对象 | |------|--------------------|--------------------------------------|------------|----------------| | DDL | 定义数据库结构 | `CREATE`, `ALTER`, `DROP` | 否[^5] | 表、索引等对象 | | DML | 操作数据 | `INSERT`, `UPDATE`, `DELETE` | 是[^4] | 表中的数据 | | DQL | 查询数据 | `SELECT` | 不涉及 | 数据检索 | | DCL | 权限控制 | `GRANT`, `REVOKE` || 用户权限 | | TCL | 事务控制 | `COMMIT`, `ROLLBACK` || 事务逻辑 | --- ### **示例说明** - **DDL示例**: ```sql CREATE TABLE students (id INT PRIMARY KEY, name VARCHAR(50)); ``` - **DML示例**: ```sql INSERT INTO students VALUES (1, 'Alice'); ``` - **DCL示例**: ```sql GRANT SELECT ON students TO user1; ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值