Last active
July 14, 2023 21:40
-
-
Save nspalo/d5533d0113db50221d03dd62820ebae2 to your computer and use it in GitHub Desktop.
A Meme regarding Hands and Fingers translated to SQL LOL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Meme regarding Hands and Fingers translated to SQL LOL | |
* - MySQL v7.4.13 | |
*/ | |
/** | |
* Create Database | |
*/ | |
CREATE DATABASE IF NOT EXISTS `hands_fingers_meme` | |
CHARACTER SET `utf8mb4` | |
COLLATE `utf8mb4_unicode_ci` | |
; | |
USE `hands_fingers_meme`; | |
/** | |
* Create Tables | |
*/ | |
CREATE TABLE IF NOT EXISTS `hands` ( | |
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, | |
`name` VARCHAR(32) NOT NULL, | |
PRIMARY KEY (`id`) | |
); | |
CREATE TABLE IF NOT EXISTS `fingers` ( | |
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, | |
`name` VARCHAR(32) NOT NULL, | |
`number` TINYINT(1) UNSIGNED NOT NULL, | |
`hand_id` INT UNSIGNED NOT NULL, | |
PRIMARY KEY (`id`), | |
FOREIGN KEY (`hand_id`) REFERENCES `hands` (`id`) | |
); | |
/** | |
* Populate Data | |
*/ | |
INSERT INTO `hands`(`name`) | |
VALUES | |
('right'), | |
('left') | |
; | |
INSERT INTO `fingers`(`name`, `number`, `hand_id`) | |
VALUES | |
('thumb', 1, 1), | |
('index', 2, 1), | |
('middle', 3, 1), | |
('ring', 4, 1), | |
('pinky', 5, 1), | |
('thumb', 1, 2), | |
('index', 2, 2), | |
('middle', 3, 2), | |
('ring', 4, 2), | |
('pinky', 5, 2) | |
; | |
/** | |
* Create Helper Function | |
*/ | |
DROP FUNCTION IF EXISTS ucfirst; | |
DELIMITER $$ | |
CREATE FUNCTION ucfirst(str_value VARCHAR(5000)) | |
RETURNS VARCHAR(5000) | |
DETERMINISTIC | |
BEGIN | |
RETURN CONCAT( | |
UCASE(LEFT(str_value, 1)), SUBSTRING(str_value, 2) | |
); | |
END | |
$$ | |
DELIMITER ; | |
/** | |
* Queries | |
*/ | |
-- Query Sample 1 | |
SELECT | |
ucfirst(`hands`.`name`) AS `Hand`, | |
ucfirst(`fingers`.`name`) AS `Finger` | |
FROM `hands` | |
INNER JOIN `fingers` | |
ON `hands`.`id` = `fingers`.`hand_id` | |
WHERE `hands`.`id` = 1 | |
AND `fingers`.`number` = 3 | |
; | |
-- Query Sample 2 | |
SELECT | |
CONCAT(ucfirst(`hands`.`name`), ' Hand ', ucfirst(`fingers`.`name`), ' Finger') AS `Result` | |
FROM `hands` | |
INNER JOIN `fingers` | |
ON `hands`.`id` = `fingers`.`hand_id` | |
WHERE `hands`.`id` = 1 | |
AND `fingers`.`number` = 3 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment