Some of The Most Important SQL Commands
Some of The Most Important SQL Commands
SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index
and
SELECT * FROM table_name;
SQL WHERE Syntax SELECT column_name,column_name FROM table_name WHERE column_name operator value;
Example
SELECT * FROM Customers WHERE Country='Mexico'; SELECT * FROM Customers WHERE CustomerID=1;
The OR operator displays a record if either the first condition OR the second condition is true.
Example
SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';
Example
SELECT * FROM Customers WHERE Country='Germany' AND (City='Berlin' OR City='Mnchen');
- ORDER BY Example
The following SQL statement selects all customers from the "Customers" table, sorted by the "Country" column:
Example
SELECT * FROM Customers ORDER BY Country;
Example
SELECT * FROM Customers ORDER BY Country DESC;
Example
SELECT * FROM Customers ORDER BY Country,CustomerName;
It is possible to write the INSERT INTO statement in two forms. The first form does not specify the column names where the data will be inserted, only their values:
INSERT INTO table_name VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
Example
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
Example
INSERT INTO Customers (CustomerName, City, Country) VALUES ('Cardinal', 'Stavanger', 'Norway');
Example
UPDATE Customers SET ContactName='Alfred Schmidt', City='Hamburg' WHERE CustomerName='Alfreds Futterkiste';
Example
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';