code

정규화 된 전화 번호를 데이터베이스에 저장하는 표준이 있습니까?

codestyles 2020. 9. 5. 09:57
반응형

정규화 된 전화 번호를 데이터베이스에 저장하는 표준이 있습니까?


데이터베이스 필드에 전화 번호를 저장하기위한 좋은 데이터 구조는 무엇입니까? 국제 전화 번호를 처리 할 수있을만큼 유연하고 번호의 다양한 부분을 효율적으로 쿼리 할 수있는 것을 찾고 있습니다.

편집 : 여기서 사용 사례를 명확히하기 위해 현재 단일 varchar 필드에 숫자를 저장하고 고객이 입력 한 그대로 둡니다. 그런 다음 코드에서 번호가 필요하면 정규화합니다. 문제는 일치하는 전화 번호를 찾기 위해 수백만 개의 행을 쿼리하려는 경우 다음과 같은 기능이 포함된다는 것입니다.

where dbo.f_normalizenum(num1) = dbo.f_normalizenum(num2)

매우 비효율적입니다. 또한 지역 번호와 같은 것을 찾는 쿼리는 단일 varchar 필드 일 때 매우 까다로워집니다.

[편집하다]

사람들은 여기에서 많은 좋은 제안을했습니다. 감사합니다! 업데이트로 지금 내가하고있는 작업은 다음과 같습니다. 입력 한 그대로 숫자를 varchar 필드에 저장하지만 쿼리시 항목을 정규화하는 대신 레코드가 삽입 될 때 모든 작업을 수행하는 트리거가 있습니다. 또는 업데이트되었습니다. 따라서 쿼리해야하는 모든 부분에 대한 int 또는 bigint가 있으며 이러한 필드는 쿼리를 더 빠르게 실행하기 위해 인덱싱됩니다.


첫째, 국가 코드 외에는 실제 표준이 없습니다. 최선의 방법은 특정 전화 번호가 속한 국가를 국가 코드로 인식하고 해당 국가의 형식에 따라 나머지 번호를 처리하는 것입니다.

그러나 일반적으로 전화 장비 등은 표준화되어 있으므로 거의 항상 주어진 전화 번호를 다음 구성 요소로 나눌 수 있습니다.

  • C 국가 코드 1-10 자리 (현재는 4 자리 이하이지만 변경 될 수 있음)
  • 지역 번호 (지방 / 주 / 지역) 코드 0-10 자리 (실제로 하나의 지역 번호가 아닌 지역 필드와 지역 필드가 별도로 필요할 수 있음)
  • E 교환 (접두사 또는 스위치) 코드 0-10 자리
  • L 라인 번호 1-10 자리

이 방법을 사용하면 국가, 지역 및 교환 코드가 같기 때문에 서로 가까운 사람을 찾을 수 있도록 잠재적으로 번호를 구분할 수 있습니다. 더 이상 의지 할 수없는 휴대 전화로.

또한 각 국가마다 다른 표준이 있습니다. 미국에서는 항상 (AAA) EEE-LLLL에 의존 할 수 있지만, 다른 국가에서는 도시 (AAA) EE-LLL에서 교환이 가능하고 농촌 지역 (AAA) LLLL에서는 단순히 줄 번호가있을 수 있습니다. 어떤 형식의 트리에서 맨 위에서 시작하여 정보가있는대로 형식을 지정해야합니다. 예를 들어, 국가 코드 0에는 나머지 번호에 대해 알려진 형식이 있지만 국가 코드 5432의 경우 나머지 번호를 이해하기 전에 지역 번호를 검사해야 할 수 있습니다.

또한 미국 번호 인 경우 하나의 숫자가 너무 많고 (광고 또는 기타 목적을 위해 전체 표현이 필요할 수 있음) 미국에서는 문자가 해당 문자에 매핑된다는 사실을 인식해야하는 vanity등의 숫자 를 처리 (800) Lucky-Guy할 수 있습니다. 독일과는 다릅니다.

전체 번호를 텍스트 필드 (국제화 포함)로 별도로 저장하여 나중에 돌아가서 상황이 변경 될 때 번호를 다시 구문 분석하거나 누군가가 특정 국가의 형식을 구문 분석하는 잘못된 방법을 제출하는 경우 백업으로 사용할 수도 있습니다. 정보를 잃습니다.


KISS-많은 미국 웹 사이트가 지겨워지고 있습니다. 그들은 우편 번호와 전화 번호를 확인하기 위해 영리하게 작성된 코드를 가지고 있습니다. 완벽하게 유효한 노르웨이 연락처 정보를 입력 할 때 자주 거부되는 것을 발견합니다.

좀 더 고급에 대한 특별한 필요가 없다면 문자열로 남겨 두십시오.


E.164Wikipedia 페이지는 알아야 할 모든 것을 알려줍니다.


제안 된 구조는 다음과 같습니다. 피드백에 감사드립니다.

전화 데이터베이스 필드는 다음 형식의 varchar (42) 여야합니다.

CountryCode-번호 x 내선

예를 들어 미국에서는 다음과 같이 할 수 있습니다.

1-2125551234x1234

지역 번호 / 번호 (212) 555 1234 및 내선 번호 1234가있는 미국 번호 (국가 번호 1)를 나타냅니다.

국가 코드를 대시로 구분하면 데이터를 읽는 사람에게 국가 코드가 명확 해집니다. 이것은 아니다 엄격하게 국가 코드 "가 있기 때문에 필요한 접두사 코드 "(당신이 그들을 왼쪽에서 오른쪽으로 읽을 수 있습니다 당신은 항상 명확하게 국가를 결정할 수있을 것이다). 그러나 국가 코드는 길이가 다양하기 때문에 (현재 1 ~ 4 자 사이) 어떤 종류의 구분 기호를 사용하지 않는 한 국가 코드를 한 눈에 쉽게 알 수 없습니다.

나는 "x"를 사용하여 확장자를 분리합니다. 그렇지 않으면 어떤 것이 번호이고 어떤 것이 확장자인지 알아내는 것이 (많은 경우) 실제로 불가능하기 때문입니다.

이러한 방식으로 국가 코드 및 내선 번호를 포함한 전체 번호를 단일 데이터베이스 필드에 저장할 수 있습니다. 그러면 지금까지 힘들게 해왔 던 사용자 정의 함수에 조인하는 대신 쿼리 속도를 높이는 데 사용할 수 있습니다. .

varchar (42)를 선택한 이유는 무엇입니까? 음, 우선 국제 전화 번호는 길이가 다양하므로 "var"가됩니다. 나는 대시와 "x"를 저장하고있다. 그래서 "char"를 설명한다. 어쨌든 당신은 전화 번호에 대해 정수 연산을하지 않을 것이다. (나는 추측한다) 그래서 숫자 형을 사용하는 것은별로 의미가 없다. . 42의 길이는 Adam Davis의 대답에 따라 더해진 모든 필드의 가능한 최대 길이를 사용하고 대시와 'x'에 2를 더했습니다.


E.164를 찾으십시오. 기본적으로 국가 접두사 및 선택적 pbx 접미사로 시작하는 코드로 전화 번호를 저장합니다. 디스플레이는 현지화 문제입니다. 유효성 검사도 수행 할 수 있지만 국가 접두사를 기반으로하는 현지화 문제이기도합니다.

For example, +12125551212+202 would be formatted in the en_US locale as (212) 555-1212 x202. It would have a different format in en_GB or de_DE.

There is quite a bit of info out there about ITU-T E.164, but it's pretty cryptic.


I personally like the idea of storing a normalized varchar phone number (e.g. 9991234567) then, of course, formatting that phone number inline as you display it.

This way all the data in your database is "clean" and free of formatting


Perhaps storing the phone number sections in different columns, allowing for blank or null entries?


Ok, so based on the info on this page, here is a start on an international phone number validator:

function validatePhone(phoneNumber) {
    var valid = true;
    var stripped = phoneNumber.replace(/[\(\)\.\-\ \+\x]/g, '');    

    if(phoneNumber == ""){
        valid = false;
    }else if (isNaN(parseInt(stripped))) {
        valid = false;
    }else if (stripped.length > 40) {
        valid = false;
    }
    return valid;
}

Loosely based on a script from this page: http://www.webcheatsheet.com/javascript/form_validation.php


The standard for formatting numbers is e.164, You should always store numbers in this format. You should never allow the extension number in the same field with the phone number, those should be stored separately. As for numeric vs alphanumeric, It depends on what you're going to be doing with that data.


Storage

Store phones in RFC 3966 (like +1-202-555-0252, +1-202-555-7166;ext=22). The main difference from E.164 are

  • No limit on the length
  • Support of extensions

To optimise performance of view operations, store the phone in the National/International format next to the RFC 3966 field.

Don't store the country code in a separate field unless you have a serious reason for that. Why? Because you shouldn't ask for the country code on the UI.

Mostly, people enter the phones as they hear them. E.g. if the local format will start from 0 or 8, it'd be annoying for the user to make the number transformation in the head (like, "OK, don't type '0', choose the country and type the rest of what the person said in this field").

Parsing

Google has your back and you can validate and parse any phone number with using their libphonenumber library. There are ports to almost any language.

So let the user just enter "0449053501" or "04 4905 3501" or "(04) 4905 3501". The tool will figure out the rest for you.

See the official demo, to get a feeling of how much does it help.


I think free text (maybe varchar(25)) is the most widely used standard. This will allow for any format, either domestic or international.

I guess the main driving factor may be how exactly you're querying these numbers and what you're doing with them.


I find most web forms correctly allow for the country code, area code, then the remaining 7 digits but almost always forget to allow entry of an extension. This almost always ends up making me utter angry words, since at work we don't have a receptionist, and my ext.# is needed to reach me.


I find most web forms correctly allow for the country code, area code, then the remaining 7 digits but almost always forget to allow entry of an extension. This almost always ends up making me utter angry words, since at work we don't have a receptionist, and my ext.# is needed to reach me.

I would have to check, but I think our DB schema is similar. We hold a country code (it might default to the US, not sure), area code, 7 digits, and extension.


What about storing a freetext column that shows a user-friendly version of the telephone number, then a normalised version that removes spaces, brackets and expands '+'. For example:

User friendly: +44 (0)181 4642542

Normalized: 00441814642542


I would go for a freetext field and a field that contains a purely numeric version of the phone number. I would leave the representation of the phone number to the user and use the normalized field specifically for phone number comparisons in TAPI-based applications or when trying to find double entries in a phone directory. Of course it does not hurt providing the user with an entry scheme that adds intelligence like separate fields for country code (if necessary), area code, base number and extension.


Where are you getting the phone numbers from? If you're getting them from part of the phone network, you'll get a string of digits and a number type and plan, eg

441234567890 type/plan 0x11 (which means international E.164)

In most cases the best thing to do is to store all of these as they are, and normalise for display, though storing normalised numbers can be useful if you want to use them as a unique key or similar.


User friendly: +44 (0)181 464 2542 normalised: 00441814642542

The (0) is not valid in the international format. See the ITU-T E.123 standard.

The "normalised" format would not be useful to US readers as they use 011 for international access.


I've used 3 different ways to store phone numbers depending on the usage requirements.

  1. If the number is being stored just for human retrieval and won't be used for searching its stored in a string type field exactly as the user entered it.
  2. If the field is going to be searched on then any extra characters, such as +, spaces and brackets etc are removed and the remaining number stored in a string type field.
  3. Finally, if the phone number is going to be used by a computer/phone application, then in this case it would need to be entered and stored as a valid phone number usable by the system, this option of course, being the hardest to code for.

참고URL : https://stackoverflow.com/questions/41925/is-there-a-standard-for-storing-normalized-phone-numbers-in-a-database

반응형