完整項目:網上圖書商城(一、MySQL數據庫設計)未完


一、建立數據庫

CREATE DATABASE IF NOT EXISTS bookshop CHARACTER utf8;

二、建立數據庫表

1、建立用戶表

#用戶表(用戶id號,用戶名,用戶密碼,手機號,性別,注冊日期,最近登錄時間)
CREATE
TABLE b_user( uId int AUTO_INCREMENT PRIMARY KEY, uName varchar(20) NOT NULL UNIQUE KEY, uPwd varchar(20) NOT NULL, uPhone char(11) NOT NULL, uSex varchar(2) DEFAULT '', uRegistetime datatime, uLastdate datatime );

2、建立圖書類型表

#圖書類型(圖書類型id號,圖書類型名,圖書簡介)
create table b_booktype(
typeId int auto_increment primary key,
typeName varchar(50) not null,
typeNote text 
);

3、建立圖書信息表

#圖書信息表(書id號,書類型id,書名,簡介,作者,價格)
create table b_bookinfo(
bookId int auto_increment primary key,
bookTypeid int not null,
bookName varchar(50) not null,
bookNote text,
bookAuthor varchar(10) not null default '佚名',
bookMoney float(6,2) not null,
);

4、建立管理員表

#管理員表(管理員id號,賬號,密碼,權限等級)
create table b_manager(
aId int AUTO_INCREMENT PRIMARY KEY,
aName varchar(10) NOT NULL UNIQUE KEY,
aPwd varchar(20) NOT NULL,
aLevel int NOT NULL default '1'
);

5、建立購物車表

#購物車表(購物車id,用戶id,訂單總數,訂單總價格,是否支付,支付方式)
create table b_shoppingcart(
sId int auto_increment primary key,
uId int not null,
sOrdercount int not null,
sTotalprice float(8,2) not null,
sIspay varchar(6) default '未支付',
sPaymethod varchar(20) default '',
CONSTRAINT chk_sIspay CHECK (sIspay in ('未支付','已支付'))
);

6、建立訂單表

#訂單表(訂單id,用戶id,書id號,訂單日期,訂單數量,訂單價格,備注,收件人,地址,收件人手機號,訂單狀態)
create table b_order(
oId int auto_increment primary key,
uId int not null,
bookId int not null,
oDate datetime not null,
oNum int not null,
oPrice float(6,2) not null,
oMessage varchar(100),
oAddressee varchar(20) not null,
oAddress varchar(100) not null,
oPhone varchar(11) not null,
oStatus varchar(6) default '未完成',
CONSTRAINT chk_oStatus CHECK (sStatus in ('未完成','已完成'))
);

三、建立觸發器

1、

#1、當用戶表insert一條信息之后,將數據庫當前時間賦值給注冊日期和最近登錄日期
DELIMITER //
CREATE TRIGGER user_time
BEFORE INSERT 
ON b_user FOR EACH ROW
BEGIN
    UPDATE b_user SET uRegistetime=NOW(),uLastdate=NOW() WHERE uId=new.uId;
END;
//
DELIMITER ;

2、

#2、當訂單表中使用insert語句之后,將所有的書價乘以數量賦值給訂單價格,再將所有同一個用戶id的訂單放入到購物車中。
delimiter //
create trigger order_numAndShoppingcart
before insert on b_order for each row
begin
    declare sum int;
    declare price float;
    set price=(select bookMoney from b_bookinfo as bi where bi.bookId=new.bookId);
    set sum=(select count(oId) from b_order as bo where bo.uId=new.uId);
    update b_order set oPrice=price*new.oNum where uId=new.uId;
    if (select uId from b_shoppingcart where uId=new.uId is null) then
        insert b_shoppingcart(uId,sOrdercount,sTotalprice) values(new.uId,sum,price*new.oNum);
    else
        update b_shoppingcart set sOrdercount=sum,sTotalprice=sTotalprice+(price*new.oNum) where uId=new.uId;
    end if;
end;
//
delimiter //

3、查看觸發器

show triggers from bookshop;

 

四、存儲過程

1、

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM