172 lines
5.8 KiB
SQL
172 lines
5.8 KiB
SQL
drop table if exists `admin`;
|
|
drop table if exists user;
|
|
drop table if exists book;
|
|
drop table if exists document;
|
|
drop table if exists note;
|
|
drop table if exists record;
|
|
drop table if exists typetable;
|
|
drop table if exists author;
|
|
drop table if exists book_author;
|
|
drop table if exists book_type;
|
|
drop table if exists user_stat;
|
|
create table `admin`
|
|
(
|
|
passwd varchar(100) NOT NULL
|
|
);
|
|
-- insert into `admin` (`passwd`) values('');
|
|
-- sqlite default no foreign key
|
|
PRAGMA FOREIGN_KEYS=ON;
|
|
|
|
-- user
|
|
drop table if exists user;
|
|
create table user
|
|
(
|
|
user_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_name varchar(100) NOT NULL UNIQUE,
|
|
user_mail varchar(100) NOT NULL UNIQUE,
|
|
user_passwd varchar(100) NOT NULL,
|
|
user_limit int NOT NULL,
|
|
user_regtime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
-- insert into user (`user_name`, `user_mail`, `user_passwd`, `user_limit`) values ('catfood', 'a@a.com', 'lil0', 100);
|
|
|
|
-- book
|
|
drop table if exists book;
|
|
create table book
|
|
(
|
|
book_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
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
|
|
);
|
|
|
|
-- insert into book (`book_name`, `book_isbn`, `book_publisher`, `book_pubdate`, `book_lang`, `user_id`) values ('mom pig', 'emm', NULL, NULL, 'CN', 1);
|
|
|
|
-- document
|
|
drop table if exists document;
|
|
create table document
|
|
(
|
|
doc_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
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
|
|
);
|
|
create index `idx_doc_name` on document(doc_name);
|
|
|
|
-- insert into document (`doc_name`,`doc_url`,`doc_size`,`doc_type`,`book_id`,`user_id`) values ("a.pdf", 'c:/shit/a.pdf', 10, 'pdf', 1, 1);
|
|
|
|
-- note
|
|
drop table if exists note;
|
|
create table note
|
|
(
|
|
note_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
note_name varchar(100) NOT NULL,
|
|
note_date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
note_content text 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`) ON DELETE CASCADE,
|
|
CONSTRAINT `fk_note_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE CASCADE
|
|
);
|
|
create index `idx_note_name` on note(note_name);
|
|
|
|
-- insert into note (`note_name`, `note_content`,`book_id`,`user_id`) values ('long ago', 'well, nothing', 1, 1);
|
|
|
|
-- record
|
|
drop table if exists record;
|
|
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
|
|
);
|
|
|
|
-- insert into record (`record_type`,`doc_URL`,`user_id`)
|
|
-- values ('write', 'c:\a.pdf', 1);
|
|
|
|
-- typetable
|
|
drop table if exists typetable;
|
|
create table typetable
|
|
(
|
|
type_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
type_name varchar(20) NOT NULL UNIQUE
|
|
);
|
|
-- insert into typetable (`type_name`) values ('sjot');
|
|
|
|
-- author
|
|
drop table if exists author;
|
|
create table author
|
|
(
|
|
author_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
author_name varchar(50) NOT NULL
|
|
);
|
|
create index `idx_author_name` on author(author_name);
|
|
-- insert into author (`author_name`) values ('alabama.shit');
|
|
|
|
-- book_author
|
|
drop table if exists book_author;
|
|
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
|
|
);
|
|
-- insert into book_author values (1,1);
|
|
|
|
-- book_type
|
|
drop table if exists book_type;
|
|
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
|
|
);
|
|
-- insert into book_type values (1,1);
|
|
|
|
-- user_stat
|
|
drop table if exists user_stat;
|
|
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)
|
|
);
|
|
-- insert into user_stat values (1,1,1,1,1,1);
|
|
|
|
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; |