Data type: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
(16 intermediate revisions by the same user not shown)
Line 1: Line 1:
資料表欄位設計時,針對不同資料類型,建議的資料型態(data type)
資料表欄位設計時,針對不同資料類型,建議的資料型態 (data type)


== examples of data and suggested datatype ==
== examples of data and suggested datatype ==
=== birth year ===
=== birth year ===
* data type: int
* data type: int
* example: Range from 1905 ~ 2013 (108 yeas old) from the Sing-up form from outlook.com {{access| date=2013-02-04}}
* example: Range from 1905 ~ 2013 (108 yeas old) from the Sign-up form from outlook.com {{access| date=2013-02-04}}
* range/limit: 122 yeas old<ref>[http://zh.wikipedia.org/wiki/%E6%9C%80%E5%B9%B4%E9%95%B7%E8%80%85 最年長者 - 維基百科,自由的百科全書]: 「根據金氏世界紀錄大全紀錄的最長壽者是活了122年的雅娜·卡爾曼特」、[https://zh.wikipedia.org/zh-tw/%E5%90%84%E5%9B%BD%E4%BA%BA%E5%8F%A3%E9%A2%84%E6%9C%9F%E5%AF%BF%E5%91%BD%E5%88%97%E8%A1%A8 各國人口預期壽命列表</ref>
* range/limit: 122 yeas old<ref>[http://zh.wikipedia.org/wiki/%E6%9C%80%E5%B9%B4%E9%95%B7%E8%80%85 最年長者 - 維基百科,自由的百科全書]: 「根據金氏世界紀錄大全紀錄的最長壽者是活了122年的雅娜·卡爾曼特」、[https://zh.wikipedia.org/zh-tw/%E5%90%84%E5%9B%BD%E4%BA%BA%E5%8F%A3%E9%A2%84%E6%9C%9F%E5%AF%BF%E5%91%BD%E5%88%97%E8%A1%A8 各國人口預期壽命列表</ref>


Line 34: Line 34:
=== Chinese name ===
=== Chinese name ===
* 最長姓名有13個字 <ref> 中國新聞網 (2010). [http://dailynews.sina.com/bg/tw/twpolitics/chinanews/20101104/00481967448.html 台灣“獨一姓氏”者達149人 最長姓名有13個字_台灣頻道_新浪網-北美]</ref><ref>[http://news.ltn.com.tw/news/life/paper/780935 姓名8個字 外交部不給護照 - 生活 - 自由時報電子報]</ref>
* 最長姓名有13個字 <ref> 中國新聞網 (2010). [http://dailynews.sina.com/bg/tw/twpolitics/chinanews/20101104/00481967448.html 台灣“獨一姓氏”者達149人 最長姓名有13個字_台灣頻道_新浪網-北美]</ref><ref>[http://news.ltn.com.tw/news/life/paper/780935 姓名8個字 外交部不給護照 - 生活 - 自由時報電子報]</ref>
* 參考別人: LINE姓名欄位長度限制: 15字(15個中文字或15英文字母)
* 參考他人作法: LINE 姓名欄位長度限制: 15字(15個中文字或15英文字母)


=== unix timestamp ===
=== unix timestamp ===
Line 47: Line 47:


=== 學校代碼 ===
=== 學校代碼 ===
School ID defined by MOE at Taiwan / 學校代碼<ref>[http://www.edu.tw/statistics/content.aspx?site_content_sn=25656 各級學校名錄--教育部統計處 Department of Statistics]{{access | date=2012-02-16}}</ref>
School ID defined by MOE at Taiwan / 學校代碼<ref>[https://depart.moe.edu.tw/ED4500/News.aspx?n=63F5AB3D02A8BBAC&sms=1FF9979D10DBF9F3 各級學校名錄--教育部統計處 Department of Statistics]{{access | date=2019-10-13}}</ref>
* integer: 4(university) ~ 6
* integer: 4(university) ~ 6
* ex: 0001(國立政治大學)、373607(臺北市立華江國小)。前面可能有零。
* ex: 0001(國立政治大學)、373607(臺北市立華江國小)。前面可能有零。
* range/limit:  
* range/limit:


=== 地址 ===
=== 地址 ===
Line 59: Line 59:
* DECIMAL(18,12)<ref>[http://stackoverflow.com/questions/159255/what-is-the-ideal-data-type-for-latitude-longitude mysql - What is the ideal data type for latitude / longitude? - Stack Overflow]</ref> or FLOAT( 10, 6 )<ref>[http://code.google.com/apis/maps/articles/phpsqlsearch.html Creating a Store Locator with PHP, MySQL & Google Maps - Google Maps API Family - Google Code]</ref> or VARCHAR( 30 )
* DECIMAL(18,12)<ref>[http://stackoverflow.com/questions/159255/what-is-the-ideal-data-type-for-latitude-longitude mysql - What is the ideal data type for latitude / longitude? - Stack Overflow]</ref> or FLOAT( 10, 6 )<ref>[http://code.google.com/apis/maps/articles/phpsqlsearch.html Creating a Store Locator with PHP, MySQL & Google Maps - Google Maps API Family - Google Code]</ref> or VARCHAR( 30 )
* ex: 37.401724,-122.114646
* ex: 37.401724,-122.114646
* range/limit:
* range/limit: 「緯度座標的整數須介於 -90 和 90 之間。經度座標的整數須介於 -180 和 180 之間。<ref>[https://support.google.com/maps/answer/18539?co=GENIE.Platform%3DDesktop&hl=zh-Hant 找出或輸入經緯度 - 電腦 - Google 地圖說明]</ref>」


=== 價錢/金額 ===
=== 價錢/金額 ===
Line 77: Line 77:
* varchar at least eight (8) characters<ref>[https://www.owasp.org/index.php/Password_length_%26_complexity Password length & complexity - OWASP] "Minimum length. Passwords should be at least eight (8) characters long." </ref>
* varchar at least eight (8) characters<ref>[https://www.owasp.org/index.php/Password_length_%26_complexity Password length & complexity - OWASP] "Minimum length. Passwords should be at least eight (8) characters long." </ref>


=== 雜湊碼 (hash value) ===
=== 雜湊碼 (hash value) e.g. MD5, SHA ===
* [https://zh.wikipedia.org/wiki/MD5 MD5]: CHAR(32)<ref>[https://stackoverflow.com/questions/14922208/can-i-use-varchar32-for-md5-values php - Can I use VARCHAR(32) for md5() values? - Stack Overflow]</ref>
[https://zh.wikipedia.org/wiki/MD5 MD5]:
* [https://zh.wikipedia.org/wiki/SHA%E5%AE%B6%E6%97%8F SHA] 256<ref>[https://stackoverflow.com/questions/2240973/how-long-is-the-sha256-hash mysql - How long is the SHA256 hash? - Stack Overflow]</ref><ref>[http://fishjerky.blogspot.com/2013/06/md5sha512.html 魚乾的筆記本: MD5被破解了,要改用SHA]</ref>:  
* CHAR(32) or VARCHAR(32)<ref>[https://stackoverflow.com/questions/14922208/can-i-use-varchar32-for-md5-values php - Can I use VARCHAR(32) for md5() values? - Stack Overflow]</ref>
** (1) HEX: CHAR(64) Using [http://php.net/manual/en/function.hash-file.php PHP: hash_file()], MySQL [https://dev.mysql.com/doc/refman/5.6/en/encryption-functions.html#function_sha2 SHA2(str, hash_length)]
 
** (2) Binary: BINARY(32) In PHP using [http://php.net/manual/en/function.hex2bin.php hex2bin()] e.g. {{kbd | key=<nowiki>echo hex2bin(hash_file('sha256', $file_path));</nowiki>}}, In MySQL using [https://www.w3resource.com/mysql/string-functions/mysql-unhex-function.php UNHEX() function] e.g. {{kbd | key=<nowiki>SELECT UNHEX(SHA2('The quick brown fox jumped over the lazy dog.', 256))</nowiki>}}
[https://zh.wikipedia.org/wiki/SHA%E5%AE%B6%E6%97%8F SHA]<ref>[https://stackoverflow.com/questions/2240973/how-long-is-the-sha256-hash mysql - How long is the SHA256 hash? - Stack Overflow]</ref><ref>[http://fishjerky.blogspot.com/2013/06/md5sha512.html 魚乾的筆記本: MD5被破解了,要改用SHA]</ref>:  
* (1) HEX: CHAR(64) Using [http://php.net/manual/en/function.hash-file.php PHP: hash_file()] or [https://www.php.net/manual/en/function.hash.php PHP: hash()], MySQL [https://dev.mysql.com/doc/refman/5.6/en/encryption-functions.html#function_sha2 SHA2(str, hash_length)]
* (2) Binary: BINARY(32) In PHP using [http://php.net/manual/en/function.hex2bin.php hex2bin()] e.g. {{kbd | key=<nowiki>echo hex2bin(hash_file('sha256', $file_path));</nowiki>}}, In MySQL using [https://www.w3resource.com/mysql/string-functions/mysql-unhex-function.php UNHEX() function] e.g. {{kbd | key=<nowiki>SELECT UNHEX(SHA2('The quick brown fox jumped over the lazy dog.', 256))</nowiki>}}
* (3) CHAR(60) if using the MySQL [https://www.w3resource.com/mysql/encryption-and-compression-functions/sha1().php SHA1() function]


==== Retrieve the hash value from string or file content ====
==== Retrieve the hash value from string or file content ====
Line 108: Line 111:
* PHP: [http://php.net/manual/en/function.bin2hex.php bin2hex()] e.g. {{kbd | key=<nowiki>echo bin2hex(hex2bin(hash('sha256', 'The quick brown fox jumped over the lazy dog.')));</nowiki>}}
* PHP: [http://php.net/manual/en/function.bin2hex.php bin2hex()] e.g. {{kbd | key=<nowiki>echo bin2hex(hex2bin(hash('sha256', 'The quick brown fox jumped over the lazy dog.')));</nowiki>}}
* MySQL: [https://dev.mysql.com/doc/refman/8.0/en/hexadecimal-literals.html Hexadecimal Literals] e.g. {{kbd | key=<nowiki>SELECT HEX(UNHEX(SHA2('The quick brown fox jumped over the lazy dog.', 256)));</nowiki>}}<ref>[https://stackoverflow.com/questions/14608413/storing-a-binary-sha1-hash-into-a-mysql-binary20-column insert - Storing a binary SHA1 hash into a mySQL BINARY(20) column - Stack Overflow]</ref>
* MySQL: [https://dev.mysql.com/doc/refman/8.0/en/hexadecimal-literals.html Hexadecimal Literals] e.g. {{kbd | key=<nowiki>SELECT HEX(UNHEX(SHA2('The quick brown fox jumped over the lazy dog.', 256)));</nowiki>}}<ref>[https://stackoverflow.com/questions/14608413/storing-a-binary-sha1-hash-into-a-mysql-binary20-column insert - Storing a binary SHA1 hash into a mySQL BINARY(20) column - Stack Overflow]</ref>
== GUID 全域唯一識別碼字串 ==
[https://zh.wikipedia.org/zh-tw/%E5%85%A8%E5%B1%80%E5%94%AF%E4%B8%80%E6%A0%87%E8%AF%86%E7%AC%A6 全域唯一識別碼 - 維基百科,自由的百科全書]
* BINARY(16)<ref>[https://stackoverflow.com/questions/412341/how-should-i-store-guid-in-mysql-tables How should I store GUID in MySQL tables? - Stack Overflow]</ref>
* VARCHAR(36) or CHAR(36)
=== Store the GUID value ===
<pre>
SELECT UUID(); 
-- 4470beb9-ab1e-11ec-bd92-00155de8c33a
INSERT INTO `sometable`
(`guid_char`, `guid_binary`)
VALUES (
'4470beb9-ab1e-11ec-bd92-00155de8c33a',
UNHEX(REPLACE("4470beb9-ab1e-11ec-bd92-00155de8c33a", "-",""))
);
</pre>
{{exclaim}} Need to replace the - symbol with whitespace from GUID value. Or it will not able to retrieve the GUID value
==== Retrieve the GUID value ====
<pre>
SELECT `guid_char`, HEX(`guid_binary`)
FROM `sometable`;
</pre>


=== membership ===
=== membership ===
Line 113: Line 143:


=== 台灣公司統一編號 ===
=== 台灣公司統一編號 ===
* 八位數字 因為可能以 0 開頭,所以建議使用 {{kbd | key= VARCHAR(8)}},而不建議使用 {{kbd | key= INT(8)}} <ref>[https://www.etax.nat.gov.tw/etwmain/web/ETW113W1_1 公示資料查詢服務-財政部稅務入口網]</ref><ref>[http://herolin.webhop.me/entry/is-valid-TW-company-ID/ » 營利事業統一編號驗證完全手冊(Javascript,Java,C#,PHP) - Hero Think~用手摀住我的嘴]</ref>
* 八位數字 因為可能以 0 開頭,所以建議使用 {{kbd | key= VARCHAR(8)}},而不建議使用 {{kbd | key= INT(8)}} <ref>[https://www.etax.nat.gov.tw/etwmain/web/ETW113W1_1 公示資料查詢服務-財政部稅務入口網]</ref><ref>[http://herolin.webhop.me/entry/is-valid-TW-company-ID/ » 營利事業統一編號驗證完全手冊(Javascript,Java,C#,PHP) - Hero Think~用手摀住我的嘴]</ref> {{exclaim}} 早期公司統一編號不是八位數字
 
=== 台灣身分證號/統一證號 ===
* 最長 10 位文字,所以建議使用 {{kbd | key= VARCHAR(10)}}:(1)身分證號:1碼英文字母加上9碼數字組成、(2) 統一證號:2碼英文字母加上8碼數字,一共10個字元組成  <ref>[https://www.moi.gov.tw/News_Content.aspx?n=2&s=211965&sms=9009 新式外來人口統一證號(宣導手冊)]</ref>。


=== 商品序號或商品條碼 ===
=== 商品序號或商品條碼 ===
Line 183: Line 216:
* [http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm Oracle Data Types]
* [http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm Oracle Data Types]
* [http://www.databaseanswers.org/data_models/index.htm Industry Data Models]
* [http://www.databaseanswers.org/data_models/index.htm Industry Data Models]
 
* [https://dba.stackexchange.com/questions/tagged/database-design Newest 'database-design' Questions - Database Administrators Stack Exchange]


{{Template:Build a website}}
{{Template:Build a website}}

Revision as of 10:18, 29 March 2022

資料表欄位設計時,針對不同資料類型,建議的資料型態 (data type)

examples of data and suggested datatype

birth year

  • data type: int
  • example: Range from 1905 ~ 2013 (108 yeas old) from the Sign-up form from outlook.com [Last visited: 2013-02-04]
  • range/limit: 122 yeas old[1]

file name

file name for Win Os windows.png FTFS ; Linux Os linux.png ex3 and ex4 file system types[2].

  • varchar(255) [3][4] Icon_exclaim.gif If the files hosted by Windows server, you should also take the limit of PATH length into account besides the limit of filename length.

IP

IP(v4)

  • varchar(15)
  • ex: 255.255.255.255 [5]

range/limit

  • IP range - Classless inter-domain routing (CIDR)[6]
    • 12 or 24 bytes (IPv4 and IPv6 networks)[7]
    • ex: 69.208.0.0/32

IPv6: 128位元長度,以16位元為一組,每組以冒號":"隔開,可以分為8組,每組以4位元十六進制方式表示

    • data type:
    • ex: 2001:0db8:85a3:08d3:1319:8a2e:0370:7344

網址

Chinese name

  • 最長姓名有13個字 [10][11]
  • 參考他人作法: LINE 姓名欄位長度限制: 15字(15個中文字或15英文字母)

unix timestamp

  • bigint(10) ;
  • ex: 1328664539
  • range/limit:

duration 時長

  • data type: TIMETIME values may range from '-838:59:59' to '838:59:59'」[12]
  • format: 小時:分鐘:秒 hh:mm:ss
  • 參考別人: Google form 問卷題目選項的小時 0~72 ; 分鐘: 00~59: 秒: 00~59

學校代碼

School ID defined by MOE at Taiwan / 學校代碼[13]

  • integer: 4(university) ~ 6
  • ex: 0001(國立政治大學)、373607(臺北市立華江國小)。前面可能有零。
  • range/limit:

地址

  • 資料類型: TEXT or VARCHAR(255)[14]
  • 參考別人: 觀察 Google payment 付款地址的文字輸入框並沒有限制文字長度

經緯度 (經度,緯度)

  • DECIMAL(18,12)[15] or FLOAT( 10, 6 )[16] or VARCHAR( 30 )
  • ex: 37.401724,-122.114646
  • range/limit: 「緯度座標的整數須介於 -90 和 90 之間。經度座標的整數須介於 -180 和 180 之間。[17]

價錢/金額

  • DECIMAL(10,2)[18][19]
  • ex:
  • range/limit: 0.00 ~ 99999999.99

台灣縣市欄位值

台灣縣市欄位值 下拉式選單[20]

$city = array("基隆市", "台北市", "新北市", "桃園縣", "新竹市", "新竹縣", "苗栗縣", "台中市", 
        "彰化縣", "南投縣", "雲林縣", "嘉義市", "嘉義縣", "台南市", "高雄市", "屏東縣", "台東縣", "花蓮縣", 
        "宜蘭縣", "澎湖縣", "金門縣", "連江縣");

密碼

  • varchar at least eight (8) characters[21]

雜湊碼 (hash value) e.g. MD5, SHA

MD5:

  • CHAR(32) or VARCHAR(32)[22]

SHA[23][24]:

Retrieve the hash value from string or file content

PHP:

<?php
/* Create a file to calculate hash of */
file_put_contents('example.txt', 'The quick brown fox jumped over the lazy dog.');

echo hash_file('sha256', 'example.txt');
// expected result: 68b1282b91de2c054c36629cb8dd447f12f096d3e3c587978dc2248444633483
// 64 characters

// calculate the hash of string
echo hash('sha256', 'The quick brown fox jumped over the lazy dog.');
?>

MySQL:

SELECT SHA2('The quick brown fox jumped over the lazy dog.', 256)

Retrieve the hash value from binary value

  • PHP: bin2hex() e.g. echo bin2hex(hex2bin(hash('sha256', 'The quick brown fox jumped over the lazy dog.')));
  • MySQL: Hexadecimal Literals e.g. SELECT HEX(UNHEX(SHA2('The quick brown fox jumped over the lazy dog.', 256)));[25]

GUID 全域唯一識別碼字串

全域唯一識別碼 - 維基百科,自由的百科全書

  • BINARY(16)[26]
  • VARCHAR(36) or CHAR(36)

Store the GUID value

SELECT UUID();  
-- 4470beb9-ab1e-11ec-bd92-00155de8c33a

INSERT INTO `sometable`
(`guid_char`, `guid_binary`)
VALUES (
'4470beb9-ab1e-11ec-bd92-00155de8c33a',
UNHEX(REPLACE("4470beb9-ab1e-11ec-bd92-00155de8c33a", "-",""))
);

Icon_exclaim.gif Need to replace the - symbol with whitespace from GUID value. Or it will not able to retrieve the GUID value


Retrieve the GUID value

SELECT `guid_char`, HEX(`guid_binary`) 
FROM `sometable`;

membership

台灣公司統一編號

  • 八位數字 因為可能以 0 開頭,所以建議使用 VARCHAR(8),而不建議使用 INT(8) [27][28] Icon_exclaim.gif 早期公司統一編號不是八位數字

台灣身分證號/統一證號

  • 最長 10 位文字,所以建議使用 VARCHAR(10):(1)身分證號:1碼英文字母加上9碼數字組成、(2) 統一證號:2碼英文字母加上8碼數字,一共10個字元組成 [29]

商品序號或商品條碼

MySQL: VARCHAR(14)

書籍

國際電話號碼

使用 Google libphonenumber 套件驗證國際電話號碼格式 – Frochu – Medium

很長的文字

  • MySQL: LONGTEXT: 4GB / TEXT: 65,535 bytes ~64kb [30][31]
  • MsSQL: NVARCHAR(max): 2GB [32]
 (left blank intentionally)
* data
** data type
** ex:
** range/limit:

comparision of datatypes in different database server

numeric

bigint

text

Storing Unicode text[35]

  • MySQL: varchar / MS SQL 2008: nvarchar
  • MySQL: LONGTEXT (4GB)[36] / MS SQL 2008: nvarchar(max) (2GB)[37]

date and time

DATETIME: ex: "2013-06-13 03:33:33"

  • MS SQL 2008 & MySQL are equivalent

TIMESTAMP: MS SQL 2008 and MySQL are NOT equivalent[38] Icon_exclaim.gif

  • MS SQL 2008[39] ex: 0x00000000000007D3
  • MySQL ex: 2013-06-13 03:33:33

further reading

tools

ER圖(entity-relationship diagram)

產生資料庫結構表格文件

參考其他資料表的結構設計[40]

  • DESCRIBE table_name;

references

  1. 最年長者 - 維基百科,自由的百科全書: 「根據金氏世界紀錄大全紀錄的最長壽者是活了122年的雅娜·卡爾曼特」、[https://zh.wikipedia.org/zh-tw/%E5%90%84%E5%9B%BD%E4%BA%BA%E5%8F%A3%E9%A2%84%E6%9C%9F%E5%AF%BF%E5%91%BD%E5%88%97%E8%A1%A8 各國人口預期壽命列表
  2. Type df -aT to list the file system types in Linux
  3. Comparison of file systems - Wikipedia, the free encyclopedia
  4. Naming Files, Paths, and Namespaces (Windows)
  5. Datatype for storing ip address in SQL Server - Stack Overflow
  6. Help:Range blocks - MediaWiki
  7. PostgreSQL: Documentation: Manuals: Network Address Types
  8. sql - Best database field type for a URL - Stack Overflow
  9. 中國新聞網 (2010). 台灣“獨一姓氏”者達149人 最長姓名有13個字_台灣頻道_新浪網-北美
  10. 姓名8個字 外交部不給護照 - 生活 - 自由時報電子報
  11. MySQL :: MySQL 5.0 Reference Manual :: 11.3.2 The TIME Type
  12. 各級學校名錄--教育部統計處 Department of Statistics[Last visited: 2019-10-13]
  13. address
  14. mysql - What is the ideal data type for latitude / longitude? - Stack Overflow
  15. Creating a Store Locator with PHP, MySQL & Google Maps - Google Maps API Family - Google Code
  16. 找出或輸入經緯度 - 電腦 - Google 地圖說明
  17. sql - Best Data Type for Currency - Stack Overflow[Last visited: 2012-03-07]
  18. mysql 用什么数据类型表示价格?_百度知道[Last visited: 2015-05-25]
  19. 取自中華郵政全球資訊網
  20. Password length & complexity - OWASP "Minimum length. Passwords should be at least eight (8) characters long."
  21. php - Can I use VARCHAR(32) for md5() values? - Stack Overflow
  22. mysql - How long is the SHA256 hash? - Stack Overflow
  23. 魚乾的筆記本: MD5被破解了,要改用SHA
  24. insert - Storing a binary SHA1 hash into a mySQL BINARY(20) column - Stack Overflow
  25. How should I store GUID in MySQL tables? - Stack Overflow
  26. 公示資料查詢服務-財政部稅務入口網
  27. » 營利事業統一編號驗證完全手冊(Javascript,Java,C#,PHP) - Hero Think~用手摀住我的嘴
  28. 新式外來人口統一證號(宣導手冊)
  29. MySQL :: MySQL 5.7 Reference Manual :: 11.1.3 String Type Overview
  30. MySQL TEXT 格式 的 長度限制 - Tsung's Blog
  31. nchar and nvarchar (Transact-SQL)
  32. int、bigint、smallint 和 tinyint (Transact-SQL)
  33. MySQL :: MySQL 5.5 Reference Manual :: 11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT
  34. Whats SQL Server NVARCHAR(max) equivalent in MySQL? - Database Administrators Stack Exchange
  35. nchar 和 nvarchar (Transact-SQL)
  36. SQL Server 2008 timestamp data type - Stack Overflow
  37. timestamp (Transact-SQL)
  38. MySQL :: MySQL 5.0 Reference Manual :: 3.4 Getting Information About Databases and Tables

further reading


Web site design and development process