Advertisement
yarin0600

כארמל

Nov 7th, 2023
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.15 KB | None | 0 0
  1. CREATE TABLE Users(
  2.         id int PRIMARY KEY,
  3.         name varchar(255),
  4.         address varchar(255),
  5.         email varchar(255)
  6.     );
  7.  
  8. CREATE TABLE Orders(
  9.         id int PRIMARY KEY,
  10.         user_id int,
  11.         total_price double,
  12.         FOREIGN KEY (user_id) REFERENCES Users(id)
  13.     );
  14.  
  15.  
  16. CREATE TABLE Products(
  17.         serial_code int PRIMARY KEY,
  18.         name varchar(255),
  19.         description TEXT,
  20.         price double,
  21.         quantity int,
  22.         size varchar(255)
  23.     );
  24.  
  25.  
  26. CREATE TABLE Order_Products(
  27.         id int,
  28.         user_id int,
  29.         serial_code int,
  30.         PRIMARY KEY (id, serial_code)
  31.     );
  32.  
  33.  
  34.  
  35.  
  36. -- Users
  37. INSERT INTO Users (id, name, address, email)
  38. VALUES
  39.     (1, 'John Doe', '123 Main St, City, Country', '[email protected]'),
  40.     (2, 'Jane Smith', '456 Elm St, City, Country', '[email protected]'),
  41.     (3, 'Bob Johnson', '789 Oak St, City, Country', '[email protected]');
  42.  
  43. -- Products
  44. INSERT INTO Products (serial_code, name, description, price, quantity, size)
  45. VALUES
  46.     (101, 'Product A', 'This is product A', 50.2, 100, 'Medium'),
  47.     (102, 'Product B', 'Product B description', 30.6, 50, 'Large'),
  48.     (103, 'Product C', 'Product C details', 49.99 ,75, 'Small');
  49.  
  50. -- Orders
  51. INSERT INTO Orders (id, user_id, total_price)
  52. VALUES
  53.     (1, 1, 150.99),
  54.     (2, 2, 75.50),
  55.     (3, 3, 210.25);
  56.  
  57. -- Order_Products
  58. INSERT INTO Order_Products (id, user_id, serial_code)
  59. VALUES
  60.     (1, 1, 101),
  61.     (2, 1, 102),
  62.     (3, 2, 101),
  63.     (4, 3, 101),
  64.     (5, 3, 103);
  65.  
  66.  
  67. 1. we get order_id = 6, user_id = 1, product_id_1 = 101, product_id_2 = 102
  68.    update the database:
  69.        
  70.         First updating Order_Products:
  71.             INSERT INTO Order_Products (id, user_id, serial_code)
  72.                 VALUES (6, 1, 101), (6, 1, 102);
  73.        
  74.         Then update Orders:
  75.         A.  INSERT INTO Orders (id, user_id, total_price)
  76.             SELECT 6, 1, sum(price) from Products where serial_code =
  77.                                         (SELECT serial_code from Order_Products where id = 6);
  78.                                        
  79.         B.  INSERT INTO Orders (id, user_id, total_price)
  80.             SELECT 6, 1, sum(price) from Products where Products.serial_code IN
  81.                                     (SELECT serial_code FROM Order_Products WHERE id = 6);
  82.                                        
  83.                                        
  84.     Ask Carmel what is the difference between A. and B. and what is 'IN'?
  85.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement