SQL
SQL
#############################################################
Sintaxe SQL
#############################################################
#############################################################
#############################################################
Alguns dos comandos SQL mais importantes
#############################################################
SELECT - extrai dados de um banco de dados
UPDATE - atualiza dados em um banco de dados
DELETE - exclui dados de um banco de dados
INSERT INTO - insere novos dados em um banco de dados
CREATE DATABASE - cria um novo banco de dados
ALTER DATABASE - modifica um banco de dados
CREATE TABLE - cria uma nova tabela
ALTER TABLE - modifica uma tabela
DROP TABLE - exclui uma tabela
CREATE INDEX - cria um índice (chave de pesquisa)
DROP INDEX - exclui um índice
#############################################################
Instrução SQL SELECT:
#############################################################
A instrução SELECT é usada para selecionar ou projetar os dados de um banco de
dados.
Sintaxe
SELECT column1, column2, ...
FROM table_name;
Aqui, coluna1, coluna2, ... são os nomes de campo da tabela da qual você deseja
selecionar dados.
Exemplo:
Retornar dados da tabela Clientes:
#############################################################
2º The SQL SELECT DISTINCT Statement
#############################################################
The SELECT DISTINCT statement is used to return only distinct (different) values.
Syntax
Example:
Select all the different countries from the "Customers" table:
Example
SELECT Country FROM Customers;
Count Distinct
By using the DISTINCT keyword in a function called COUNT, we can return the number
of different countries.
Example
SELECT COUNT(DISTINCT Country) FROM Customers;
###########################################################
SQL WHERE Clause
###########################################################
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
Syntax
Note: The WHERE clause is not only used in SELECT statements, it is also used in
UPDATE, DELETE, etc.!
Example
SELECT * FROM Customers
WHERE CustomerID=1;
Example
Select all customers with a CustomerID greater than 80:
NOTE:
DESC
The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
Example
Sort the products from highest to lowest price:
Order Alphabetically
For string values the ORDER BY keyword will order alphabetically:
Example
Sort the products alphatbetically by ProductName: