drop database if exists bigwork;
create database bigwork;

create table `admin`
(
    passwd varchar(100) NOT NULL
);

insert into `admin` (`passwd`) values('');

create table user
(
user_id         int             PRIMARY KEY AUTO_INCREMENT,
user_name       varchar(100)    NOT NULL,
user_mail       varchar(100)    NOT NULL,
user_passwd     varchar(100)    NOT NULL,
user_limit      int             NOT NULL,
user_regtime    datetime        NOT NULL
) 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,
user_id         int             NOT NULL,
CONSTRAINT `fk_book_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`)
) 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    datetime        NOT NULL,
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`),
CONSTRAINT `fk_document_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`),
INDEX (doc_name(10)),
INDEX (doc_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

create table note
(
note_id         int             PRIMARY KEY AUTO_INCREMENT,
note_name       varchar(100)    NOT NULL,
note_date       datetime        NOT NULL,
note_content    mediumtext      NOT NULL DEFAULT '',
book_id         int             NOT NULL,
user_id         int             NOT NULL,
CONSTRAINT `fk_note_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`),
CONSTRAINT `fk_note_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`),
INDEX (note_name(10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

create table record
(
record_time timestamp       PRIMARY KEY,
record_type varchar(10)     NOT NULL,
doc_URL     varchar(100)    NOT NULL,
user_id     int             NOT NULL,
INDEX (record_type)
) 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 (type_name)
) 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,
INDEX (author_name(10))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

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`),
CONSTRAINT `fk_ba_author_id` FOREIGN KEY (`author_id`) REFERENCES author(`author_id`)
) 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`),
CONSTRAINT `fk_bt_type_id` FOREIGN KEY (`type_id`) REFERENCES typetable(`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

create table user_stat
(
user_id         int PRIMARY KEY,
user_usedspace  int NOT NULL DEFAULT 0,
user_bookcount  int NOT NULL DEFAULT 0,
user_doccount   int NOT NULL DEFAULT 0,
user_typecount  int NOT NULL DEFAULT 0,
CONSTRAINT `fk_user_stat_user` FOREIGN KEY REFERENCES user(`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
