here is the sql script to create the database, at the end is the procedure
im using.
i very much appreciate the help...
bryan
-----------------------------------------------------
CREATE TABLE BCustomerCA (
username char(19) NOT NULL,
fullname varchar(27) NULL,
credit_card_nbr char(18) NULL,
password char(7) NULL,
credit_card_expiry_date datetime NULL,
mailing__address varchar(100) NULL,
phone_nbr char(14) NULL,
email_address char(50) NULL,
login_status bit NULL,
credit_card_name char(27) NULL
)
go
ALTER TABLE BCustomerCA
ADD PRIMARY KEY NONCLUSTERED (username)
go
CREATE TABLE BManufacturerCA (
manufacturer_id char(14) NOT NULL,
manufacturer_name varchar(50) NULL,
email_address char(50) NULL,
contact_name varchar(27) NULL,
phone_nbr char(14) NULL
)
go
ALTER TABLE BManufacturerCA
ADD PRIMARY KEY NONCLUSTERED (manufacturer_id)
go
CREATE TABLE BOrderItemCA (
part_id char(18) NOT NULL,
po_nbr char(18) NOT NULL,
item_qty int NULL
)
go
ALTER TABLE BOrderItemCA
ADD PRIMARY KEY NONCLUSTERED (part_id, po_nbr)
go
CREATE TABLE BPartCA (
part_id char(18) NOT NULL,
part_name varchar(50) NULL,
stock_size int NULL,
manufacturer_id char(14) NULL,
part_price decimal(10,2) NULL
)
go
ALTER TABLE BPartCA
ADD PRIMARY KEY NONCLUSTERED (part_id)
go
CREATE TABLE BPurchaseOrderCA (
po_nbr char(18) NOT NULL,
po_timetable datetime NULL,
isprocessed bit NULL,
username char(19) NULL
)
go
ALTER TABLE BPurchaseOrderCA
ADD PRIMARY KEY NONCLUSTERED (po_nbr)
go
CREATE TABLE BStaffCA (
username char(19) NOT NULL,
password char(7) NULL,
login_status bit NULL,
fullname varchar(27) NULL
)
go
ALTER TABLE BStaffCA
ADD PRIMARY KEY NONCLUSTERED (username)
go
ALTER TABLE BOrderItemCA
ADD FOREIGN KEY (po_nbr)
REFERENCES BPurchaseOrderCA
go
ALTER TABLE BOrderItemCA
ADD FOREIGN KEY (part_id)
REFERENCES BPartCA
go
ALTER TABLE BPartCA
ADD FOREIGN KEY (manufacturer_id)
REFERENCES BManufacturerCA
go
ALTER TABLE BPurchaseOrderCA
ADD FOREIGN KEY (username)
REFERENCES BCustomerCA
go
create trigger tD_BCustomerCA on BCustomerCA for DELETE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* DELETE trigger on BCustomerCA */
begin
declare @errno int,
@errmsg varchar(255)
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BCustomerCA R/5 BPurchaseOrderCA ON PARENT DELETE SET NULL */
update BPurchaseOrderCA
set
/* BPurchaseOrderCA.username = NULL */
BPurchaseOrderCA.username = NULL
from BPurchaseOrderCA,deleted
where
/* BPurchaseOrderCA.username = deleted.username */
BPurchaseOrderCA.username = deleted.username
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tU_BCustomerCA on BCustomerCA for UPDATE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* UPDATE trigger on BCustomerCA */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@insusername char(19),
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BCustomerCA R/5 BPurchaseOrderCA ON PARENT UPDATE SET NULL */
if
/* update(username) */
update(username)
begin
update BPurchaseOrderCA
set
/* BPurchaseOrderCA.username = NULL */
BPurchaseOrderCA.username = NULL
from BPurchaseOrderCA,deleted
where
/* BPurchaseOrderCA.username = deleted.username */
BPurchaseOrderCA.username = deleted.username
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tD_BManufacturerCA on BManufacturerCA for DELETE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* DELETE trigger on BManufacturerCA */
begin
declare @errno int,
@errmsg varchar(255)
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BManufacturerCA R/4 BPartCA ON PARENT DELETE SET NULL */
update BPartCA
set
/* BPartCA.manufacturer_id = NULL */
BPartCA.manufacturer_id = NULL
from BPartCA,deleted
where
/* BPartCA.manufacturer_id = deleted.manufacturer_id */
BPartCA.manufacturer_id = deleted.manufacturer_id
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tU_BManufacturerCA on BManufacturerCA for UPDATE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* UPDATE trigger on BManufacturerCA */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@insmanufacturer_id char(14),
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BManufacturerCA R/4 BPartCA ON PARENT UPDATE SET NULL */
if
/* update(manufacturer_id) */
update(manufacturer_id)
begin
update BPartCA
set
/* BPartCA.manufacturer_id = NULL */
BPartCA.manufacturer_id = NULL
from BPartCA,deleted
where
/* BPartCA.manufacturer_id = deleted.manufacturer_id */
BPartCA.manufacturer_id = deleted.manufacturer_id
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tI_BOrderItemCA on BOrderItemCA for INSERT as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* INSERT trigger on BOrderItemCA */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BPurchaseOrderCA R/3 BOrderItemCA ON CHILD INSERT RESTRICT */
if
/* update(po_nbr) */
update(po_nbr)
begin
select @nullcnt = 0
select @validcnt = count(*)
from inserted,BPurchaseOrderCA
where
/* inserted.po_nbr = BPurchaseOrderCA.po_nbr */
inserted.po_nbr = BPurchaseOrderCA.po_nbr
/* */
if @validcnt + @nullcnt != @numrows
begin
select @errno = 30002,
@errmsg = 'Cannot INSERT BOrderItemCA because BPurchaseOrderCA
does not exist.'
goto error
end
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BPartCA R/2 BOrderItemCA ON CHILD INSERT RESTRICT */
if
/* update(part_id) */
update(part_id)
begin
select @nullcnt = 0
select @validcnt = count(*)
from inserted,BPartCA
where
/* inserted.part_id = BPartCA.part_id */
inserted.part_id = BPartCA.part_id
/* */
if @validcnt + @nullcnt != @numrows
begin
select @errno = 30002,
@errmsg = 'Cannot INSERT BOrderItemCA because BPartCA does not
exist.'
goto error
end
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tU_BOrderItemCA on BOrderItemCA for UPDATE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* UPDATE trigger on BOrderItemCA */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@inspart_id char(18),
@inspo_nbr char(18),
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BPurchaseOrderCA R/3 BOrderItemCA ON CHILD UPDATE RESTRICT */
if
/* update(po_nbr) */
update(po_nbr)
begin
select @nullcnt = 0
select @validcnt = count(*)
from inserted,BPurchaseOrderCA
where
/* inserted.po_nbr = BPurchaseOrderCA.po_nbr */
inserted.po_nbr = BPurchaseOrderCA.po_nbr
/* */
if @validcnt + @nullcnt != @numrows
begin
select @errno = 30007,
@errmsg = 'Cannot UPDATE BOrderItemCA because BPurchaseOrderCA
does not exist.'
goto error
end
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BPartCA R/2 BOrderItemCA ON CHILD UPDATE RESTRICT */
if
/* update(part_id) */
update(part_id)
begin
select @nullcnt = 0
select @validcnt = count(*)
from inserted,BPartCA
where
/* inserted.part_id = BPartCA.part_id */
inserted.part_id = BPartCA.part_id
/* */
if @validcnt + @nullcnt != @numrows
begin
select @errno = 30007,
@errmsg = 'Cannot UPDATE BOrderItemCA because BPartCA does not
exist.'
goto error
end
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tD_BPartCA on BPartCA for DELETE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* DELETE trigger on BPartCA */
begin
declare @errno int,
@errmsg varchar(255)
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BPartCA R/2 BOrderItemCA ON PARENT DELETE RESTRICT */
if exists (
select * from deleted,BOrderItemCA
where
/* BOrderItemCA.part_id = deleted.part_id */
BOrderItemCA.part_id = deleted.part_id
)
begin
select @errno = 30001,
@errmsg = 'Cannot DELETE BPartCA because BOrderItemCA exists.'
goto error
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tI_BPartCA on BPartCA for INSERT as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* INSERT trigger on BPartCA */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BManufacturerCA R/4 BPartCA ON CHILD INSERT SET NULL */
if
/* update(manufacturer_id) */
update(manufacturer_id)
begin
update BPartCA
set
/* BPartCA.manufacturer_id = NULL */
BPartCA.manufacturer_id = NULL
from BPartCA,inserted
where
/* */
not exists (
select * from BManufacturerCA
where
/* inserted.manufacturer_id = BManufacturerCA.manufacturer_id */
inserted.manufacturer_id = BManufacturerCA.manufacturer_id
)
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tU_BPartCA on BPartCA for UPDATE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* UPDATE trigger on BPartCA */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@inspart_id char(18),
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BPartCA R/2 BOrderItemCA ON PARENT UPDATE RESTRICT */
if
/* update(part_id) */
update(part_id)
begin
if exists (
select * from deleted,BOrderItemCA
where
/* BOrderItemCA.part_id = deleted.part_id */
BOrderItemCA.part_id = deleted.part_id
)
begin
select @errno = 30005,
@errmsg = 'Cannot UPDATE BPartCA because BOrderItemCA exists.'
goto error
end
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BManufacturerCA R/4 BPartCA ON CHILD UPDATE SET NULL */
if
/* update(manufacturer_id) */
update(manufacturer_id)
begin
update BPartCA
set
/* BPartCA.manufacturer_id = NULL */
BPartCA.manufacturer_id = NULL
from BPartCA,inserted
where
/* */
not exists (
select * from BManufacturerCA
where
/* inserted.manufacturer_id = BManufacturerCA.manufacturer_id */
inserted.manufacturer_id = BManufacturerCA.manufacturer_id
)
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tD_BPurchaseOrderCA on BPurchaseOrderCA for DELETE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* DELETE trigger on BPurchaseOrderCA */
begin
declare @errno int,
@errmsg varchar(255)
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BPurchaseOrderCA R/3 BOrderItemCA ON PARENT DELETE RESTRICT */
if exists (
select * from deleted,BOrderItemCA
where
/* BOrderItemCA.po_nbr = deleted.po_nbr */
BOrderItemCA.po_nbr = deleted.po_nbr
)
begin
select @errno = 30001,
@errmsg = 'Cannot DELETE BPurchaseOrderCA because BOrderItemCA
exists.'
goto error
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tI_BPurchaseOrderCA on BPurchaseOrderCA for INSERT as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* INSERT trigger on BPurchaseOrderCA */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BCustomerCA R/5 BPurchaseOrderCA ON CHILD INSERT SET NULL */
if
/* update(username) */
update(username)
begin
update BPurchaseOrderCA
set
/* BPurchaseOrderCA.username = NULL */
BPurchaseOrderCA.username = NULL
from BPurchaseOrderCA,inserted
where
/* */
not exists (
select * from BCustomerCA
where
/* inserted.username = BCustomerCA.username */
inserted.username = BCustomerCA.username
)
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
create trigger tU_BPurchaseOrderCA on BPurchaseOrderCA for UPDATE as
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* UPDATE trigger on BPurchaseOrderCA */
begin
declare @numrows int,
@nullcnt int,
@validcnt int,
@inspo_nbr char(18),
@errno int,
@errmsg varchar(255)
select @numrows = @@rowcount
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BPurchaseOrderCA R/3 BOrderItemCA ON PARENT UPDATE RESTRICT */
if
/* update(po_nbr) */
update(po_nbr)
begin
if exists (
select * from deleted,BOrderItemCA
where
/* BOrderItemCA.po_nbr = deleted.po_nbr */
BOrderItemCA.po_nbr = deleted.po_nbr
)
begin
select @errno = 30005,
@errmsg = 'Cannot UPDATE BPurchaseOrderCA because BOrderItemCA
exists.'
goto error
end
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
/* BCustomerCA R/5 BPurchaseOrderCA ON CHILD UPDATE SET NULL */
if
/* update(username) */
update(username)
begin
update BPurchaseOrderCA
set
/* BPurchaseOrderCA.username = NULL */
BPurchaseOrderCA.username = NULL
from BPurchaseOrderCA,inserted
where
/* */
not exists (
select * from BCustomerCA
where
/* inserted.username = BCustomerCA.username */
inserted.username = BCustomerCA.username
)
end
/* ERwin Builtin Thu Jan 29 14:56:59 2004 */
return
error:
raiserror @errno @errmsg
rollback transaction
end
go
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE addCustomerSQL
(
@username char(19),
@fullname varchar(27),
@password char(7),
@credit_card_nbr char(18),
@credit_card_expiry_date datetime,
@mailing_address varchar(100),
@phone_nbr char(14),
@email_address char(50),
@login_status bit,
@credit_card_name char(27)
)
AS
INSERT INTO BCustomerCA VALUES (@username , @fullname, @password,
@credit_card_nbr, @credit_card_expiry_date, @mailing_address, @phone_nbr,
@email_address, @login_status, @credit_card_name )
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO