m
爲總長度,n
爲小數精確位數(0<= n <=6)JSONmysql
JSON數據類型的優點:
- 自動驗證存儲在JSON列中的JSON文檔 。無效的文檔會產生錯誤。
- 優化的存儲格式。存儲在JSON列中的JSON文檔將 轉換爲內部格式,以容許快速讀取文檔元素。當服務器稍後必須讀取以此二進制格式存儲的JSON值時,不須要從文本表示中解析該值。二進制格式的結構使服務器可以直接經過鍵或數組索引查找子對象或嵌套值,而無需在文檔中讀取它們以前或以後的全部值。
exp
)column-name
, ......) REFERENCES foreign-table
(column-name
, ......) ON [DELETE | UPDATE] [SET NULL | RESTRICT | CASCADE | NO ACTION | SET DEFAULT]exp
)REFERENCES foreign-table
(column-name
, ......) ON [DELETE | UPDATE] [SET NULL | RESTRICT | CASCADE | NO ACTION | SET DEFAULT]sql
注意: 整數或浮點列能夠具備附加屬性
AUTO_INCREMENT
。一般該值爲value+1
,這裏value
是目前在表中的列的最大值。若是插入NULL時自動爲該列設至此值。數組
CREATE TABLE T_DEPT ( DEPTNO INTEGER(2) PRIMARY KEY, DNAME VARCHAR(15), LOC VARCHAR(15) ) DEFAULT CHARACTER SET utf8; INSERT INTO T_DEPT VALUES(10, 'ACCOUNTING', 'NEW YORK'); INSERT INTO T_DEPT VALUES(20, 'RESEARCH', 'DALLAS'); INSERT INTO T_DEPT VALUES(30, 'SALES', 'CHICAGO'); INSERT INTO T_DEPT VALUES(40, 'OPERATIONS', 'BOSTON'); CREATE TABLE T_EMP ( EMPNO INTEGER(4) PRIMARY KEY, ENAME VARCHAR(15), JOB VARCHAR(9), MGR INTEGER(4), HIREDATE DATE, SAL DOUBLE(7,2), COMM DOUBLE(7,2), DEPTNO INTEGER(2) REFERENCES T_DEPT(DEPTNO) ON UPDATE CASCADE ) DEFAULT CHARACTER SET utf8; INSERT INTO T_EMP VALUES(7369, 'SMITH', 'CLERK', 7902, CURRENT_DATE, 800, NULL, 20); INSERT INTO T_EMP VALUES(7499, 'ALLEN', 'SALESMAN', 7698, CURRENT_DATE, 1600, 300, 30); INSERT INTO T_EMP VALUES(7521, 'WARD', 'SALESMAN', 7698, CURRENT_DATE, 1250, 500, 30); INSERT INTO T_EMP VALUES(7566, 'JONES', 'MANAGER', 7839, CURRENT_DATE, 2975, NULL, 20); INSERT INTO T_EMP VALUES(7654, 'MARTIN', 'SALESMAN', 7698, CURRENT_DATE, 1250, 1400, 30); INSERT INTO T_EMP VALUES(7698, 'BLAKE', 'MANAGER', 7839, CURRENT_DATE, 2850, NULL, 30); INSERT INTO T_EMP VALUES(7782, 'CLARK', 'MANAGER', 7839, CURRENT_DATE, 2450, NULL, 10); INSERT INTO T_EMP VALUES(7788, 'SCOTT', 'ANALYST', 7566, CURRENT_DATE, 3000, NULL, 20); INSERT INTO T_EMP VALUES(7839, 'KING', 'PRESIDENT',NULL, CURRENT_DATE, 5000, NULL, 10); INSERT INTO T_EMP VALUES(7844, 'TURNER', 'SALESMAN', 7698, CURRENT_DATE, 1500, 0, 30); INSERT INTO T_EMP VALUES(7876, 'ADAMS', 'CLERK', 7788, CURRENT_DATE, 1100, NULL, 20); INSERT INTO T_EMP VALUES(7900, 'JAMES', 'CLERK', 7698, CURRENT_DATE, 950, NULL, 30); INSERT INTO T_EMP VALUES(7902, 'FORD', 'ANALYST', 7566, CURRENT_DATE, 3000, NULL, 20); INSERT INTO T_EMP VALUES(7934, 'MILLER', 'CLERK', 7782, CURRENT_DATE, 1300, NULL, 10); CREATE TABLE T_SALGRADE ( GRADE INTEGER PRIMARY KEY AUTO_INCREMENT, LOSAL INTEGER, HISAL INTEGER ) DEFAULT CHARACTER SET utf8; INSERT INTO T_SALGRADE VALUES(1, 700, 1200); INSERT INTO T_SALGRADE VALUES(2, 1201, 1400); INSERT INTO T_SALGRADE VALUES(3, 1401, 2000); INSERT INTO T_SALGRADE VALUES(4, 2001, 3000); INSERT INTO T_SALGRADE VALUES(5, 3001, 9999); CREATE TABLE T_JSON ( JOSN_TEXT JSON DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO T_JSON VALUES \ ( '{"type": \ [{"lavel": 1, "price": 500.12}, \ {"lavel": 2, "price": 800.78}], \ "width": 50, "height": 100}'\ ); --搜索JSON值 mysql> SELECT JSON_EXTRACT(JOSN_TEXT, '$.width') from T_JSON; +------------------------------------+ | JSON_EXTRACT(JOSN_TEXT, '$.width') | +------------------------------------+ | 50 | +------------------------------------+ mysql> SELECT JSON_EXTRACT(JOSN_TEXT, '$.type[1].lavel') from T_JSON; +--------------------------------------------+ | JSON_EXTRACT(JOSN_TEXT, '$.type[1].lavel') | +--------------------------------------------+ | 2 | +--------------------------------------------+