Database Design Tips - database

I must have an efficient way to connect two records from a table.
The table holds records of Laboratory tests which have a start date and a duration in days. I got a request from my client that they want to have a connection between the tests (e.g. Test2 starts after Test1 ends) and if the first test is postponed therefore also the second test will be postponed. Also they would like to have tests inserted where they can specify that the test2 starts with test1.
How do I implement it in Dataverse tables?
I tried using modelling a table "Connection" where Test1, Test2, and Connection Type are the primary Keys and Startdate is a column. But I have no idea how to deal with the option that there is no connection.

You have a Test table
Test
----
Test ID
Start date
Duration in days
Where Test ID is the auto-incrementing integer blind primary key.
You also have a Connection table
Connection
----------
Connection ID
Starting Test ID
Connecting Test ID
Connection Type (start-start, end-start)
Where Connection ID is the auto-incrementing integer blind primary key. You also have unique indexes on (Starting Test ID, Connecting Test ID) and (Connecting Test ID, Starting Test ID).
You can access the Test table directly, or you can access the Test table through the Connection table.

Related

SSIS designer Visual Studio foreign keys integration

I need to integrate two similar databases into third DB3. DB3 is almost the same as DB1.
First database DB1:
Addresses table with: primary key AddressId
People table with: primary key PersonId , foreign key AddressId
Second database DB2:
It is pretty similar, but in other language
Data from DB1 to DB3 flows smoothly, table after table. For example I have 1000 records in DB3 table named Addresses from DB1 and 1000 records in table named People from DB1.
Let's suppose Person with number 30 in DB3 (after transfering from DB1) has the IdAddress number 20.
Address with number 40 in DB2 has the ID number 1040 in DB3 and the Person has ID number 30 in DB2 and 1030 in DB3.
While transferring table People from B2 to B3 we need to know the address ID is not 40 but 1040.
I'm trying to use lookup to find existing record, but I'm newbie in SSIS VS designer. Could you help me? How can I resolve this problem?
Suggestions
You can do this using Lookup Transformation component as you mentioned, but first you have to:
Select the basic information of each table that can distinguish each logical entity. Example if talking about Persons you can choose the Fullname+ Mothername + Date Of Birth , ...
After selecting this attributes you have to add a Lookup Transformation
Map thiese columns between Source and Lookup table
Select the ID column (from Lookup table) as Output and rename the column to be NewID
Select Ignore failure option to handle non matches situation
After doing these steps, if the same person was inserted previously you will get the ID in NewID column, else NewID is NULL
Additional Information
Microsoft Docs - Lookup Transformation
UNDERSTAND SSIS LOOKUP TRANSFORMATION WITH AN EXAMPLE STEP BY STEP
SSIS Lookup Transformation
Lookup Transformation in SSIS

Transfer data from one database to another database with different schema

i have problem to Transfer data from one sqlserver 2008 r2 to another sql server 2012 databases with different schema, here is some different scenario,
database 1
database 1 with tables Firm and Client, these both have FirmId and ClientId primary key as int datatype,
FirmId is int datatype as reference key used in Client table.
database 2
database 2 with same tables Firm and Client, these both have FirmId and ClientId but primary key as uniqueidentifier,
FirmId is uniqueidentifier datatype as reference key used in Client table.
problem
the problem is not to copy data from 1 database table to 2 database table, but the problem is to maintain the reference key's Firm table into Client table. because there is datatype change.
i am using sql server 2008 r2 and sql server 2012
please help me to resolve / find the solution, i really appreciate your valuable time and effort. thanks
I'll take a stab at it even if I am far from an expert on SQLServer - here is a general procedure (you will have to repeat it for all tables where you have to replace INT with UID, of course...).
I will use Table A to refer to the parent (Firm, if I understand your example clearly) and Table B to refer to the child (Client, I believe).
Delete the relations pointing to Table A
Remove the identity from the id column of Table A
Create a new column with Uniqueidentifier on Table A
Generate values for the Uniqueidentifier column
Add the new Uniqueidentifier column in all the child tables (Table B)
Use the OLD id column to map your child record & update the new Uniqueidentifier value from your parent table.
Drop all the id columns
Recreate the relations
Having said that, I just want to add a warning to you: converting to UID is, according to some, a very bad idea. But if you really need to do that, you can script (and test) the above mentioned procedure.

use tablockx for insert non duplicable data outside a primary key

In SQL Server I have a table with the following data: first name, last name, birthplace, etc, etc.
The table has an identity ID column (the primary key of the table). In the interface of my application, the user can modify this data until they hit a button named "Close Record", when this happens I have to generate a Closed_Record_ID with syntax year + consecutive_number. For all the records in my table, the order they entered the database (given by the identity ID), may not be the same in wich they were closed, so I have to generate a new consecutive number.
How should I use the Tablockx hint, or what should I do to avoid duplicate consecutive numbers in the Closed_Record_ID column?

Trying to reset Identity value for SQL Azure DB Table column

I am using SQL Azure. I have a deployment DB and a test DB
I would like to add some new lookup records to the Test DB, to test new code.
Initially My Deployment DB's Identity was set to 200000+ for the PKs, and my Test DB to 100000+, to prevent PK collisions when syncing using such tools as Redgates' SQL data compare.
Unfortunately I made a mistake, and copied the Deployment DB as the new Test DB, since we required a more up to date dataset. As a result my Test DB now starts at 200000+. So I now have the risk of conflicts when syncing with the Deployment DB.
I would normallly just use:
DBCC CHECKIDENT('TableName', RESEED, 105000)
However SQL Azure does not support this.
I have come across a workaround:
set identity_insert TableName on -- this basically turns off IDENTITY
INSERT INTO TableName(id, name) VALUES (104999,'Test Reset Identity Start 104999') -- so we can jam any value for column ID
set identity_insert TableName off -- then turn it back on
INSERT INTO TableName(name) VALUES ('Test Reset Identity End') -- ID starts at 105000, in theory, from this point
SELECT * FROM TableName
However the new Identify value always seems to take the highest PK in the table as the last seed, rather than the PK value from the last inserted record.
I would rather not rebuild the table.
Is there another approach to resetting the identity value for my situation, ie to a lesser number ie 104999 rather the current 204999?
Thanks in advance.
EDIT 1
It may be I can only check the identity value to a value greater than the current value ie to say 206000 ?
EDIT 2
Perhaps there is an argument that Identity value should never be reseed to less than the max PK value, even if there is 100,000 spare numbers, as one day you will get a collision.

Incorrect Duplicate insert problem with SQL Server CE 3.5

I am not able to insert data into my table anymore!
Here's my table design.
intId is the Primary Key, there's no explicit unique constraint defined on it, has identity increment set to 1 and identity seed to 1.
I am inserting data into this table thru LINQ.
testDB.tbl_Vehicle.InsertOnSubmit(newVehicle);
testDB.SubmitChanges();
All this used to work till now, and all of a sudden it stopped working!
It now says
A duplicate value cannot be inserted into a unique index. [ Table name = tbl_Vehicle,Constraint name = PK_tbl_Vehicle ]
More info: This desktop application has 1 executable and 1 .sdf file. It was developed on Win 7 and recently was moved to Win XP system. But that shouldn't be a problem as there are other tables I am inserting into with similar logic and table design.
Do one thin make use of SQL profiler and check the query fire on insert statement.
More on check the database Table again and if possbile set the seed for the primary i.e identity column.

Resources