Physical Database Design-Qualifying columns for valid values

Source: Internet
Author: User

I. Description of the problem

In fact, this article is very simple, because everyone is solved by this method, I decided to use their own language to describe the problem clearly.

Suppose we have a column that can only fetch certain valid values. For example, a user table, we have a last Name column, we need to limit the value inside the Chinese surname, such as: Zhou Chansunli Lingwei romanization.

second, anti-pattern

For this problem, only beginners may use this method, that is, using a check constraint or trigger to qualify a column value, such as:

  CHECK (LastName in (' Zhao ',' qian ', ' Sun ', ' Li '

The disadvantages of doing this are as follows:

1, it is difficult to get all the optional values, if I want to make a drop-down list, let the user choose the last name that can be entered, then the SQL statement is complex, you need to query the system view.

2, add the optional value, if we want to add a foreign surname. Then you need to modify the check constraint or trigger.

3, delete the optional value, assuming that from today does not support foreign surnames, but the database has already had a foreign surname, you have to keep this obsolete value. But the drop-down list box is no longer available to the user, which is a big hassle.

4, transplant poor, CHECK, constraint, trigger syntax in a variety of databases are not the same, the transplant is very difficult.

Third, the solution

Create a checklist with each row containing the values that allow the last name to appear, and then define a foreign key constraint.

This you know, no more nonsense.

Unless you are sure that these values will not change, you can do so by using a check constraint or a trigger. For example, Gender: male, female. Otherwise, it is better to use a checklist.

Physical Database Design-Qualifying columns for valid values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.