Insight needed.
Upon attempting to create a foreign key on the second table in the database, I receive the following error;
"There is already an object named 'FK_EquipMaster' in the database."
When creating multiple tables using CREATE TABLE and specifying foreign key constraints, should each successive CREATE TABLE block contain a different name for the foreign key constraint?
Thanks in advance.
The code is as follows;
Upon attempting to create a foreign key on the second table in the database, I receive the following error;
"There is already an object named 'FK_EquipMaster' in the database."
When creating multiple tables using CREATE TABLE and specifying foreign key constraints, should each successive CREATE TABLE block contain a different name for the foreign key constraint?
Thanks in advance.
The code is as follows;
Code:
USE master ;
GO
DROP DATABASE EquipmentLocation ;
GO
CREATE DATABASE EquipmentLocation
go
Use EquipmentLocation
Go
CREATE TABLE #EquipMaster (
[JurisdictionCode] [varchar] (60 ) NULL,
[AccountNo] [varchar] (60 ) NULL,
[SequenceNbr] [varchar] (60 ) NULL,
[SaleAmount] [varchar] (60 ) NULL,
[SaleDate] [varchar] (60 ) NULL,
[TaxAmount] [varchar] (60 ) NULL,
)
.
.
.
CREATE TABLE EquipMaster (
[JurisdictionCode] [varchar] (5) NOT NULL,
[AccountNo] [varchar] (45) NOT NULL,
[SequenceNbr] int NOT NULL,
[SaleAmount] int NULL,
[SaleDate] date NULL,
[TaxAmount] int NULL,
Constraint PK_EquipMaster Primary Key ([JurisdictionCode], [AccountNo], [SequenceNbr])
--CreatedBy varchar (255) default system_user
--CreatedDate date NULL Default GETDATE()
)
.
.
.
CREATE TABLE #EquipmentData1 (
[JurisdictionCode] [varchar] (60 ) NULL,
[AccountNo] [varchar] (60 ) NULL,
[SequenceNbr] [varchar] (60 ) NULL,
[Zipcode] [varchar] (60 ) NULL,
[SaleAmount] [varchar] (60 ) NULL,
[SaleDate] [varchar] (60 ) NULL,
)
CREATE TABLE [Equipment_2016](
[id] int identity (1,1) NOT NULL primary key,
[JurisdictionCode] [varchar] (5 ) NOT NULL,
[AccountNo] [varchar] (45 ) NOT NULL,
[SequenceNbr] int NOT NULL,
[Zipcode] [varchar] (9) NULL,
[SaleAmount] int NULL,
[SaleDate] date NULL, ---format is (YYYYMMDD)
constraint FK_EquipMaster FOREIGN KEY ([JurisdictionCode], [AccountNo], [SequenceNbr]) REFERENCES EquipMaster ([JurisdictionCode], [AccountNumber], [SequenceNbr]
--CreatedBy varchar (255) default system_user
--CreatedDate date NULL Default GETDATE()
)
.
.
.
BULK INSERT #EquipmentData1
FROM 'C:\EquipmentLocation\Data\201706EquipmentData.txt'
WITH
(
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n',
FIRSTROW = 2
)
.
.
.
CREATE TABLE [Equipment_2017](
[id] int identity (1,1) NOT NULL primary key,
[JurisdictionCode] [varchar] (5 ) NOT NULL,
[AccountNo] [varchar] (45 ) NOT NULL,
[SequenceNbr] int NOT NULL,
[Zipcode] [varchar] (9) NULL,
[SaleAmount] int NULL,
[SaleDate] date NULL, ---format is (YYYYMMDD)
constraint FK_EquipMaster FOREIGN KEY ([JurisdictionCode], [AccountNo], [SequenceNbr]) REFERENCES EquipMaster ([JurisdictionCode], [AccountNumber], [SequenceNbr]
--CreatedBy varchar (255) default system_user
--CreatedDate date NULL Default GETDATE()
)