-- Create a customer information table
Create Table custominfo
(
CID nvarchar (20) not null primary key,
Cpassword nvarchar (30) not null check (LEN (cpassword)> = 6 ),
Csum int not null default (0)
)
Go
Insert into custominfo (CID, cpassword, csum)
Values ('zhang san', 'zhangsan', 0)
Go
Insert into custominfo (CID, cpassword, csum)
Values ('Li si', 'lisi123 ', 0)
Go
Insert into custominfo (CID, cpassword, csum)
Values ('wang wu', 'wangwu', 0)
Go
-- Create a purchase record table
Create Table buynote
(
Buyannal datetime not null,
CID nvarchar (20) not null foreign key references custominfo (CID ),
PID nvarchar (10) Not null foreign key references product (productid ),
[Count] int not null default (1)
)
Go
-- Create a point level table
Create Table integrallevel
(
Startcount int not null primary key,
[Name] nvarchar (10) Not null
)
Go
Insert into integrallevel (startcount, [name])
Values (0, 'common card users ')
Go
Insert into integrallevel (startcount, [name])
Values (10, 'Copper card users ')
Go
Insert into integrallevel (startcount, [name])
Values (20, 'Silver card users ')
Go
Insert into integrallevel (startcount, [name])
Values (40, 'Card users ')
Go
Insert into integrallevel (startcount, [name])
Values (60, 'vip card users ')
Go
-- Create a trigger
Create trigger buyfoodtrigger
On buynote
For insert
As
Begin
Declare @ CID nvarchar (20), @ count int
Select @ cid = CID, @ COUNT = [count] From inserted
Update custominfo set csum = csum + @ count where cid = @ CID
Print 'Thank you for your purchase. Your points have been recorded! '
End
Go
-- Create an SP for shopping
Create procedure buyfood
@ CID nvarchar (20 ),
@ PID nvarchar (10 ),
@ Count int
As
Begin
Insert into buynote (buyannal, CID, PID, [count]) values (getdate (), @ CID, @ PID, @ count)
Print 'Congratulations! '
End
Go
Execute buyfood 'zhang san', 'zhangsan', 5
Go
-- Create a general customer information table
Create Table totalcustom
(
CID nvarchar (20) not null primary key,
Cpassword nvarchar (30) not null check (LEN (cpassword)> = 6 ),
Csum int not null default (0)
)
Go
Insert into totalcustom (CID, cpassword, csum)
Values ('zhang sanfeng', 'zhangsanfeng', 0)
Go
Insert into totalcustom (CID, cpassword, csum)
Values ('wang la5', 'wanglaowu', 0)
Go
-- Create a scalar function for query level
Create Function getlevelbyid
(
@ CID nvarchar (20)
)
Returns nvarchar (28)
As
Begin
Declare @ csum int, @ level nvarchar (25)
Set @ level = @ CID + '==> no such card number'
If exists (select CID from custominfo where cid = @ CID)
Begin
Set @ csum = (select csum from custominfo where cid = @ CID)
Set @ level = (select top 1 [name] From integrallevel where startcount <= @ csum order by DESC)
Set @ level = @ CID + '---' + @ level
End
Return @ level
End
Go