shopping cart database design and the flow of putting the orders into the tables
I'm creating a database in SQL Server 2005 to store orders taken.
table [customers] : customer detail table primary key is the customer_ID which will be an identity autoincremental
table [orders] : holds 3 columns, [orderid](which is also the pk),[product_id],[quantity]
table [linking] : holds 2 columns, [customerid](as foreign key), [orderid](foreign key as well)
ordering flow : when customer checked out, the customer's detail will be stored to ta开发者_StackOverflow中文版ble[customers] in which a unique customer_ID will be generated.
next, using that customer_ID, the products in the shopping cart will be stored into table[orders] .
now, the problem is: how do i retrieve the auto_generated customer_ID from the table[customers]? assuming that a lot of users are checking out at the same time? After inserting the customer's detail into the table[customer], I have to use the customer_ID in the table[linking] to pair up with the orderid.
Write a few SQL stored procedures to do this work for you. You can call this from your web application's code using ADO.NET.
- Call proc
CreateCustomer
. It creates your CustomerID. - Call proc
CreateOrderForCust
.
CREATE PROC CreateCustomer
@Name varchar(100),
@Address varchar(100)
AS
DECLARE @CustomerID int;
INSERT INTO CUSTOMER([Name],[Addr]) VALUES (@Name, @Addr);
SELECT @CustomerID = SCOPE_IDENTITY();
RETURN @CustomerID;
...
CREATE PROC CreateOrderForCust
@CustomerID int,
@SKU int,
@Qty int
AS
.....
精彩评论