DBBigWork/sql/db_create.sql
2022-12-11 21:43:19 +08:00

134 lines
5.3 KiB
SQL

use bigwork;
drop table if exists `admin`;
drop table if exists user_stat;
drop table if exists book_type;
drop table if exists book_author;
drop table if exists author;
drop table if exists typetable;
drop table if exists record;
drop table if exists note;
drop table if exists document;
drop table if exists book;
drop table if exists user;
create table `admin`
(
passwd varchar(200) NOT NULL
);
-- insert into `admin` (`passwd`) values('');
create table user
(
user_id int PRIMARY KEY AUTO_INCREMENT,
user_name varchar(100) NOT NULL UNIQUE,
user_mail varchar(100) NOT NULL UNIQUE,
user_passwd varchar(200) NOT NULL,
user_limit int NOT NULL,
user_regtime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create table book
(
book_id int PRIMARY KEY AUTO_INCREMENT,
book_name varchar(100) NOT NULL,
book_isbn varchar(100) DEFAULT NULL,
book_publisher varchar(100) DEFAULT NULL,
book_pubdate datetime DEFAULT NULL,
book_lang varchar(100) DEFAULT NULL,
book_author varchar(100) DEFAULT NULL,
user_id int NOT NULL,
CONSTRAINT `fk_book_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create table document
(
doc_id int PRIMARY KEY AUTO_INCREMENT,
doc_name varchar(100) NOT NULL,
doc_url varchar(100) NOT NULL,
doc_size int NOT NULL,
doc_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
doc_type varchar(100) NOT NULL,
book_id int NOT NULL,
user_id int NOT NULL,
CONSTRAINT `fk_document_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE CASCADE,
CONSTRAINT `fk_document_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create index `idx_doc_name` on document(doc_name);
create table note
(
note_id int PRIMARY KEY AUTO_INCREMENT,
note_name varchar(100) NOT NULL,
note_date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
note_content text NOT NULL,
book_id int NOT NULL,
user_id int NOT NULL,
CONSTRAINT `fk_note_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE CASCADE,
CONSTRAINT `fk_note_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create index `idx_note_name` on note(note_name);
create table record
(
record_time timestamp PRIMARY KEY DEFAULT CURRENT_TIMESTAMP,
record_type varchar(10) NOT NULL,
doc_URL varchar(100) NOT NULL,
user_id int NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create table typetable
(
type_id int PRIMARY KEY AUTO_INCREMENT,
type_name varchar(20) NOT NULL UNIQUE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create table author
(
author_id int PRIMARY KEY AUTO_INCREMENT,
author_name varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create index `idx_author_name` on author(author_name);
create table book_author
(
author_id int NOT NULL,
book_id int NOT NULL,
PRIMARY KEY (`author_id`, `book_id`),
CONSTRAINT `fk_ba_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE RESTRICT,
CONSTRAINT `fk_ba_author_id` FOREIGN KEY (`author_id`) REFERENCES author(`author_id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create table book_type
(
type_id int NOT NULL,
book_id int NOT NULL,
PRIMARY KEY (`type_id`, `book_id`),
CONSTRAINT `fk_bt_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE CASCADE,
CONSTRAINT `fk_bt_type_id` FOREIGN KEY (`type_id`) REFERENCES typetable(`type_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create table user_stat
(
user_id int PRIMARY KEY,
user_limit int NOT NULL,
user_usedspace int NOT NULL DEFAULT 0,
user_bookcount int NOT NULL DEFAULT 0,
user_doccount int NOT NULL DEFAULT 0,
user_notecount int NOT NULL DEFAULT 0,
CONSTRAINT `fk_user_stat_user` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE CASCADE,
CONSTRAINT `ck_usedspace` CHECK (user_usedspace <= user_limit)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
drop view if exists `v_book_to_types`;
create view `v_book_to_types` as
select book_id, typetable.type_id as type_id, type_name from book_type natural join typetable;
drop view if exists `v_type_to_book`;
create view `v_type_to_book` as
select book_id,
book_name,
book_isbn,
book_publisher,
book_lang,
book_author,
user_id,
type_id,
type_name
from book natural join book_type natural join typetable;