Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. docker run -it -e POSTGRES_USER=username -e POSTGRES_PASSWORD=password postgres:11.2-alpine
  2. docker container ls
  3. docker exec -it b77fe8a3722d bash
  4.  
  5. psql -U username
  6.  
  7. CREATE DATABASE ecommerce;
  8.  
  9. CREATE TABLE users (id SERIAL, name TEXT);
  10. CREATE TABLE adresses (id SERIAL, numero INTEGER, street TEXT, postal_code INTEGER, town TEXT, user_id INTEGER);
  11. CREATE TABLE products (id SERIAL, name TEXT, description TEXT);
  12. CREATE TABLE Orders (id SERIAL, order_date TIMESTAMP, amount INTEGER, user_id INTEGER);
  13. CREATE TABLE order_products (order_id INTEGER, product_id INTEGER);
  14.  
  15. INSERT INTO users (name, genre) VALUES ('Adeline', 'F');
  16. INSERT INTO adresses (numero, street, postal_code, town, user_id) VALUES (3, 'rue Napoleon', 75001, 'Paris', 1);
  17. INSERT INTO products (name, description) VALUES ('Pull', 'Pull gris'), ('Tshirt', 'Tshirt blanc'), ('Jean', 'Jean brut'), ('Chaussettes', 'Chaussettes noires'), ('Pantalon', 'Pantalon noir'), ('Jupe', 'Jupe bleue');
  18. INSERT INTO orders (order_date, amount, user_id) VALUES ('01/18/2019', 58, 1);
  19. INSERT INTO order_products (order_id, product_id) VALUES (1, 2), (1,4), (1,3);
  20.  
  21. - Donnez les users qui ont des commandes
  22. SELECT (name) FROM users u INNER JOIN orders o ON u.id = o.user_id GROUP BY (name);
  23. - Donnez les users qui n'ont pas de commandes
  24. SELECT (name) FROM users u LEFT JOIN orders o ON u.id = o.user_id WHERE o.user_id IS NULL GROUP BY (name);
  25. - Donnez les adresses des users qui ont commandé
  26. SELECT a.* FROM adresses a INNER JOIN orders o ON a.user_id = o.user_id;
  27. - Donnez le nombre de commande par mois
  28. select date_part('month', order_date) as month, count(id) from orders GROUP BY (month);
  29. - Donnez la moyenne des montants de commandes
  30. select avg(amount) from orders;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement