Table of Contents
一、以字符串存储各种数据类型,通过行转列实现实体属性读取。
二、预定义大量的冗余列,根据用户对实体属性的类型设置匹配对应的列。
三、使用XML数据类型存储动态列数据。
四、为用户定义的实体动态创建表。
Home Database Mysql Tutorial 可动态扩展的数据库模型设计

可动态扩展的数据库模型设计

Jun 07, 2016 pm 03:24 PM
dynamic us Expand database Model design

在通常的数据库设计中,我们定义了每个实体有多少个属性,每个属性的数据类型是什么,有多长,是否允许为空,有什么约束条件等,这些定义是完全静态的,系统创建时就全部定义好,不能动态修改。但是对于实体的属性变化很快,或者实体和属性由用户在系统中自

在通常的数据库设计中,我们定义了每个实体有多少个属性,每个属性的数据类型是什么,有多长,是否允许为空,有什么约束条件等,这些定义是完全静态的,系统创建时就全部定义好,不能动态修改。但是对于实体的属性变化很快,或者实体和属性由用户在系统中自行定义的情况下,那么就需要一个可以动态扩展的数据库模型,以保存各种动态产生的数据。

比如我们要做一个电子商务网站,需要建立一个商品表以保存各种要卖出的商品的属性。但是商品的属性各种各样,不同类别的商品在属性上千差万别,不可能建立一个静态的商品表来存储所有的属性。这个时候就需要建立动态的数据库模型。

常见的动态扩展的数据库设计方法有以下几种:

一、以字符串存储各种数据类型,通过行转列实现实体属性读取。

以前提到的电子商务网站的商品实体为例,我们可以建立两个表“商品”和“商品属性”,商品表为普通的商品属性,可以将商品名称、价格等大部分商品的公共属性放到该表中。商品表与商品属性表形成一对多关系,商品属性表只需要定义商品“属性名”和“属性值”这两个属性用于保存一个商品的各个属性。

可动态扩展的数据库模型设计

这样在每读取一个商品时,可以读取该商品的属性集合,然后将属性集合重新绑定到对象,将该对象暂时在页面上。

这种做法的优点是灵活,可以为商品创建无数个不同的属性,可以应对电商这种快速变化,快速上线的需求。缺点是后期做统计的时候会很慢,因为需要行转列,如果要涉及到各种Join查询之类的也会很麻烦。

二、预定义大量的冗余列,根据用户对实体属性的类型设置匹配对应的列。

如果我们不希望行转列的话,那么可以预先定义好数据列,由于不确定是哪种数据类型,所以我们可以将表的列定义的特别多,每个不同的数据类型都定义几个或者十来个列,这些列都是允许为空的,如果没有使用已经预定义好的列,并不会占据多少数据空间。

在SharePoint 2007或者更早的版本中,对列表的数据存储就是采用这种方式,以下是SharePoint2007中的AllUserData表的结构。基本上为每种数据类型定义了十来个到几十个的列,用户在创建不同的列表时,都可以使用这个表存储列表数据。

可动态扩展的数据库模型设计

这种数据库设计方法的优点是不会存在行转列的问题,所以在join或者出报表时性能较好,缺点就是使得一个表的列特别多,而且大部分列在大多数情况下是不使用的,而且扩展比较困难,比如我们要定义17个bit类型的列,但是系统默认只有16个,这种情况下,就需要在数据库中使用2行数据来表示1行列表数据。

三、使用XML数据类型存储动态列数据。

XML数据类型是SQL的一个标准,目前主流的数据库都支持XML数据类型,数据库为XML提供专门的语法以快速检索和操作XML数据。在新版的SharePoint中,就使用XML来存储用户自定义列表的内容。

对于前面提到的商品表和商品属性表,其实也可以只建立商品表,在该表中添加一XML类型的列,用于存储商品的各种属性。这是比较推荐的一种处理方法。

四、为用户定义的实体动态创建表。

还有一直动态方法是在程序中动态创建表,用户每在程序中定义一个实体的时候,就好根据用户定义创建一个对应的表。比如微软的Dynamic CRM就是这样实现的。用户可以在系统中创建大量的实体,并且还可以定义实体之间的关系,系统就会按照用户的定义创建对应的表,以及外键。

这种方法的优点是性能好,每个实体与其数据库表相对应,不存在大量的冗余列,也不会存在行转列的问题。缺点是开发难度大,对用户的要求高;而且在创建好实体并且存储了大量数据后,如果想要修改实体属性,那么将很困难。

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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
1248
24
No OpenAI data required, join the list of large code models! UIUC releases StarCoder-15B-Instruct No OpenAI data required, join the list of large code models! UIUC releases StarCoder-15B-Instruct Jun 13, 2024 pm 01:59 PM

At the forefront of software technology, UIUC Zhang Lingming's group, together with researchers from the BigCode organization, recently announced the StarCoder2-15B-Instruct large code model. This innovative achievement achieved a significant breakthrough in code generation tasks, successfully surpassing CodeLlama-70B-Instruct and reaching the top of the code generation performance list. The unique feature of StarCoder2-15B-Instruct is its pure self-alignment strategy. The entire training process is open, transparent, and completely autonomous and controllable. The model generates thousands of instructions via StarCoder2-15B in response to fine-tuning the StarCoder-15B base model without relying on expensive manual annotation.

Honor Magic V3 debuts AI defocus eye protection technology: effectively alleviates the development of myopia Honor Magic V3 debuts AI defocus eye protection technology: effectively alleviates the development of myopia Jul 18, 2024 am 09:27 AM

According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of ​​​​sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

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

Yolov10: Detailed explanation, deployment and application all in one place! Yolov10: Detailed explanation, deployment and application all in one place! Jun 07, 2024 pm 12:05 PM

1. Introduction Over the past few years, YOLOs have become the dominant paradigm in the field of real-time object detection due to its effective balance between computational cost and detection performance. Researchers have explored YOLO's architectural design, optimization goals, data expansion strategies, etc., and have made significant progress. At the same time, relying on non-maximum suppression (NMS) for post-processing hinders end-to-end deployment of YOLO and adversely affects inference latency. In YOLOs, the design of various components lacks comprehensive and thorough inspection, resulting in significant computational redundancy and limiting the capabilities of the model. It offers suboptimal efficiency, and relatively large potential for performance improvement. In this work, the goal is to further improve the performance efficiency boundary of YOLO from both post-processing and model architecture. to this end

Honor X60i mobile phone is on sale starting from 1,399 yuan: visual quadrilateral OLED direct screen Honor X60i mobile phone is on sale starting from 1,399 yuan: visual quadrilateral OLED direct screen Jul 29, 2024 pm 08:25 PM

According to news on July 29, the Honor X60i mobile phone is officially on sale today, starting at 1,399 yuan. In terms of design, the Honor X60i mobile phone adopts a straight screen design with a hole in the center and almost unbounded ultra-narrow borders on all four sides, which greatly broadens the field of view. Honor X60i parameters Display: 6.7-inch high-definition display Battery: 5000mAh large-capacity battery Processor: Dimensity 6080 processor (TSMC 6nm, 2x2.4G A76+6×2G A55) System: MagicOS8.0 system Other features: 5G signal enhancement, smart capsule, under-screen fingerprint, dual MIC, noise reduction, knowledge Q&A, photography capabilities: rear dual camera system: 50 million pixels main camera, 2 million pixels auxiliary lens, front selfie lens: 8 million pixels, price: 8GB

Tsinghua University took over and YOLOv10 came out: the performance was greatly improved and it was on the GitHub hot list Tsinghua University took over and YOLOv10 came out: the performance was greatly improved and it was on the GitHub hot list Jun 06, 2024 pm 12:20 PM

The benchmark YOLO series of target detection systems has once again received a major upgrade. Since the release of YOLOv9 in February this year, the baton of the YOLO (YouOnlyLookOnce) series has been passed to the hands of researchers at Tsinghua University. Last weekend, the news of the launch of YOLOv10 attracted the attention of the AI ​​community. It is considered a breakthrough framework in the field of computer vision and is known for its real-time end-to-end object detection capabilities, continuing the legacy of the YOLO series by providing a powerful solution that combines efficiency and accuracy. Paper address: https://arxiv.org/pdf/2405.14458 Project address: https://github.com/THU-MIG/yo

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

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.

New stacking process! Xiaomi MIX Fold 4 is equipped with Jinshajiang 'three-dimensional special-shaped' battery for the first time New stacking process! Xiaomi MIX Fold 4 is equipped with Jinshajiang 'three-dimensional special-shaped' battery for the first time Jul 20, 2024 am 03:20 AM

According to news on July 19, Xiaomi MIX Fold 4, the first flagship folding new phone, was officially released tonight and is equipped with a "three-dimensional special-shaped battery" for the first time. According to reports, Xiaomi MIX Fold4 has achieved a major breakthrough in battery technology and designed an innovative "three-dimensional special-shaped battery" specifically for folding screens. Traditional folding screen devices mostly use conventional square batteries, which have low space utilization efficiency. In order to solve this problem, Xiaomi did not use the common winding battery cells, but developed a new lamination process to create a new form of battery, which greatly improved the space utilization. Battery Technology Innovation In order to accurately alternately stack positive and negative electrode sheets and ensure the safe embedding of lithium ions, Xiaomi has developed a new ultrasonic welding machine and lamination machine to improve welding and cutting accuracy.

See all articles