这两种键什么意思并比较?请举1例说明面试是时候人家问我的

解决方案 »

  1.   

    什么是surrogate key?
    在数据库的表设计中,通常大家都会将一些必须的信息设计到表当中去,比如一个USER表,那么user_name, user_address可能就是必须的信息,为了表示表数据的唯一性,可能会使用user_name当作primary key。
    这种设计很常见很普遍,当时却会有一些潜在的问题,那就是如果想对user_name做修改的话,那么将变得比较困难,因为user_name有可能是其他的表中的foreign key, 这样不仅需要修改本表中的user_name, 还需要修改其他表中的user_name。如果在表的设计中增加一个附加的字段user_id, 并把它做为primary key, 那么我们再修改user_name的时候,就不会出现刚才所说的那些问题了。这里user_id就是所谓的surrogate key.
      

  2.   

    In relational model database design, a natural key is a candidate key that has a logical relationship to the attributes within that row. A natural key is sometimes called a domain key.The main advantage of a natural key over a surrogate key, which has no such logical relationship, is that it already exists; there is no need to add a new, artificial column to the schema. Using a natural key (when one can be identified) also simplifies data quality: It ensures that there can only be one row for a key; this "one version of the truth" can be verified, because the natural key is based on a real-world observation.