Showing posts with label reference. Show all posts
Showing posts with label reference. Show all posts

Friday, March 30, 2012

How to insert records into two tables that references each other?

I have two tables , A1 and B1 . B1 has a reference key pointing to A1.ID which is a primary key. Now I want to INSERT a record into both table.

CREATE TABLE a1 (
T_ID INTEGER IDENTITY(1,1) PRIMARY KEY,
T_Test INTEGER NOT NULL,
);

CREATE TABLE B1 (
B_ID INTEGER IDENTITY(1,1) PRIMARY KEY,
T_SID INTEGER NOT NULL REFERENCES a1(T_ID),
);

-- A function to get the next primary key value

-- Get the next unique key from A1
CREATE FUNCTION [dbo].[getTicketNo]
(
)
RETURNS INT
AS
BEGIN
RETURN (SELECT Max(T_ID) from A1)
END

I can only insert a record into a table at a time. and I rely on dbo.getTicketNo() to get the lastest T_ID

I wonder how do you insert a new record into A1 and B1 and maintain referential integrity? You must call Insert twice to accomplish the job right?

What I want to insert a record into B1.SID is the primary key value (T_ID) that was created during "Insert into A1 values (0)", using dbo.getTicketNo()
I worry if I am going to insert a record into A1 first, then insert a record into B1 next, there is a chance the data wont be consistent; as I illustrated a situation below:

t A1 B1
0 (0)Insert
1 (1)Insert
2 (0)Insert
3 (1)Insert

dbo.getTicketNo() at t3 may return the wrong value if another process (1)insert is executed, that will change the primary key number to 2 which is incorrect

Can I lock up A1 and then unlock A1 after I finished working with B1?

If so, what would happen to the application if it tries to access into A1 during it is locked?

If you ask me this not a good approach to get the identity value.
You can use the following objects to get the last inserted identity value.

@.@.Identity or Scope_Identity()

1.Insert the data on Master table (A1)
2.Then get the last inserted value from those system functions or global varibale
3.Use the values on Child table (B1)

Example,

Insert into A1(T_Test) values (@.T_Test);
Insert into B1(T_SID) values (@.@.Identity)
--or
Insert into B1(T_SID) values (Scope_Identity())

NOTE: Don't use MAX function to get the last inserted identity value. It may be
Inserted by other concurrent users.

|||


Thank you ManiD

From your code, is there any chance that another INSERT query is executed by other concurrent users after the 1st Insert and before the 2nd Insert in your case?

time 0:00:00 (User 0) Insert into A1(T_Test) values (@.T_Test);
time 0:00:20 (User 1) Insert into A1(T_Test) values (@.T_Test);
time 0:00:21 (User 0) Insert into B1(T_SID) values (@.@.Identity)

If it is possible, what is the best approach to ensure that the second Insert into B1 is consistent to the master A1?

Thanks for your precious time

|||

In this case,

@.@.Identity hold the User1 Identity data..

So it always good idea to use SCOPE_IDENTITY() instead of using @.@.Identity..

SCOPE_IDENTITY always hold the current user's identity data...

|||

Ok,,, thank you ManiD

Let me do some research on SCOPE_Indentity() ...

Thank you for your kind replies

|||

When I google it, found some people mispelled SCOPE_IDENTITY() to SCOPE_INDENTITY() .....

Here is the MSDN for SCOPE_IDENTITY() if anyone got the similar issues can have a look

http://msdn2.microsoft.com/en-us/library/ms190315.aspx

Friday, February 24, 2012

How to implement a data base design for multi language support?

Hi,
I have searched the web for several hours now to find a reference solution
based on the available features of Sql Server 2005 to implement a data base
with multi language support.
I would like to create a database that is able to store multiple
translations (text in different languages) for the same original value.
--> tblProducts, Field ProductDescription --> One field, manny languages
If I query this table/field with a current UI-Culture from
ASP.net/WindowsForms it should be 'intelligent' and return the value based on
the UI-Culture. It should work like the culture stuff in the GUI...
I am lookig for a solution, where I can store all the language relevant
informations in the same field. Do I have to use an XML-field or a user
defined field? Is there something like a "best practise" for this kind of
database design available?
I would like to avoid a 1 to many relation for all culture relevant fields
in my database...
Thanks for any hint,
DirkAt previous company i worked at they had multi language support and
they used what was like a codes table. w/ a language type ID
associated w/ all the words on the various pages. depending on the
language type code it would pull in the words to the page. so they
just stored 10 differenet sets of language codes in the db. they
brought it in fresh everytime w/ a select statement.|||Hi Thomas,
thanks for your reply. I'm sure this approch works but I would like to make
it easier. With your solution I have to join the tables every time I need a
lagnuage relevant value.
I was thinking about using the Sql Server 2005 FieldType 'xml' and store a
xml strcture in every relevant field like
<values>
<value culture='en-us'>
car
</value>
<value culture='de-de'>
Auto
</value>
</values>
I just want to hear some solution from others just to make it no so
complicated...
Thanx,
Dirk

How to implement a data base design for multi language support?

Hi,
I have searched the web for several hours now to find a reference solution
based on the available features of Sql Server 2005 to implement a data base
with multi language support.
I would like to create a database that is able to store multiple
translations (text in different languages) for the same original value.
--> tblProducts, Field ProductDescription --> One field, manny languages
If I query this table/field with a current UI-Culture from
ASP.net/WindowsForms it should be 'intelligent' and return the value based o
n
the UI-Culture. It should work like the culture stuff in the GUI...
I am lookig for a solution, where I can store all the language relevant
informations in the same field. Do I have to use an XML-field or a user
defined field? Is there something like a "best practise" for this kind of
database design available?
I would like to avoid a 1 to many relation for all culture relevant fields
in my database...
Thanks for any hint,
DirkAt previous company i worked at they had multi language support and
they used what was like a codes table. w/ a language type ID
associated w/ all the words on the various pages. depending on the
language type code it would pull in the words to the page. so they
just stored 10 differenet sets of language codes in the db. they
brought it in fresh everytime w/ a select statement.

How to implement a data base design for multi language support?

Hi,
I have searched the web for several hours now to find a reference solution
based on the available features of Sql Server 2005 to implement a data base
with multi language support.
I would like to create a database that is able to store multiple
translations (text in different languages) for the same original value.
--> tblProducts, Field ProductDescription --> One field, manny languages
If I query this table/field with a current UI-Culture from
ASP.net/WindowsForms it should be 'intelligent' and return the value based on
the UI-Culture. It should work like the culture stuff in the GUI...
I am lookig for a solution, where I can store all the language relevant
informations in the same field. Do I have to use an XML-field or a user
defined field? Is there something like a "best practise" for this kind of
database design available?
I would like to avoid a 1 to many relation for all culture relevant fields
in my database...
Thanks for any hint,
Dirk
At previous company i worked at they had multi language support and
they used what was like a codes table. w/ a language type ID
associated w/ all the words on the various pages. depending on the
language type code it would pull in the words to the page. so they
just stored 10 differenet sets of language codes in the db. they
brought it in fresh everytime w/ a select statement.