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.