Relationships Between 3 Tables in SQL Server - sql-server

Relationships between Tables
This is a True or Not???
and what is the Key of Table 3 ?

The point is you don't need any relation between Table1 and Table3!
Relations between tables are being used to point to a specific record of data using a primary key (usually an ID), which in this chart you can find that specific record of Table1 using the relation between Table2 and Table3.
I hope this answer helps :)

Related

View from a Join of two tables in SQL Server

Tables to Join
I attached an image explaining issues that I currently face at the moment. I was looking for a way to join two tables. Where first table has a relationship to the previous table that its foreign key in first table. The second table also have foreign key of the first table.
What I want is to output the information is a list of the records of the second table that has the foreign key of the first table as well as the foreign key of the previous table that only linked to first table only.
Thank you very much for your support and I am looking forward to hear from you all soon
If you are using SQL Server, try creating a view function. You can easily add tables and view the sql results after you connect all of them. It also has a built in function that will automatically creates the sql queries corresponding to your created view function.
May you give this a try?
select a.clm,b.clm,c.clm from tableA as a
inner join table b as b on a.clm = b.clm
inner join table c as c on c.clm = b.clm
where clm is the column that could be joined from table A as well as table B as well as table c
If ever, kindly give us a bit more info about

How do I do a cross-DB MERGE INTO into an identical table while maintaining an identity key?

I have two tables on two different databases, DB1.Category and DB2.Category.
I need to merge all values so that DB1.Category and DB2.Category are identical, but I need to maintain the PK ID, CategoryID.
The CategoryID is an identity column with an increment and seed of 1 in DB1, but no identity in DB2.
Is there a way to sync all data in these tables from DB1 to DB2 while maintaining the PK?
This is what I have so far:
MERGE DB1.dbo.Category AS TARGET
USING DB2.dbo.Category AS SOURCE
ON (TARGET.MarketplaceName = SOURCE.MarketplaceName
AND TARGET.MarketplaceCategoryCode = SOURCE.MarketplaceCategoryCode
AND TARGET.MarketplaceCategoryName = SOURCE.MarketplaceCategoryName)
WHEN NOT MATCHED BY TARGET
INSERT(--*FIELDS*-
)
I am a little confused by your question. The Title says "Identical table" but the body of your question you point out difference. Can you provide the structure of the 2 tables?

Refactoring database tables to have a common "base" table

In my database I have some tables that looks something like:
table1
---------------
id name
table2
---------
id name ParnetId <-This is an ID from Table 1
table3
---------------
id name ParnetId <-This is an ID from Table 2
In the past, It seemed that the three tables are completely separate entities but now (due to a new requested feature...) I need to reference from another table any of the three tables.
Something like:
table4
---------------
id name foreignKey <-This ID should be from Table 1 OR Table 2 OR Table 3
So, I figured I could add a table that will hold the Id's for tables 1-3 and reference it from the table 4 (+ from tables 1-3 for the Id column).
Please note that I am working on a live database that already contains data.
My questions are:
Is there anything I need to watch out while performing the refactoring process?
How would I migrate the existing records id's from tables 1-3 to table 4 (relating to identical id's in the three tables)?
Is there a better strategy you can think of?
A new table (eg. CommonTable) with its own primary key, with nullable fields as foreign keys to table1, table2 and table3 seems like the way to go.
That way table4 can have a single foreign key to CommonTable, which may link to either table1 or table2 or table3.
A second approach would be to create a view (eg. CommonView) over table1, table2 and table3. You would need to concoct a fake primary key for this - perhaps "t1-xxx", "t2-xxx", "t3-xxx" for table1/2/3. I've used this technique as a temporary data migration mechanism, but wouldn't consider it as a long term solution.
A third approach, probably the easiest to implement, is simply to have multiple nullable foreign keys on table4:
table1Id
table2Id
table3Id
I would strongly advise against having a single field which is a foreign key to more than one table. I've seen this in action and it's a nightmare to work with. Think of the future developers confusion.
I would suggest having 7 tables in total table4, table1, table2, table3, t4t1link, t4t2link and t4t3link.
t4tXlink table should contain the primary keys from table4 and tableX. Thus you can have proper constraints in your database.
If you have just 1 table to contain 3 separate foreignkeys you can not have constraint that disallows values from other 2 columns.

How to insert Master/Detail record in SQL Server?

I have three tables: Employee, Emp_Address and Emp_AddressDetail.
Employee table is master and Emp_Address is detail.
Emp_Address is master and Emp_AddressDetail is detail.
I want to copy all rows from a table to another table.
How can I do it?
If I understand your question correct you want to insert data from one table to another? If so, you should have a look at the INTO statement. http://msdn.microsoft.com/en-us/library/ms188029.aspx
SELECT * INTO dbo.OneTable FROM Production.AnotherTable
If the tables have foreign keys defined, and those foreign key definitions need to be set on the new database, than you need to take a look at the data diagrams, and identify any tables that do not have foreign key columns. In your case the copy order will be Employee, Emp_Address, Emp_AddressDetail
If you do not have explicit SQL Server maintained foreign keys, or if the foreign keys are not set on the target database, you can just copy the data in any order you like.
Note that it's entirely possible to paint yourself into a corner, e.g. if there was field in the Employee table called PrimaryAddress that would be a foreign key to the Emp_Address table.

SQL Server how to maintain GUID across tables in same DB

I want to create a DB , where each table's PK will be GUID and which will be unique across the DB,
Example: my DB name is 'LOCATION'. And I have 3 table as 'CITY' , 'STATE' and 'COUNTRY'.
I want that all the 3 tables have same PK field as GUID ,and that value will be unique across DB.
How to do this in SQL Server, any idea? I have never used SQL Server before, so it will be helpful if briefly explained.
create table CITY (
ID uniqueidentifier not null primary key default newid(),
.
.
.
)
Repeat for the other tables.
What do you mean exactly ?
Just create the table, add an Id field to each table, set the data type of the Id field to 'uniqueidentifier', and you're good to go.
Next, add a primary constraint on those columns, and make sure that, when inserting a new record you assign a new guid to that column (for instance, by using the newid() function).
I can't think of any good reason to have a unique number shared by 3 tables, why not just give each table a unique index with a foreign key reference? Indexed fields are queried quicker than random numbers would be.
I would create a 'Location' table with foreign keys CityId, StateId & CountryId to link them logically.
edit:
If you are adding a unique id across the City, State and Country tables then why not just have them as fields in the same table? I would have thought that your reason for splitting them into 3 tables was to reduce duplication in the database.

Resources