Beruflich Dokumente
Kultur Dokumente
Introduction:
MySQL is a DBMS (DataBase
Management System) i.e. it is a databse
software.
MySQL is 5.0 can be downloaded from the
website http://dev.mysql.com/downloads.
MySQL runs on all major platforms such
as Windows, Linux, Solaris, Mac OSX etc.
What is SQL?
Full form of SQL is Structured Query
Language. SQL is a language designed
for communicating with the databases. It is
a basic tool for accessing data in a
relational database.
Database:
Database is collection of data stored in
some organized fashion. We can consider
it to be similar to a filing cabinet.
We have files in the filing cabinet and
related data is stored in specific files. A file
is called as a table in DBMS.
Hence a database is container of number
of tables.
Relational Database:
When information can be presented in the
form of tables in a database, regardless of
how it is stored, the arrangement of data is
called as relational database. The term
relational database comes from the fact that
data columns are related to each other by
virtue of their values.
DOUBLE: Stores normal eight byte doubleprecision floating point values. Precision is
between 25 to 53.
2.
3. String datatypes:
CHAR: Fixed length string from 1 to 255
characters long. Its size must be
specified at the time of creating a table.
If the length is not specified then MySQL
assumes it to be 1.
Field
Type
Null
Key
CUST_ID
int(11)
NO
MUL
BILLNO
int(11)
NO
PRI
AMT
decimal(10,2)
NO
Default
Extra
auto_increment
ALTER STATEMENT:
ALTER statement is used to change the
structure of a table after it has been created.
ALTER statement is used to add columns or
to delete columns from the table. Also it is
used to change type and width of the
columns and to rename the columns.
For e.g.
ALTER TABLE BILL
ADD DT_PUR DATE NULL;
A new column with name DT_PUR will be
added to the table BILL.
ALTER TABLE CUST
ADD GENDER CHAR(1) NOT NULL;
CUST_ID
1
2
3
4
5
6
7
9
NAME
BEENA
GEETA
ANAND
ATUL
RAHUL
LATA
AJAY
DEEPA
CITY
MUMBAI
NAGPUR
PUNE
MUMBAI
MUMBAI
PUNE
MUMBAI
MUMBAI
DISTINCT:
To get distinct CUST_ID values from table
BILL we use statement
SELECT DISTINCT CUST_ID FROM
BILL;
If DISTINCT is not specified then all the
rows will be retrieved. That means ALL is
the default value.
ORDER BY
Data in a table can be sorted in ascending
or descending order with help of clause
ORDER BY
For e.g.
SELECT * FROM CUST ORDER BY NAME;
SELECT * FROM CUST ORDER BY NAME
DESC;
11000
11000
16000
11000
16000
11000
LIKE operator:
SELECT * FROM CUST WHERE NAME LIKE 'A
%';
The above command gives list of all rows for which
NAME begins with letter A
SELECT * FROM CUST WHERE NAME LIKE 'a
%';
The above command gives list of all rows for
which NAME begins with letter a.
IN operator:
IN is a keyword used in WHERE clause to
specify a list of values to be matched
using an OR comparison.
e.g. SELECT CUST_ID, NAME
FROM CUST WHERE CUST_ID IN
(2,4) ORDER BY NAME;
It returns CUST_ID and NAME for those
rows for which CUST_ID has value 2 OR
4 in alphabetical order of NAME.
UPDATE statement:
To modify data in a table UPDATE
statement is used.
It can be used in two ways.
1. To update specific rows in a table.
2. To update all the rows in a table.
DELETE statement:
DELETE is used to
1. To delete specific rows from a table
2. To delete all rows from a table
For e.g.
DELETE FROM CUST
WHERE CUST_ID=5;
DELETE FROM BILL WHERE
AMT>=5000;
If we execute DELETE statement without
WHERE clause then all the contents of a
table get deleted.
DROP statement:
It is used to delete entire table.
e.g. DROP TABLE CUST;
This statement deletes table CUST. There is
no confirmation nor there is an undo.
It can also be used to delete entire
database
e.g. DROP DATABASE SHOP;
Aggregate functions:
Aggregate functions are used to
summarize the data without actually
retrieving it.
AVG(): It returns average value of a
column.
GROUP BY clause:
The GROUP BY clause is used to sort the
data into groups for the purpose of
aggregation.
Suppose we want the number of times
each customer has purchased from the
shop then for this we use GROUP BY
clause as follows
SELECT CUST_ID,COUNT(*)
FROM BILL GROUP BY CUST_ID;
SELECT AVG(AMT) FROM BILL
GROUP BY CUST_ID;
CUST_ID
COUNT(*)
7
6
5
4
3
2
1
1
1
2
1
2
2
1
SUM(AMT)
11000
12000
20000
16000
27000
21000
11000
MySQL functions:
CONCAT() function is used to combine two
or more strings.
SELECT
CONCAT(NAME , STAYS IN ,CITY) FROM
CUSTOMER;
SELECT
CONCAT(TRIM('
MONDAY
SELECT
CONCAT(RTRIM(' MONDAY
SELECT
CONCAT(LTRIM('
MONDAY
'),' 'TUESDAY');
'),' ','TUESDAY');
'),' ','TUESDAY');
SELECT MID('COMPUTER',4,3);
SELECT SUBSTRING('COMPUTER',4,3);
SELECT NOW();
Returns the current date and time.
SELECT TIME(NOW());
Returns the current time.
SELECT TIME('2009-12-14 19:30:00');
Returns the time. i.e. 19:30:00
SELECT CURDATE();
Returns the current date.
MONTH()
Returns month from given date
SELECT MONTH('2009-09-10');
MONTHNAME()
Returns name of the month from given date
SELECT MONTHNAME('2009-09-10');
YEAR()
Returns year from given date
SELECT YEAR('2009-09-10');
POW() or POWER()
Returns a number raised to a power.
SELECT POW(3,2);
MOD()
Returns remainder after dividing first
number by the second
SELECT MOD(5,4);
ROUND()
It rounds off the number to specified number of
decimal places.
SELECT ROUND(123.5678,2);
SQRT()
Returns square root of a positive number
SELECT SQRT(9);
EXP()
Returns e raised to specified power.
SELECT EXP(2);
HAVING clause:
HAVING clause is used to add additional control
to the aggregation of rows in a GROUP BY
operation.
Suppose we want list of all those customers who
have purchased at least twice from the shop we
use HAVING clause as follows
SELECT CUST_ID, COUNT(*) FROM BILL
GROUP BY CUST_ID HAVING COUNT(*)>=2;
CUST_ID
SUM(AMT)
1
2
3
4
5
6
7
11000
26000
36000
25000
20000
12000
16000
CUST_ID
COUNT(*)
SUM(AMT)
11000
20000
25000
12000
SELECT CUST_ID,COUNT(*),SUM(AMT)
FROM BILL
WHERE DT_PUR>='2009-01-01'
GROUP BY CUST_ID HAVING
SUM(AMT)>=10000 ORDER BY
SUM(AMT);
CUST_ID
COUNT(*)
SUM(AMT)
11000
12000
20000
25000
JOIN:
Relational Tables:
The two tables discussed so far are
relational tables. Table CUST contains
information about customers visiting a
shop such as customer name, address
etc. Customer identity (CUST_ID) is the
primary key.
SELECT NAME,AMT
FROM BILL INNER JOIN CUST ON
CUST.CUST_ID=BILL.CUST_ID AND
AMT>=10000;
NAME
GEETA
GEETA
ANAND
ANAND
RAHUL
RAHUL
LATA
AJAY
AMT
11000
10000
16000
11000
10000
10000
12000
11000
CUST_ID
CNAME
AMT
7 AJAY
11000
7 AJAY
5000
3 ANAND
16000
3 ANAND
11000
4 ATUL
16000
4 ATUL
9000
2 GEETA
11000
6 LATA
12000
5 RAHUL
10000
or
SELECT CUST.* ,BILL.*
FROM BILL INNER JOIN CUST
ON BILL.CUST_ID=CUST.CUST_ID
ORDER BY NAME;
CUST_ID
NAME
CITY
PHONE
AJAY
MUMBAI
252
11000
AJAY
MUMBAI
265
5000
ANAND
PUNE
4545545
262
16000
ANAND
PUNE
4545545
263
11000
ATUL
MUMBAI
259
16000
ATUL
MUMBAI
264
9000
GEETA
NAGPUR
261
11000
LATA
PUNE
266
12000
2009-09-09
RAHUL
MUMBAI
251
10000
2007-10-10
24444444
CUST_ID
BILLNO
AMT
DT_PUR
2007-08-04
2009-09-09
Subqueries:
A query that is wrapped within another
query. It is also known as inner query.
A subquery is a SELECT statement within
another statement. Subqueries always
appear as part of WHERE clause or
HAVING clause.
Outer Query:
A query which contains an inner query or
subquery.
Inner Query:
A query inside another query. It is also
known as subquery.
Noncorrelated Subquery:
A subquery that stands alone and does not
reference anything from the outer query.
Correlated Subquery:
A subquery that relies on values returned
from the outer query
CUST_ID
7
3
4
2
6
5
NAME
AJAY
ANAND
ATUL
GEETA
LATA
RAHUL
CITY
MUMBAI
PUNE
MUMBAI
NAGPUR
PUNE
MUMBAI