Professional Documents
Culture Documents
- Cost
Commercial License Also Available for Mission Critical Systems
- Open Source
- Cross Platform
- Powerful
- Web Based Compatibility
Obtaining MySQL
Linux Installation:
$ rpm i MySQL*.rpm
Installing MySQL
Windows Installation:
mysql -uusername -p
Naming Rules:
PRIMARY KEY
UNIQUE
NOT NULL
DEFAULT
CHECK
AUTO_INCREMENT:
mysql> ALTER TABLE tablename add AUTO_INCREMENT
= number
Temporary Tables
MySQL supports several different storage engines that are used to handle different
table types
--------
( MEMORY , InnoDB , MyISAM , BLACKHOL , MRG_MYIS
CSV , ARCHIVE )
MyISAM (Default): Tables stored on disk in three files with these extensions: .MYI, .MYD,
.frm (High Speed Storage)
InnoDB: Alternative disk based table handler that supports extra features than MyISAM
(Fast Transactional)
Memory: Very fast storage engine that store tables in memory only, usually used for
temporary tables
Actions:
CASCADE
SET NULL
NO ACTION (Default)
Note: If you do not use database level foreign key constraint, you
must ensure that your application will maintain referential integrity!
Indexes
Partial Index:
mysql> CREATE INDEX indexname ON tablename
(fieldname (number))
mysql> ALTER TABLE tablename ADD INDEX
(fieldname(number))
Compound Index:
mysql> CREATE INDEX indexname ON tablename (field
1, field 2)
mysql> ALTER TABLE tablename ADD INDEX (field 1, field
2)
Dropping Indexes
1- AND
2- OR
3- NOT
SQL joins are used to query data from two or more tables,
based on a relationship between certain columns in these
tables
The LEFT JOIN keyword returns all rows from the left
table (first table), even if there are no matches in
the right table (second table)
mysql> SELECT fields FROM table1 UNION SELECT fields FROM table2
Arithmetic Operators:
+, -, *, /
DIV, MOD (%)
Precedence: multiplication and division have higher priority
than addition and subtraction
NULL Values:
NULL is no value, it is not the same as zero
Performing any kind of arithmetic operation which one of
operand is NULL, result always NULL
Use IS NULL or IS NOT NULL, comparing column = NULL does not
work
MySQL Functions
RAND()
ROUND(number, decimal)
CEILING(number)
FLOOR(number)
TRUNCATE(number, decimal)
It does not round the number, but simply
removes from it decimal places
POW(a, b)
Raises a to b
SQRT(number)
It calculate the square root of a number
Numeric Functions
LOG(base, number)
It calculate the logarithm of a number based on first
argument
EXP(number)
It raises natural log base (e) to the power given in
argument
PI()
Shows the value of pi number
Numeric Functions
SIN(value)
COS(value)
TAN(value)
DEGREES()
RADIANS()
IF()
mysql> SELECT fields, IF(condition, true, false) FROM table
IFNULL(value1,value2)
Provides a shortcut when you expect to see NULL
values
If the first argument is null, the second argument is
returned, otherwise the first argument will returned
NULLIF(value1,value2)
Use NULLIF to return NULL when two values are the
same. If the two expressions passed to NULLIF are
the same, the result is NULL, otherwise the first
argument will returned
String Operators
Comparison Operators:
IN(‘value1’, ‘value2’)
LIKE pattern (use % and _ to build a pattern)
BETWEEN ‘char1’ AND ‘char2’
BINARY Ordering:
For case sensitive ordering, user ORDR BY BINARY
CONCAT(text1, text2)
Joins two or more strings
LTRIM(string)
Removes space characters from the left
RTRIM(string)
Removes space characters from the right
String Functions
LOCATE(keyword, string)
Returns the position of a substring within a string
LENGTH(string)
Returns the total number of characters in a strings
SUBSTRING()
Returns only a fixed portion of a string
SUBSTRING(string, start, length)
SUBSTRING_INDEX()
Returns a part from a single string
SUBSTRING_INDEX(string, delimiter, part)
String Functions
UPPER()
Convert string to uppercase
LOWER()
Convert string to lowercase
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
Date Functions
CURDATE()
Returns the current date
CURTIME()
Returns the current time
NOW()
Returns the current date and time as a single
value
Date Functions
DATE_FORMAT(date, format)
%M – Month name
%m – Two digits month number
%U – Week number
%u – Week number
%W – Weekday name
%Y – Four digits year
%y – Two digits year
Date Functions
DATEDIFF(date, date)
Returns the difference between two dates
TIMEDIFF(time, time)
Returns the difference between two times
Aggregate Functions
1- COUNT()
Counts values in a query
It never counts columns with null values
2- SUM()
Computes a total from the data in a specified column
3- AVG()
Compute an average of the data in a specified
column
Aggregate Functions
4- MIN()
Return the smallest value from a column
5- MAX()
Return the greatest value from a column
You can reference a view just like you reference a real table in
a select statement
You can also use some views with insert, update, delete
statement
You can insert into a view with inner joined tables if all
the columns you insert into are from the same table
You cannot insert into any view that uses the UNION or
UNION ALL operator
Altering & Dropping
Views
mysql_connect(server,username,password)
mysql_select_db(dbname)
mysql_query(query, connection)
mysql_num_rows(recordset)
mysql_fetch_assoc(recordset)
End of
Database
Implementation