sensitive。在MySQL中,对于Column
Collate其约定的命名方法如下:
*_bin:
表示的是binary
case
sensitive
collation,也就是说是区分大小写的
*_cs:
case
sensitive
collation,区分大小写
*_ci:
case
insensitive
collation,不区分大小写
###########
#
Start
binary
collation
example
###########
mysql>
create
table
case_bin_test
(word
VARCHAR(10))
CHARACTER
SET
latin1
COLLATE
latin1_bin
Query
OK,
0
rows
affected
(0.02
sec)
mysql>
INSERT
INTO
case_bin_test
VALUES
('Frank'),('Google'),('froogle'),('flickr'),('FlicKr')
Query
OK,
5
rows
affected
(0.00
sec)
Records:
5
Duplicates:
0
Warnings:
0
mysql>
SELECT
*
FROM
case_bin_test
WHERE
word
LIKE
'f%'
+---------+
|
word
|
+---------+
|
froogle
|
|
flickr
|
+---------+
2
rows
in
set
(0.00
sec)
mysql>
SELECT
*
FROM
case_bin_test
WHERE
word
LIKE
'F%'
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.00
sec)
###########
#
End
###########
2、另外一种方法
###########
#
Start
case
sensitive
collation
example
###########
mysql>
create
table
case_cs_test
(word
VARCHAR(10))
CHARACTER
SET
latin1
COLLATE
latin1_general_cs
Query
OK,
0
rows
affected
(0.08
sec)
mysql>
INSERT
INTO
case_cs_test
VALUES
('Frank'),('Google'),('froogle'),('flickr'),('FlicKr')
Query
OK,
5
rows
affected
(0.00
sec)
Records:
5
Duplicates:
0
Warnings:
0
mysql>
SELECT
*
FROM
case_cs_test
WHERE
word
LIKE
'F%'
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.00
sec)
mysql>
SELECT
*
FROM
case_cs_test
WHERE
word
LIKE
'f%'
+---------+
|
word
|
+---------+
|
froogle
|
|
flickr
|
+---------+
2
rows
in
set
(0.00
sec)
###########
#
end
###########
3、还有一种方法就是在查询时指定collation
mysql>
create
table
case_test
(word
VARCHAR(10))
CHARACTER
SET
latin1
Query
OK,
0
rows
affected
(0.01
sec)
mysql>
INSERT
INTO
case_test
VALUES
('Frank'),('Google'),('froogle'),('flickr'),('FlicKr')
Query
OK,
7
rows
affected
(0.01
sec)
Records:
7
Duplicates:
0
Warnings:
0
mysql>
SELECT
*
FROM
case_test
WHERE
word
LIKE
'f%'
+---------+
|
word
|
+---------+
|
Frank
|
|
froogle
|
|
flickr
|
|
FlicKr
|
+---------+
6
rows
in
set
(0.01
sec)
mysql>
SELECT
*
FROM
case_test
WHERE
word
LIKE
'F%'
+---------+
|
word
|
+---------+
|
Frank
|
|
froogle
|
|
flickr
|
|
FlicKr
|
+---------+
6
rows
in
set
(0.01
sec)
mysql>
SELECT
*
FROM
case_test
WHERE
word
COLLATE
latin1_bin
LIKE
'F%'
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.05
sec)
mysql>
SELECT
*
FROM
case_test
WHERE
word
COLLATE
latin1_bin
LIKE
'f%'
+---------+
|
word
|
+---------+
|
froogle
|
|
flickr
|
+---------+
2
rows
in
set
(0.00
sec)
mysql>
SELECT
*
FROM
case_test
WHERE
word
LIKE
'f%'
COLLATE
latin1_bin
+---------+
|
word
|
+---------+
|
froogle
|
|
flickr
|
+---------+
2
rows
in
set
(0.00
sec)
mysql>
SELECT
*
FROM
case_test
WHERE
word
LIKE
'F%'
COLLATE
latin1_bin
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.01
sec)
mysql>
SELECT
*
FROM
case_test
WHERE
word
LIKE
'F%'
COLLATE
latin1_general_cs
+---------+
|
word
|
+---------+
|
Frank
|
|
FlicKr
|
+---------+
4
rows
in
set
(0.04
sec)
南大通用,人大金仓谈及数据库的发展历史,就不得不提及三位数据库领域的开拓者,分别是Frank、Micheal和JimGray,他们为数据库理论奠定了坚实的基础,都获得了图灵奖。
早在1972年,Micheal最早提出了Ingres数据库,于2014年获得图灵奖,Ingres数据库最后分化衍生为Sybase与Postgres两部分。
其中Postgres数据库有大量分析函数,适用于分析型事务,尤其是OLAP。1972年,埃里森在硅谷开发了Oracle数据库,再到1983年IBM开发了DB2数据库,同年Tdata诞生,直到1995年MySQL数据库诞生。
而如今的Oracle于2009年收购了MySQL,这样一来就同时拥有了Oracle和开源的MySQL两套数据库,MySQL的创始人在离开后又开发了一套数据库MariaDB,现在国内有很多银行,像亿联银行等新的银行都在使用MariaDB。
JSON (JavaScriptObject Notation) 是一种轻量级的数据交换格式,主要用于传送数据。JSON采用了独立于语言的文本格式,类似XML,但是比XML简单,易读并且易编写。对机器来说易于解析和生成,并且会减少网络带宽的传输。由于JSON格式可以解耦javascript客户端应用与Restful服务器端的方法调用,因而在互联网应用中被大量使用。
JSON的格式非常简单:名称/键值。之前MySQL版本里面要实现这样的存储,要么用VARCHAR要么用TEXT大文本。 MySQL5.7发布后,专门设计了JSON数据类型以及关于这种类型的检索以及其他函数解析。我们先看看MySQL老版本的JSON存取。
示例表结构:
CREATE TABLE json_test(
id INT,
person_desc TEXT
)ENGINE INNODB
我们来插入一条记录:
INSERT INTO json_test VALUES (1,'{
"programmers": [{
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
}, {
"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
}, {
"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"
}],
"authors": [{
"firstName": "Isaac",
"lastName": "Asimov",
"genre": "sciencefiction"
}, {
"firstName": "Tad",
"lastName": "Williams",
"genre":"fantasy"
}, {
"firstName": "Frank",
"lastName": "Peretti",
"genre": "christianfiction"
}],
"musicians": [{
"firstName": "Eric",
"lastName": "Clapton",
"instrument": "guitar"
}, {
"firstName": "Sergei",
"lastName": "Rachmaninoff",
"instrument": "piano"
}]
}')
那一般我们遇到这样来存储JSON格式的话,只能把这条记录取出来交个应用程序,由应用程
来解析。如此一来,JSON又和特定的应用程序耦合在一起,其便利性的优势大打折扣。
现在到了MySQL5.7,可以支持对JSON进行属性的解析,我们重新修改下表结构:
ALTER TABLE json_test MODIFY person_desc json
先看看插入的这行JSON数据有哪些KEY:
mysql>SELECT id,json_keys(person_desc) as "keys" FROM json_test\G
*************************** 1. row***************************
id: 1
keys: ["authors", "musicians","programmers"]
1 row in set (0.00 sec)
我们可以看到,里面有三个KEY,分别为authors,musicians,programmers。那现在找一
KEY把对应的值拿出来:
mysql>SELECT json_extract(AUTHORS,'$.lastName[0]') AS 'name', AUTHORS FROM
->(
->SELECT id,json_extract(person_desc,'$.authors[0][0]') AS "authors" FROM json_test
->UNION ALL
->SELECT id,json_extract(person_desc,'$.authors[1][0]') AS "authors" FROM json_test
->UNION ALL
->SELECT id,json_extract(person_desc,'$.authors[2][0]') AS "authors" FROM json_test
->) AS T1
->ORDER BY NAME DESC\G
*************************** 1. row***************************
name:"Williams"
AUTHORS: {"genre": "fantasy","lastName": "Williams", "firstName":"Tad"}
*************************** 2. row***************************
name:"Peretti"
AUTHORS: {"genre":"christianfiction", "lastName": "Peretti","firstName":
"Frank"}*************************** 3. row***************************
name:"Asimov"
AUTHORS: {"genre": "sciencefiction","lastName": "Asimov", "firstName":"Isaac"}
3 rows in set (0.00 sec)
现在来把详细的值罗列出来:
mysql>SELECT
->json_extract(AUTHORS,'$.firstName[0]') AS "firstname",
->json_extract(AUTHORS,'$.lastName[0]')AS "lastname",
->json_extract(AUTHORS,'$.genre[0]') AS"genre"
->FROM
->(
->SELECT id,json_extract(person_desc,'$.authors[0]')AS "authors" FROM json
_test
->) AS T\G
*************************** 1. row***************************
firstname: "Isaac"
lastname:"Asimov"
genre:"sciencefiction"
1 row in set (0.00 sec)
我们进一步来演示把authors 这个KEY对应的所有对象删掉。
mysql>UPDATE json_test
->SET person_desc =json_remove(person_desc,'$.authors')\G
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查找下对应的KEY,发现已经被删除掉了。
mysql>SELECT json_contains_path(person_desc,'all','$.authors')as authors_exists FROM
json_test\G
*************************** 1. row***************************
authors_exists: 0
1 row in set (0.00 sec)
总结下,虽然MySQL5.7开始支持JSON数据类型,但是我建议如果要使用的话,最好是把这的值取出来,然后在应用程序段来计算。毕竟数据库是用来处理结构化数据的,大量的未预先定义schema的json解析,会拖累数据库的性能。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)