BuyerAccounts和SellerAccounts表都引用帐户表。创建帐户INSERT
问题描述:
我正在设计表格来存储网站用户的帐户。网站有两种类型的帐户,BuyerAccounts和SellerAccounts。我的方法是创建一个主帐户表,该表将存储两种帐户通用的信息,然后为BuyerAccounts和SellerAccounts分别创建一个表。BuyerAccounts和SellerAccounts表都引用帐户表。创建帐户INSERT
这是基本的设置..
create table Accounts
(
UserID_PK int identity not null primary key,
Email varchar(50) not null unique,
UserPassword varchar(50) not null,
Salt varchar(50) not null,
AccountType tinyint not null,
);
create table SellerAccounts
(
SellerID_PK int not null primary key foreign key references Accounts(UserID_PK),
SellerColumn int
);
create table BuyerAccounts
(
BuyerID_PK int not null primary key foreign key references Accounts(UserID_PK),
DeliveryAddress string,
BuyerColumn string
);
每个SellerAccounts和BuyerAccounts都有一个主键,这也是一个外键引用的账表的主键。
到目前为止,这一切似乎都没问题,但我是初学者,所以请说,如果我迄今为止做了错误或错误的事情。
创建帐户时,我想在帐户中创建记录,并在BuyerAccounts或SellerAccounts中创建记录。
我该怎么做?我将把它作为一个存储过程,但我主要关心的是如何将INSERT插入到两个链接的表中。假设正在创建一个BuyerAccount。帐户创建是否必须分成两个插入?首先进入账户,以便该账户的UserID_PK也将是BuyerAccounts的BuyerID_PK,然后可以将其余账户信息插入到BuyerAccounts中?这是应该如何工作?
如何编写一个返回UserID_PK的INSERT,以便第二次插入它?或者甚至更好的SQL Server可以做些聪明的事情,只需为我填写UserID_PK和BuyerID_PK的值,并且只需一个插入即可完成所有工作?
谢谢你的帮助!
答
例如
declare @id int
insert Accounts values('[email protected]','pass','bla',1)
select @id = SCOPE_IDENTITY()
insert SellerAccounts values(@id,1)
select * from SellerAccounts
非常感谢你的帮助。 – 2011-06-06 12:02:02