如何将一个表中的主(代理)密钥添加到另一个表的外键中?

时间:2015-11-01 14:14:54

标签: c# sql-server database database-design

我正在尝试将Customers表中的CustId(主键)添加到CustomerAddress表中的CustID(外键)中。但是我无法自动添加外键。我该怎么办。下面是我的架构(我从我的SQL Server实例复制它)

CREATE TABLE [dbo].[Customers] 
(
           [CustId]           INT           IDENTITY (1, 1) NOT NULL,
           [FirstName]        NVARCHAR (50) NOT NULL,
           [MiddleName]       NVARCHAR (50) NULL,
            [LastName]         NVARCHAR (50) NOT NULL,
            [Salutation]       NVARCHAR (10) NULL,
            [Position]         NVARCHAR (50) NULL,
            [OrganizationType] NVARCHAR (50) NULL,
            [PhoneNumber]      NVARCHAR (50) NOT NULL,
            [Ext]              NCHAR (10)    NULL,
            [FaxNumber]        NVARCHAR (50) NULL,
            [CellNumber]       NVARCHAR (50) NULL,
            [EmailAddress]     NVARCHAR (50) NOT NULL,
            [EmailPermission]  NCHAR (10)    NOT NULL,
            [Password]         NVARCHAR (50) NOT NULL,
            PRIMARY KEY CLUSTERED ([CustId] ASC)
);

CREATE TABLE [dbo].[CustomerAddress] 
(
            [AddressID] INT NOT NULL IDENTITY (1, 1),
            [CustID]           INT,
            [OrganizationName] NVARCHAR (50) NOT NULL,
            [Division]         NVARCHAR (50) NULL,
            [Department]       NVARCHAR (50) NOT NULL,
            [BuildingRoom]     NVARCHAR (50) NULL,
            [Street]           NVARCHAR (50) NULL,
            [City]             NVARCHAR (50) NULL,
            [POBox]            NVARCHAR (50) NULL,
            [Province]         NVARCHAR (50) NULL,
            [PostalCode]       NCHAR (10)    NOT NULL,
            [Country]          NVARCHAR (30) NOT NULL,
            [AddressType]      CHAR (10)     NOT NULL,
            PRIMARY KEY CLUSTERED ([AddressID]),
            CONSTRAINT [FK_CustID] FOREIGN KEY ([CustID]) REFERENCES [dbo].[Customers] ([CustId])
);

用于插入数据的C#代码:

public int AddCustomerDeliveryAddress(CustomerAddressBLL NewCustomerDeliveryAddressBLL)
{
            string sql = string.Format(@"Insert into CustomerAddress (OrganizationName,Division,Department,BuildingRoom,Street,City,POBox,Province,PostalCode,Country,AddressType)
                        Values(@OrganizationName,@Division,@Department,@BuildingRoom,@Street,@City,@POBox,@Province,@PostalCode,@Country,@AddressType)");

            db.AddParameter("@OrganizationName", NewCustomerDeliveryAddressBLL.Organization);
            db.AddParameter("@Division", NewCustomerDeliveryAddressBLL.Division);
            db.AddParameter("@Department", NewCustomerDeliveryAddressBLL.Department);
            db.AddParameter("@BuildingRoom", NewCustomerDeliveryAddressBLL.BuildingRoom);
            db.AddParameter("@Street", NewCustomerDeliveryAddressBLL.Street);
            db.AddParameter("@City", NewCustomerDeliveryAddressBLL.City);
            db.AddParameter("@POBox", NewCustomerDeliveryAddressBLL.PoBox);
            db.AddParameter("@Province", NewCustomerDeliveryAddressBLL.Province);
            db.AddParameter("@PostalCode", NewCustomerDeliveryAddressBLL.PostalCode);
            db.AddParameter("@Country", NewCustomerDeliveryAddressBLL.Country);
            db.AddParameter("@AddressType", NewCustomerDeliveryAddressBLL.AddressType);

            return db.ExecuteNonQuery(sql);
}

1 个答案:

答案 0 :(得分:2)

如果在“纯”T-SQL中执行此操作,则需要使用以下代码:

-- declare variable for your identity
DECLARE @NewCustId INT;

-- insert into your Customers table
INSERT INTO dbo.Customers([FirstName], [MiddleName], [LastName], ......)
VALUES ('John', 'Robert', 'Doe', ........);

-- get the newly inserted Identity value
SET @NewCustId = SCOPE_IDENTITY();

-- insert into CustomerAddress table
INSERT INTO dbo.CustomerAddress ([CustID], [OrganizationName], [Division], ......)
VALUES(@NewCustId, 'Orgname', 'Division', .....)

更新:好的,它是C#代码 - 您需要将其更改为最后包含SELECT SCOPE_IDENTITY()

string sql = string.Format(@"INSERT INTO dbo.CustomerAddress (OrganizationName, Division, Department, BuildingRoom, Street, City, POBox, Province, PostalCode, Country, AddressType)
                             VALUES (@OrganizationName, @Division, @Department, @BuildingRoom, @Street, @City, @POBox, @Province, @PostalCode, @Country, @AddressType); 
                             SELECT SCOPE_IDENTITY();");

并使用此调用:

int newCustId = (int)db.ExecuteScalar(sql);

所以您现在从NewCustID电话中取回INSERT - 现在在第二个插页中使用该值,您需要将数据插入CustomerAddress