Skip to content

Instantly share code, notes, and snippets.

View gabidavila's full-sized avatar
💁‍♀️
I try to solve all my problems with a single SQL query.

Gabriela Ferrara gabidavila

💁‍♀️
I try to solve all my problems with a single SQL query.
View GitHub Profile
SET DEFAULT ROLE ALL TO
'lisa_simpson'@'%',
'millhouse_houten'@'localhost',
'homer_simpson'@'localhost',
'millhouse_houten'@'localhost';
CREATE DATABASE `store` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE store;
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(127) NOT NULL,
`last_name` varchar(128) NOT NULL,
`email` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
CREATE TABLE `orders_items` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`order_id` int(10) unsigned NOT NULL,
`product_id` int(10) unsigned NOT NULL,
`product_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00',
`quantity` int(10) unsigned NOT NULL DEFAULT 1,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` varchar(45) NOT NULL DEFAULT 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
SELECT
`id`, `order_id`, `product_id`, `product_price`, `quantity`
FROM `orders_items`
LIMIT 5;
-- Virtual Column added on line 9
CREATE TABLE `orders_items` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`order_id` int(10) unsigned NOT NULL,
`product_id` int(10) unsigned NOT NULL,
`product_price` decimal(10,2) unsigned NOT NULL DEFAULT '0.00',
`quantity` int(10) unsigned NOT NULL DEFAULT 1,
`total_item_price` decimal(10,2) AS (`quantity` * `product_price`),
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

The basic example

As an example I am going to use an e-commerce database as based on my past experience of what I have seen and worked. You will probably have at least these tables or something similar:

  • users - stores user info
  • products - stores product info like price and description
  • orders - stores the user_id, date of order
  • orders_items - stores product_id, order_id, quantity and price at the time of purchase

This is the whole DB: Gist.

import math
number_lines = int(raw_input())
def printhalf(word):
counterLetter = 0
counterNewWord = 0
newWord = ""
for letter in word:
if(counterLetter % 2 == 0):
{
"id":765689235827556352,
"url":"https://t.co/OCnMWRDige",
"lang":"en",
"name":"Laravel SP Meetup",
"id_str":"765689235827556352",
"status":{
"id":831216568705114112,
"geo":null,
"lang":"pt",
DELIMITER $$
CREATE FUNCTION to_json_array(input_string TEXT, delimiter_char CHAR(1))
RETURNS JSON
BEGIN
RETURN CONCAT("[", TRIM(BOTH delimiter_char FROM input_string), "]");
END$$
DELIMITER ;

YOU WILL LOSE ALL DATA

sudo service mysql stop
wget https://dev.mysql.com/get/mysql-apt-config_0.8.6-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.6-1_all.deb
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt autoremove
sudo apt-get autoclean
sudo apt-get install mysql-server