Commandes memo
Tables
Show Table Schema
DESCRIBE TABLE_NAME;
Create table
CREATE TABLE nom_de_la_table
(
colonne1 type_donnees,
colonne2 type_donnees,
colonne3 type_donnees,
colonne4 type_donnees
)
Users & Permissions
Create User & Del User
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
DROP USER ‘username’@‘localhost’;
Users Password
Change Password User
UPDATE mysql.user SET Password=PASSWORD('new-password-here') WHERE USER='user-name-here' AND Host='host-name-here';
Privileges
Add Privileges:
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
- ALL PRIVILEGES- as we saw previously, this would allow a MySQL user full access to a designated database (or if no database is selected, global access across the system)
- CREATE- allows them to create new tables or databases
- DROP- allows them to them to delete tables or databases
- DELETE- allows them to delete rows from tables
- SERT- allows them to insert rows into tables
- SELECT- allows them to use the SELECT command to read through databases
- UPDATE- allow them to update table rows
- GRANT OPTION- allows them to grant or remove other users' privileges
Revoke Privileges
REVOKE type_of_permission ON database_name.table_name FROM ‘username’@‘localhost’;
Show user privileges
SHOW GRANTS username;
Memos Divers
Insérer des données dans une table, en récupérant des valeur de cette table avec une condition, et ajouter une valeur fixe.
Par exemple, si j'ai une table mappant l'id d'un utilisateur avec l'id d'un objet stocké dans une autre table, afin de relier les 2.
Je veux ajouter dans ma table "user_object" des valeurs, où je copie les droits d'un autre user. Je récupère dans la table toutes les valeurs avec l'id de cet user(id 1), et j'ajoute les données dans la table en me basant sur cette requête + l'id de l'user que je veux ajouter (id 2).
INSERT INTO user_object (id_user, id_object) SELECT id_user,2 FROM user_object WHERE value2=1
Docs
→ SQL.SH Create Tables
→ How To Create a New User and Grant Permissions in MySQL
→ MySQL Change a User Password
→ Mémo SQL pour la Data Science
→ Jointure SQL
→ CheatSheet SQL