1NF 的要求:1,各行沒有順序關系;2,各列也沒有順序關系;3,不允許重復的行;4,不允許null的列。
因此,實際上是要求:1,要有unique key;2,不允許nullable屬性。當然這里的unique key可以是一個attribute,也可以是一個superkey。如果一個relation滿足1NF,則所有的attribute自然為一個superkey
super-key是區分各個行的attribute的集合,minimal super-key稱作一個candidate key
所有的candidate key中,一般聲明一個為primary key。
2NF 首先是1NF,之后,要求每一個non-prime attribute依賴于candidate key的整個集合,而非一部分。例如(Employee, Skill, Location)這樣一個表,Employee加上Skill才是一個candidate key(因為一個人可能有多個技能),而Location則僅僅依賴于Employee,不依賴于Skill,因此這不是一個2NF。
如果一個relation 不在2NF 中,則這個關系中包含冗余信息。
3NF 首先是2NF,其次,要求所有的non-prime attributes不能傳遞地依賴于candidate key。直觀來說, non-prime attributes must provide something about the key, and nothing else.?
換言之,有傳遞依賴的,不能滿足3NF。
例如,(Tournament, Year, Winner, Winner Birthday)中,(Tournament, Year)是一個candidate key,但是Winner Birthday是通過Winner與(T, Y)建立關聯的,這就不滿足3NF。
不滿足3NF可能會造成不一致,例如同一個Winner在不同的比賽或年份出現,他的生日可能會不一致。解決辦法是拆開這個表為兩張表。
Boyce-Codd NF( BCNF 或者3.5NF),比3NF稍微強一點。
?
Primary Key :在多個candidate key中,選擇一個作為row的唯一標識。這個可以是一個 surrogate key (人為引入的,如auto_increment的序列號),可以是某個特定的屬性,也可以是多個屬性共同組成的key(稱作 composite key )
Foreign Key :外鍵,完整性約束的一種,要求該屬性在另一表的某個key中出現。
Alternate key :primary key之外的其他key。
?
Clustered Index and Non-Clustered Index :前者決定了row的存儲位置(因而只有一個),后者只是邏輯上的index,用于加速查找。許多RDBMS會默認為key創建index。
?
Prepared Statements:SQL語句的模板,一般沒有更復雜的邏輯(如分支、跳轉等)。PS的目的在于只編譯并優化一次,同時降低SQL注入的可能。
Stored Procedure:存儲過程,存儲于DBMS服務器端的特殊函數,一般用vendor-specific的語言編寫,除了可以使用SQL外,還有該語言的一些高級特性,一般包含條件判斷、分支、跳轉等。這種subroutine會被預編譯,并存儲到數據庫服務器中。
常見的SP有,Oracle的PL/SQL、MS SQL Server的Transact-SQL、DB2的SQL-PL、PostgreSQL的PL/pgSQL。
SP的好處是預編譯過且存儲于服務器端,因此可以更好地繼承到DB中(如被trigger觸發)、減少網絡開銷、隱藏業務邏輯、防范SQL注入攻擊、自定義access right等。
?
trigger:事前觸發,事后觸發,行級觸發。
?
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的過程其實就是單個表最小化的過程,減少表內依賴,通過輔助表來建立表間依賴。
Normalization后的好處:
1,Free 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會去除冗余,簡化表的結構。但是代價是很多情況下,數據庫操作要針對好幾張表來進行,這樣的join操作代價比較高。
解決辦法:1,在table之上建立view,然后在view上創建index來提高性能。
2,denormalize一些表。通常是在normalization之后,針對性能瓶頸來增加冗余來提高性能。
?
DML(data manipulation language):
?????? 它們是SELECT、UPDATE、INSERT、DELETE,就象它的名字一樣,這4條命令是用來對數據庫里的數據進行操作的語言
DDL(data definition language):
?????? DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定義或改變表(TABLE)的結構,數據類型,表之間的鏈接和約束等初始化工作上,他們大多在建立表時使用
DCL(Data Control Language):
?????? 是數據庫控制功能。是用來設置或更改數據庫用戶或角色權限的語句,包括(grant,deny,revoke等)語句。在默認狀態下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人員才有權力執行DCL
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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