Home Backend Development PHP Tutorial PHP与MySQL学习笔记8:重要概念与设计Web数据库

PHP与MySQL学习笔记8:重要概念与设计Web数据库

Jun 23, 2016 pm 01:15 PM

1、存储引擎

MySQL支持许多不同的“存储引擎”,也叫作“表格类型”。每个表可是使用不同的存储引擎,而且可以轻松地对它们进行转换。

创建表时可以选择一个表格类型:

CREATE TABLE table TYPE = type....

修改表类型:

alter table orders type = innodb;


1)MyISAM,默认类型

它基于传统的ISAM类型,Indexed Sequential Access Method(有索引的顺序访问方法)的缩写,它是存储记录和文件的标准方法。

MyISAM特点:

MyISAM具有检查和修复表格的大多数工具,表格可以被压缩,支持全文搜索。但是它们不是事务安全的,也不支持外键。


2)InnoDB

该类型表是事务安全的,也就是说,它提供了 COMMIT 和 ROLLBACK功能。InnoDB支持外键。 虽然比MyISAM表要慢些,但是如果应用程序需要一个事务安全的存储引擎,建议使用。


注:在大多数Web应用程序中,通常都会使用MyISAM或InnoDB表格或者二者的结合。


3)MEMORY (以前的 HEAP)

该类型表,存储在内存中,表的索引是哈希分布的。

MEMORY表格运行速度非常快,但是如果发生崩溃,数据将丢失。

建议:MEMORY表格适合保存临时或者派生的数据,应该在CREATE TABLE语句中指定MAX_ROWS,否则这些表可能吞噬所有内存。同样,它们不能加入BLOB、TEXT或AUTO INCREMENT列。


4)MERGE

这些表允许你为了查询目的,把MyISAM表的集合作为一个单个表。因此,你可在某些操作系统中,避开最大文件大小限制。


5)ARCHIVE

这些表保存了大量数据,但是只有少量脚注(footprint)。这种类型的表只支持INSERT 和SELECT 查询。不支持DELETE、UPDATE 和 REPLACE。此外,也不使用索引。


6)CSV

这些表保存在服务器的单个文件中,它包含用逗号隔离的数据。

可以方便地用Excel等第三方工具打开。


建议:

当对一个表格使用大量的SELECT  和 INSERT 语句(或者二者结合)时,应该使用MyISAM 表格,因为在执行这两种命令时,MyISAM是最快的。对于很多Web程序(例如分类)来说,MyISAM是最佳选择。如果需要全文搜索功能,也应该使用MyISAM功能。

当事务非常重要(例如存储财务数据),或在INSERT 和 SELECT 语句是交错执行的情况下(例如在线的消息栏或论坛系统),应该使用 InnoDB。InnoDB在MySQL5.6版本中好像支持全文索引了。


2、事务

事务是确保数据库一致的机制,尤其是在发生错误或服务器崩溃情况下确保数据库一致的机制。

事务是一个或一系列的查询,这些查询可以保证能够在数据库中作为一个整体全部执行或者全部不执行。这样,数据库才能在无论事务是否完成的情况下保持一致状态。

比如,银行转账,比如保证从一个账户减少和另一个账户增加的操作完整完成。


ACID原则,就是描述事务安全性的4个需求:

Atomicity(原子性)---一个事务必须是原子性的,它必须是作为一个整体完全执行或者完全不执行。

Consistency(一致性)---一个事务必须能够使数据库处于一致的状态。

Isoltion(孤立性)---未完全完成的事务不能被数据库的其他用户所见,也就是说,在事务完全完成之前,它们都是孤立的。

Durability(持续性)---一旦写入到数据库后,事务必须是永久的而且持续的。

注意:一个事务被永久地写入到数据库中称作该事务被提交了。一个没有写入到数据库中的事务(因此数据库将状态重置到事务开始之前的状态)称作事务被回滚了。


3、关系数据库

关系数据库代替普通文件的优点:

1)关系数据库比普通文件的数据访问速度更快。

2)关系数据库更容易查询并提取满足特定条件的数据。

3)关系数据库具有专门的内置机制处理并发访问。

4)关系数据库可以提供对数据的随机访问

5)关系数据库具有内置的权限系统。


4、关系数据库的一些基本概念

1)关系数据库由“关系”组成,这些关系通常称为“表格”


2)列

“列”又叫做“域”或者“属性”

每一列都有一个唯一的名称,和一个相关的数据类型。

3)行

每一行具有相同的格式,也具有相同的属性。行也叫“记录”。

4)值

每个值必须与该列定义的数据类型相同。

5)键

我们必须有一个能够识别每一个特定记录的方法。

表中的标志列成为“键”或“主键”。

数据库由多个表组成,可以使用键作为表格之间的引用。



6)模式

数据库整套表格的完整设计称为数据库的“模式”。它是数据库的设计蓝图。

一个模式应该显示表格及表格的列、各个表的主键和外键。

可以包含示例数据来解析这些数据的含义。



7)关系

关系数据库中有3种基本的关系类型,一对一、一对多、多对多。


4、设计Web数据库

知道什么时候需要一个新表,以及需要哪些键,需要掌握很高的技巧。但是在大多数情况下,我们可以遵循一些基本的原则。


1)考虑实际建模的对象,现实世界对应的对象

2)避免冗余数据

冗余数据将导致两个主要问题:

a. 空间的浪费

b. 数据更新的不一致,数据的完整性将被破坏。(修改、插入和删除时容易导致)

c. 使用原子列值:每一行的每个属性只存储一个数据。

如果我们想在格子里存多个数据值,其实相当于创建了一个表中表,这样系统就不能只计算匹配字段了,而必须分析每个属性值,看系统中是否包含一个匹配。所以,看情况而定吧。

d. 选择有意义的键

e. 考虑需要询问数据库的问题,想一想我们希望数据库回答什么问题,然后确认数据库中是否已经包含所有需要的数据,并且在表之间要有适当的关联。

f. 避免多个空属性的设计。

数据库里有很多空值,很糟糕。首先,浪费空间。其次,在统计列总量或对其他数值列应用计算函数时可能导致错误。还有,当用户看到表中一部分为空时。也很迷惑,他们也不知道是否因为该属性是无关的,还是数据库中有错误,或者是数据尚未输入。


5、表类型总结

1)描述现实世界对象的简单表

2)描述两个现实世界对象的多对多关系的关联表。


6、Web数据库的架构

通常,Web服务器软件,PHP引擎和数据库服务器都在同一台机器上运行。但是数据库服务器在另外一台机器上运行也很常见,这样是出于安全性、提高性能以及负载均衡的原因。

随着应用程序在大小和复杂度上不断增加,我们可能会将PHP应用程序分成不同的层,通常,包括与MySQL交互的数据库层、包含了应用程序的业务逻辑成、管理HTML输出的表示层。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles