标准化 PostgreSQL 数据库
normalize PostgreSQL database
I have a PostgresQL database that I populate programmatically in my Java application code. There is a table "Message" - it stores incoming messages from the mail service. And also "Phone" - which stores phone numbers. The UUID for each entity is automatically generated. The problem is that if the message can contain the same number. And in the database, due to auto-generation, phone_id is always different (see photo). How to avoid this?
一种做法是在 phone_id
字段上使用约束以避免具有相同 phone_number
.
的重复实体
如果您使用 database-independent 库来管理 Liquibase 等数据库,您可以通过
实现
<changeSet author="liquibase-docs" id="dropUniqueConstraint-example">
<dropUniqueConstraint catalogName="cat"
constraintName="const_name"
schemaName="your schma name"
tableName="Message"
uniqueColumns="phone_number"/>
或直接在数据库中
ALTER TABLE Message ADD CONSTRAINT constraint_name UNIQUE (phone_number);
满足2NF is to create alternate table which foreign key pointing to phome_number in Message
table with One to Many
association或Many to Many
的另一种方法,如果它们具有双向关系。
虽然这个答案与规范化无关,但我希望它能有所帮助。如果你想 link 一个 UUID 号码到 phone 号码,你也可以生成 UUID v3 或 UUID v5 而不是 UUIDv4。 UUIDv4 是随机的,而 UUID v3/v5 对于相同的输入总是相同的。
生成 UUIDv3:
package com.example;
import java.util.UUID;
public class Example {
public static void main(String[] args) {
String string1 = "89207143040";
String string2 = "8 920 714 30 40";
String string3 = "+8 920 714-30-40";
// Remove all non numeric chars from the phone number and generate a UUIDs v3.
UUID uuid1 = UUID.nameUUIDFromBytes(string1.replaceAll("[^\d.]", "").getBytes());
UUID uuid2 = UUID.nameUUIDFromBytes(string2.replaceAll("[^\d.]", "").getBytes());
UUID uuid3 = UUID.nameUUIDFromBytes(string3.replaceAll("[^\d.]", "").getBytes());
System.out.println(uuid1 + " -> " + string1);
System.out.println(uuid2 + " -> " + string2);
System.out.println(uuid3 + " -> " + string3);
}
}
输出:
84345d67-45a8-365e-8da7-5d5c90c1ce0c -> 89207143040
84345d67-45a8-365e-8da7-5d5c90c1ce0c -> 8 920 714 30 40
84345d67-45a8-365e-8da7-5d5c90c1ce0c -> +8 920 714-30-40
一种做法是在 phone_id
字段上使用约束以避免具有相同 phone_number
.
如果您使用 database-independent 库来管理 Liquibase 等数据库,您可以通过
实现<changeSet author="liquibase-docs" id="dropUniqueConstraint-example">
<dropUniqueConstraint catalogName="cat"
constraintName="const_name"
schemaName="your schma name"
tableName="Message"
uniqueColumns="phone_number"/>
或直接在数据库中
ALTER TABLE Message ADD CONSTRAINT constraint_name UNIQUE (phone_number);
满足2NF is to create alternate table which foreign key pointing to phome_number in Message
table with One to Many
association或Many to Many
的另一种方法,如果它们具有双向关系。
虽然这个答案与规范化无关,但我希望它能有所帮助。如果你想 link 一个 UUID 号码到 phone 号码,你也可以生成 UUID v3 或 UUID v5 而不是 UUIDv4。 UUIDv4 是随机的,而 UUID v3/v5 对于相同的输入总是相同的。
生成 UUIDv3:
package com.example;
import java.util.UUID;
public class Example {
public static void main(String[] args) {
String string1 = "89207143040";
String string2 = "8 920 714 30 40";
String string3 = "+8 920 714-30-40";
// Remove all non numeric chars from the phone number and generate a UUIDs v3.
UUID uuid1 = UUID.nameUUIDFromBytes(string1.replaceAll("[^\d.]", "").getBytes());
UUID uuid2 = UUID.nameUUIDFromBytes(string2.replaceAll("[^\d.]", "").getBytes());
UUID uuid3 = UUID.nameUUIDFromBytes(string3.replaceAll("[^\d.]", "").getBytes());
System.out.println(uuid1 + " -> " + string1);
System.out.println(uuid2 + " -> " + string2);
System.out.println(uuid3 + " -> " + string3);
}
}
输出:
84345d67-45a8-365e-8da7-5d5c90c1ce0c -> 89207143040
84345d67-45a8-365e-8da7-5d5c90c1ce0c -> 8 920 714 30 40
84345d67-45a8-365e-8da7-5d5c90c1ce0c -> +8 920 714-30-40