创建数据库代码:
DROP DATABASE IF EXISTS petShop;
CREATE DATABASE petShop; USE petShop;
/*创建表*/
CREATE TABLE `petStore`(
id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` CHAR() NULL,
`password` CHAR() NULL,
`balance` INT() NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8; CREATE TABLE `petOwner`(
id INT() AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` CHAR() NULL,
`password` CHAR() NULL,
`money` INT() NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8; CREATE TABLE `pet`(
id INT() AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` CHAR() NOT NULL,
`typeName` CHAR() NULL,
`health` INT() NULL,
`love` INT() NULL,
`birthday` TIMESTAMP NULL,
`owner_id` INT() NULL,
`store_id` INT() NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8; CREATE TABLE `account`(
`id` INT() AUTO_INCREMENT NOT NULL PRIMARY KEY,
`deal_type` INT() NULL,
`pet_id` INT() NULL,
`seller_id` INT() NULL,
`buyer_id` INT() NULL,
`price` INT() NULL,
`deal_time` TIMESTAMP NULL
)ENGINE=INNODB DEFAULT CHARSET=utf8; /*创建外键*/
ALTER TABLE `account` ADD CONSTRAINT fk_account_pet FOREIGN KEY(pet_id) REFERENCES `pet` (`id`);
ALTER TABLE `account` ADD CONSTRAINT fk_account_petOwner FOREIGN KEY(seller_id) REFERENCES `petOwner` (`id`);
ALTER TABLE `pet` ADD CONSTRAINT fk_pet_petOwner FOREIGN KEY(owner_id) REFERENCES `petOwner` (`id`) ;
ALTER TABLE `pet` ADD CONSTRAINT fk_pet_petStore FOREIGN KEY(store_id) REFERENCES petStore (`id`); /*插入数据*/
INSERT INTO `petowner` (id, `name`, `passWord`, `money`) VALUES (, '小明', '', );
INSERT INTO `petowner` (id, `name`, `passWord`, `money`) VALUES (, '小强 ', '', ); INSERT INTO `petstore` (id, `name`, `passWord`,`balance` )VALUES (, '北京信息中心', '123456 ', );
INSERT INTO `petstore` (id, `name`, `passWord`,`balance` ) VALUES (, '重庆观音桥 ', '123456 ', ); INSERT `pet` (`id`, `name`,`typeName`, `health`, `love`, `birthday`, `owner_id`, `store_id`) VALUES (, '花花 ', 'dog', , , '2015-08-20', , );
INSERT `pet` (`id`, `name`,`typeName`, `health`, `love`, `birthday`, `owner_id`, `store_id`) VALUES (, '贝贝', 'penguin', , , '2015-08-20', NULL, );
INSERT `pet` (`id`, `name`,`typeName`, `health`, `love`, `birthday`, `owner_id`, `store_id`) VALUES (, '成成','dog', , , '2015-09-10', NULL, );
INSERT `pet` (`id`, `name`,`typeName`, `health`, `love`, `birthday`, `owner_id`, `store_id`) VALUES (, '露露','bird', , , '2016-01-10', NULL, );
INSERT `pet` (`id`, `name`,`typeName`, `health`, `love`, `birthday`, `owner_id`, `store_id`) VALUES (, '老虎','tiger', , , '2016-02-10', , );
INSERT `pet` (`id`, `name`,`typeName`, `health`, `love`, `birthday`, `owner_id`, `store_id`) VALUES (, '老虎','tiger', , , '2016-3-15', NULL, );
INSERT `pet` (`id`, `name`,`typeName`, `health`, `love`, `birthday`, `owner_id`, `store_id`) VALUES (, '老虎','tiger', , , '2016-2-15', NULL, );
INSERT `pet` (`id`, `name`,`typeName`, `health`, `love`, `birthday`, `owner_id`, `store_id`) VALUES (, '狮子','lion', , , '2016-4-15', NULL, ); INSERT `account` (`id`, `deal_type`,`pet_id`, `seller_id`, `buyer_id`, `price`,`deal_time`) VALUES (, , , , , , '2016-08-20');
INSERT `account` (`id`, `deal_type`,`pet_id`, `seller_id`, `buyer_id`, `price`,`deal_time`) VALUES (, , , , , ,'2016-08-20');
INSERT `account` (`id`, `deal_type`,`pet_id`, `seller_id`, `buyer_id`, `price`,`deal_time`) VALUES (, , , , , , '2016-09-10');
INSERT `account` (`id`, `deal_type`,`pet_id`, `seller_id`, `buyer_id`, `price`,`deal_time`) VALUES (, , , , , , '2016-09-10');
INSERT `account` (`id`, `deal_type`,`pet_id`, `seller_id`, `buyer_id`, `price`,`deal_time`) VALUES (, , , , , , '2016-10-15'); /*创建本地用户epet,密码0000*/
GRANT ALL ON petShop.* TO `epet`@`localhost` IDENTIFIED BY '' SELECT id,NAME,typeName,health,love,birthday,owner_id,store_id FROM pet
整体架构:
具体代码: