table of Contents
2.2 The difference between char and varchar
3.2 The difference between datetime and timestamp
4.1.1 Column-level constraints
4.7 The similarities and differences between primary key constraints and unique constraints
1. Numerical
1.1 Integer
tinyint: 1 byte
smallint: 2 bytes
mediumint: 3 bytes
int/integer: 4 bytes
bigint: 8 bytes
Features:
1) If the unsigned keyword is not set with or without symbols, the default is with symbols
2) If the inserted data exceeds the integer range, an out of range exception will be reported, and the critical value will be inserted
3) If the length is not set, there will be a default length. The length represents the maximum width of the display. If you want to enable 0 filling, you need to use it with zerofill
1.2 Decimals
1.2.1 Fixed-point
DEC(M,D):
DECIMAL(M,D):
1.2.2 Floating point numbers
float(M,D): 4 bytes
double(M,D): 8 bytes
1.2.3 Features
1) M: digits of integer part + digits of decimal part D: digits of decimal part
2) M and D can be omitted, decimal M defaults to 10, D defaults to 0; float and double will determine the precision according to the precision of the inserted value
3) High accuracy of fixed-point
2. Character type
2.1 Short text
char(M): the number of M characters
varchar(M): the number of M characters
2.2 The difference between char and varchar
Writing | The meaning of M | Features | Space consumption | effectiveness | |
char | char(M) | The maximum number of characters, can be omitted, the default is 1 | Fixed-length characters | Cost | high |
varchar | varchar(M) | Maximum number of characters, cannot be omitted | Variable length characters | save | low |
2.2 Long text
text: is a large object that can store a lot of data
Field Type | Number of storage bytes |
TINYTEXT | 256 |
TEXT | 65535 |
MEDIUMTEXT | 16777215 |
LONGTEXT | 4294967295 |
blob: Binary large object, a container that can store large amounts of data
Field Type | Number of storage bytes |
TinyBlob | 256 |
Blob | 65535 |
MediumBlob | 16777215 |
LongBlob | 4294967295 |
3. Date type
3.1 Specific date types
date: 4 bytes
datetime: 8 bytes
timestamp: 4 bytes
time: 3 bytes
year: 1 byte
3.2 The difference between datetime and timestamp
1) Timestamp is related to the actual urban area and can better reflect the actual date, while datetime can only reflect the local time zone at the time of insertion
2) The attribute of timestamp is affected by MySql version and SQL Mode
4. Five constraints
4.1 Constraint classification
Constraint: a restriction, used to limit the data in the table, in order to ensure the accuracy and reliability of the data in the table
4.1.1 Column-level constraints
The six constraints are supported grammatically, but the foreign key constraints have no effect
4.1.2 Table-level constraints
Except for non-empty and default, all others are supported
4.2 Non-empty constraints
NOT NULL: used to ensure that the value of the field is not empty
4.3 Default constraints
DEFAULT: used to ensure that the field has a default value
4.4 Primary key constraints
PRIMARY KEY: used to ensure that the value of the field is unique and non-empty
4.5 Unique Constraint
UNIQUE: used to ensure that the value of the field is unique and can be empty
4.6 Foreign key constraints
POREIGN KEY: used to limit the relationship between two tables
4.7 The similarities and differences between primary key constraints and unique constraints
Uniqueness | Whether it is allowed to be empty | There can be several in a single table | Whether to allow combination | |
Primary key constraint | can | Can't | At most one | Allowed, but not recommended |
Unique constraint | can | can | Can be multiple | Allowed, but not recommended |