1NF 的要求:1,各行沒(méi)有順序關(guān)系;2,各列也沒(méi)有順序關(guān)系;3,不允許重復(fù)的行;4,不允許null的列。
因此,實(shí)際上是要求:1,要有unique key;2,不允許nullable屬性。當(dāng)然這里的unique key可以是一個(gè)attribute,也可以是一個(gè)superkey。如果一個(gè)relation滿足1NF,則所有的attribute自然為一個(gè)superkey
super-key是區(qū)分各個(gè)行的attribute的集合,minimal super-key稱作一個(gè)candidate key
所有的candidate key中,一般聲明一個(gè)為primary key。
2NF 首先是1NF,之后,要求每一個(gè)non-prime attribute依賴于candidate key的整個(gè)集合,而非一部分。例如(Employee, Skill, Location)這樣一個(gè)表,Employee加上Skill才是一個(gè)candidate key(因?yàn)橐粋€(gè)人可能有多個(gè)技能),而Location則僅僅依賴于Employee,不依賴于Skill,因此這不是一個(gè)2NF。
如果一個(gè)relation 不在2NF 中,則這個(gè)關(guān)系中包含冗余信息。
3NF 首先是2NF,其次,要求所有的non-prime attributes不能傳遞地依賴于candidate key。直觀來(lái)說(shuō), non-prime attributes must provide something about the key, and nothing else.?
換言之,有傳遞依賴的,不能滿足3NF。
例如,(Tournament, Year, Winner, Winner Birthday)中,(Tournament, Year)是一個(gè)candidate key,但是Winner Birthday是通過(guò)Winner與(T, Y)建立關(guān)聯(lián)的,這就不滿足3NF。
不滿足3NF可能會(huì)造成不一致,例如同一個(gè)Winner在不同的比賽或年份出現(xiàn),他的生日可能會(huì)不一致。解決辦法是拆開這個(gè)表為兩張表。
Boyce-Codd NF( BCNF 或者3.5NF),比3NF稍微強(qiáng)一點(diǎn)。
?
Primary Key :在多個(gè)candidate key中,選擇一個(gè)作為row的唯一標(biāo)識(shí)。這個(gè)可以是一個(gè) surrogate key (人為引入的,如auto_increment的序列號(hào)),可以是某個(gè)特定的屬性,也可以是多個(gè)屬性共同組成的key(稱作 composite key )
Foreign Key :外鍵,完整性約束的一種,要求該屬性在另一表的某個(gè)key中出現(xiàn)。
Alternate key :primary key之外的其他key。
?
Clustered Index and Non-Clustered Index :前者決定了row的存儲(chǔ)位置(因而只有一個(gè)),后者只是邏輯上的index,用于加速查找。許多RDBMS會(huì)默認(rèn)為key創(chuàng)建index。
?
Prepared Statements:SQL語(yǔ)句的模板,一般沒(méi)有更復(fù)雜的邏輯(如分支、跳轉(zhuǎn)等)。PS的目的在于只編譯并優(yōu)化一次,同時(shí)降低SQL注入的可能。
Stored Procedure:存儲(chǔ)過(guò)程,存儲(chǔ)于DBMS服務(wù)器端的特殊函數(shù),一般用vendor-specific的語(yǔ)言編寫,除了可以使用SQL外,還有該語(yǔ)言的一些高級(jí)特性,一般包含條件判斷、分支、跳轉(zhuǎn)等。這種subroutine會(huì)被預(yù)編譯,并存儲(chǔ)到數(shù)據(jù)庫(kù)服務(wù)器中。
常見的SP有,Oracle的PL/SQL、MS SQL Server的Transact-SQL、DB2的SQL-PL、PostgreSQL的PL/pgSQL。
SP的好處是預(yù)編譯過(guò)且存儲(chǔ)于服務(wù)器端,因此可以更好地繼承到DB中(如被trigger觸發(fā))、減少網(wǎng)絡(luò)開銷、隱藏業(yè)務(wù)邏輯、防范SQL注入攻擊、自定義access right等。
?
trigger:事前觸發(fā),事后觸發(fā),行級(jí)觸發(fā)。
?
Database Normalization:
the process of organizing data to minimize redundancy is called normalization .
The goal of database normalization is to decompose relations with anomalies in order to produce smaller, well-structured relations. Normalization usually involves dividing large tables into smaller (and less redundant) tables and defining relationships between them. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships.
Informally, a relational database table (the computerized representation of a relation) is often described as "normalized" if it is in the Third Normal Form . Most 3NF tables are free of insertion, update, and deletion anomalies, i.e. in most cases 3NF tables adhere to BCNF, 4NF, and 5NF (but typically not 6NF).
A standard piece of database design guidance is that the designer should create a fully normalized design; selective denormalization can subsequently be performed for performance reasons.
Normalization的過(guò)程其實(shí)就是單個(gè)表最小化的過(guò)程,減少表內(nèi)依賴,通過(guò)輔助表來(lái)建立表間依賴。
Normalization后的好處:
1,F(xiàn)ree the database of modification anomalies
2,Minimize redesign when extending the database structure
3,Make the data model more informative to users
4,Avoid bias towards any particular pattern of querying
?
In? computing ,?denormalization?is the process of attempting to optimze the read performance of a? database ?by adding redundant data or by grouping data .
Normalization會(huì)去除冗余,簡(jiǎn)化表的結(jié)構(gòu)。但是代價(jià)是很多情況下,數(shù)據(jù)庫(kù)操作要針對(duì)好幾張表來(lái)進(jìn)行,這樣的join操作代價(jià)比較高。
解決辦法:1,在table之上建立view,然后在view上創(chuàng)建index來(lái)提高性能。
2,denormalize一些表。通常是在normalization之后,針對(duì)性能瓶頸來(lái)增加冗余來(lái)提高性能。
?
DML(data manipulation language):
?????? 它們是SELECT、UPDATE、INSERT、DELETE,就象它的名字一樣,這4條命令是用來(lái)對(duì)數(shù)據(jù)庫(kù)里的數(shù)據(jù)進(jìn)行操作的語(yǔ)言
DDL(data definition language):
?????? DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定義或改變表(TABLE)的結(jié)構(gòu),數(shù)據(jù)類型,表之間的鏈接和約束等初始化工作上,他們大多在建立表時(shí)使用
DCL(Data Control Language):
?????? 是數(shù)據(jù)庫(kù)控制功能。是用來(lái)設(shè)置或更改數(shù)據(jù)庫(kù)用戶或角色權(quán)限的語(yǔ)句,包括(grant,deny,revoke等)語(yǔ)句。在默認(rèn)狀態(tài)下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人員才有權(quán)力執(zhí)行DCL
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
