一些資料庫筆記

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.