#Common data types
/*
Numerical type:
integer
decimal:
fixed-point
floating-point number
Character type:
shorter text: char, varchar
Longer text: text, blob (longer binary data)
date type:
*/
#1. Integer
/*
classification:
tinyint, smallint, mediumint, int/integer, bigint
1 2 3 4 8
Features:
① If you do not set unsigned or signed, the default is signed. If you want to set unsigned, you need to add the unsigned keyword. ② If the
inserted value exceeds the range of the integer, an out of range exception will be reported, and a critical value will be inserted. Value
③ If the length is not set, there will be a default length.
The length represents the maximum width of the display. If it is not enough, it will be filled with 0 on the left, but it must be used with zerofill!
*/
#1. How to set unsigned and signed
DROP TABLE IF EXISTS tab_int;
CREATE TABLE tab_int(
t1 INT(7) ZEROFILL,
t2 INT(7) ZEROFILL
);
DESC tab_int;
INSERT INTO tab_int VALUES(-123456);
INSERT INTO tab_int VALUES(-123456,-123456);
INSERT INTO tab_int VALUES(2147483648,4294967296);
INSERT INTO tab_int VALUES(123,123);
SELECT * FROM tab_int;
#2. Decimal
/*
classification:
1. Floating-point type
float(M,D)
double(M,D)
2. Fixed-point type
dec(M,D)
decimal(M,D)
Features:
①
M: integer part + decimal part
D: decimal part
If it exceeds the range, insert a critical value
②
Both M and D can be omitted.
If it is decimal, M defaults to 10, and D defaults to 0.
If it is float or double, the precision will be determined according to the precision of the inserted value.
③The precision of the fixed-point type is high. If the precision of the inserted value is required to be high, such as currency calculation, etc., consider using it
*/
#test M and D
DROP TABLE tab_float;
CREATE TABLE tab_float(
f1 FLOAT,
f2 DOUBLE,
f3 DECIMAL
);
SELECT * FROM tab_float;
DESC tab_float;
INSERT INTO tab_float VALUES(123.4523,123.4523,123.4523);
INSERT INTO tab_float VALUES(123.456,123.456,123.456);
INSERT INTO tab_float VALUES(123.4,123.4,123.4);
INSERT INTO tab_float VALUES(1523.4,1523.4,1523.4);
#Principle:
/*
The simpler the selected type, the better, and the smaller the type that can save the value, the better
*/
#3. Character type
/*
Shorter text:
char
varchar
other:
binary and varbinary are used to hold shorter binary
enums are used to hold enumeration
sets are used to hold collections
Longer text:
text
blob (larger binary)
Features:
The meaning of writing M Features Space consumption Efficiency
char char(M) The maximum number of characters, can be omitted, the default is 1 Fixed-length characters Relatively expensive High
varchar varchar(M) The maximum number of characters, can not be omitted Variable-length characters are more economical and low
*/
CREATE TABLE tab_char(
c1 ENUM('a','b','c')
);
INSERT INTO tab_char VALUES('a');
INSERT INTO tab_char VALUES('b');
INSERT INTO tab_char VALUES('c');
INSERT INTO tab_char VALUES('m');
INSERT INTO tab_char VALUES('A');
SELECT * FROM tab_set;
CREATE TABLE tab_set(
s1 SET('a','b','c','d')
);
INSERT INTO tab_set VALUES('a');
INSERT INTO tab_set VALUES('A,B');
INSERT INTO tab_set VALUES('a,c,d');
#4. Date type
/*
Classification:
date only saves the date
time only saves the time
year only saves the year
datetime saves date + time
timestamp saves date + time
Features:
Influenced by byte range, time zone, etc.
datetime 8 1000——9999 is not affected by
timestamp 4 1970-2038 is not affected
*/
CREATE TABLE tab_date(
t1 DATETIME,
t2 TIMESTAMP
);
INSERT INTO tab_date VALUES(NOW(),NOW());
SELECT * FROM tab_date;
SHOW VARIABLES LIKE 'time_zone';
SET time_zone='+9:00';