SQL, The Structured Query Language
SQL, The Structured Query Language
Query Language
Overview
Introduction
DDL Commands
DML Commands
SQL Statements, Operators, Clauses
Aggregate Functions
Structured Query Language (SQL)
The ANSI standard language for the definition
and manipulation of relational database.
ASP
To use SQL to get the data you want
Example:
INSERT INTO countries VALUES('C1','India',1001);
Insert multiple rows by a single insert
statement
INSERT INTO countries VALUES
('C0001','India',1001),
('C0002','USA',1007),
('C0003','UK',1003);
SQL: UPDATE Statement
To update the content of the table:
UPDATE statement syntax:
UPDATE <table name> SET <attr> = <value>
WHERE <selection condition>;
Example: UPDATE employees SET SALARY = 8000
WHERE employee_id = 105 AND salary < 5000;
SQL: DELETE Statement
To delete rows from the table:
DELETE statement syntax:
DELETE FROM <table name>
WHERE <condition>;
Example: SQL> DELETE FROM CUSTOMERS
WHERE ID = 6;
after
query
Note: If the WHERE clause is omitted all rows of data are deleted from the table.
SQL Statements, Operations, Clauses
SQL Statements:
Select
SQL Operations:
Join
Left Join
Right Join
Like
SQL Clauses:
Order By
Group By
Having
SQL: SELECT Statement
A basic SELECT statement includes 3 clauses
after
query
after
query
SQL: Join operation
A join can be specified in the FROM clause
which list the two input relations and the
WHERE clause which lists the join condition.
SQL: Join operation (cont.)
INNER JOIN
SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
INNER JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Inner Join
Left Join
after
CUSTOMERS query ORDERS
SQL: Join operation (cont.)
RIGHT JOIN
SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS
RIGHT JOIN ORDERS ON CUSTOMERS.ID = RDERS.CUSTOMER_ID;
RIGHT Join
after
CUSTOMERS query ORDERS
SQL: Like operation
Pattern matching selection
% (arbitrary string)
SELECT *
FROM emp
WHERE ID like ‘%01’;
finds ID that ends with 01, e.g. 1001, 2001, etc
_ (a single character)
SELECT *
FROM emp
WHERE ID like ‘_01_’;
finds ID that has the second and third character
as 01, e.g. 1010, 1011, 1012, 1013, etc
SQL: The ORDER BY Clause
after after
query query
SQL: The GROUP BY Clause
The function to divide the tuples into groups and
returns an aggregate for each group.
Usually, it is an aggregate function’s companion
EXAMPLE: SELECT NAME, SUM(SALARY) FROM
CUSTOMERS GROUP BY NAME;
after
query
SQL: The HAVING Clause
after
query
SQL: Aggregate Functions
Are used to provide summarization information for
SQL statements, which return a single value.
COUNT(attr)
SUM(attr)
MAX(attr)
MIN(attr)
AVG(attr)
The reason query cache does not work in the first line is the usage of the CURDATE()
function. This applies to all non-deterministic functions like NOW() and RAND() etc... Since
the return result of the function can change, MySQL decides to disable query caching for that
query.
Query Optimization
Techniques:
2. EXPLAIN Your SELECT Queries
Using the EXPLAIN keyword can give you insight on what MySQL is doing to execute your
query. This can help you spot the bottlenecks and other problems with your query or table
structures. The results of an EXPLAIN query will show you which indexes are being utilized,
how the table is being scanned and sorted.
Query Optimization
Techniques:
LIMIT 1 When Getting a Unique Row
Example:
Without Limit:
$r = mysql_query("SELECT * FROM user WHERE username= user1'");
With Limit:
$r = mysql_query("SELECT * FROM user WHERE username= user1' LIMIT 1");
Query Optimization
Techniques:
4. Index the Search Fields
Query Optimization
Techniques:
5. Index and Use Same Column Types for Joins
Example:
If your application contains many JOIN queries, you need to make sure that the columns you
join by are indexed on both tables. This affects how MySQL internally optimizes the join
operation.
Query Optimization
Techniques:
6. Do Not ORDER BY RAND()
7. Avoid SELECT *
The more data is read from the tables, the slower the query will become. It increases the time
it takes for the disk operations. Also when the database server is separate from the web
server, you will have longer network delays due to the data having to be transferred between
the servers.
Query Optimization
Techniques:
8. Always Have an id Field
In every table have an id column that is the PRIMARY KEY, AUTO_INCREMENT and one of
the flavors of INT. Also preferably UNSIGNED, since the value can not be negative.
ENUM type columns are very fast and compact. Internally they are stored like TINYINT, yet
they can contain and display string values. This makes them a perfect candidate for certain
fields.
If you have a field, which will contain only a few different kinds of values, use ENUM instead
of VARCHAR. For example, it could be a column named "status", and only contain values
such as "active", "inactive", "pending", "expired" etc...
NULL columns require additional space and they can add complexity to your comparison
statements. Just avoid them when you can. However, I understand some people might have
very specific reasons to have NULL values, which is not always a bad thing.
Query Optimization
Techniques:
11. Prepared Statements
In every table have an id column that is the PRIMARY KEY, AUTO_INCREMENT and one of
the flavors of INT. Also preferably UNSIGNED, since the value can not be negative.
$state = ‘Punjab’;
// bind parameters
$stmt->bind_param(1, $state);
CREATE A NEW USER
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
REVOKE A PERMISSION
REVOKE [type of permission] ON [database name].[table name] FROM ‘[username]’@‘localhost’;
LIST OF COMMON POSSIBLE PERMISSIONS
FOR USER:
1. CREATE- allows them to create new tables or databases
5. SELECT- allows them to use the Select command to read through databases
1. Commit command: Commit command is used to permanently save any transaaction into
database.
Syntax: commit;
3. Rollback command: This command restores the database to last commited state. It is
also use with savepoint command to jump to a savepoint in a transaction.
Ronald R. Plew, Ryan K. Stephens. Teach Yourself SQL in 24 Hours 3rd Edition.
Sams Publishing, 2003.
SQL http://en.wikipedia.org/wiki/SQL
W3C http://www.w3schools.com/sql/sql_tryit.asp
Tutorialspoint https://www.tutorialspoint.com