-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcashback_api_data_base.sql
71 lines (55 loc) · 1.83 KB
/
cashback_api_data_base.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-- select * from orders
CREATE DATABASE cashback_api
WITH
OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'Portuguese_Brazil.1252'
LC_CTYPE = 'Portuguese_Brazil.1252'
TABLESPACE = pg_default
CONNECTION LIMIT = -1
IS_TEMPLATE = False;
CREATE TABLE customers (
_id character varying(40) NOT NULL PRIMARY KEY,
name character varying(40) NOT NULL,
cpf character varying(40) NOT NULL,
city character varying(40) NOT NULL,
phone character varying(40) NOT NULL,
cashback real DEFAULT 0,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
deleted_at timestamp with time zone
);
CREATE TABLE orders (
_id character varying(40) NOT NULL PRIMARY KEY,
customer_id character varying(40) NOT NULL,
total REAL NOT NULL,
total_with_discount REAL NOT NULL,
cashback REAL NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
deleted_at timestamp with time zone
);
CREATE TABLE users (
_id character varying(40) NOT NULL PRIMARY KEY,
name character varying(100) NOT NULL,
email character varying(100) NOT NULL,
password character varying(200) NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
deleted_at timestamp with time zone
);
ALTER TABLE ONLY orders
ADD CONSTRAINT fk_orders_customers FOREIGN KEY (customer_id) REFERENCES customers;
-- SELECT * FROM customers ORDER BY created_at DESC
-- SELECT * FROM orders
SELECT
orders._id as order_id,
orders.customer_id,
customers.name as customer_name,
orders.total,
orders.total_with_discount,
orders.cashback,
orders.created_at as order_date
FROM orders INNER JOIN customers ON orders.customer_id = customers._id
-- WHERE orders.deleted_at is null
ORDER BY orders.created_at DESC