sql debug

This commit is contained in:
ridethepig 2022-11-12 00:52:00 +08:00
parent 5c7640acdc
commit 7e08cb3f04
6 changed files with 219 additions and 4 deletions

3
.gitignore vendored
View File

@ -5,4 +5,5 @@
*.tmp *.tmp
$* $*
~$* ~$*
.$* .$*
*.db

View File

@ -87,7 +87,7 @@ create table book_author
author_id int NOT NULL, author_id int NOT NULL,
book_id int NOT NULL, book_id int NOT NULL,
PRIMARY KEY (`author_id`, `book_id`), PRIMARY KEY (`author_id`, `book_id`),
CONSTRAINT `fk_ba_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_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`) CONSTRAINT `fk_ba_author_id` FOREIGN KEY (`author_id`) REFERENCES author(`author_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
@ -96,11 +96,11 @@ create table book_type
type_id int NOT NULL, type_id int NOT NULL,
book_id int NOT NULL, book_id int NOT NULL,
PRIMARY KEY (`type_id`, `book_id`), PRIMARY KEY (`type_id`, `book_id`),
CONSTRAINT `fk_bt_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_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`) CONSTRAINT `fk_bt_type_id` FOREIGN KEY (`type_id`) REFERENCES typetable(`type_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
create table book_type create table user_stat
( (
user_id int PRIMARY KEY, user_id int PRIMARY KEY,
user_usedspace int NOT NULL DEFAULT 0, user_usedspace int NOT NULL DEFAULT 0,

152
sql/db_create_sqlite.sql Normal file
View File

@ -0,0 +1,152 @@
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,
user_id int NOT NULL,
CONSTRAINT `fk_book_user_id` FOREIGN KEY (`user_id`) REFERENCES user(`user_id`) ON DELETE RESTRICT
);
-- 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 RESTRICT,
CONSTRAINT `fk_document_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE RESTRICT
);
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 RESTRICT,
CONSTRAINT `fk_note_book_id` FOREIGN KEY (`book_id`) REFERENCES book(`book_id`) ON DELETE RESTRICT
);
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 RESTRICT,
CONSTRAINT `fk_bt_type_id` FOREIGN KEY (`type_id`) REFERENCES typetable(`type_id`) ON DELETE RESTRICT
);
-- 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_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 RESTRICT
);
-- insert into user_stat values (1,1,1,1,1);

62
sql/trigger_sqlite.sql Normal file
View File

@ -0,0 +1,62 @@
drop trigger if exists `trig_create_user_stat`;
create trigger `trig_create_user_stat`
after insert on user
for each row begin
insert into user_stat (`user_id`) values(NEW.user_id);
end;
drop trigger if exists `trig_delete_user_stat`;
create trigger `trig_delete_user_stat`
after delete on user
for each row begin
delete from user_stat where user_id=OLD.user_id;
end;
drop trigger if exists `trig_update_stat_book_ins`;
create trigger `trig_update_stat_book_ins`
after insert on book
for each row begin
update user_stat set user_bookcount=user_bookcount+1 where user_stat.user_id=NEW.user_id;
end;
drop trigger if exists `trig_update_stat_book_del`;
create trigger `trig_update_stat_book_del`
after delete on book
for each row begin
update user_stat set user_bookcount=user_bookcount-1 where user_stat.user_id=OLD.user_id;
end;
drop trigger if exists `trig_update_stat_doc_ins`;
create trigger `trig_update_stat_doc_ins`
after insert on document
for each row begin
update user_stat set user_doccount=user_doccount+1 where user_stat.user_id=NEW.user_id;
update user_stat set user_usedspace=user_usedspace+NEW.doc_size where user_stat.user_id=NEW.user_id;
end;
drop trigger if exists `trig_update_stat_doc_del`;
create trigger `trig_update_stat_doc_del`
after delete on document
for each row begin
update user_stat set user_doccount=user_doccount-1 where user_stat.user_id=OLD.user_id;
update user_stat set user_usedspace=user_usedspace-OLD.doc_size where user_stat.user_id=OLD.user_id;
end;
drop trigger if exists `trig_update_stat_note_ins`;
create trigger `trig_update_stat_note_ins`
after insert on note
for each row begin
update user_stat set user_notecount=user_notecount+1 where user_stat.user_id=NEW.user_id;
end;
drop trigger if exists `trig_update_stat_note_del`;
create trigger `trig_update_stat_note_del`
after delete on note
for each row begin
update user_stat set user_notecount=user_notecount-1 where user_stat.user_id=OLD.user_id;
end;

0
src/test/test_sqlite.py Normal file
View File

Binary file not shown.