我正在寻找一个“最佳实践”,如果你在数据库中存储国家代码,但找不到“这是正确的方式”。我想存储2个字符的国家代码和国家电话代码(例如德国将是"DE“和"+49")。
实际上,我的计划如下:使用country_codes创建一个表countries和一个表。如下所示:
TABLE: countries
id INT(11)
code CHAR(2)
TABLE: country_codes
id INT(11)
country_id INT(11) FORGEIGN KEY (countries -> id)
phone_code VARHAR(6)我想我需要把它们分开,因为有些国家有多个电话号码。这样,一个国家可以有多个电话代码。
但我的问题是:这是做到这一点的“最佳实践”吗?如果我想在“所有”国家推出我的应用程序,或者如果我想要将应用程序翻译成多种语言(在这种情况下,我想使用countries表也用于不同的语言),从这一点来看,“这将起作用”也更多地从那个角度来看。
如果这很重要:我计划在这个应用程序中使用laravel。
发布于 2018-05-06 23:08:53
国家代码被标准化为two letters by ISO 3166-1-alpha-2,因此以这种方式存储它们将会起作用。在表格中包括国家名称通常很有帮助,这样用户就可以选择正确的国家,而不必知道所有的代码。
电话号码还远没有标准化。国际电信联盟提供recommendation E.164来表示实际的电话号码(在电话行话中称为“电话簿号码”)。国家/地区代码定义为一到三位数字。北美(包括美国、加拿大和许多加勒比海国家)都是North American Numbering Plan的一部分,并共享国家代码1。
目录号码前通常以+开头,并以点作为标点。因此,例如,发布的纽约市查号服务号码是(或当他们还拥有这样的服务时) +1.212.555.1212。如果您从欧洲某个地方拨打该号码,您将看到+,并替换为您当地的国际前缀。在NANP中,多个国家具有相同的国家代码。
但是,英国是很奇怪的。从国外打来的电话是+44.exchange.number。但是从国内打长途电话是(0) exchange.number。
我的观点是:如果您试图在软件中使用国家代码来编写目录号码,就很难做到这一点。您最好让用户提供带有国际前缀的电话号码。
您绝对不应该将E.164国家/地区代码与ISO 3166两个字母的国家/地区代码绑定在一起,将它们作为不同的列放在表的同一行中。您需要两个单独的表来作为未来的证明。标准化组织是不同的,它们做自己的事情,所以您的数据模型应该反映这一点。
发布于 2018-05-06 23:01:02
我的数据库如下所示:
> id int(11) Auto Increment (Just an ID (primary key))
> iso char(2) (2-letters ISO code)
> name varchar(80) (normalized name (all uppercase))
> nicename varchar(80) (Nicely formatted name)
> iso3 char(3) NULL (3-letters ISO code)
> numcode smallint(6) NULL (numeric ISO code)
> phonecode int(5) (phone code like '1' for USA, without '+')这应该是足够的。你得到了用户的电话号码,删除了开头的零,删除了任何非数字字符,从数据库中添加了一个国家代码,你就可以开始了!
示例:
1)用户输入(045) 111-22-33,德国
2)将其转换为451112233
3)从DB中添加德国的代码(49)。你会得到49451112233。如果愿意,可以添加'+‘。
4)现在您可以使用Twilio或任何其他服务拨打电话或发送短信。
如果你想“轻松”地将网站翻译成其他语言,请将所有文本存储在数据库中,并根据用户的语言偏好选择合适的版本。
发布于 2018-05-07 02:12:17
基于这些答案,我将执行以下操作:
DB表:
+------------------------------------------------------------+
| Table: countries |
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| iso_code2 | char(2) | NO | | NULL | |
| iso_code3 | char(3) | NO | | NULL | |
| num_code | int(3) | NO | | NULL | |
| name | varchar(48) | NO | | NULL | |
| nicename | varchar(48) | NO | | NULL | |
+--------------+--------------+------+-----+---------+-------+
// will store all countries available
+------------------------------------------------------------+
| Table: country_phonecodes |
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| country_id | int(11) | NO | | NULL | |
| phonce_code | int(6) | NO | | NULL | |
+--------------+--------------+------+-----+---------+-------+
// based on this page: https://countrycode.org/ there are
// countries with more than one code
// and also codes can be 6 chars long
+------------------------------------------------------------+
| Table: languages |
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| code | char(2) | NO | | NULL | |
| locale | char(5) | NO | | NULL | |
| name | varchar(50) | NO | | NULL | |
| native_name | varchar(50) | NO | | NULL | |
| flag | varchar(10) | NO | | NULL | |
+--------------+--------------+------+-----+---------+-------+
// table for available translations of the app
+------------------------------------------------------------+
| Table: country_languages |
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| country_id | int(11) | NO | | NULL | |
| language_id | int(11) | NO | | NULL | |
+--------------+--------------+------+-----+---------+-------+
// table for language suggestions for a given country和一些示例插入:
+---------------------------------------------------------------------------------------+
| Inserts: countries |
+-----+------------+------------+-----------+---------------------+---------------------+
| id | iso_code2 | iso_code3 | num_code | name | nicename |
+-----+------------+------------+-----------+---------------------+---------------------+
| 1 | de | deu | 276 | GERMANY | Germany |
| 2 | do | dom | 214 | DOMINICAN REPUBLIC | Dominican Republic |
| 3 | be | bel | 056 | BELGIUM | Belgium |
+-----+------------+------------+-----------+---------------------+---------------------+
+----------------------------------+
| Inserts: country_phonecodes |
+-----+-------------+--------------+
| id | country_id | phonce_code |
+-----+-------------+--------------+
| 1 | 1 | 49 |
| 2 | 2 | 1809 |
| 3 | 2 | 1829 |
| 4 | 2 | 1849 |
| 5 | 3 | 32 |
+-----+-------------+--------------+
+----------------------------------------------------------+
| Inserts: languages |
+-----+-------+---------+---------+--------------+---------+
| id | code | locale | name | native_name | flag |
+-----+-------+---------+---------+--------------+---------+
| 1 | de | de_DE | German | Deutsch | de.svg |
| 2 | do | es_DO | Spanish | Español | es.png |
| 3 | be | fr_BE | French | Français | fr.jpg |
| 4 | be | nl_BE | Dutch | Nederlands | nl.png |
| 5 | be | de_BE | German | Deutsch | de.svg |
+-----+-------+---------+---------+--------------+---------+
+----------------------------------+
| Inserts: country_languages |
+-----+-------------+--------------+
| id | country_id | language_id |
+-----+-------------+--------------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 3 | 4 |
| 5 | 3 | 5 |
+-----+-------------+--------------+我认为这应该适用于任何需要国家/地区列表和/或i18n的项目。
如果用户来自比利时,他可以从可用语言/翻译列表中进行选择。他将得到FR,NL和DE的建议,但仍然可以选择es_DO作为首选语言。
我认为这应该涵盖所有需求-但如果有人看到其中的问题或有想法/评论:如果我能改进这个解决方案,我会很高兴:)
https://stackoverflow.com/questions/50200954
复制相似问题