Hi all,
I need help in auto populating the primary key values in foreign key table while inserting data in foreign key table. For Example: I have created table:
create table Patient
(
PatientId int IDENTITY(1,1) primary key,
FirstName varchar(50),
SurName varchar(50),
Gender char(20),
)
Say 5 rows are there in this Patient
Table:
Say First Row value is: 1, Priya, Kumari, Female
I have created the Guardians
Table:
create table Guardians
(
GuardiansId int identity(1,1) primary key,
PatientId int foreign key references Patient(PatientId),
FirstName varchar(50),
SurName varchar(50),
Gender char(20),
RelationToPatient varchar(50),
)
In this table Insert operations are like this:
insert into Guardians(FirstName, SurName, Gender,RelationToPatient)values('Sumit','Kumar','Male','Wife')
While selecting the Guardians Table PatientId showing NULL
values: My query is while inserting the values in Guardians
Table PatientId
should be auto Populated which will come from Patient
Table...
- My second problem is: How to create the Identity column as
varchar
. For example: suppose I want to increment myGuardians
Table with 'GRD0001', 'GRD0002', 'GRD0003'
like this...
Thanks, S.D