Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29
MYSQL
Some of The Most Important SQL Commands
1. SELECT - extracts data from a database
2. UPDATE - updates data in a database 3. DELETE - deletes data from a database 4. INSERT INTO - inserts new data into a database 5. CREATE DATABASE - creates a new database 6. ALTER DATABASE - modifies a database 7. CREATE TABLE - creates a new table 8. ALTER TABLE - modifies a table 9. DROP TABLE - deletes a table 18.MySQL SELECT Statement
• The SELECT statement is used to select data from
a database. • The data returned is stored in a result table, called the result-set.
SELECT column1, column2, ...
FROM table_name; • Here, column1, column2, ... are the field names of the table you want to select data from. Cont.. • If you want to select all the fields available in the table, use the following syntax:
SELECT * FROM table_name;
19. SELECT DISTINCT Statement • The SELECT DISTINCT statement is used to return only distinct (different) values • Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values • Eliminates duplication • Syntax: SELECT DISTINCT column1, column2, ... FROM table_name; 20.Count Distinct values • The following SQL statement counts and returns the number of different (distinct) “values” in the table SELECT COUNT(DISTINCT coulmname) FROM tab le_name; • SELECT COUNT(DISTINCT Country) FROM Cust omers; 21. WHERE Clause
• The WHERE clause is used to filter records.
• It is used to extract only those records that fulfill a specified condition.
• SELECT column1, column2, ...
FROM table_name WHERE condition; OR • Select * from table_name WHERE condition;
• WHERE clause is not only used in SELECT statements, it is also used
in UPDATE, DELETE, etc.! Cont.. • Text Fields vs. Numeric Fields • SQL requires single quotes around text values • However, numeric fields should not be enclosed in quotes: e.g: SELECT * FROM Customers WHERE CustomerID = 1; SELECT * FROM Customers WHERE Country = 'Mexico'; Operators 22.Comparison Operators = Equal SELECT * FROM Products WHERE Price = 18;
> Greater than SELECT * FROM
Products WHERE Price > 30; < Less than SELECT * FROM Products WHERE Price < 30; >= Greater than or equal SELECT * FROM Products WHERE Price >= 30; <= Less than or equal SELECT * FROM Products WHERE Price <= 30; <> Not equal. SELECT * FROM In some versions of Products SQL this operator may WHERE Price <> 18; be written as != 23. Logical Operators AND OR NOT • The WHERE clause can be combined with AND, OR, and NOT operators. • The AND and OR operators are used to filter records based on more than one condition: • Retrieve record that meet multiple conditions • The AND operator displays a record if all the conditions separated by AND are TRUE. • The OR operator displays a record if any of the conditions separated by OR is TRUE. • Retrieves record if at least one condition is met • The NOT operator displays a record if the condition(s) is NOT TRUE. • AND syntax SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND condit ion3 ...; e.g:
SELECT * FROM Customers
WHERE Country = 'Germany' AND City = 'Berlin'; • OR Syntax SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ... ; E.g:
• SELECT * FROM Customers
WHERE City = 'Berlin' OR City = 'Stuttgart'; • NOT Syntax SELECT column1, column2, ... FROM table_name WHERE NOT condition; E.g:
• SELECT * FROM Customers
WHERE NOT Country = 'Germany'; Combining AND, OR and NOT • You can also combine the AND, OR and NOT operators. e.g: Combining AND & OR SELECT * FROM Customers WHERE Country = 'Germany' AND (City = 'Berlin' OR City = 'Stuttgart'); Combining Not & AND SELECT * FROM Customers WHERE NOT Country = 'Germany' AND NOT Country = 'USA'; 24.ORDER BY Keyword • The ORDER BY keyword is used to sort the result-set in ascending or descending order. • The ORDER BY keyword sorts the records in ascending order by default. • To sort the records in descending order, use the DESC keyword. • Syntax SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC; e.g: SELECT * FROM Customers ORDER BY Country;
Above SQL statement selects all customers from the
"Customers" table, sorted by the "Country" column: ORDER BY DESC • The following SQL statement selects all customers from the "Customers" table, sorted DESCENDING by the “id" column
SELECT * FROM Customers
ORDER BY Customerid DESC; SELECT * FROM Customers ORDER BY Country DESC; ORDER BY Several Columns
• Below SQL statement selects all customers
from the "Customers" table, sorted by the "Country" and the "CustomerName" column. • This means that it orders by Country, but if some rows have the same Country, it orders them by CustomerName: Syntax1: SELECT * FROM Customers ORDER BY Country, CustomerName; Cont..
Syntax2:
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
• Above SQL statement selects all customers from
the "Customers" table, sorted ascending by the "Country" • and descending by the "CustomerName" column: 25. NULL VALUES • A field with a NULL value is a field with no value.
• To check NULL values We have to use the IS
NULL and IS NOT NULL operators.
(not possible with operators like = , <>)
Cont.. i. IS NULL SELECT column_names FROM table_name WHERE column_name IS NULL; e.g:
SELECT CustomerName, ContactName, Address
FROM Customers WHERE Address IS NULL;
Above SQL lists all customers with a NULL value in the
"Address" field: Cont.. ii. IS NOT NULL SELECT column_names FROM table_name WHERE column_name IS NOT NULL; e.g: SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL;
Above SQL lists all customers with a value in the
"Address" field: 26.UPDATE Statement • The UPDATE statement is used to modify the existing records in a table. UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
The WHERE clause specifies which record(s)
that should be updated. If you omit the WHERE clause, all records in the table will be updated! Cont.. i. Updates single record UPDATE Customers SET ContactName = 'Alfred', City = 'Frankfurt' WHERE CustomerID = 1;
The SQL statement updates the first customer
(CustomerID = 1) with a new contact person and a new city. Cont..
ii. UPDATE Multiple Records
UPDATE Customers SET PostalCode = 00000 WHERE Country = 'Mexico';
SQL statement will update the PostalCode to
00000 for all records where country is "Mexico": Cont..
iii. Updating All records/No WHERE clause
• If you omit the WHERE clause, ALL records will
be updated
UPDATE Customers SET PostalCode = 00000; 27.DELETE Statement • To delete a record • The DELETE statement is used to delete existing records in a table.
DELETE FROM table_name WHERE condition;
The WHERE clause specifies which record(s)
should be deleted. If you omit the WHERE clause, all records in the table will be deleted! Cont..
i. Removing specific records
DELETE FROM Customers WHERE CustomerNam e=‘abc’;
ii. Deleting All Records
DELETE FROM table_name; SQL statement deletes all rows in the "Customers" table, without deleting the table, i.e. table structure, attributes, and indexes will be intact: