CRUD

本文介绍了计算机编程中常见的CRUD操作,即创建(Create)、读取(Read)、更新(Update)和删除(Delete),这些操作是持久化存储的基础。文章还探讨了CRUD在数据库应用及用户界面设计中的应用,并提供了与SQL语句、HTTP方法和DDS操作的对应关系。

Create, read, update and delete

From Wikipedia, the free encyclopedia
"CRUD" redirects here. For other uses, see  Crud.

In computer programmingcreate, read, update and delete[1] (as an acronym CRUD) (Sometimes called SCRUD with an "S" for Search) are the four basic functions of persistent storage.[2] Sometimes CRUD is expanded with the words retrieve instead of readmodify instead of update, or destroy instead of delete. It is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information; often using computer-based forms and reports. The term was likely first popularized by James Martin in his 1983 book Managing the Data-base Environment.[1][3] The acronym may be extended to CRUDL to cover listing of large data sets which bring additional complexity such as pagination when the data sets are too large to hold easily in memory.

Another variation of CRUD is BREAD, an acronym for "Browse, Read, Edit, Add, Delete".[citation needed] This extension is mostly used in context with data protection concepts, when it is legally not allowed to delete data directly. Locking the data prevents the access for users without destroying still needed data. Yet another variation, used before CRUD became more common, is MADS, an acronym for "Modify, All, Delete, Show."[citation needed]

Database applications[edit]

The acronym CRUD refers to all of the major functions that are implemented in relational database applications. Each letter in the acronym can map to a standard SQL statement, HTTP method (this is typically used to build RESTful APIs[4]) or DDS operation:

Operation SQL HTTP DDS
CreateINSERTPUT / POSTwrite
Read (Retrieve)SELECTGETread / take
Update (Modify)UPDATEPOST / PUT / PATCHwrite
Delete (Destroy)DELETEDELETEdispose

The comparison of the database oriented CRUD operations to HTTP methods has some flaws. Strictly speaking, both PUT and POST can create resources; the key difference is that POST leaves it for the server to decide at what URI to make the new resource available, whilst PUT dictates what URI to use; URIs are of course a concept that doesn't really line up with CRUD. The significant point about PUT is that it will replace whatever resource the URI was previously referring to with a brand new version, hence the PUT method being listed for Update as well. PUT is a 'replace' operation, which one could argue is not 'update'.

Although a relational database provides a common persistence layer in software applications, numerous other persistence layers exist. CRUD functionality can be implemented with an object database, an XML databaseflat text files, custom file formats, tape, or card, for example.

User interface[edit]

CRUD is also relevant at the user interface level of most applications. For example, in address book software, the basic storage unit is an individual contact entry. As a bare minimum, the software must allow the user to

  • Create or add new entries
  • Read, retrieve, search, or view existing entries
  • Update or edit existing entries
  • Delete/deactivate existing entries

Without at least these four operations, the software cannot be considered complete. Because these operations are so fundamental, they are often documented and described under one comprehensive heading, such as "contact management", "content management" or "contact maintenance" (or "document management" in general, depending on the basic storage unit for the particular application).

See also[edit]

References[edit]

  1. Jump up to: a b Managing the Data-base Environment, p. 381, at Google Books
  2. Jump up ^ Heller, Martin (29 January 2007). "REST and CRUD: the Impedance Mismatch"Developer World. InfoWorld.
  3. Jump up ^ Martin, James (1983). Managing the Data-base Environment. Englewood Cliffs, New Jersey: Prentice-Hall. p. 381. ISBN 0-135-50582-8.
  4. Jump up ^ Tom Spencer (2014). "No REST for the whippet.".
07-25
CRUD操作是指创建(Create)、读取(Read)、更新(Update)和删除(Delete)四种基础数据操作,广泛应用于数据库管理和软件开发领域。这些操作构成了数据交互的核心流程,是构建各类信息系统的基础。 在关系型数据库中,CRUD操作通常通过SQL语句实现。例如,使用`INSERT`语句进行创建操作,`SELECT`语句进行读取操作,`UPDATE`语句用于更新数据,而`DELETE`语句则用于删除记录。这些操作可以单独使用,也可以结合事务处理来确保数据一致性和完整性[^2]。 对于非关系型数据库(如NoSQL数据库),CRUD操作同样适用,但具体的实现方式可能有所不同。例如,在文档型数据库MongoDB中,创建操作使用`insert`方法,读取操作使用`find`方法,更新操作使用`update`方法,删除操作使用`remove`方法。这些操作的设计理念与SQL中的CRUD操作类似,但在语法和实现细节上有所不同[^1]。 在编程语言层面,CRUD操作通常需要结合数据库连接库或框架来实现。例如,在Python中可以使用`mysql-connector-python`库与MySQL数据库进行交互。通过该库,开发者可以建立数据库连接、执行SQL语句并处理结果集。以下是Python中使用`mysql-connector-python`实现CRUD操作的示例代码: ```python import mysql.connector # 建立数据库连接 conn = mysql.connector.connect( host="localhost", user="root", password="password", database="test_db" ) cursor = conn.cursor() # 创建操作 cursor.execute("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')") conn.commit() # 读取操作 cursor.execute("SELECT * FROM users") for row in cursor.fetchall(): print(row) # 更新操作 cursor.execute("UPDATE users SET email='new_email@example.com' WHERE name='Alice'") conn.commit() # 删除操作 cursor.execute("DELETE FROM users WHERE name='Alice'") conn.commit() # 关闭连接 cursor.close() conn.close() ``` 在前端开发中,CRUD操作通常涉及与后端API的交互。例如,使用JavaScript的`fetch` API发送HTTP请求来实现数据的创建、读取、更新和删除。以下是一个使用JavaScript实现CRUD操作的示例: ```javascript // 创建操作 fetch('/api/users', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Alice', email: 'alice@example.com' }) }); // 读取操作 fetch('/api/users') .then(response => response.json()) .then(data => console.log(data)); // 更新操作 fetch('/api/users/1', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: 'Alice', email: 'new_email@example.com' }) }); // 删除操作 fetch('/api/users/1', { method: 'DELETE' }); ``` 此外,一些框架和库提供了对CRUD操作的进一步封装和优化。例如,在Java开发中,MyBatis是一个常用的持久层框架,它通过XML配置文件或注解的方式定义SQL语句,并将结果映射为Java对象。以下是一个使用MyBatis实现CRUD操作的示例: ```xml <!-- 创建操作 --> <insert id="insertUser" parameterType="com.example.User"> INSERT INTO users (name, email) VALUES (#{name}, #{email}); </insert> <!-- 读取操作 --> <select id="selectUser" resultType="com.example.User"> SELECT * FROM users WHERE id = #{id}; </select> <!-- 更新操作 --> <update id="updateUser" parameterType="com.example.User"> UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id}; </update> <!-- 删除操作 --> <delete id="deleteUser" parameterType="int"> DELETE FROM users WHERE id = #{id}; </delete> ``` 通过上述方法,开发者可以灵活地实现CRUD操作,并根据具体需求选择合适的工具和框架。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值