Data type # ape learning database operations Python

### a, MySQL data types

Column data type is defined in the basic rules of what type of data and how the data is actually stored can be stored

Data stored in the data type restrictions column column. For example, numeric data types can only accept data columns of numeric type

When designing tables, particular attention should be the type of data used. Using the wrong type of data can severely affect the function and performance of the application.

Change the column is not a trivial matter that contains data (and doing so may result in loss of data).

Data Type: integer, floating point, string, date, etc.

------------------------------------

1 ####, string data types

The most common data type is string data type. They are stored string, such as name, address, electrical

Telephone number, zip code and so on.

Regardless of the type used in the form of string data, string values ​​must be enclosed in quotes

There are two basic types of strings, respectively, and a variable length string of fixed length string

- fixed-length string: char

1. Accept fixed length character string of a length specified in table creation.

  Storage is not more than a fixed length column data characters of specified length.

2. After the specified length, it will be assigned a fixed memory space for storing data

```

char (7) No matter how many characters are actually inserted, it will occupy seven character positions

```

- variable length string varchar

Variable-length strings are stored

    varchar (7) If the actual insert 4 characters, then it has only four character position, of course, can not exceed the data length of the insert 7 characters.

----------------------------------------------------

##### Note

    Since the variable-length data types so flexible, why use fixed-length data types?

 

    Answer: Because the performance, MySQL processing fixed length column column faster than the processing becomes long.

+ Text variable-length text type storage

![image](./imgs/3232559721674152AA6C45916CE1C086.png)

---

2 ####, value type

Storing numeric data type value. MySQL supports multiple numeric data types, having different values ​​of each storage range. The larger the support of the range, the more storage space required

Unlike the string, the value should not be enclosed in quotation marks

![image](./imgs/3D63395B658D42489CE95C2157C15213.png)

    decimal (5, 2) represents the total value of five, representing two decimal

    tinyint 1 byte (8 bits) 0-255. -128,127

    int 4 bytes. -21 million 2.1 billion. 0-42 one hundred million

    float. 

 

    No special MySQL data type stored currency generally used DECIMAL (8, 2)

#### signed or unsigned

All numeric data types (except BIT and BOOLEAN) may be signed or unsigned

- Signed number array may store positive or negative values

- unsigned number array can store only positive numbers.

- as signed by default, but if you know you need to store a negative value, you can use the keywords UNSIGNED

---

##### Note

    If the zip code value is stored as a type similar to 01234, the value of 1234 will be saved, string type required in this case

    Phone number should be stored it what?

---

#### 3, the date and time types

MySQL using specialized data type to store date and time values

![image](./imgs/B493924B597B48D7AE436F5EFFB09DCC.png)

    datetime 8 bytes 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59

---

#### 4, binary data types

Binary data may be stored in any type of data (even binary information), such as images, multimedia, word processing documents, etc.

![image](./imgs/B8CCF6C0BCA5402B907D02F8C415B019.png)

---

### II, Field Constraints

- unsigned Unsigned (used for numeric type, expressed as a positive number, can not write can represent positive and negative numbers)

- Back Type field limits the width of the bracketed 

  -. Char (5) varchar (7) is appended to the character string type represents the length limit

  - int (4) does not make sense, default is unsigned int int (11), signed int (10)

  - int (4) unsigned zerofill only when provided with a leading zero to int type, set the width of the meaningful int.

- not null can not be empty, when the operation of the database if the input data for this field is NULL, the error will be

- default setting default values

- primary key for the primary key is not null, and only the general and used together with the auto-increment.

- auto_increment defined as a self-energizing properties, generally used for the primary key, the value will be automatically increased by 1

- unique unique index (data not repeat: user name) can increase the query speed, but will reduce the insertion and update speed

---

### three, MySQL operator

- arithmetic operators: +, -, *, /,%

- comparison operators: =,>, <,> =, <=, =,!

- Compare database-specific: in, not in, is null, is not null, like, between, and

- logical operators: and, or, not

- like: Support special symbols% and _;

    Where% represents any number of any character _ represents any one character

---

### Fourth, the primary key

1, each row in the table ** should have its own uniquely identifies a **, for recording two records can not be repeated, any two rows can have the same primary key value

2, should always define the primary key, though not always need a primary key, but most database designers should ensure that they have created for each table has a primary key, so that subsequent data manipulation and management.

#### Claim

- Once inserted into the table record, the primary key is best not to modify

- does not allow NULL

- Do not use the primary key column values ​​may change.

    (For example, if you use a name as the primary key to identify a supplier when the supplier consolidation and change its name, you must change the primary key.)

- increment integer types: the database will automatically assign an integer increment for each record when you insert data, so we do not worry about the primary key repeat, do not have their own pre-generated primary key

- a plurality of columns can be used as the primary key, the primary key is not used. Use as multi-column primary keys, combinations of all column values ​​must be unique

 


 

Mastery learning method, as will bend to overtake!

Learning to ape: the achievements of their own just a boutique!

Guess you like

Origin www.cnblogs.com/itxdl/p/12530690.html