Data types and constraints

The data in the database is stored in the data table. In order to store the data more accurately and ensure the correctness and validity of the data in the table, some mandatory verifications can be added to the table when creating the table, such as data types and constraints.

1. Data Type

The data type refers to the data type specified for the fields in the table when the table is created. Only the data meets the type requirements can be stored. The principle of using the data type is: enough, try to use a small value range instead of a large one. This can save more storage space.

Common data types are as follows:

  • Integer: int, bit
  • Decimal: decimal
  • String: varchar, char
  • Date time: date, time, datetime
  • Enumeration type (enum)

Data type description:

  • Decimal represents a floating-point number, such as decimal(5, 2) represents coexistence of 5 digits, and decimals occupy 2 digits.
  • Char represents a fixed-length character string, such as char(3), if you fill with'ab', a space will be added as'ab', and 3 represents the number of characters
  • Varchar represents a variable-length character string, such as varchar(3), it will store “ab” when filling with “ab”, and 3 represents the number of characters
  • For files such as pictures, audios, videos, etc., they are not stored in the database, but uploaded to a server, and then the storage path of the file is stored in the table.
  • The string text means storing large texts. It is recommended to use when the characters are greater than 4000, such as technical blogs.

 

2. Data constraints

Constraints refer to additional requirements for data based on data type restrictions.

Common constraints are as follows:

  • Primary key: The order of physical storage. MySQL recommends that the primary key field of all tables is called id, and the type is int unsigned.
  • Not null: This field is not allowed to fill in a null value.
  • Unique: The value of this field is not allowed to be repeated.
  • Default default: When the value corresponding to the field is not filled in, the default value will be used. If it is filled in, the filling in shall prevail.
  • Foreign key: To constrain the relational field. When filling in the value for the relational field, it will check whether the value exists in the associated table. If it exists, it will fill in successfully, if it does not exist, it will fail to fill and an exception will be thrown. (Foreign key) The key is to use the primary key data in a table)

 

3. Data type appendix table

1. Integer type

Types of Byte size Signed range (Signed) Unsigned range (Unsigned)
TINYINT 1 -128 ~ 127 0 ~ 255
SMALLINT 2 -32768 ~ 32767 0 ~ 65535
MEDIUMINT 3 -8388608 ~ 8388607 0 ~ 16777215
INT/INTEGER 4 -2147483648 ~2147483647 0 ~ 4294967295
BIGINT 8 -9223372036854775808 ~ 9223372036854775807 0 ~ 18446744073709551615

2. String

Types of Description scenes to be used
CHAR Fixed length, small data ID number, mobile phone number, phone number, password
VARCHAR Variable length, small data Name, address, brand, model
TEXT Variable length, the number of characters is greater than 4000 Store small articles or news
LONGTEXT Variable length, extremely large text data Store extremely large text data

3. Time Type

Types of Byte size Example
DATE 4 '2020-01-01'
TIME 3 '12:29:59'
DATETIME 8 '2020-01-01 12:29:59'
YEAR 1 '2017'
TIMESTAMP 4 '1970-01-01 00:00:01' UTC ~ '2038-01-01 00:00:01' UTC

 

4. Summary

  • Commonly used data types:
    • Integer: int, bit
    • Decimal: decimal
    • String: varchar, char
    • Date time: date, time, datetime
    • Enumeration type (enum)
  • Common constraints:
    • Primary key constraint
    • Not null constraint not null
    • Unique constraint
    • Default constraint
    • Foreign key constraints
  • Data types and constraints ensure the accuracy and completeness of the data in the table

Guess you like

Origin blog.csdn.net/weixin_48135624/article/details/115225804