alamide的笔记库「 87篇笔记 」「 小破站已建 0 天啦 🐶 」


MySQL 基础信息

2023-03-16, by alamide

1.一些🍪

2.数据类型

2.1 整数类型

Type Storage (Bytes) Minimum Value Signed Minimum Value Unsigned Maximum Value Signed Maximum Value Unsigned
TINYINT 1 -128 0 127 255
SMALLINT 2 -32768 0 32767 65535
MEDIUMINT 3 -8388608 0 8388607 16777215
INT 4 -2147483648 0 2147483647 4294967295
BIGINT 8 -263 0 263-1 264-1

2.2 浮点数

2.3 定点数

2.4 文本

2.4.1 CHARACTER AND COLLATE

A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set. Let's make the distinction clear with an example of an imaginary character set.

Suppose that we have an alphabet with four letters: A, B, a, b. We give each letter a number: A = 0, B = 1, a = 2, b = 3. The letter A is a symbol, the number 0 is the encoding for A, and the combination of all four letters and their encodings is a character set.

Suppose that we want to compare two string values, A and B. The simplest way to do this is to look at the encodings: 0 for A and 1 for B. Because 0 is less than 1, we say A is less than B. What we've just done is apply a collation to our character set. The collation is a set of rules (only one rule in this case): “compare the encodings.” We call this simplest of all possible collations a binary collation.

But what if we want to say that the lowercase and uppercase letters are equivalent? Then we would have at least two rules: (1) treat the lowercase letters a and b as equivalent to A and B; (2) then compare the encodings. We call this a case-insensitive collation. It is a little more complex than a binary collation.

In real life, most character sets have many characters: not just A and B but whole alphabets, sometimes multiple alphabets or eastern writing systems with thousands of characters, along with many special symbols and punctuation marks. Also in real life, most collations have many rules, not just for whether to distinguish lettercase, but also for whether to distinguish accents (an “accent” is a mark attached to a character as in German Ö), and for multiple-character mappings (such as the rule that Ö = OE in one of the two German collations).

指定 CHARACTER SET 之后,会有默认的 COLLATE

2.4.2 ENUM

CREATE TABLE shirts (
    name VARCHAR(40),
    size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);

2.4.3 SET

A SET is a string object that can have zero or more values, each of which must be chosen from a list of permitted values specified when the table is created. SET column values that consist of multiple set members are specified with members separated by commas (,). A consequence of this is that SET member values should not themselves contain commas.

CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');

SELECT col FROM myset;

out:

+------+
| col  |
+------+
| a,d  |
| a,d  |
| a,d  |
| a,d  |
| a,d  |
+------+

2.5 时间

2.5.1 数据类型

类型 字节 日期格式 最小值 最大值
YEAR 1 YYYY或YY 1901 2155
TIME 3 HH:MM:SS -838:59:59 838:59:59
DATE 3 YYYY-MM-DD 1000-01-01 9999-12-03
DATETIME 8 YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 9999-12-31 23:59:59
TIMESTAMP 4 YYYY-MM-DD HH:MM:SS 1970-01-01 00:00:00 UTC 2038-01-19 03:14:07 UTC

2.5.2 TIMESTAMP AND DATETIME

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable.

CREATE TABLE t_date (
    id INTEGER PRIMARY KEY AUTO_INCREMENT,
    `year` YEAR,
    `time` TIME,
    `date` DATE,
    `date_time` DATETIME,
    `time_stamp` TIMESTAMP
);

INSERT INTO t_date (id, year, time, date, date_time, time_stamp)
VALUES  (1, 2023, '21:21:03', '2023-03-15', '2023-03-15 21:21:03', '2023-03-15 21:21:03');

SELECT * FROM t_date;
idyeartimedatedate_timetime_stamp
1202321:21:032023-03-152023-03-15 21:21:032023-03-15 21:21:03

修改时区

SET time_zone = 'US/Eastern'; -- 改变当前 Session 的时区,并不会影响其它 Session (SET GLOBAL time_zone = timezone; 改变全局)
SELECT * FROM t_date;
idyeartimedatedate_timetime_stamp
1202321:21:032023-03-152023-03-15 21:21:032023-03-15 17:21:03
Tags: DB - MySQL
~ belongs to alamide@163.com