"High-performance Database" three

Source: Internet
Author: User

It is now the third lecture, and I do not know the audience geometry ... Say good words, send flowers, said not good words, throw an egg! At least let me chair3 know a few people listen.

Okay, cut the crap, now begin:

Points:

Constraints

Default value

Calculated fields

Index

This is the most common part of database design and programming, listen to my one by one future

1. Constraint.

Constraints? What is a constraint? That is, the numeric qualification of a field. To maintain the most party purity of database data. First-class programmers should know the scope of a field to fill in.

Well, the theory does not say, give an example:

Create Table People (
Name varchar(20) Not Null, --姓名
Age int Not Null Check(Age>0) --年龄
)

The guys looked at age int is not Null check (age>0), and Check (age>0) in is a value that prevents users from accidentally filling in <0. Haha, is the womb even-1 years old?

Apparently the State Department has no such rules. Age>0 must therefore be forced.

2, the default value.

What is the default value I don't need to tell you. In datasheet design, null-minimizing fields are avoided. The default value is used.

Still give an example to be persuasive! See:

Create Table People (
Name varchar(20) Not Null, --姓名
Sex bit Not Null Default 1, --性别
Age int Not Null Check(Age>0) --年龄
)

Did you see it? Sex bit not Null Default 1, gender, also "male" or "female", with a number indicated also 1 or 0. You must use NOT NULL when you prevent more occurrences of a data field, such as null.

Take care of a lot of lazy general customers (as if to say oneself), give him a default "male" Good! Alas, after all, men and women do not play equality, many places are more men. (In pain ...) )

Here is just an example, a lot of places can be used, such as date. Try to avoid null, and using NOT NULL + default can purify your database.

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.