前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Database Design Guidelines

Database Design Guidelines

作者头像
绿巨人
发布2018-07-04 15:30:32
3960
发布2018-07-04 15:30:32
举报
文章被收录于专栏:绿巨人专栏绿巨人专栏

Database Design Guidelines

Principles

  • Support popular databases

Name Style

  • Table Name Style: Pascal Example: Employee It is fine that you may use the singular style or plural style. But singular style is preferred here.
  • Column Name Style: Pascal Example: HouseAddress Avoid to repeat table name in the column name, for example, For table 'Employee', use 'Name' for the column of employee name.
  • Primary Key Name Style: Pascal, PK_${TableName} Example: PK_Employee
  • Index Name Style: Pascal, UK_\({Column1Name}_\){Column2Name} for unique indexes. Use IK_\({Column1Name}_\){Column2Name} for non-unique indexes. Example: UK_Code
  • Foreign Key Name Style: Pascal, FK_\({Column1Name}_\){Column2Name} Example: FK_DepartmentId

Columns

Text columns

  • Do use unicode data type to define text columns.
  • Do use nvarchar for variable length text columns.

Date/Date Time

  • If the value of a date column is expected to present same text result among different time zone, use numeric or text data type to define date/date-time columns. If we define a column with date data type, default, most program language will use date-time type to store values from the column, we should realize the date value will be changed when the data is transited to a different time-zone. For example: 2001/01/15, On the database server, timezone is +8. 2001/01/15, On the application server, timezone is +8. 2001/01/15 01:00:00, On the client, timezone is +9. So if it is a problem to you, try to use a integer or text data type to store the value. Tips: consider the case carefully. ask these questions: If your application will be used in different time-zone? If you only use date part for the column? If you only use time part for the column?

Nullable

  • Avoid to define a column as nullable. Avoid to use null would reduce programing bugs, and bring better performance.
  • Do Not use null for numeric columns if it has same meaning as zero.
  • Do Not use null for text columns if it has same meaning as empty.
  • May use null in a reference column provide that null is acceptable.

Keys

Primary Keys

  • Do create a primary key for any tables.
  • Recommend to use one column for the primary key.
  • Use '${TableName}Id' for the name of the primary key column, the column is denoted as 'Id' columns.
  • Recommend to use integer data type for the primary key column.
  • Avoid change the value of the primary key column after created.
  • It is acceptable to show the the value of the primary key column to customers if need.

Foreign Keys

  • Do create a foreign key if there is a relation between 2 tables. It is useful to keep data integrity.
  • Carefully use delete cascade clause when creating the foreign keys relationship. Tips: Be careful, Use delete cascade is also dangerous. consider it twice before use it.

Database-specific constraints

  • Avoid to use reversed words for name of objects of databases Tips: please read the reversed words from database you will use
  • Avoid to use system prefix in your object names
  • Check length limitation of object names of databases

Tables

Entity Tables

Like departments, employee etc, we store these kind of information into entity tables. In most case, the table like:

  • Definition sample
代码语言:javascript
复制
Employee {EmployeeId, Code, Name, DepartmentId, ...}.

If in your system, an employee only exists in one department, you may use above definition.

Tree Tables

Like most organization, departments will be constructed as a tree, there are some top level departments (or is a root department), except these top level departments, departments must have one and only have one parent department. We define this kind of table as:

  • Definition sample
代码语言:javascript
复制
Department{DepartmentId, Code, Name, **ParentId**, ...}.

In most case, for the usage convenience, we will define a tree table for each tree relationship, denoted as a tree table.

  • Definition sample DepartmentTree{ParentId, ChildId}. For a parent department, The table stores itself and all its descendants. From a view of a child department, the table stores itself and all its ancestors.

Hierarchical Tables

Different as tree tables, in the hierarchical tables, a child would have 0 to n parents. For example, groups and users:

  • Rules
    • an user would exists in multiple groups.
    • an groups would exists in multiple groups.
  • Definition sample
代码语言:javascript
复制
User(userId, Name, isGroup)
GroupUser(parentId, userId)
GroupUserTree(parentId, userId)

The table GroupUser stores the direct relationships. Instead, the table GroupUserTRee stores the redundant relationships like tree tables above.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-09-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Database Design Guidelines
    • Principles
      • Name Style
        • Columns
          • Text columns
          • Date/Date Time
          • Nullable
        • Keys
          • Primary Keys
          • Foreign Keys
        • Database-specific constraints
          • Tables
            • Entity Tables
            • Tree Tables
            • Hierarchical Tables
        相关产品与服务
        数据库
        云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档