I'm very new to SQL Server and especially relational data so the question I have is very complex for me, but it's something I've been tasked with. I have a small database acting as an infrastructure inventory into which I'm importing data. The database has a table called 'vcenters' storing all vcenters and the primary key from this links to the foreign key in several other related tables (e.g. hosts, VMs, Clusters).
CREATE TABLE [dbo].[vcenters]( [vCenterID] [int] NOT NULL, [vCenterName] [varchar](20) NOT NULL, [CustomerID] [varchar](10) NOT NULL, CONSTRAINT [PK_vcenters] PRIMARY KEY CLUSTERED ( [vCenterID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[vcenters] WITH CHECK ADD CONSTRAINT [FK_vcenters_Customers] FOREIGN KEY([CustomerID]) REFERENCES [dbo].[Customers] ([CustomerID]) GO
So the vcenters table contains an numeric index field which is auto-incremented each time data is inserted (1,2,3 etc) and a name field. When I go to import the name into one of the other linked tables I instead need to look up the index from the vcenters table and insert the numeric value but I don't know how to do this?
CREATE TABLE [dbo].[Hosts]( [HostID] [int] NOT NULL, [HostName] [varchar](25) NOT NULL, [vCenterID] [int] NOT NULL, CONSTRAINT [PK_Hosts_1] PRIMARY KEY CLUSTERED ( [HostID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[Hosts] WITH CHECK ADD CONSTRAINT [FK_Hosts_vcenters] FOREIGN KEY([vCenterID]) REFERENCES [dbo].[vcenters] ([vCenterID]) GO
I understand inner joins but if anyone could help with this then I'd appreciate it. If I'm not making myself clear then please say where and I'll elaborate. Oh, and I'm using SQL Server Express 2014 (as a testbed).
Thanks in advance
Adam