#20 数据库设计规范
DB 设计规范 编码风格 2021-09-18随手写的,以命名为主(原来想写的是命名规范,结果写超了纲)。
#19 MySQL 相关的命令汇总
MySQL DB 2021-01-06通过 apt 和 apt-file 列出 mysql 提供的所有可执行文件。
#18 MySQL 版本历史
DB MySQL 2021-01-06#17 MySQL 管理语句
DB MySQL 2020-03-25
#16
MySQL 字段类型 varchar
和 text
的差异
DB MySQL
2019-11-25
一次数据库表结构调整,引起了我对 MySQL 字段类型 TEXT
和 VARCHAR
的思考。
#15 MySQL 数据类型总结
DB MySQL 2019-08-25#14 一道有趣的 MySQL 小题目
DB MySQL 2019-04-19在开源中国每日动弹中看到这么一道题目,蛮有意思,还学到了一个新的 MySQL 语法:CTE。
#13 MySQL: wait for table metadata lock
MySQL DB 2019-03-29#12 MySQL 笔记
DB MySQL 2017-12-06几处小的知识点
#11 MySQL Boolean
DB MySQL 2017-09-01我们通常使用以下几种方式表示布尔值:
tinyint(1)
bit(1)
enum('Y','N')
在 MySQL 中 bool
, boolean
是 tinyint(1)
的别名。
如果只是一个 True OR False 的布尔型,没有比 bit(1)
更合适的了。
但是也有些时候,我们有好几个 bool 型用一个字段表示,最好用 bit(m)
,我也用过 int
型。
附:不同值的长度
Type | Bit | Byte | Note |
---|---|---|---|
tinyint |
8 | 1 | |
smallint |
16 | 2 | |
middleint |
24 | 3 | |
int |
32 | 4 | |
bigint |
64 | 8 | |
bit(m) |
m | (m+7)/8 | |
binary[(m)] |
m | m 默认值:1 | |
varbinary(m) |
(m+1) | ||
tinyblob |
(L+1) | 1B 长度,最长 255B | |
blob[(m)] |
(L+2) | 2B 长度,最长 64KB - 1 | |
mediumblob |
(L+3) | 3B 长度,最长 16MB - 1 | |
longblob |
(L+4) | 4B 长度,最长 4GB - 1 | |
enum('a',..) |
1/2 | 最多可以存储 2^16 - 1 个值 | |
set('a',..) |
1/2/3/4/8 | 最多可以存储 64 个值 |
PS: blob
如果指定长度 m
,MySQL 会选择足够存储数据的最小长度。
PS: MySQL set 类型
参考资料与拓展阅读
- MySQL 文档 (v8.0), 11.9 Using Data Types from Other Database Engines
- MySQL 文档 (v5.7), 11.9 Using Data Types from Other Database Engines
- MySQL 文档 (v5.6), 11.8 Using Data Types from Other Database Engines
#10 数据库:应该怎么存储 IP 地址
DB MySQL 2017-08-25IP 地址的存储方式就两种:字符串,整型数。
表示方法
ipv4 VARCHAR(15) -- xx.xx.xx.xx
ipv6 VARCHAR(39) -- xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
ipv4 INT UNSIGNED
ipv6 BINARY(16)
-- 如果同时需要表达 IPv4 和 IPv6
ip VARCHAR(39)
ip BINARY(16)
PS:我还在有些地方看到过这种采用 12 位整数表示 IPv4 的方法:'192.168.0.1' => 192168000001, 如果存到 MySQL 的话,只能采用 BIGINT
或 VARCHAR(12)
了。
如果省略中间的分号,ipv6 只需要 VARCHAR(32)
就行了。
整型和字符串的转换
INET_ATON
/INET_NTOA
INET6_ATON
/INET6_NTOA
#9 MongoDB 聚集(数据表)中的 ID
DB MongoDB 2016-06-01#8 SQL 的历史
DB SQL 2016-03-14历史
- 70 年代初,IBM 公司开发了 SEQUEL 语言 (Structured English Query Language,结构化英语查询语言),用于管理 RDB。
- 70 年代末,IBM 和甲骨文分别开始开发基于 SQL 的 RDBMS。
PS: IBM 的产品就包括大名鼎鼎的 DB2,世界上最早的 SQL 数据库。
PS: 甲骨文当时还叫做 Relational Software, Inc - 1980 年,由于商标问题,SEQUEL 改名 SQL。
虽然官方发音是ess-cue-el
, 但至今为止,不少人还是将其读做/ˈsiːkwəl/
。 - 1986 年被美国国家标准学会标准化(ANSI X3.135-1986)
- 1987 年,ISO 采纳 ANSI SQL (ISO 9075:1987),所以这个版本也被称之为 SQL87。
- 后来,SQL 陆续推出 89,92,1999, 2003 .... 多个版本。
应该是 ISO 负责制定和维护吧,也无所谓啦。
版本
SQL-86 (or SQL-87) is the ISO 9075:1987 standard of 1987
SQL-89 is the ISO/IEC 9075:1989 standard of 1989
SQL-92 is the ISO/IEC 9075:1992 standard of 1992
SQL:1999 is the ISO/IEC 9075:1999 standard of 1999
SQL:2003 is the ISO/IEC 9075:2003 standard of 2003
SQL:2006 is the ISO/IEC 9075:2006 standard of 2006
SQL:2008 is the ISO/IEC 9075:2008 standard of 2008
SQL:2011 is the ISO/IEC 9075:2011 standard of 2011
SQL:2016 is the ISO/IEC 9075:2016 standard of 2016
Year | Name | Alias | Comments |
---|---|---|---|
1986 | SQL-86 | SQL-87 | First formalized by ANSI |
1989 | SQL-89 | Minor revision that added integrity constraints | |
1992 | SQL-92 | SQL2 | Major revision (ISO 9075) |
1999 | SQL:1999 | SQL3 | |
2003 | SQL:2003 | ||
2006 | SQL:2006 | ||
2008 | SQL:2008 | ||
2011 | SQL:2011 | ||
2016 | SQL:2016 | ||
2019 | SQL:2019 |
SQL:1999
Added regular expression matching, recursive queries (e.g. transitive closure), triggers, support for procedural and control-of-flow statements, nonscalar types (arrays), and some object-oriented features (e.g. structured types), support for embedding SQL in Java (SQL/OLB) and vice versa (SQL/JRT)
2003
Introduced XML-related features (SQL/XML), window functions, standardized sequences, and columns with autogenerated values (including identity columns)
2006
ISO/IEC 9075-14:2006 defines ways that SQL can be used with XML. It defines ways of importing and storing XML data in an SQL database, manipulating it within the database, and publishing both XML and conventional SQL-data in XML form. In addition, it lets applications integrate queries into their SQL code with XQuery, the XML Query Language published by the World Wide Web Consortium (W3C), to concurrently access ordinary SQL-data and XML documents.
2008
Legalizes ORDER BY outside cursor definitions. Adds INSTEAD OF triggers, TRUNCATE statement,[34] FETCH clause
2011
Adds temporal data (PERIOD FOR)[35] (more information at: Temporal database#History). Enhancements for window functions and FETCH clause.
2016
Adds row pattern matching, polymorphic table functions, JSON
2019
Adds Part 15, multidimensional arrays (MDarray type and operators)
https://en.wikibooks.org/wiki/Structured_Query_Language
https://en.wikibooks.org/wiki/MySQL
https://en.wikibooks.org/wiki/PostgreSQL
https://en.wikibooks.org/wiki/SQLite
https://en.wikipedia.org/wiki/SQL_reserved_words
ISO 9075
最新的 SQL 标准一共分成 9 个部分(Part 5,6,7,8,12 可能是被废弃了):
- Part 1: Framework (SQL/Framework)
基本概念 - Part 2: Foundation (SQL/Foundation)
基础语法 - Part 3: Call-Level Interface (SQL/CLI)
应该是编程语言方面的接口 - Part 4: Persistent stored modules (SQL/PSM)
SQL 面向过程编程 - Part 9: Management of External Data (SQL/MED)
- Part 10: Object language bindings (SQL/OLB)
Java SQLJ 相关内容 - Part 11: Information and definition schemas (SQL/Schemata)
- Part 13: SQL Routines and types using the Java TM programming language (SQL/JRT)
又是 Java 相关 - Part 14: XML-Related Specifications (SQL/XML)
XML 相关
PS: 前缀 ISO/IEC 9075-<n>:2016 – Information technology – Database languages – SQL –
省略。
PS: 还有一个拓展标准:ISO/IEC 13249 SQL Multimedia and Application Packages
变种
多数数据库没有严格按照标准来实现,导致不通平台上的 SQL 语句是不能跨平台的。
以下是两种主要的 SQL 方言:
- T-SQL(Transact-SQL): SQLServer
- PL/SQL: Oracle