CREATE TABLE [dbo].[BuildingAddress]
(
[BuildingAddressID] [int] NOT NULL IDENTITY(1, 1),
[BuildingFID] [int] NOT NULL,
[AddressTypeFID] [int] NOT NULL,
[Address1] [dbo].[Address] NULL,
[Address2] [dbo].[Address] NULL,
[Address3] [dbo].[Address] NULL,
[City] [dbo].[AddressCity] NULL,
[State] [dbo].[AddressState] NULL,
[PostalCode] [dbo].[AddressPostalCode] NULL,
[CountryCodeStandardFID] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BuildingAddress] ADD CONSTRAINT [CK_BuildingAddress_MainAddressType] CHECK (([dbo].[GetAddressTypeName]([AddressTypeFID])='Main'))
GO
ALTER TABLE [dbo].[BuildingAddress] ADD CONSTRAINT [PK_BuildingAddress] PRIMARY KEY CLUSTERED ([BuildingAddressID]) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [ix_BuildingAddress_Building] ON [dbo].[BuildingAddress] ([BuildingFID]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[BuildingAddress] ADD CONSTRAINT [FK_BuildingAddress_AddressType] FOREIGN KEY ([AddressTypeFID]) REFERENCES [dbo].[AddressType] ([AddressTypeID])
GO
ALTER TABLE [dbo].[BuildingAddress] ADD CONSTRAINT [FK_BuildingAddress_Buildings] FOREIGN KEY ([BuildingFID]) REFERENCES [dbo].[Buildings] ([BuildingID]) ON DELETE CASCADE
GO
ALTER TABLE [dbo].[BuildingAddress] ADD CONSTRAINT [FK_BuildingAddress_CountryCodeStandard] FOREIGN KEY ([CountryCodeStandardFID]) REFERENCES [dbo].[CountryCodeStandard] ([CountryCodeStandardID])
GO
GRANT SELECT ON [dbo].[BuildingAddress] TO [MssExec]
GRANT INSERT ON [dbo].[BuildingAddress] TO [MssExec]
GRANT DELETE ON [dbo].[BuildingAddress] TO [MssExec]
GRANT UPDATE ON [dbo].[BuildingAddress] TO [MssExec]
GO