1.建用户,建库,建表,增删查改,数据类型,数据类型操作函数和运算符
2.事务的使用,begin开始事务,出错rollback,完成时commit
3.执行语句是使用returning 返回指定值,如insert into users(id) ('10001') returning id;
4.在官方文档根据关键字搜索内容,如搜transaction搜事务相关
Chapter 8. Data Types
https://www.postgresql.org/docs/9.6/static/datatype.html
Chapter 9. Functions and Operators
https://www.postgresql.org/docs/9.6/static/functions.html
中文网站
http://www.yiibai.com/html/postgresql/2013/080435.html
1 前端注意事项: 2 a) 如果有字段为空,在前端做默认处理,如: 3 1.nickname为空,将其值设置为id; 4 2.icon为空,将其值设置为默认icon(默认icon由前端决定); 5 b) createTime参数由后端处理,前端只能获取; 6 7 8 9 10 11 12 13 1.objects(id,nickname,icon,isUser,custom) 14 15 create table if not exists objects ( 16 id serial8 primary key, 17 nickname varchar(200), 18 icon varchar(500), 19 isUser bool not null, 20 custom jsonb 21 ); 22 select setval('objects_id_seq',10000);--设置起始值 23 例子: 24 insert into objects(nickname,icon,isUser) values('dy','icon',true); 25 insert into objects(nickname,icon,isUser) values('xl','icon1',true); 26 insert into objects(nickname,icon,isUser) values('emb','icon2',false); 27 28 29 30 2.users(id,password,isManager) 31 32 create table if not exists users ( 33 id int8 references objects(id) on delete cascade, 34 password varchar(200) not null, 35 isManager bool not null, 36 primary key(id) 37 ); 38 39 例子: 40 insert into users(id,password,isManager) values(10001,'123456',false); 41 insert into users(id,password,isManager) values(10002,'123456',false); 42 43 44 3.groups(id,createtime,custom) 45 46 create table if not exists groups ( 47 id int8 references objects(id) on delete cascade, 48 createTime timestamp with time zone default now(), 49 primary key(id) 50 ); 51 52 例子: 53 insert into groups(id) values(10003); 54 55 56 57 4.members(groupID,userID) 58 59 create table if not exists members ( 60 groupID int8 references objects(id) on delete cascade, 61 userID int8 references objects(id) on delete cascade, 62 primary key(groupID,userID) 63 ); 64 65 例子: 66 insert into members(groupID,userID) values 67 (10003,10001), 68 (10003,10002); 69 70 71 72 5.messages(id,content,sender,receiver,createTime) 73 74 create table if not exists messages ( 75 id serial8 primary key, 76 content varchar(2000) not null, 77 sender int8 references objects(id) on delete cascade, 78 receiver int8 references objects(id) on delete cascade, 79 createTime timestamp with time zone default now() 80 ); 81 82 例子: 83 insert into messages(content,sender,receiver) values('hello world!',10001,10002); 84 85 86 查看插入: 87 select * from objects; 88 select * from users; 89 select * from groups; 90 select * from members; 91 select * from messages; 92 清理: 93 delete from objects;
1 前端注意事项: 2 a) 如果有字段为空,在前端做默认处理,如: 3 1.nickname为空,将其值设置为id; 4 2.icon为空,将其值设置为默认icon(默认icon由前端决定); 5 b) createTime参数由后端处理,前端只能获取; 6 7 2.sessions表(senderID int64 fk,receiverID int64 fk,lastMsgID int64 fk,pk(senderID,receiverID)); 8 3.users表加account string字段,帐号为手机号; 9 4.objects表icon字段改为iconURL; 10 11 12 --写那个groups表时发现外键没有默认not null ,即可以插入insert into groups(id) values(100000005);,所以把所有的都加了not null 13 14 create table if not exists objects ( 15 id serial8 primary key, 16 nickname varchar(200) not null, --nickname修改为not null 17 iconURL varchar(500) default '/icon/default.ico' not null, 18 isUser bool not null, 19 custom jsonb default '{}'::jsonb not null 20 ); 21 --例子: 22 select setval('objects_id_seq',99999999);--设置起始值 23 insert into objects(nickname,isUser) values('老猫',true); 24 insert into objects(nickname,isUser) values('dy',true); 25 insert into objects(nickname,isUser) values('Ficow',true); 26 insert into objects(nickname,isUser) values('Decaf',true); 27 insert into objects(nickname,isUser) values('Pismery',true); 28 insert into objects(nickname,isUser) values('嵌入式讨论群',false); 29 insert into objects(nickname,isUser) values('吹水群6',false); 30 insert into objects(nickname,isUser) values('kemin',true); 31 insert into objects(nickname,isUser) values('jimmy',true); 32 --2.users(id int64 fk pk,account string unique,password string,lastOnlineTime time) 33 create table if not exists users ( 34 id int8 primary key, 35 account varchar(200) not null, 36 password varchar(200) not null, 37 lastOnlineTime timestamp with time zone default now() not null 38 ); 39 alter table users drop constraint if exists fk_id; 40 alter table users drop constraint if exists unique_account; 41 alter table users add constraint fk_id foreign key (id) references objects(id) on delete cascade; 42 alter table users add constraint unique_account unique(account); 43 44 --例子: 45 insert into users(id,account,password) values(100000000,'13580452503','123456'); 46 insert into users(id,account,password) values(100000001,'15602309617','123456'); 47 insert into users(id,account,password) values(100000002,'15521133823','123456'); 48 insert into users(id,account,password) values(100000003,'18826054758','123456'); 49 insert into users(id,account,password) values(100000004,'15626030466','123456'); 50 insert into users(id,account,password) values(100000007,'15602303588','123456'); 51 insert into users(id,account,password) values(100000008,'18675502863','123456'); 52 --3.groups(id int64 fk pk,owner int64 fk,createtime time) 53 create table if not exists groups ( 54 id int8 primary key, 55 owner int8 not null, 56 createTime timestamp with time zone default now() not null 57 ); 58 alter table groups drop constraint if exists fk_id; 59 alter table groups drop constraint if exists fk_owner; 60 alter table groups add constraint fk_id foreign key (id) references objects(id) on delete cascade; 61 alter table groups add constraint fk_owner foreign key (owner) references objects(id) on delete cascade; 62 63 --例子: 64 insert into groups(id,owner) values(100000005,100000000); 65 insert into groups(id,owner) values(100000006,100000000); 66 --4.members(groupID int64 fk,userID int64 fk,pk(groupID,userID)) 67 68 create table if not exists members ( 69 groupID int8 not null, 70 userID int8 not null, 71 isManager bool not null, 72 primary key (groupID,userID) 73 ); 74 alter table members drop constraint if exists fk_groupID; 75 alter table members drop constraint if exists fk_userID; 76 alter table members add constraint fk_groupID foreign key (groupID) references objects(id) on delete cascade; 77 alter table members add constraint fk_userID foreign key (userID) references objects(id) on delete cascade; 78 79 --例子: 80 insert into members(groupID,userID,isManager) values 81 (100000005,100000000,true), 82 (100000005,100000001,false), 83 (100000005,100000002,false), 84 (100000005,100000003,false), 85 (100000005,100000004,false), 86 (100000005,100000007,false), 87 (100000005,100000008,false); 88 insert into members(groupID,userID,isManager) values 89 (100000006,100000000,true), 90 (100000006,100000001,false), 91 (100000006,100000002,true), 92 (100000006,100000003,false); 93 DROP TYPE IF EXISTS MsgType; 94 CREATE TYPE MsgType AS ENUM ('text', 'image','audio', 'file'); 95 --5.messages(id int64 pk,content string,sender int64 fk,receiver int64 fk,createTime time) 96 97 create table if not exists messages ( 98 id serial8 primary key, 99 content varchar(2000) not null, 100 sender int8 not null, 101 receiver int8 not null, 102 type MsgType not null, 103 createTime timestamp with time zone default now() not null 104 ); 105 alter table messages drop constraint if exists fk_sender; 106 alter table messages drop constraint if exists fk_receiver; 107 alter table messages add constraint fk_sender foreign key (sender) references objects(id) on delete cascade; 108 alter table messages add constraint fk_receiver foreign key (receiver) references objects(id) on delete cascade; 109 110 --例子: 111 insert into messages(content,sender,receiver,type) values('10001:你好 user2!我要给你发一条很长的消息,然后让你可以测试一下你的消息气泡的高度是不是有问题!',100000001,100000002,'text'); 112 insert into messages(content,sender,receiver,type) values('10002:嘿 user1!在吗?',100000002,100000001,'text'); 113 insert into messages(content,sender,receiver,type) values('10002:嘿 user4!呃,没事。。。',100000002,100000004,'text'); 114 insert into messages(content,sender,receiver,type) values('10003:你好 user1! user1。。。。。。。',100000003,100000001,'text'); 115 insert into messages(content,sender,receiver,type) values('10001:嘿 group5!我要给你发一条很长的消息,然后让你可以测试一下你的消息气泡的高度是不是有问题!正常吗?',100000001,100000005,'text'); 116 insert into messages(content,sender,receiver,type) values('10001:嘿 group6!呃,我来测试一下你的消息气泡的高度是不是有问题!',100000001,100000006,'text'); 117 insert into messages(content,sender,receiver,type) values('10002:嗨 group5!我要给你发一条很长很长很长的消息,然后让你可以测试一下你的消息气泡的高度是不是有问题!正常吗?要不然,我再说点啥?你再看下,现在看下正常不???????',100000002,100000005,'text'); 118 insert into messages(content,sender,receiver,type) values('10003:嗨 group6! 1.0版本啥时候诞生呢?',100000003,100000006,'text'); 119 insert into messages(content,sender,receiver,type) values('10004:你好 group6!我也要给你发一条,一条很长很长的消息,然后让你可以测试一下你的消息气泡的高度是不是有问题!正常了吧?',100000004,100000006,'text'); 120 --6.sessions(id serial8,senderID int64 fk,receiverID int64 fk,lastMsgID int64 fk,pk(id,senderID,receiverID)); 121 create table if not exists sessions ( 122 id serial8 not null, 123 senderID int8 not null, 124 receiverID int8 not null, 125 lastMsgID int8 not null, 126 primary key (id,senderID,receiverID) 127 ); 128 alter table sessions drop constraint if exists fk_senderID; 129 alter table sessions drop constraint if exists fk_receiverID; 130 alter table sessions drop constraint if exists fk_lastMsgID; 131 alter table sessions add constraint fk_senderID foreign key (senderID) references objects(id) on delete cascade; 132 alter table sessions add constraint fk_receiverID foreign key (receiverID) references objects(id) on delete cascade; 133 alter table sessions add constraint fk_lastMsgID foreign key (lastMsgID) references messages(id) on delete cascade; 134 135 136 --例子: 137 insert into sessions(senderID,receiverID,lastMsgID) values(100000002,100000001,2); 138 insert into sessions(senderID,receiverID,lastMsgID) values(100000002,100000004,3); 139 insert into sessions(senderID,receiverID,lastMsgID) values(100000003,100000001,4); 140 insert into sessions(senderID,receiverID,lastMsgID) values(100000002,100000005,7); 141 insert into sessions(senderID,receiverID,lastMsgID) values(100000004,100000006,9); 142 143 --index 144 drop index if exists objects_id_index; 145 drop index if exists users_account_index; 146 drop index if exists users_id_index; 147 drop index if exists groups_id_index; 148 drop index if exists members_index; 149 drop index if exists messages_index; 150 drop index if exists sessions_index; 151 create index if not exists objects_id_index on objects (id); 152 create index if not exists users_account_index on users(account); 153 create index if not exists users_id_index on users(id); 154 create index if not exists groups_id_index on groups (id); 155 create index if not exists members_index on members(groupID,userID); 156 create index if not exists messages_index on messages(sender,receiver); 157 create index if not exists sessions_index on sessions (id,senderID,receiverID); 158 159 --查表 160 select * from sessions; 161 select * from messages; 162 select * from members; 163 select * from groups; 164 select * from users; 165 select * from objects; 166 --清除数据 167 truncate objects cascade; 168 169 --删表: 170 drop table if exists sessions; 171 drop table if exists messages; 172 drop table if exists members; 173 drop table if exists groups; 174 drop table if exists users; 175 drop table if exists objects; 176 177 --约束 178 alter table users drop constraint if exists unique_account; 179 alter table users drop constraint if exists fk_id; 180 alter table groups drop constraint if exists fk_id; 181 alter table groups drop constraint if exists fk_owner; 182 alter table members drop constraint if exists fk_groupID; 183 alter table members drop constraint if exists fk_userID; 184 alter table messages drop constraint if exists fk_sender; 185 alter table messages drop constraint if exists fk_receiver; 186 alter table sessions drop constraint if exists fk_senderID; 187 alter table sessions drop constraint if exists fk_receiverID; 188 alter table sessions drop constraint if exists fk_lastMsgID; 189 alter table users add constraint unique_account unique(account); 190 alter table users add constraint fk_id foreign key (id) references objects(id) on delete cascade; 191 alter table groups add constraint fk_id foreign key (id) references objects(id) on delete cascade; 192 alter table groups add constraint fk_owner foreign key (owner) references objects(id) on delete cascade; 193 alter table members add constraint fk_groupID foreign key (groupID) references objects(id) on delete cascade; 194 alter table members add constraint fk_userID foreign key (userID) references objects(id) on delete cascade; 195 alter table messages add constraint fk_sender foreign key (sender) references objects(id) on delete cascade; 196 alter table messages add constraint fk_receiver foreign key (receiver) references objects(id) on delete cascade; 197 alter table sessions add constraint fk_senderID foreign key (senderID) references objects(id) on delete cascade; 198 alter table sessions add constraint fk_receiverID foreign key (receiverID) references objects(id) on delete cascade; 199 alter table sessions add constraint fk_lastMsgID foreign key (lastMsgID) references messages(id) on delete cascade;