自考《数据库系统原理》(6)之数据库管理
数据库的管理包括事务、恢复、并发控制、完整性和安全性等内容。说到事务,先给大家它的定义。 事务 是构成单一逻辑工作单元的操作集合,要么完整地执行,要么完全不执行。不论发生何种情况,DBS必须保证事务能正确、完整地执行。 看到这样的定义,初学的人
数据库的管理包括事务、恢复、并发控制、完整性和安全性等内容。说到事务,先给大家它的定义。
事务是构成单一逻辑工作单元的操作集合,要么完整地执行,要么完全不执行。不论发生何种情况,DBS必须保证事务能正确、完整地执行。
看到这样的定义,初学的人肯定会晕。举一个例子给大家说,去柜员机取款,当你放进去卡,事务开始(BEGIN TRANSACTION),点击确认要取出二百后,系统正常运行把钱给你,取出你的卡,这个事务结束(COMMIT)。但是如果系统正当运行的时候,忽然断电了,钱也没取出来。你是不是会担心你的银行卡会少二百块钱,这个你放心,现在事务就开始起作用了。当运行失败的时候,那么事务就会回退(ROLLBACK),你卡上的钱是不会动的。这样事务的定义就显而易见了吧,做一件事,要么你就不执行,要么你就完整地执行下来,执行了但没有成功那么就回到原始没有执行的状态。
当然这里说的事务是计算机,那么针对的是数据库。为了保证数据库中的数据是正确的,我们对其性质有什么要求呢?简单来说,就是ACID性质。对于数据库的操作来说,要么你就全部执行,要么就什么也不做,这就是原子性(Atomicity);那么事务执行的时候,数据应该一直要一致,这就是所谓的一致性(Consistency);多个事务同时执行的时候,那么系统和这些事务单独执行时的结果是一样的,此谓隔离性(Isolation);事务完成全部的操作后,对数据库的更新应该永久地存在数据库中,此谓持久性(Durability)。
对数据库的管理主要通过四个方面来实现:数据库的恢复、并发控制、完整性控制盒安全性控制。
说到恢复,首先你得做好日常工作的备份啊,这样到数据库出问题的时候,你就可以来恢复了。再说并发控制,听起来很“高大尚”,其实就是很多事务同时对数据库操作,这样很容易出问题啊,数据不一致了,中间丢了一个操作没有更新(丢失问题);一个事务操作了,都没有提交,紧接着下一个事务来读取上一个事务的操作。上一个事务修改了,但下一个事务却没动,这就导致了读出了脏数据问题;两个事务,同时读取同一个数,一个事务把数改了,另一个却没有。此谓不可重复读问题。
为了解决这些问题呢,用了封锁技术来使数据库之间的相关联的数据能够同步。有一种排他型封锁(X锁),说白了就是自己加了锁就不让别人加了,一旦加上了锁,对数据做了修改,除非全部的操作完成了,否则是不能解锁的。这样,丢失更新问题就解决了。还有一个共享锁(S锁),就是自己和别人都能够对同一事物加锁呗,但是有一点,一旦加S锁,数据不能修改,只能读取,必须升级为X锁才行。但就是这样一个好东西也会发生活锁,饿死和死锁问题。这就又引出了一个概念-可串行化。多个事务依次执行,就是串行调度,分时同时处理多个事务就是并发调度,在每个事务中,语句的先后顺序不管在哪都始终是一致的,那么并发调度的执行结果与串行调度执行结果若等价,那么这个并发调度就是可串行化的调度。还有SQL中设置事务的存取方式和隔离级别对并发操作进行了管理,知道这回事就行。
紧接着说数据的完整性,其实就是要保持数据始终是完整的,不受破坏,这样有了一套规则来保证。即所谓的完整性规则。那怎么样来定义这些规则又出现了三个约束。域约束、基本表约束和断言。没弄懂,知道有这回事就好。但是这些约束都太被动了,遇到复杂的操作还需要主动去进行,那么就有了触发器。
最后是数据库的安全性。无论什么安全都是很重要的。比如说专利,没有发明人的授权,你是不能随便用的。这就是所谓的权限问题。再说角色问题,你是提高班的一元,米老师就给你一个胸牌,允许你进入机房学习。没有了这些东西岂不是会非常的不安全啊。
说来说去,数据库就这么些东西,用自己的生活来理解它会变得非常的简单。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Transactions ensure database data integrity, including atomicity, consistency, isolation, and durability. JDBC uses the Connection interface to provide transaction control (setAutoCommit, commit, rollback). Concurrency control mechanisms coordinate concurrent operations, using locks or optimistic/pessimistic concurrency control to achieve transaction isolation to prevent data inconsistencies.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.
