Learning MySQL http://sqlzoo.net/wiki/select_basics Angola Africa 1246700 20609294 100990000000 1) Single quotes SELECT population FROM world WHERE name = Germany 2) Division SELECT name, gdp/population FROM world WHERE area > 50000000 3) AND 4) IN (check) SELECT name, continent FROM world WHERE area < 2000 AND gdp > 500000000 SELECT name, population FROM world WHERE name IN ( Finland, Norway, Denmark, Sweden ) 5) Wild cards and patten matching / LIKE SELECT name FROM world WHERE name LIKE G% 6) BETWEEN name continent area population gdp Afghanistan Asia 652230 25500100 20343000000 Albania Europe 28748 2831741 12960000000 Algeria Africa 2381741 37100000 188681000000 Andorra Europe 468 78115 3712000000 SELECT name, area/1000 FROM world WHERE area BETWEEN 203453 AND 53453 ------------------------------------------------------------------------------------------------------------------------ SQL is divided up into two parts: Data Manipulation Language (DML) and Data Definition Language (DLL). Query 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
DLL Commands CREATE DATABASE - creates 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 SELECTING To SELECT from a table, you use the format SELECT lastname, firstname FROM persontable Using an * selects ALL. NAVIGATION Most database software allows you to navigate: Move-To-First-Record Get-Record-Content Move-To-Next-Record DISTINCT Used to select only the distinct (different) results. SELECT DISTINCT City FROM persons Returns: City Sandnes Stavanger
WHERE clause SELECT column_name(s) FROM table_name WHERE column_name operator value The WHERE clause is used to extract only those records that fulfill a specific criterion. SELECT * FROM Persons WHERE City= Sandnes Returns: Quotes around text fields SQL uses single quotes around text values. Numeric values should not be enclosed in quotes. SELECT * FROM Persons WHERE FirstName = Trove SELECT * FROM Persons WHERE Year =1995 Operators allowed in the WHERE clause = Equal < > Not equal (some versions it s =) > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN - between an inclusive range LIKE - search for a pattern IN - to specify multiple possible values for a column
AND & OR operators The OR operator displays a record if either of the first or second conditions are true. The AND operator displays a record if both the first and the second conditions are true. - AND SELECT * FROM Persons WHERE FirstName= Trove AND Lastname= Svendson Result: - OR SELECT * FROM Persons WHERE FirstName= Trove OR FirstName= Ola Results: - AND & OR SELECT * FROM Persons WHERE LastName= Svendson AND (FirstName= Tove OR FirstName= Ola ) Result:
ORDER BY keyword The ORDER BY keyword is used to sort the result-set by a specified column. SELECT * FROM Persons ORDER BY LastName Result: (Ordered by last name characters) To ORDER BY DESC... descending order we do this: SELECT * FROM Persons ORDER BY LastName DESC Result: (Opposite to above) 4 Nilsen Tom Vingvn 23 Stavanger 4 Nilsen Tom Vingvn 23 Stavanger 4 Nilsen Tom Vingvn 23 Stavanger
INSERT INTO statement The INSERT INTO statement is used to insert a new row in a table. It is possible to write the INSERT INTO statement in two forms. The first form doesn t specify the column names where the data will be inserted, only their values. INSERT INTO table_name VALUES (value1, value2, value3,...) INSERT INTO table_name (column1, column2, column3,... ) VALUES (value1, value2, value3,... ) The second form specifies both the column names and the values to be inserted. INSERT INTO Persons VALUES (4, Nilsen, Johan, Bakken 2, Stavanger ) Result: 4 Nilsen Johan Bakken 2 Stavanger To insert data into specific columns: INSERT INTO Pearsons (P_Id, LastName, FirstName) VALUES (5, Tjessem, Jakob ) Result: 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob
SQL UPDATE statement The UPDATE statement is used to update existing records in a table. The WHERE clause MUST be present or all fields will be updated with the new info. UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value UPDATE Persons SET Address= Nissestien 67, City= Sandnes WHERE LastName= Tjessem AND FirstName= Jakob Result: 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob Nissestien 67 Sandnes
The DELETE statement The DELETE statement is used to delete rows ina table. DELETE FROM table_name WHERE some_column=some_value