简体   繁体   English

如何将自定义ID列与表的主键列同步

[英]How to synchronize custom ID column with table's primary key column

Primarily, sorry for being too descriptive. 首先,很抱歉描述性太强。 I am storing patient's info in sql db by creating a custom ID field "PatientID" and I have a primary key field "ID". 我通过创建自定义ID字段“ PatientID”将患者的信息存储在sql db中,并且我有一个主键字段“ ID”。 PatientID has a pattern "PID-1" or "PID-2" so on and so forth. PatientID具有模式“ PID-1”或“ PID-2”,依此类推。 I want to synchronize both IDs. 我要同步两个ID。 Like if table's ID for "John" is 4 then its patientID should also be "PID-4". 就像表的“ John”的ID为4一样,则其PatientID也应为“ PID-4”。 For this, I have done some coding like if no record exist then start saving patientID from "PID-1" and then for all next record first find the max id of patient from ID field and increment it by 1 and concatenate it with "PID" + (tableID+1). 为此,我做了一些编码,例如如果不存在任何记录,然后开始从“ PID-1”保存PatientID,然后对于所有下一条记录,首先从ID字段中找到患者的最大id,并将其增加1,然后将其与“ PID连接“ +(tableID + 1)。

 ID    PatientID
 1      PID-1
 2      PID-2
 3      PID-3
 4      PID-4

Now, for an instance, while adding more record an exception is thrown although record is not saved but ID gets incremented. 现在,对于一个实例,虽然添加了更多记录,但记录未保存但ID却增加了,但会引发异常。 And here comes the problem. 问题来了。 Suppose, some bug comes and record for ID 5 could not be saved in the db, after fixing that bug when program runs correctly it put the ID 6 rather than 5. And for the patientID it puts "PID-5" due to MAX query. 假设出现了一些错误,并且ID 5的记录无法保存到数据库中,当该错误在程序正确运行时修复后,将ID 6而不是5放置了。对于PatientID,由于MAX查询而将其放置“ PID-5” 。 From here both IDs start being distinct. 从这里开始,两个ID开始是不同的。 Same problem for deleting, if I am deleting last record from above table ie, 4 and PID-4, the next record's ID would be 5 while PatientID would be "PID-4". 对于删除同样的问题,如果我要从上表中删除最后一条记录,即4和PID-4,则下一条记录的ID将为5,而PatientID将为“ PID-4”。 This was the whole problematic picture of handling both IDs from my side. 这是从我这边处理两个ID的整个问题。 Any alternate solution or any modification in my idea or any better idea then mine would be highly appreciated. 我们将对任何其他解决方案或我的想法进行任何修改,或者对我的想法进行任何修改。

In SQL Server you could create a computed column for this purpose: 在SQL Server中,您可以为此创建一个计算列:

ALTER TABLE Patient DROP COLUMN PatientID;
GO
ALTER TABLE Patient ADD COLUMN PatientID AS ('PID-'+CAST(ID AS VARCHAR(15));

For more info regarding computed columns, please have a look here . 有关计算列的更多信息,请在此处查看

Note : I assumed that your table's name is Patient . 注意 :我假设您表的名称为Patient You have to change this correspondingly if this is not the name of your table. 如果这不是表名,则必须相应地进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取表的PRIMARY KEY列名 - How to get PRIMARY KEY column name of a table 将一个自定义主键列添加到“身份用户”表 - Add a custom primary key column to Identity User table 无法首先使用实体​​框架代码将其他指定的列设置为主键,而不是将ID列设置为表 - unable to set other specified column as primary key instead of Id column to table using entity framework code first 为NHibernate中的主键列生成顺序ID - Generate sequential Id for primary key column in NHibernate C#如何创建现有表列的主键 - C# How create primary key to existing table column 在没有主键的表中的列上设置IsPrimaryKey = true - Setting IsPrimaryKey=true on column in table with no primary key 使用两列主键和另一列来规范化表 - Normalize a table with a two-column primary key and one other column 实体框架6 GUID作为主键:无法将值NULL插入表'FileStore'的列'Id'中; 列不允许为空 - Entity Framework 6 GUID as primary key: Cannot insert the value NULL into column 'Id', table 'FileStore'; column does not allow nulls 如何更快地在SQLite中列出主键列? - How to list the primary key column in SQLite faster? 如何设置等于主键的列? - How to set a column equal to the Primary key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM