Download presentation
Presentation is loading. Please wait.
Published byJonathan Doyle Modified over 8 years ago
1
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language
2
Consists of the queries that enable the developer to modify the data contained The SQL server processes these queries and returns a result set or a status notification SQL is a non procedural language providing syntax for extracting data, including a syntax to update, insert, and delete records. The Query and Update commands together form the Data Manipulation (DML) part of SQL SELECT UPDATE DELETE INSERT INTO Data Manipulation Language (DML)
3
SELECT SELECT: it is used to retrieve data from a table based on some criteria It specifies a coma separated list of fields to be retrieved, and the FROM clause specifies the table(s) to be accessed. The results are stored in a result table known as the result-set. The * symbol can be used to represent all of the fields Format: SELECT column_name(s) FROM table_name Example: SELECT LastName, FirstName, Address FROM Students;
4
SELECT (cont.) SELECT DISTINCT: this keyword is used to eliminate duplicate values from the table. If there are multiple values of a specified field, the distinct result-set will display only one. Format: SELECT DISTINCT column_name(s) FROM table_name Example: SELECT DISTINCT ShipName FROM Orders; LIMIT X: this keyword specifies the number of rows to be returned from the beginning of the result-set. X specifies the rows returned Format: SELECT column_name(s) FROM table_name LIMIT X; Example: SELECT ShipName FROM Orders LIMIT 10;
5
SELECT (cont.) WHERE Clause: It is used to select a field when a certain criteria set of conditions are desired The WHERE Clause is optional To create the conditions (called selection criteria) SQL provides a set of operators to further qualify what criteria should be specified
6
Where Operators OperatorDescriptionExample =Equal toWHERE country = ‘ireland’ <>, !=Not equal toWHERE country != ‘USA’ >Greater thanWHERE salary > 28000 <Less thanWHERE age < 35 >=, <=Greater/Less Than or EqualWHERE cost >=1200 IS [NOT] NULLIs NULL (vo value) or Not NULLWHERE birth = NULL BETWEENBetween an inclusive rangeWHERE last_name BETWEEN ‘Doherty’ AND ‘McDAID’ LIKESearch for a value like a patternWHERE name LIKE ‘D%’ NOT LIKESearch for a value not like a patternWHERE country NOT LIKE ‘Sw%’ !, NOTLogical not for negationWHERE age ! 10; ||, ORLogical ORWHERE order_number > 10 || part_number = 80 &&, ANDLogical ANDWHERE age>12 && age < 21 XORExclusive ORWHERE status XOR
7
Using Quotes: Quotes are always an issue in programming languages. (single quotes?, double quotes?, when?) SQL uses single quotes around text values (MySQL also accepts double quotes) Numeric Values should not be enclosed in quotes. Comparing Strings: When comparing strings using =, the string must be exactly as typed for the condition to be true – this include length and type of characters. NULL: Null means that there is not a value in the field, or it is unknown, but does not mean a value of zero.
8
LIKE – NOT LIKE: The pattern matching operator can be used as a condition in the WHERE clause, allowing the selection of rows that are ‘like’ or match a pattern A percent sign (%) can be used as a wildcard to match any possible character that might appear before and/or after the character(s) specified. A _ is used to match a single character. The LIKE/NOT LIKE condition can be used in any valid SQL statement, including SELECT, INSERT, UPDATE or DELETE.
9
Examples of the wildcard % uses: SELECT CompanyName, Country FROM Customers WHERE country LIKE ‘SW%’; Returns all the customers and countries in which the country starts with “SW” i.e. Sweden, Switzerland SELECT City, Country FROM suppliers WHERE City LIKE ‘%o’; Returns all cities and countries where the % matches any city that ends with a letter o. SELECT CompanyName FROM customers WHERE CompanyName LIKE ‘%Super%’ Returns all company names where the % matches any company name that contains the pattern “Super”
10
Examples of the wildcard _ uses: SELECT Extension, Firstname FROM Employees WHERE extension LIKE ‘4_ _’; Returns all extensions and first names where the extension has three characters and the first character is a 4. ORDER BY: Used to sort the output of a query in either ascending (ASC, the default) or descending (DESC) order where the values being sorted are either strings or numbers Format: SELECT column_name(s) FROM table_name [WHERE condition] ORDER BY column [ASC, DESC] Example: SELECT Company, Ordernumber FROM Orders ORDER BY ASC Company;
11
INSERT The INSERT statement is used to insert new rows into a table. After the VALUES keyword, a comma-separated list of column names follows Usually, the tables have a PRIMARY KEY column that is usually set to auto-increment; when this is the case, the id of the table is created by the database engine automatically Letting the database increment the PRIMARY KEY ensures that the value is always unique. Format: INSERT INTO table_name VALUES (value1, value2, … ) INSERT INTO table_name (column1, column2,…) VALUES (value1, value2, … ) Example: INSERT INTO Shippers (CompanyName, Phone) VALUES (‘FEDEX’,’416-555-1221’);
12
UPDATE The UPDATE statement is used to modify data in a table. The UPDATE command is followed by the name of the table, followed by the SET statement to indicate what field will be changed, and then the new value that will be assigned to the field The WHERE clause further qualifies what data is to be modified, thereby limiting the scope of the UPDATE Format: UPDATE table_name SET column_name = new value WHERE column_name = some_value; Example: UPDATE orders SET ShipCountry=‘Spain’ WHERE CustomerId = ‘whitc’;
13
DELETE The DELETE statement is used to delete rows DELETE uses the FROM clause to specify the name of the table that contains the data you want to delete The WHERE clause specifies the criteria to identify what data should be removed. BE CAREFUL: Without the WHERE clause ALL ROWS are DELETED If the ORDER BY clause is specified, the rows are deleted in the order that is specified. The LIMIT clause places a limit on the number of rows that can be deleted. Format: DELETE FROM table_name WHERE column_name = some_value; Example: DELETE FROM orders WHERE ShipCountry = ‘Greenland’;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.