2. About MySQL
➢ Ideal for both small and large applications
➢ Developed, distributed, and supported by Oracle Corporation
➢ Facebook serves some of its data using MySQL ( Follow the updates :
https://www.facebook.com/MySQLatFacebook )
➢ Facebook version of MySQL : https://github.com/facebook/mysql-5.6
➢ MySQL Customers: http://www.mysql.com/customers/industry/?id=
➢ Free to download and use
www.PracticalCoding.inwww.PracticalCoding.in
3. WebScaleSQL?
A collaboration among engineers from several companies that face
similar challenges in running MySQL at scale, and seek greater
performance from a database technology tailored for their needs.
Contributions from MySQL engineering teams at Facebook, Google,
LinkedIn, and Twitter
Recently, the $168B e-commerce giant Alibaba MySQL team has joined
WebScaleSQL.
http://webscalesql.org/
www.PracticalCoding.inwww.PracticalCoding.in
5. Connecting to MySQL
➢ MySQLi extension (the "i" stands for improved)
➢ PDO (PHP Data Objects)
www.PracticalCoding.inwww.PracticalCoding.in
6. Get started with MySQL
Let's create a password for root user :
# mysqladmin -u root password NEWPASSWORD
Changing root password:
# mysqladmin -p -u root password NEWPASSWORD1
Let’s enter the world of MySQL:
# mysql -u root -p
www.PracticalCoding.inwww.PracticalCoding.in
7. MySQL Statements
➢ Don't quote database, table, or column names
➢ Don't quote column types or modifiers
➢ Don't quote numerical values
➢ Quote (single or double) non-numeric values
➢ Quote file names and passwords
➢ User names are NOT quoted in GRANT or REVOKE statements, but
they are quoted in other statements.
www.PracticalCoding.inwww.PracticalCoding.in
8. Viewing Databases and Tables
➢# show databases;
➢# use <dbName>;
➢# show tables;
www.PracticalCoding.inwww.PracticalCoding.in
9. Some common MySQL operations
➢ mysql> SET PASSWORD=PASSWORD('new_password');
➢ mysql> CREATE USER 'test1'@'localhost' IDENTIFIED BY
'welcome123';
➢ mysql> GRANT ALL PRIVILEGES ON * . * TO 'test1'@'localhost';
➢ mysql> FLUSH PRIVILEGES;
➢ mysql> DROP USER ‘test1’@‘localhost’;
www.PracticalCoding.inwww.PracticalCoding.in
10. Let’s try REVOKE ...
List of other common possible permissions that users can enjoy :
➢ ALL PRIVILEGES- as we saw previously, this would allow a MySQL user all access to
a designated database (or if no database is selected, across the system)
➢ CREATE- allows them to create new tables or databases
➢ DROP- allows them to them to delete tables or databases
➢ DELETE- allows them to delete rows from tables
➢ INSERT- allows them to insert rows into tables
➢ SELECT- allows them to use the Select command to read through databases
➢ UPDATE- allow them to update table rows
➢ GRANT OPTION- allows them to grant or remove other users' privileges
www.PracticalCoding.inwww.PracticalCoding.in
11. Databases
➢ Creating Databases
mysql> CREATE DATABASE <dbName>;
➢ Deleting Databases
mysql> drop database <dbName>;
➢ Using Databases
mysql> use <dbName>;
www.PracticalCoding.inwww.PracticalCoding.in
12. Before we move on to tables...
Type Description
BIGINT The BIGINT data type supports integer values ranging
between -9,223,372,036,854,775,808 and
9,223,372,036,854,775,807.
BIT The BIT data type supports binary values ranging between 1
and 64 bits
FLOAT The FLOAT data type stores approximate numeric values.
For instance, defining a column as FLOAT(5,3) will store
12.4785 as 12.479, because the defined precision is 3.
INT The INT data type supports integer values ranging between -
2,147,483,648 and 2,147,483,647.
MEDIUMINT The MEDIUMINT data type supports integer values ranging
between -8,388,608 and 8,388,607.
SMALLINT The SMALLINT data type supports integer values ranging
between 32,768 and 32,767.
TINYINT The TINYINT data type supports integer values ranging
between -128 and 127
13. Commonly used string data types continued...
Type Description
BLOB/LONGBLOB/MEDIUMBLOB/
TINYBLOB
The BLOB, MEDIUMBLOB, TINYBLOB, and LONGBLOB types are
used to store data such as images or binary files, and store up to
65,545.
CHAR
The CHAR type stores between 0 and 255 characters. Any column
defined as a CHAR will consume all of the allotted space
regardless of the stored string size. For instance, any CHAR
column defined as CHAR(25) will require the same amount of
space (that required to store 25 characters) no matter the size of
the stored string.
TEXT/LONGTEXT/MEDIUMTEX/
TINYTEXT
The TEXT, LONGTEXT, MEDIUM, and TINYTEXT types store up to
65,534, 4,294,967,295, 16,777,215, and 255 characters, respectively.
VARCHAR
The VARCHAR type stores up to 65,535 characters. Unlike CHAR,
each VARCHAR instance requires only the space required to store
the provided string, plus one or two additional bytes depending
on the string length.
www.PracticalCoding.in
14. Date and time types...
Type Description
DATE
The DATE type represents dates in the format ’YYYY-MM-DD’, and has
a range of ’1000-01-01’ to ’9999-12-31’.
DATETIME
The DATETIME type represents values containing both a date and a
corresponding time in the format ’YYYY-MM-DD HH:MM:SS’. It has a
range of ’1000-01-01 00:00:00’ to ’9999-12-31 23:59:59’
TIME The TIME type represents temporal values in the format ’HH:MM:SS’,
ranging from ’-838:59-59’ to ’838:59:59’
TIMESTAMP
Like DATETIME, the TIMESTAMP type represents values containing
both a date and time, and sports a format identical to DATETIME. Its
range is ’1970-01-01 00:00:01’ UTC to ’2038-01-09 03:14:07’ UTC. The
TIMESTAMP differs from other data types in that it can be
automatically assigned the current date/time and automatically
updated at INSERT and UPDATE time.
YEAR
The YEAR type represents years, and supports a two- (’YY’) and four-
digit format (’YYYY’). The two-digit format supports a range of 70
(1970) to 69 (2069). The four-digit format supports a range of 1901 to
2155.
www.PracticalCoding.in
16. Inserting data into Tables
INSERT INTO table_name (column1, column2, column3,...) VALUES
(value1, value2, value3,...)
mysql> INSERT INTO SiteUsers (firstname, lastname, email)
VALUES ('Arvind', 'Gandhi', 'arvind@test.com');
mysql> INSERT INTO SiteUsers values ('','Johny', 'Doepp',
'johny@example.com','');
www.PracticalCoding.inwww.PracticalCoding.in
17. Show table data
-- List all the rows of the specified columns
SELECT column1Name, column2Name, ... FROM tableName
SELECT * | column1Name AS alias1, ..., columnNName AS aliasN
FROM tableName
WHERE criteria
GROUP BY columnName
ORDER BY columnName ASC|DESC, ...
HAVING groupConstraints
LIMIT count | offset count
mysql> select * from SiteUsers;
mysql> select firstname from SiteUsers where id=1;
mysql> select firstname,lastname from SiteUsers where id=1;
mysql> select * from SiteUsers where firstname='john’;
mysql> select * from SiteUsers where id<2;
www.PracticalCoding.in
18. Altering tables
ALTER TABLE tableName ... -- Modify a table, e.g., ADD COLUMN and
DROP COLUMN
ALTER TABLE tableName ADD columnDefinition
ALTER TABLE tableName DROP columnName
ALTER TABLE tableName ADD FOREIGN KEY (columnName) REFERENCES
tableName (columnNmae)
ALTER TABLE tableName DROP FOREIGN KEY constraintName
mysql> alter table SiteUsers add column address text not null;
mysql> alter table SiteUsers drop address;
mysql> alter table SiteUsers change firstname firstname VARCHAR(60);
mysql> alter table SiteUsers modify firstname VARCHAR(100);
www.PracticalCoding.inwww.PracticalCoding.in
19. Alter tables continued...
ALTER COLUMN
Used to set or remove the default value for a column. Example:
ALTER TABLE MyTable ALTER COLUMN foo SET DEFAULT 'bar';
ALTER TABLE MyTable ALTER COLUMN foo DROP DEFAULT;
CHANGE COLUMN
Used to rename a column, change its datatype, or move it within the schema. Example:
ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL FIRST;
ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL AFTER baz;
MODIFY COLUMN
Used to do everything CHANGE COLUMN can, but without renaming the column.
Example:
ALTER TABLE MyTable MODIFY COLUMN foo VARCHAR(32) NOT NULL AFTER baz;
www.PracticalCoding.inwww.PracticalCoding.in
20. Updating tables
UPDATE tableName SET columnName = {value|NULL|DEFAULT},
... WHERE criteria
Example:
mysql> update SiteUsers set firstname = 'Amitabhny' where
id=3;
mysql> update SiteUsers set firstname =
'Amitabhny',lastname='Bachchan' where id=3;
www.PracticalCoding.inwww.PracticalCoding.in
21. Deleting table rows
-- Delete all rows from the table. Use with extreme care!
DELETE FROM tableName
-- Delete only row(s) that meets the criteria
DELETE FROM tableName WHERE criteria
www.PracticalCoding.inwww.PracticalCoding.in
CREATE TABLE table_name ( column1 definition, column2 definition, ... columnN definition );
mysql> CREATE TABLE SiteUsers (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)
mysql> create table mytable1 (id INT PRIMARY KEY AUTO_INCREMENT , firstname VARCHAR(50), lastname VARCHAR(50), email VARCHAR(100));
Query OK, 0 rows affected (0.16 sec)
-----------------------------------------------------------------------------------------------
mysql> DROP TABLE SiteUsers;
-----------------------------------------------------------------------------------------------
mysql> DESCRIBE SiteUsers;
-----------------------------------------------------------------------------------------------
mysql> select * from SiteUsers WHERE firstname LIKE 'Joh%';
mysql> select * from SiteUsers WHERE firstname LIKE '%ny';
mysql> select * from SiteUsers WHERE firstname LIKE '%mit%';
mysql> select * from SiteUsers WHERE firstname LIKE '____';