---
# System prepended metadata

title: 岑復興數據庫結構
tags: [david]

---

# 岑復興數據庫結構

[TOC]

###### tags: `david`

---

## 雪花算法

```sql
CREATE SEQUENCE IF NOT EXISTS "public"."global_id_sequence"
MAXVALUE 9223372036854775807;

CREATE OR REPLACE FUNCTION "public"."id_generator"(OUT result bigint) AS $$
DECLARE
	our_epoch bigint := 1288834974657; -- change as needed
	seq_id bigint;
	now_millis bigint;
	-- the id of this DB shard, must be set for each
	-- schema shard you have - you could pass this as a parameter too
	shard_id int := 1;
BEGIN
	SELECT nextval('public.global_id_sequence') % 1024 INTO seq_id;

	SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
	result := (now_millis - our_epoch) << 23;
	result := result | (shard_id << 10);
	result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;
```

## 資料表結構

1. [用户(表)](https://hackmd.io/@cOdEthEfUckUp/rynkrDE25)
2. [角色(枚举)、角色(表)](https://hackmd.io/@cOdEthEfUckUp/ryuOFwN35)
3. [权限(表)](https://hackmd.io/@cOdEthEfUckUp/BJyMsD435)
4. [产品(表)](https://hackmd.io/@cOdEthEfUckUp/B1jM3wV29)
5. [产品图(表)](https://hackmd.io/@cOdEthEfUckUp/rkdJTDNhq)
6. [产品标签(表)](https://hackmd.io/@cOdEthEfUckUp/SJ30pDE25)
7. [产品分类(表)](https://hackmd.io/@cOdEthEfUckUp/rytTRvVhq)
8. [订单(表)](https://hackmd.io/@cOdEthEfUckUp/HysmZuNn5)
9. [订单明细(表)](https://hackmd.io/@cOdEthEfUckUp/SyoOMuV35)
