forked from mouredev/hello-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Conceptos avanzados
- Loading branch information
Showing
7 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
INDEX | ||
Clase 6: https://twitch.tv/videos/1997815022 | ||
*/ | ||
|
||
-- Crea un índice llamado "idx_name" en la tabla "users" asociado al campo "name" | ||
CREATE INDEX idx_name ON users(name); | ||
|
||
-- Crea un índice único llamado "idx_name" en la tabla "users" asociado al campo "name" | ||
CREATE UNIQUE INDEX idx_name ON users(name); | ||
|
||
-- Crea un índice llamado "idx_name_surname" en la tabla "users" asociado a los campos "name" y "surname" | ||
CREATE UNIQUE INDEX idx_name_surname ON users(name, surname); | ||
|
||
-- Elimina el índice llamado "idx_name" | ||
DROP INDEX idx_name ON users; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
TRIGGERS | ||
Clase 6: https://twitch.tv/videos/1997815022 | ||
*/ | ||
|
||
-- Crea una tabla de historial para usar en el ejemplo | ||
CREATE TABLE `hello_mysql`.`email_history` ( | ||
`email_history_id` INT NOT NULL AUTO_INCREMENT, | ||
`user_id` INT NOT NULL, | ||
`email` VARCHAR(100) NULL, | ||
PRIMARY KEY (`email_history_id`), | ||
UNIQUE INDEX `email_history_id_UNIQUE` (`email_history_id` ASC) VISIBLE); | ||
|
||
-- Crea un trigger llamado "tg_email" que guarda el email previo en la tabla "email_history" siempre | ||
-- que se actualiza el campo "email" en la tabla "users" | ||
|
||
-- DELIMITER es una directiva que sirve para cambiar el delimitador de instrucciones SQL, que por defecto es ; | ||
-- Se utiliza cuando se define un bloque de código como un procedimiento donde se requieren múltiples | ||
-- instrucciones SQL terminadas con punto y coma dentro de un mismo bloque. | ||
DELIMITER // | ||
CREATE TRIGGER tg_email | ||
AFTER UPDATE ON users | ||
FOR EACH ROW | ||
BEGIN | ||
IF OLD.email <> NEW.email THEN | ||
INSERT INTO email_history (user_id, email) | ||
VALUES (OLD.user_id, OLD.email); | ||
END IF; | ||
END// | ||
|
||
-- Actualiza el campo "email" del usuario 1 la tabla "users" para probar el trigger | ||
UPDATE users SET email = '[email protected]' WHERE user_id = 1 | ||
|
||
-- Elimina el trigger llamado "tg_email" | ||
DROP TRIGGER tg_email; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
VIEWS | ||
Clase 6: https://twitch.tv/videos/1997815022 | ||
*/ | ||
|
||
-- Crea unaa vista llamada "v_adult_users" con los nombres y edades de usuarios de la table "users" | ||
-- que tienen una edad igual o mayor a 18 años. | ||
CREATE VIEW v_adult_users AS | ||
SELECT name, age | ||
FROM users | ||
WHERE age >= 18; | ||
|
||
SELECT * FROM v_adult_users; | ||
|
||
-- Elimina la vista llamada "v_adult_users" | ||
DROP VIEW v_adult_users; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
STORED PROCEDURES | ||
Clase 6: https://twitch.tv/videos/1997815022 | ||
*/ | ||
|
||
-- Crea un procedimiento almacenado llamado "p_all_users" que obtiene todos los datos de "users" | ||
DELIMITER // | ||
CREATE PROCEDURE p_all_users() | ||
BEGIN | ||
SELECT * FROM users; | ||
END// | ||
|
||
-- Invoca al procedimiento almacenado llamado "p_all_users" | ||
CALL p_all_users; | ||
|
||
-- Crea un procedimiento almacenado llamado "p_age_users" parametrizado para | ||
-- obtener usuarios con edad variable | ||
DELIMITER // | ||
CREATE PROCEDURE p_age_users(IN age_param int) | ||
BEGIN | ||
SELECT * FROM users WHERE age = age_param; | ||
END// | ||
|
||
-- Invoca al procedimiento almacenado llamado "p_age_users" con un parámetro de valor 30 | ||
CALL p_age_users(30); | ||
|
||
-- Elimina el procedimiento almacenado llamado "p_age_users" | ||
DROP PROCEDURE p_age_users; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
TRANSACTIONS | ||
Clase 6: https://twitch.tv/videos/1997815022 | ||
*/ | ||
|
||
-- Inicia una nueva transacción. Desde este punto, todas las modificaciones realizadas en la | ||
-- base de datos son temporales y solo son visibles dentro de esta transacción | ||
START TRANSACTION | ||
|
||
-- Finaliza una transacción con éxito. Cuando se ejecuta, todos los cambios realizados en la | ||
-- base de datos durante la transacción actual se hacen permanentes y visibles | ||
COMMIT | ||
|
||
-- Deshace las operaciones realizadas en una transacción, revirtiendo la base de datos | ||
-- al estado en que se encontraba antes de iniciar la transacción | ||
ROLLBACK |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# CONNECTORS | ||
# Clase 6: https://twitch.tv/videos/1997815022 | ||
|
||
# Ejemplo de conexión desde Python a una base de datos local | ||
# Se ejemplifica cómo evitar SQL INJECTION | ||
import mysql.connector | ||
|
||
|
||
def print_user(user): | ||
|
||
config = { | ||
"host": "127.0.0.1", | ||
"port": "3306", | ||
"database": "hello_mysql", | ||
"user": "root", | ||
"password": "root1234" | ||
} | ||
|
||
connection = mysql.connector.connect(**config) | ||
cursor = connection.cursor() | ||
|
||
query = "SELECT * FROM users WHERE name=%s;" | ||
print(query) | ||
cursor.execute(query, (user,)) | ||
result = cursor.fetchall() | ||
|
||
for row in result: | ||
print(row) | ||
|
||
cursor.close() | ||
connection.close() | ||
|
||
|
||
print_user("Brais") | ||
# print_user("'; UPDATE users SET age = '15' WHERE user_id = 1; --") |
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