SlideShare a Scribd company logo
1 of 6
Download to read offline
Adolfo Nasol

MYSQL CHEAT SHEET - PART 1

http://cavhost.com

Short List of MySQL Commands
Conventions used here:
MySQL key words are shown in CAPS
User-specified names are in small letters
Optional items are enclosed in square brackets [ ]
Items in parentheses must appear in the command, along with the parentheses
Items that can be repeated as often as desired are indicated by an ellipsis ...
Quoting in MySQL statments
Don't quote database, table, or column names
Don't quote column types or modifiers
Don't quote numerical values
Quote (single or double) non-numeric values
Quote file names and passwords
User names are NOT quoted in GRANT or REVOKE statements, but they are
quoted in other statements.

General Commands
USE database_name
Change to this database. You need to change to some database when you first
connect to MySQL.
SHOW DATABASES
Lists all MySQL databases on the system.
SHOW TABLES [FROM database_name]
Lists all tables from the current database or from the database given in the
command.
DESCRIBE table_name
SHOW FIELDS FROM table_name
SHOW COLUMNS FROM table_name

1 of 6

http://danreb.com

Cavhost Web Solutions
Adolfo Nasol

MYSQL CHEAT SHEET - PART 1

http://cavhost.com

These commands all give a list of all columns (fields) from the given table, along
with column type and other info.
SHOW INDEX FROM table_name
Lists all indexes from this tables.
SET PASSWORD=PASSWORD('new_password')
Allows the user to set his/her own password.

Table Commands
CREATE TABLE table_name (create_clause1, create_clause2, ...)
Creates a table with columns as indicated in the create clauses.
create_clause
column name followed by column type, followed optionally by modifiers. For
example, "gene_id INT AUTO_INCREMENT PRIMARY KEY" (without the quotes)
creates a column of type integer with the modifiers described below.
create_clause modifiers
AUTO_INCREMENT : each data record is assigned the next sequential
number when it is given a NULL value.
PRIMARY KEY : Items in this column have unique names, and the table is
indexed automatically based on this column. One column must be the
PRIMARY KEY, and only one column may be the PRIMARY KEY. This column
should also be NOT NULL.
NOT NULL : No NULL values are allowed in this column: a NULL generates
an error message as the data is inserted into the table.
DEFAULT value : If a NULL value is used in the data for this column, the
default value is entered instead.

DROP TABLE table_name
Removes the table from the database. Permanently! So be careful with this
command!
ALTER TABLE table_name ADD (create_clause1, create_clause2, ...)
Adds the listed columns to the table.

2 of 6

http://danreb.com

Cavhost Web Solutions
Adolfo Nasol

MYSQL CHEAT SHEET - PART 1

http://cavhost.com

ALTER TABLE table_name DROP column_name
Drops the listed columns from the table.
ALTER TABLE table_name MODIFY create_clause
Changes the type or modifiers to a column. Using MODIFY means that the column
keeps the same name even though its type is altered. MySQL attempts to convert
the data to match the new type: this can cause problems.
ALTER TABLE table_name CHANGE column_name create_clause
Changes the name and type or modifiers of a column. Using CHANGE (instead of
MODIFY) implies that the column is getting a new name.
ALTER TABLE table_name ADD INDEX [index_name] (column_name1,
column_name2, ...)
CREATE INDEX index_name ON table_name (column_name1, column_name2, ...)
Adds an index to this table, based on the listed columns. Note that the order of the
columns is important, because additional indexes are created from all subsets of
the listed columns reading from left to write. The index name is optional if you
use ALTER TABLE, but it is necesary if you use CREATE INDEX. Rarely is the name
of an index useful (in my experience).

Data Commands
INSERT [INTO] table_name VALUES (value1, value2, ...)
Insert a complete row of data, giving a value (or NULL) for every column in the
proper order.
INSERT [INTO] table_name (column_name1, column_name2, ...) VALUES (value1,
value2, ...)
INSERT [INTO] table_name SET column_name1=value1, column_name2=value2,
...
Insert data into the listed columns only. Alternate forms, with the SET form
showing column assignments more explicitly.
INSERT [INTO] table_name (column_name1, column_name2, ...) SELECT
list_of_fields_from_another_table FROM other_table_name WHERE where_clause

3 of 6

http://danreb.com

Cavhost Web Solutions
Adolfo Nasol

MYSQL CHEAT SHEET - PART 1

http://cavhost.com

Inserts the data resulting from a SELECT statement into the listed columns. Be
sure the number of items taken from the old table match the number of columns
they are put into!
DELETE FROM table_name WHERE where_clause
Delete rows that meet the conditions of the where_clause. If the WHERE statement
is omitted, the table is emptied, although its structure remains intact.
UPDATE table_name SET column_name1=value1, column_name2=value2, ...
[WHERE where_clause]
Alters the data within a column based on the conditions in the where_clause.
LOAD DATA LOCAL INFILE 'path to external file' INTO TABLE table_name
Loads data from the listed file into the table. The default assumption is that fields
in the file are separated by tabs, and each data record is separated from the others
by a newline. It also assumes that nothing is quoted: quote marks are considered
to be part of the data. Also, it assumes that the number of data fields matches the
number of table columns. Columns that are AUTO_INCREMENT should have NULL
as their value in the file.
LOAD DATA LOCAL INFILE 'path to external file' [FIELDS TERMINATED BY
'termination_character'] [FIELDS ENCLOSED BY 'quoting character'] [LINES
TERMINATED BY 'line termination character'] FROM table_name
Loads data from the listed file into the table, using the field termination character
listed (default is tab t), and/or the listed quoting character (default is nothing),
and/or the listed line termination chacracter (default is a newline n).
SELECT column_name1, column_name2, ... INTO OUTFILE 'path to external file'
[FIELDS TERMINATED BY 'termination_character'] [FIELDS ENCLOSED BY
'quoting character'] [LINES TERMINATED BY 'line termination character'] FROM
table_name [WHERE where_clause]
Allows you to move data from a table into an external file. The field and line
termination clauses are the same as for LOAD above. Several tricky features:
1. Note the positions of the table_name and where_clause, after the external file
is given.
2. You must use a complete path, not just a file name. Otherwise MySQL
attempts to write to the directory where the database is stored, where you
don't have permission to write.
3. The user who is writing the file is 'mysql', not you! This means that user
'mysql' needs permission to write to the directory you specify. The best way
4 of 6

http://danreb.com

Cavhost Web Solutions
Adolfo Nasol

MYSQL CHEAT SHEET - PART 1

http://cavhost.com

to do that is to creat a new directory under your home directory, then change
the directory's permission to 777, then write to it. For example: mkdir
mysql_output, chmod 777 mysql_output.

Privilege Commands
Most of the commands below require MySQL root access
GRANT USAGE ON *.* TO user_name@localhost [IDENTIFIED BY 'password']
Creates a new user on MySQL, with no rights to do anything. The IDENTIFED BY
clause creates or changes the MySQL password, which is not necessarily the same
as the user's system password. The @localhost after the user name allows usage
on the local system, which is usually what we do; leaving this off allows the user
to access the database from another system. User name NOT in quotes.
GRANT SELECT ON *.* TO user_name@localhost
In general, unless data is supposed to be kept private, all users should be able to
view it. A debatable point, and most databases will only grant SELECT privileges
on particular databases. There is no way to grant privileges on all databses
EXCEPT specifically enumerated ones.
GRANT ALL ON database_name.* TO user_name@localhost
Grants permissions on all tables for a specific database (database_name.*) to a
user. Permissions are for: ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT,
UPDATE.
FLUSH PRIVILEGES
Needed to get updated privileges to work immediately. You need RELOAD
privileges to get this to work.
SET PASSWORD=PASSWORD('new_password')
Allows the user to set his/her own password.
REVOKE ALL ON [database_name.]* FROM user_name@localhost
Revokes all permissions for the user, but leaves the user in the MySQL database.
This can be done for all databases using "ON *", or for all tables within a specific
databse, using "ON database_name.*".
5 of 6

http://danreb.com

Cavhost Web Solutions
Adolfo Nasol

MYSQL CHEAT SHEET - PART 1

http://cavhost.com

DELETE FROM mysql.user WHERE user='user_name@localhost'
Removes the user from the database, which revokes all privileges. Note that the
user name is in quotes here.
UPDATE mysql.user SET password=PASSWORD('my_password') WHERE
user='user_name'
Sets the user's password. The PASSWORD function encrypts it; otherwise it will be
in plain text.
SELECT user, host, password, select_priv, insert_priv, shutdown_priv, grant_priv
FROM mysql.user
A good view of all users and their approximate privileges. If there is a password, it
will by an encrytped string; if not, this field is blank. Select is a very general
privlege; insert allows table manipulation within a database; shutdown allows
major system changes, and should only be usable by root; the ability to grant
permissions is separate from the others.
SELECT user, host, db, select_priv, insert_priv, grant_priv FROM mysql.db
View permissions for individual databases.

6 of 6

http://danreb.com

Cavhost Web Solutions

More Related Content

What's hot (20)

Module 3
Module 3Module 3
Module 3
 
Ch05
Ch05Ch05
Ch05
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Sql commands
Sql commandsSql commands
Sql commands
 
Ddl commands
Ddl commandsDdl commands
Ddl commands
 
Sq lite
Sq liteSq lite
Sq lite
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)
 
ADBMS Unit-II c
ADBMS Unit-II cADBMS Unit-II c
ADBMS Unit-II c
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Mysql database
Mysql databaseMysql database
Mysql database
 

Viewers also liked

Event Registration System Part 2
Event Registration System Part 2Event Registration System Part 2
Event Registration System Part 2Adolfo Nasol
 
Creating Drupal 7 subtheme
Creating Drupal 7 subthemeCreating Drupal 7 subtheme
Creating Drupal 7 subthemeAdolfo Nasol
 
Mysql cheatsheet - Part 2
Mysql cheatsheet - Part 2Mysql cheatsheet - Part 2
Mysql cheatsheet - Part 2Adolfo Nasol
 
Drupal debugging tips
Drupal debugging tipsDrupal debugging tips
Drupal debugging tipsAdolfo Nasol
 
Protocolo captura 2011_eu
Protocolo captura 2011_euProtocolo captura 2011_eu
Protocolo captura 2011_euKatalogador
 
AFO in missione - Seconda parte
AFO in missione - Seconda parteAFO in missione - Seconda parte
AFO in missione - Seconda parteMaike Loes
 
Power writing
Power writingPower writing
Power writingkatled
 
Formulación inorgánica
Formulación inorgánicaFormulación inorgánica
Formulación inorgánicabesteiroalonso
 
Hw to factor
Hw to factorHw to factor
Hw to factor41142391
 
FMP Storyboard
FMP StoryboardFMP Storyboard
FMP Storyboardrnesbit
 
Mavaca - Venezuela
Mavaca - VenezuelaMavaca - Venezuela
Mavaca - VenezuelaMaike Loes
 
Lectionline ii domenica del t o anno b
Lectionline ii domenica del t o anno bLectionline ii domenica del t o anno b
Lectionline ii domenica del t o anno bMaike Loes
 
Clayton Cox, Mike McWilliams, Brian Jestice, and Jake Klein's Cup project
Clayton Cox, Mike McWilliams, Brian Jestice, and Jake Klein's Cup projectClayton Cox, Mike McWilliams, Brian Jestice, and Jake Klein's Cup project
Clayton Cox, Mike McWilliams, Brian Jestice, and Jake Klein's Cup projectLostHunter
 
Giornata migranti rifugiati 2014
Giornata migranti rifugiati   2014Giornata migranti rifugiati   2014
Giornata migranti rifugiati 2014Maike Loes
 

Viewers also liked (20)

Event Registration System Part 2
Event Registration System Part 2Event Registration System Part 2
Event Registration System Part 2
 
Creating Drupal 7 subtheme
Creating Drupal 7 subthemeCreating Drupal 7 subtheme
Creating Drupal 7 subtheme
 
Mysql cheatsheet - Part 2
Mysql cheatsheet - Part 2Mysql cheatsheet - Part 2
Mysql cheatsheet - Part 2
 
Drupal debugging tips
Drupal debugging tipsDrupal debugging tips
Drupal debugging tips
 
Protocolo captura 2011_eu
Protocolo captura 2011_euProtocolo captura 2011_eu
Protocolo captura 2011_eu
 
US Strategy Weekly
US Strategy Weekly US Strategy Weekly
US Strategy Weekly
 
AFO in missione - Seconda parte
AFO in missione - Seconda parteAFO in missione - Seconda parte
AFO in missione - Seconda parte
 
Power writing
Power writingPower writing
Power writing
 
Formulación inorgánica
Formulación inorgánicaFormulación inorgánica
Formulación inorgánica
 
Hw to factor
Hw to factorHw to factor
Hw to factor
 
FMP Storyboard
FMP StoryboardFMP Storyboard
FMP Storyboard
 
Mavaca - Venezuela
Mavaca - VenezuelaMavaca - Venezuela
Mavaca - Venezuela
 
Lectionline ii domenica del t o anno b
Lectionline ii domenica del t o anno bLectionline ii domenica del t o anno b
Lectionline ii domenica del t o anno b
 
sabrina
sabrinasabrina
sabrina
 
Clayton Cox, Mike McWilliams, Brian Jestice, and Jake Klein's Cup project
Clayton Cox, Mike McWilliams, Brian Jestice, and Jake Klein's Cup projectClayton Cox, Mike McWilliams, Brian Jestice, and Jake Klein's Cup project
Clayton Cox, Mike McWilliams, Brian Jestice, and Jake Klein's Cup project
 
arguments
argumentsarguments
arguments
 
Scuola dante alighieri
Scuola dante alighieriScuola dante alighieri
Scuola dante alighieri
 
Balconades i finestres de terrassa
Balconades i finestres de terrassaBalconades i finestres de terrassa
Balconades i finestres de terrassa
 
Giornata migranti rifugiati 2014
Giornata migranti rifugiati   2014Giornata migranti rifugiati   2014
Giornata migranti rifugiati 2014
 
Pareidolia
PareidoliaPareidolia
Pareidolia
 

Similar to Mysql cheatsheet (20)

MYSQL
MYSQLMYSQL
MYSQL
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
My sql with querys
My sql with querysMy sql with querys
My sql with querys
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
Sql smart reference_by_prasad
Sql smart reference_by_prasadSql smart reference_by_prasad
Sql smart reference_by_prasad
 
Introduction to MySQL - Part 2
Introduction to MySQL - Part 2Introduction to MySQL - Part 2
Introduction to MySQL - Part 2
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
Les10
Les10Les10
Les10
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql
SqlSql
Sql
 
My sql Syntax
My sql SyntaxMy sql Syntax
My sql Syntax
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 
mySQL and Relational Databases
mySQL and Relational DatabasesmySQL and Relational Databases
mySQL and Relational Databases
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
sql.pptx
sql.pptxsql.pptx
sql.pptx
 
SQL Query
SQL QuerySQL Query
SQL Query
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Sql basics
Sql basicsSql basics
Sql basics
 

More from Adolfo Nasol

Managing drupal views in code
Managing drupal views in codeManaging drupal views in code
Managing drupal views in codeAdolfo Nasol
 
Events Registration System Part 1
Events Registration System Part 1Events Registration System Part 1
Events Registration System Part 1Adolfo Nasol
 
Installing mandriva linux mandriva community wiki
Installing mandriva linux   mandriva community wikiInstalling mandriva linux   mandriva community wiki
Installing mandriva linux mandriva community wikiAdolfo Nasol
 
Drush for drupal website builder
Drush for drupal website builderDrush for drupal website builder
Drush for drupal website builderAdolfo Nasol
 
Converting (X)HTML/CSS template to Drupal 7 Theme
Converting (X)HTML/CSS template to Drupal 7 ThemeConverting (X)HTML/CSS template to Drupal 7 Theme
Converting (X)HTML/CSS template to Drupal 7 ThemeAdolfo Nasol
 
Chs nc2 reviewer - with oral questioning
Chs nc2 reviewer - with oral questioningChs nc2 reviewer - with oral questioning
Chs nc2 reviewer - with oral questioningAdolfo Nasol
 
Operating System Concepts : Reports
Operating System Concepts : ReportsOperating System Concepts : Reports
Operating System Concepts : ReportsAdolfo Nasol
 
Drupal Checklist for Site Builder and Web admin
Drupal Checklist for Site Builder and Web adminDrupal Checklist for Site Builder and Web admin
Drupal Checklist for Site Builder and Web adminAdolfo Nasol
 

More from Adolfo Nasol (10)

Managing drupal views in code
Managing drupal views in codeManaging drupal views in code
Managing drupal views in code
 
Events Registration System Part 1
Events Registration System Part 1Events Registration System Part 1
Events Registration System Part 1
 
Installing mandriva linux mandriva community wiki
Installing mandriva linux   mandriva community wikiInstalling mandriva linux   mandriva community wiki
Installing mandriva linux mandriva community wiki
 
Drush for drupal website builder
Drush for drupal website builderDrush for drupal website builder
Drush for drupal website builder
 
Converting (X)HTML/CSS template to Drupal 7 Theme
Converting (X)HTML/CSS template to Drupal 7 ThemeConverting (X)HTML/CSS template to Drupal 7 Theme
Converting (X)HTML/CSS template to Drupal 7 Theme
 
Research methods
Research methodsResearch methods
Research methods
 
Personality
PersonalityPersonality
Personality
 
Chs nc2 reviewer - with oral questioning
Chs nc2 reviewer - with oral questioningChs nc2 reviewer - with oral questioning
Chs nc2 reviewer - with oral questioning
 
Operating System Concepts : Reports
Operating System Concepts : ReportsOperating System Concepts : Reports
Operating System Concepts : Reports
 
Drupal Checklist for Site Builder and Web admin
Drupal Checklist for Site Builder and Web adminDrupal Checklist for Site Builder and Web admin
Drupal Checklist for Site Builder and Web admin
 

Recently uploaded

Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFMichael Gough
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...itnewsafrica
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...Karmanjay Verma
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Nikki Chapple
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Jeffrey Haguewood
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialJoão Esperancinha
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 

Recently uploaded (20)

Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
All These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDFAll These Sophisticated Attacks, Can We Really Detect Them - PDF
All These Sophisticated Attacks, Can We Really Detect Them - PDF
 
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
Irene Moetsana-Moeng: Stakeholders in Cybersecurity: Collaborative Defence fo...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...React JS; all concepts. Contains React Features, JSX, functional & Class comp...
React JS; all concepts. Contains React Features, JSX, functional & Class comp...
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
Microsoft 365 Copilot: How to boost your productivity with AI – Part two: Dat...
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
Email Marketing Automation for Bonterra Impact Management (fka Social Solutio...
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Kuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorialKuma Meshes Part I - The basics - A tutorial
Kuma Meshes Part I - The basics - A tutorial
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 

Mysql cheatsheet

  • 1. Adolfo Nasol MYSQL CHEAT SHEET - PART 1 http://cavhost.com Short List of MySQL Commands Conventions used here: MySQL key words are shown in CAPS User-specified names are in small letters Optional items are enclosed in square brackets [ ] Items in parentheses must appear in the command, along with the parentheses Items that can be repeated as often as desired are indicated by an ellipsis ... Quoting in MySQL statments Don't quote database, table, or column names Don't quote column types or modifiers Don't quote numerical values Quote (single or double) non-numeric values Quote file names and passwords User names are NOT quoted in GRANT or REVOKE statements, but they are quoted in other statements. General Commands USE database_name Change to this database. You need to change to some database when you first connect to MySQL. SHOW DATABASES Lists all MySQL databases on the system. SHOW TABLES [FROM database_name] Lists all tables from the current database or from the database given in the command. DESCRIBE table_name SHOW FIELDS FROM table_name SHOW COLUMNS FROM table_name 1 of 6 http://danreb.com Cavhost Web Solutions
  • 2. Adolfo Nasol MYSQL CHEAT SHEET - PART 1 http://cavhost.com These commands all give a list of all columns (fields) from the given table, along with column type and other info. SHOW INDEX FROM table_name Lists all indexes from this tables. SET PASSWORD=PASSWORD('new_password') Allows the user to set his/her own password. Table Commands CREATE TABLE table_name (create_clause1, create_clause2, ...) Creates a table with columns as indicated in the create clauses. create_clause column name followed by column type, followed optionally by modifiers. For example, "gene_id INT AUTO_INCREMENT PRIMARY KEY" (without the quotes) creates a column of type integer with the modifiers described below. create_clause modifiers AUTO_INCREMENT : each data record is assigned the next sequential number when it is given a NULL value. PRIMARY KEY : Items in this column have unique names, and the table is indexed automatically based on this column. One column must be the PRIMARY KEY, and only one column may be the PRIMARY KEY. This column should also be NOT NULL. NOT NULL : No NULL values are allowed in this column: a NULL generates an error message as the data is inserted into the table. DEFAULT value : If a NULL value is used in the data for this column, the default value is entered instead. DROP TABLE table_name Removes the table from the database. Permanently! So be careful with this command! ALTER TABLE table_name ADD (create_clause1, create_clause2, ...) Adds the listed columns to the table. 2 of 6 http://danreb.com Cavhost Web Solutions
  • 3. Adolfo Nasol MYSQL CHEAT SHEET - PART 1 http://cavhost.com ALTER TABLE table_name DROP column_name Drops the listed columns from the table. ALTER TABLE table_name MODIFY create_clause Changes the type or modifiers to a column. Using MODIFY means that the column keeps the same name even though its type is altered. MySQL attempts to convert the data to match the new type: this can cause problems. ALTER TABLE table_name CHANGE column_name create_clause Changes the name and type or modifiers of a column. Using CHANGE (instead of MODIFY) implies that the column is getting a new name. ALTER TABLE table_name ADD INDEX [index_name] (column_name1, column_name2, ...) CREATE INDEX index_name ON table_name (column_name1, column_name2, ...) Adds an index to this table, based on the listed columns. Note that the order of the columns is important, because additional indexes are created from all subsets of the listed columns reading from left to write. The index name is optional if you use ALTER TABLE, but it is necesary if you use CREATE INDEX. Rarely is the name of an index useful (in my experience). Data Commands INSERT [INTO] table_name VALUES (value1, value2, ...) Insert a complete row of data, giving a value (or NULL) for every column in the proper order. INSERT [INTO] table_name (column_name1, column_name2, ...) VALUES (value1, value2, ...) INSERT [INTO] table_name SET column_name1=value1, column_name2=value2, ... Insert data into the listed columns only. Alternate forms, with the SET form showing column assignments more explicitly. INSERT [INTO] table_name (column_name1, column_name2, ...) SELECT list_of_fields_from_another_table FROM other_table_name WHERE where_clause 3 of 6 http://danreb.com Cavhost Web Solutions
  • 4. Adolfo Nasol MYSQL CHEAT SHEET - PART 1 http://cavhost.com Inserts the data resulting from a SELECT statement into the listed columns. Be sure the number of items taken from the old table match the number of columns they are put into! DELETE FROM table_name WHERE where_clause Delete rows that meet the conditions of the where_clause. If the WHERE statement is omitted, the table is emptied, although its structure remains intact. UPDATE table_name SET column_name1=value1, column_name2=value2, ... [WHERE where_clause] Alters the data within a column based on the conditions in the where_clause. LOAD DATA LOCAL INFILE 'path to external file' INTO TABLE table_name Loads data from the listed file into the table. The default assumption is that fields in the file are separated by tabs, and each data record is separated from the others by a newline. It also assumes that nothing is quoted: quote marks are considered to be part of the data. Also, it assumes that the number of data fields matches the number of table columns. Columns that are AUTO_INCREMENT should have NULL as their value in the file. LOAD DATA LOCAL INFILE 'path to external file' [FIELDS TERMINATED BY 'termination_character'] [FIELDS ENCLOSED BY 'quoting character'] [LINES TERMINATED BY 'line termination character'] FROM table_name Loads data from the listed file into the table, using the field termination character listed (default is tab t), and/or the listed quoting character (default is nothing), and/or the listed line termination chacracter (default is a newline n). SELECT column_name1, column_name2, ... INTO OUTFILE 'path to external file' [FIELDS TERMINATED BY 'termination_character'] [FIELDS ENCLOSED BY 'quoting character'] [LINES TERMINATED BY 'line termination character'] FROM table_name [WHERE where_clause] Allows you to move data from a table into an external file. The field and line termination clauses are the same as for LOAD above. Several tricky features: 1. Note the positions of the table_name and where_clause, after the external file is given. 2. You must use a complete path, not just a file name. Otherwise MySQL attempts to write to the directory where the database is stored, where you don't have permission to write. 3. The user who is writing the file is 'mysql', not you! This means that user 'mysql' needs permission to write to the directory you specify. The best way 4 of 6 http://danreb.com Cavhost Web Solutions
  • 5. Adolfo Nasol MYSQL CHEAT SHEET - PART 1 http://cavhost.com to do that is to creat a new directory under your home directory, then change the directory's permission to 777, then write to it. For example: mkdir mysql_output, chmod 777 mysql_output. Privilege Commands Most of the commands below require MySQL root access GRANT USAGE ON *.* TO user_name@localhost [IDENTIFIED BY 'password'] Creates a new user on MySQL, with no rights to do anything. The IDENTIFED BY clause creates or changes the MySQL password, which is not necessarily the same as the user's system password. The @localhost after the user name allows usage on the local system, which is usually what we do; leaving this off allows the user to access the database from another system. User name NOT in quotes. GRANT SELECT ON *.* TO user_name@localhost In general, unless data is supposed to be kept private, all users should be able to view it. A debatable point, and most databases will only grant SELECT privileges on particular databases. There is no way to grant privileges on all databses EXCEPT specifically enumerated ones. GRANT ALL ON database_name.* TO user_name@localhost Grants permissions on all tables for a specific database (database_name.*) to a user. Permissions are for: ALTER, CREATE, DELETE, DROP, INDEX, INSERT, SELECT, UPDATE. FLUSH PRIVILEGES Needed to get updated privileges to work immediately. You need RELOAD privileges to get this to work. SET PASSWORD=PASSWORD('new_password') Allows the user to set his/her own password. REVOKE ALL ON [database_name.]* FROM user_name@localhost Revokes all permissions for the user, but leaves the user in the MySQL database. This can be done for all databases using "ON *", or for all tables within a specific databse, using "ON database_name.*". 5 of 6 http://danreb.com Cavhost Web Solutions
  • 6. Adolfo Nasol MYSQL CHEAT SHEET - PART 1 http://cavhost.com DELETE FROM mysql.user WHERE user='user_name@localhost' Removes the user from the database, which revokes all privileges. Note that the user name is in quotes here. UPDATE mysql.user SET password=PASSWORD('my_password') WHERE user='user_name' Sets the user's password. The PASSWORD function encrypts it; otherwise it will be in plain text. SELECT user, host, password, select_priv, insert_priv, shutdown_priv, grant_priv FROM mysql.user A good view of all users and their approximate privileges. If there is a password, it will by an encrytped string; if not, this field is blank. Select is a very general privlege; insert allows table manipulation within a database; shutdown allows major system changes, and should only be usable by root; the ability to grant permissions is separate from the others. SELECT user, host, db, select_priv, insert_priv, grant_priv FROM mysql.db View permissions for individual databases. 6 of 6 http://danreb.com Cavhost Web Solutions