1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| CREATE DATABASE IF NOT EXISTS `goods` CHARACTER SET utf8 COLLATE utf8_general_ci; DROP DATABASE IF EXISTS `goods`;
CREATE TABLE IF NOT EXISTS `book_types` ( `type_id` int unsigned NOT NULL AUTO_INCREMENT COMMENT '书籍类型ID', `book_type` varchar(32) NOT NULL DEFAULT '' COMMENT '书籍类型', PRIMARY KEY (`type_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `book` ( `book_id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '书籍ID', `name` varchar(32) NOT NULL DEFAULT '' COMMENT '书籍名称', `price` decimal(10,2) unsigned zerofill NOT NULL COMMENT '单价', `author` varchar(16) NOT NULL DEFAULT '' COMMENT '作家名称', `publishing_house` varchar(32) NOT NULL DEFAULT '' COMMENT '出版社', `type_id` int unsigned NOT NULL COMMENT '书籍类型ID', PRIMARY KEY (`book_id`), KEY `fk_typeid` (`type_id`), CONSTRAINT `fk_typeid` FOREIGN KEY (`type_id`) REFERENCES `book_types` (`type_id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='书籍信息';
ALTER TABLE `book` ADD COLUMN `publishing_date` date NOT NULL COMMENT '出版日期' AFTER `publishing_house`;
DROP TABLE IF EXISTS `book`; RENAME TABLE `book` TO `books`;
INSERT INTO `book_types` (`type_id`, `book_type`) VALUES (1, '计算机'); INSERT INTO `book_types` (`type_id`, `book_type`) VALUES (2, '历史'); INSERT INTO `books` (`book_id`, `name`, `price`, `author`, `publishing_house`, `publishing_date`, `type_id`) VALUES (1, 'Python编程从入门到精通', 00000049.90, '张三', '图灵出版社', '2022-01-15', 1); INSERT INTO `books` (`book_id`, `name`, `price`, `author`, `publishing_house`, `publishing_date`, `type_id`) VALUES (2, '数据结构与算法分析', 00000035.00, '李四', '清华大学出版社', '2021-09-30', 1);
UPDATE `books` SET `publishing_date`='2016-12-18' WHERE `name`='三体' AND `author`='刘慈欣'; DELETE FROM `books` WHERE `name`='活法' AND `author`='梁文道';
|