数据库设计技巧(三)
技巧|设计|数据|数据库|数据库设计
作者:allsky
在定义第四个正规化的形式前,我想首先提一下三种基本的数据关系:一对一,一对多和多对多。我们回头看一下经
过第一个正规化的users表。要是我们将url的字段放在一个独立的表中,每次在users表中插入一个记录,我们就会在urls
表中插入一行。我们将得到一个一对一的关系:用户表中的每一行,都将在urls表中找到相应的一行。对于我们的应用来
说,这既不实用也不标准。
然后看看第二个正规化的例子。对于每个用户记录,我们的表格允许有多个urls的记录与之关联。这是一个一对多的
关系,这是一个很常见的关系。
对于多对多的关系来说,就有点复杂了。在我们的第三个正规化形式的例子中,我们的一个用户与很多的url有关,而
我们想将该结构变为允许多个用户与多个的urls有关,这样我们就可以得到一个多对多的结构。在讨论前,我们先看看表
格结构会有些什么变化
users
userId name relCompId
1 Joe 1
2 Jill 2
companies
compId company company_address
1 ABC 1 Work Lane
2 XYZ 1 Job Street
urls
urlId url
1 abc.com
2 xyz.com
url_relations
relationId relatedUrlId relatedUserId
1 1 1
2 1 2
3 2 1
4 2 2
为了进一步减低数据的冗余,我们运用第四级正规化形式。我们创建了一个颇奇怪的url_relations表,里面的字段均
为主键或者foreign key。通过这个表,我们就可以消除urls表中的重复项目。以下是第四个正规化形式的具体要求:
第四个正规化形式
1.在一个多对多的关系中,独立的实体不能存放在同一个表格中
由于它仅应用于多对多的关系,因此大多数的开发者可以忽略这条规定。不过在某些情况下,它是非常实用的,这个
例子就是这样,我们通过将相同的实体分离出来,并且将关系移到它们自己的表格中,从而改进了urls表格。
为了令你更容易明白,我们举个具体的例子,以下将用一个SQL语句选择出所有属于joe的urls:
SELECT name, url FROM users, urls, url_relationsswheresurl_relations.relatedUserId = 1 AND
users.userId = 1 AND urls.urlId = url_relations.relatedUrlId
如果我们想要遍历每个人的个人信息和url信息,我们可以这样做:
SELECT name, url FROM users, urls, url_relationsswheresusers.userId = url_relations.relatedUserId AND
urls.urlId = url_relations.relatedUrlId
第五级正规化形式
还有一级正规化的形式,它并不常见,有点深奥,并且在大部分的情况下都是不必要的。它的原则是:
1.原来的表格必须可以通过由它分离出去的表格重新构建
使用这个规定的好处是,你可以确保不会在分离的表格中引入多余的列,所有你创建的表格结构都与它们的实际需要
一样大。应用这条规定是一个好习惯,不过除非你要处理一个非常大型的数据,否则你将不需要用到它。
希望这篇文章对你有用,并且可以帮助你在所有的项目中应用这些正规化的规定。你可能想知道这些方法是从哪来
的,我可以告诉你,前面三个正规化的规定是1972年,Dr. E.F. Codd在他的论文“进一步正规化数据库的关系模型中”提
出的,其余的规定是经过后来的集合理论和关系数学家理论化的。评论:正所谓物级必反,将表格分得过细有时并不好,
因为这样需要将各表进行各种的关联,这会令查询时变得复杂,而且效率也可能降低,这些正规化的规定可以参考,在实
际应用时,要根据项目的大小,必要时可以进行一些测试,以设计出更合理的表格结构。

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

AI Hentai Generator
Generate AI Hentai for free.

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

PHP function introduction—get_headers(): Overview of obtaining the response header information of the URL: In PHP development, we often need to obtain the response header information of the web page or remote resource. The PHP function get_headers() can easily obtain the response header information of the target URL and return it in the form of an array. This article will introduce the usage of get_headers() function and provide some related code examples. Usage of get_headers() function: get_header

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

Nowadays, many Windows users who love games have entered the Steam client and can search, download and play any good games. However, many users' profiles may have the exact same name, making it difficult to find a profile or even link a Steam profile to other third-party accounts or join Steam forums to share content. The profile is assigned a unique 17-digit id, which remains the same and cannot be changed by the user at any time, whereas the username or custom URL can. Regardless, some users don't know their Steamid, and it's important to know this. If you don't know how to find your account's Steamid, don't panic. In this article

Use url to encode and decode the class java.net.URLDecoder.decode(url, decoding format) decoder.decoding method for encoding and decoding. Convert into an ordinary string, URLEncoder.decode(url, encoding format) turns the ordinary string into a string in the specified format packagecom.zixue.springbootmybatis.test;importjava.io.UnsupportedEncodingException;importjava.net.URLDecoder;importjava.net. URLEncoder

Scrapy is a powerful Python crawler framework that can be used to obtain large amounts of data from the Internet. However, when developing Scrapy, we often encounter the problem of crawling duplicate URLs, which wastes a lot of time and resources and affects efficiency. This article will introduce some Scrapy optimization techniques to reduce the crawling of duplicate URLs and improve the efficiency of Scrapy crawlers. 1. Use the start_urls and allowed_domains attributes in the Scrapy crawler to

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

Preface In some cases, the prefixes in the service controller are consistent. For example, the prefix of all URLs is /context-path/api/v1, and a unified prefix needs to be added to some URLs. The conceivable solution is to modify the context-path of the service and add api/v1 to the context-path. Modifying the global prefix can solve the above problem, but there are disadvantages. If the URL has multiple prefixes, for example, some URLs require prefixes. If it is api/v2, it cannot be distinguished. If you do not want to add api/v1 to some static resources in the service, it cannot be distinguished. The following uses custom annotations to uniformly add certain URL prefixes. one,

The system in this article: centos6.5_x64 three hosts: nginx host, hostname:master.lansgg.comip:192.168.10.128 apache host, hostname:client1.lansgg.comip:192.168.10.129 1. nginx address redirection 2. nginx reverse proxy 1. Address redirection: refers to the technology that directs a user to another URL when he browses a certain URL. It is often used to convert a long URL into a shorter URL. Because when a website is to be disseminated, it is often because the URL is too long and difficult to remember; or it may be because the free web space of the Internet has changed.
