SlideShare une entreprise Scribd logo
1  sur  15
Treinamento em MySQL
•XHTML        •MySQL
•CSS          •PHP
•JavaScript
Comandos de Manipulação de Dados…
•SELECT

  •SELECT 1 + 2;
  •SELECT 3 – 5;
  •SELECT 10 * 2;
  •SELECT 158 / 3;

•FROM

  •SELECT * FROM tbl;
  •SELECT * FROM tbl1, tbl2;
  •SELECT * FROM tbl1, tbl2, tbl3;
  •SELECT tbl1.col1 FROM tbl1;
  •SELECT tbl1.col1, tbl2.col2 FROM table1, table2;
  •SELECT tbl1.col1, tbl2.col2, tbl3.col3 FROM tbl1, tbl2, tbl3;
Comandos de Manipulação de Dados…
•SELECT

  •SELECT 1 + 2;
  •SELECT 3 – 5;
  •SELECT 10 * 2;
  •SELECT 158 / 3;

•FROM

  •SELECT * FROM tbl;
  •SELECT * FROM tbl1, tbl2;
  •SELECT * FROM tbl1, tbl2, tbl3;
  •SELECT tbl1.col1 FROM tbl1;
  •SELECT tbl1.col1, tbl2.col2 FROM table1, table2;
  •SELECT tbl1.col1, tbl2.col2, tbl3.col3 FROM tbl1, tbl2, tbl3;
Comandos de Manipulação de Dados…
•WHERE;

A cláusula WHERE serve para manipular os dados
de forma a exibir apenas aqueles que realmente são
úteis. Alguns exemplos seguem abaixo:

SELECT * FROM tbl1 WHERE tbl1.nome = ‘Pedro’;
SELECT * FROM tbl1, tbl2 WHERE tbl1. colRel = tbl2.colRel;
Comandos de Manipulação de Dados…
•AND/OR/XOR

  •SELECT 1 XOR NULL;
  •SELECT * FROM tbl1, tbl2 WHERE tbl1.nome = tbl2.nome AND 1 = 1;
  •SELECT * FROM tbl1, tbl2 WHERE tbl1.nome = tbl2.nome OR 1 = 1;
  •SELECT * FROM tbl1, tbl2 WHERE tbl1.nome = tbl2.nome XOR 1 = 1;

•IN

  •SELECT 2 IN (0, 3, 5, 'wefwf');
  •SELECT 'wefwf' IN (0, 3, 5, 'wefwf');
  •SELECT * FROM tbl WHERE tbl.nome IN (‘Marcus’, ‘Augusto’);
  •SELECT * FROM tbl WHERE tbl.cidade IN (‘RJ’, ‘SP’, ‘MG’, ‘ES’);
Comandos de Manipulação de Dados…
•LIKE;

A função LIKE() serve para fazer comparações usando expressões regulares
simples, de forma a fazer buscas mais gerais.A sintaxe segue abaixo:
Os caracteres mais importantes seguem abaixo:

• % qualquer caractere em qualquer quantidade, inclusive nenhum.
• _ apenas um caractere, sendo ele qualquer.
• % casa exatamente o caractere %
• _ casa exatamente o caractere _
Comandos de Manipulação de Dados…

•BETWEEN

 SELECT * FROM tbl WHERE tbl.idade BETWEEN 18 AND 21;
 SELECT * FROM tbl WHERE tbl.peso BETWEEN 50 AND 80;

 •DISTINCT

 SELECT DISTINCT * FROM tbl1;
 SELECT DISTINCT * FROM tbl1, tbl2;
Comandos de Manipulação de Dados…

•ORDER BY

  •SELECT * FROM table1 ORDER BY table1.idTbl
  •SELECT tbl1.col1 FROM table1 ORDER BY table1.idTbl;


•GROUP BY

              SELECT * FROM table1 GROUP BY
           table1.col1;
              SELECT * FROM table1 GROUP BY
           table1.col1 DESC;
Comandos de Manipulação de Dados…
•MD5

SELECT MD5('1234');
SELECT MD5(‘EJCM');

•COUNT

SELECT COUNT (pais), região FROM paises GROUP BY regiao;

•NOW
SELECT NOW();
Comandos de Manipulação de Dados…

•INSERT

INSERT INTO tbl VALUES (1, 2, 3);
INSERT INTO tbl (idTbl, col1, col2) VALUES (DEFAULT, 1, ‘c’);


•UPDATE

UPDATE tbl1 SET col1=’a’;
UPDATE tbl1 SET col1=’a’, col2=‘b’;
Comandos de Manipulação de Dados…

•DELETE

DELETE FROM tbl1 WHERE idTbl = ‘1’;
DELETE FROM tbl1 WHERE nome LIKE ‘%Pedro%’;

•SUB-SELECT

SELECT * FROM usuarios WHERE idade = (SELECT idade
FROM membros WHERE nome=”Marcus” LIMIT 1);
Comandos de Manipulação de Dados…
•UNION

SELECT [DISTINCT] [FROM table_references]
   [WHERE where_definition] [GROUP BY {col_name} [ASC | DESC], ..
   [ORDER BY {col_name} [ASC | DESC], ...]
   [LIMIT {[offset,] row_count | row_count OFFSET offset}]

   UNION

SELECT [DISTINCT] [FROM table_references]
   [WHERE where_definition] [GROUP BY {col_name} [ASC | DESC], ..
   [ORDER BY {col_name} [ASC | DESC], ...]
   [LIMIT {[offset,] row_count | row_count OFFSET offset}]
Comandos de Manipulação de Dados…
•INNER JOIN

SELECT ... FROM table_reference INNER JOIN table_reference
{ ON condição de junção }

As duas queries se equivalem

SELECT tbl1.nome, tbl2.curso FROM tbl1 INNER JOIN tbl2
ON tbl1.idCurso = tbl2.idCurso;

SELECT tbl1.nome, tbl2.curso FROM tbl1, tbl2
WHERE tbl1.idCurso = tbl2.idCurso;
Comandos de Manipulação de Dados…
•View
Uma view(visão) é uma tabela virtual na qual os dados não estão fisicamente
armazenados. Ela é apenas uma visão de um grupo de colunas de uma ou
mais tabelas do banco de dados. No exemplo abaixo, criaremos uma view
que terá apenas as colunas COD_PRODUTO, DSC_PRODUTO.

CREATE VIEW modelo VIEW_PRODUTOS AS
     SELECT cod_produto, dsc_produto FROM
produtos;

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Treinamento MySQL: Comandos de Manipulação de Dados

  • 2. •XHTML •MySQL •CSS •PHP •JavaScript
  • 3. Comandos de Manipulação de Dados… •SELECT •SELECT 1 + 2; •SELECT 3 – 5; •SELECT 10 * 2; •SELECT 158 / 3; •FROM •SELECT * FROM tbl; •SELECT * FROM tbl1, tbl2; •SELECT * FROM tbl1, tbl2, tbl3; •SELECT tbl1.col1 FROM tbl1; •SELECT tbl1.col1, tbl2.col2 FROM table1, table2; •SELECT tbl1.col1, tbl2.col2, tbl3.col3 FROM tbl1, tbl2, tbl3;
  • 4. Comandos de Manipulação de Dados… •SELECT •SELECT 1 + 2; •SELECT 3 – 5; •SELECT 10 * 2; •SELECT 158 / 3; •FROM •SELECT * FROM tbl; •SELECT * FROM tbl1, tbl2; •SELECT * FROM tbl1, tbl2, tbl3; •SELECT tbl1.col1 FROM tbl1; •SELECT tbl1.col1, tbl2.col2 FROM table1, table2; •SELECT tbl1.col1, tbl2.col2, tbl3.col3 FROM tbl1, tbl2, tbl3;
  • 5. Comandos de Manipulação de Dados… •WHERE; A cláusula WHERE serve para manipular os dados de forma a exibir apenas aqueles que realmente são úteis. Alguns exemplos seguem abaixo: SELECT * FROM tbl1 WHERE tbl1.nome = ‘Pedro’; SELECT * FROM tbl1, tbl2 WHERE tbl1. colRel = tbl2.colRel;
  • 6. Comandos de Manipulação de Dados… •AND/OR/XOR •SELECT 1 XOR NULL; •SELECT * FROM tbl1, tbl2 WHERE tbl1.nome = tbl2.nome AND 1 = 1; •SELECT * FROM tbl1, tbl2 WHERE tbl1.nome = tbl2.nome OR 1 = 1; •SELECT * FROM tbl1, tbl2 WHERE tbl1.nome = tbl2.nome XOR 1 = 1; •IN •SELECT 2 IN (0, 3, 5, 'wefwf'); •SELECT 'wefwf' IN (0, 3, 5, 'wefwf'); •SELECT * FROM tbl WHERE tbl.nome IN (‘Marcus’, ‘Augusto’); •SELECT * FROM tbl WHERE tbl.cidade IN (‘RJ’, ‘SP’, ‘MG’, ‘ES’);
  • 7. Comandos de Manipulação de Dados… •LIKE; A função LIKE() serve para fazer comparações usando expressões regulares simples, de forma a fazer buscas mais gerais.A sintaxe segue abaixo: Os caracteres mais importantes seguem abaixo: • % qualquer caractere em qualquer quantidade, inclusive nenhum. • _ apenas um caractere, sendo ele qualquer. • % casa exatamente o caractere % • _ casa exatamente o caractere _
  • 8. Comandos de Manipulação de Dados… •BETWEEN SELECT * FROM tbl WHERE tbl.idade BETWEEN 18 AND 21; SELECT * FROM tbl WHERE tbl.peso BETWEEN 50 AND 80; •DISTINCT SELECT DISTINCT * FROM tbl1; SELECT DISTINCT * FROM tbl1, tbl2;
  • 9. Comandos de Manipulação de Dados… •ORDER BY •SELECT * FROM table1 ORDER BY table1.idTbl •SELECT tbl1.col1 FROM table1 ORDER BY table1.idTbl; •GROUP BY SELECT * FROM table1 GROUP BY table1.col1; SELECT * FROM table1 GROUP BY table1.col1 DESC;
  • 10. Comandos de Manipulação de Dados… •MD5 SELECT MD5('1234'); SELECT MD5(‘EJCM'); •COUNT SELECT COUNT (pais), região FROM paises GROUP BY regiao; •NOW SELECT NOW();
  • 11. Comandos de Manipulação de Dados… •INSERT INSERT INTO tbl VALUES (1, 2, 3); INSERT INTO tbl (idTbl, col1, col2) VALUES (DEFAULT, 1, ‘c’); •UPDATE UPDATE tbl1 SET col1=’a’; UPDATE tbl1 SET col1=’a’, col2=‘b’;
  • 12. Comandos de Manipulação de Dados… •DELETE DELETE FROM tbl1 WHERE idTbl = ‘1’; DELETE FROM tbl1 WHERE nome LIKE ‘%Pedro%’; •SUB-SELECT SELECT * FROM usuarios WHERE idade = (SELECT idade FROM membros WHERE nome=”Marcus” LIMIT 1);
  • 13. Comandos de Manipulação de Dados… •UNION SELECT [DISTINCT] [FROM table_references] [WHERE where_definition] [GROUP BY {col_name} [ASC | DESC], .. [ORDER BY {col_name} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] UNION SELECT [DISTINCT] [FROM table_references] [WHERE where_definition] [GROUP BY {col_name} [ASC | DESC], .. [ORDER BY {col_name} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}]
  • 14. Comandos de Manipulação de Dados… •INNER JOIN SELECT ... FROM table_reference INNER JOIN table_reference { ON condição de junção } As duas queries se equivalem SELECT tbl1.nome, tbl2.curso FROM tbl1 INNER JOIN tbl2 ON tbl1.idCurso = tbl2.idCurso; SELECT tbl1.nome, tbl2.curso FROM tbl1, tbl2 WHERE tbl1.idCurso = tbl2.idCurso;
  • 15. Comandos de Manipulação de Dados… •View Uma view(visão) é uma tabela virtual na qual os dados não estão fisicamente armazenados. Ela é apenas uma visão de um grupo de colunas de uma ou mais tabelas do banco de dados. No exemplo abaixo, criaremos uma view que terá apenas as colunas COD_PRODUTO, DSC_PRODUTO. CREATE VIEW modelo VIEW_PRODUTOS AS SELECT cod_produto, dsc_produto FROM produtos;