zoukankan      html  css  js  c++  java
  • 一些数据库笔记

    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

  • 相关阅读:
    IOC理论推导
    spring leile
    缓存
    动态SQL
    canvas小球运动
    jdk1.7后字符串的总结
    用ssm框架简单创建一个增删改查案列
    京东物流居家品类各区域联系人
    京东网址收藏
    京东自营申请新品打标方法
  • 原文地址:https://www.cnblogs.com/qsort/p/2259317.html
Copyright © 2011-2022 走看看