SlideShare a Scribd company logo
MySQL
Popular Open Source Database
www.PracticalCoding.inwww.PracticalCoding.in
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
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
MySQL Customers/Users
➢ Baidu.com
➢ Facebook
➢ Flickr
➢ LinkedIn
➢ Quora (http://www.quora.com/Why-does-
Quora-use-MySQL-as-the-data-store-
instead-of-NoSQLs-such-as-Cassandra-
MongoDB-or-CouchDB )
➢ StumbleUpon
➢ Tumblr
➢ Twitter
(http://www.itnews.com.au/News/
317811,twitter-paypal-reveal-
database-performance.aspx )
➢ Vimeo
➢ Wikipedia
➢ Wordpress
➢ Yelp
➢ Youtube
Reference: http://www.mysql.com/customers/industry/?id=85
www.PracticalCoding.inwww.PracticalCoding.in
Connecting to MySQL
➢ MySQLi extension (the "i" stands for improved)
➢ PDO (PHP Data Objects)
www.PracticalCoding.inwww.PracticalCoding.in
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
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
Viewing Databases and Tables
➢# show databases;
➢# use <dbName>;
➢# show tables;
www.PracticalCoding.inwww.PracticalCoding.in
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
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
Databases
➢ Creating Databases
mysql> CREATE DATABASE <dbName>;
➢ Deleting Databases
mysql> drop database <dbName>;
➢ Using Databases
mysql> use <dbName>;
www.PracticalCoding.inwww.PracticalCoding.in
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
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
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
Creating,Describing and Deleting Tables
➢ Creating tables
➢ Deleting tables
➢ Describing tables
www.PracticalCoding.inwww.PracticalCoding.in
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
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
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
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
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
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
Learn More
@
www.PracticalCoding.in

More Related Content

What's hot

What's hot (18)

Introduction to database
Introduction to databaseIntroduction to database
Introduction to database
 
Mysql Ppt
Mysql PptMysql Ppt
Mysql Ppt
 
Mysql ppt
Mysql pptMysql ppt
Mysql ppt
 
MYSQL-Database
MYSQL-DatabaseMYSQL-Database
MYSQL-Database
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
Difference Between Sql - MySql and Oracle
Difference Between Sql - MySql and OracleDifference Between Sql - MySql and Oracle
Difference Between Sql - MySql and Oracle
 
MYSQL
MYSQLMYSQL
MYSQL
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Introduction to (sql)
Introduction to (sql)Introduction to (sql)
Introduction to (sql)
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Hbase
HbaseHbase
Hbase
 
PT- Oracle session01
PT- Oracle session01 PT- Oracle session01
PT- Oracle session01
 
SQL SERVER Training in Pune Slides
SQL SERVER Training in Pune SlidesSQL SERVER Training in Pune Slides
SQL SERVER Training in Pune Slides
 
BITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQLBITS: Introduction to relational databases and MySQL - SQL
BITS: Introduction to relational databases and MySQL - SQL
 
Introduction to NoSQL CassandraDB
Introduction to NoSQL CassandraDBIntroduction to NoSQL CassandraDB
Introduction to NoSQL CassandraDB
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 

Viewers also liked

Javascript(2)
Javascript(2)Javascript(2)
Javascript(2)
tomcoh
 

Viewers also liked (14)

Group presentation 38
Group presentation 38Group presentation 38
Group presentation 38
 
J-Query Course Presentation
J-Query Course PresentationJ-Query Course Presentation
J-Query Course Presentation
 
Javascript(2)
Javascript(2)Javascript(2)
Javascript(2)
 
J Query Presentation
J Query PresentationJ Query Presentation
J Query Presentation
 
Introduction to j_query
Introduction to j_queryIntroduction to j_query
Introduction to j_query
 
Javascript
JavascriptJavascript
Javascript
 
8 introduction to_java_script
8 introduction to_java_script8 introduction to_java_script
8 introduction to_java_script
 
Java script
Java scriptJava script
Java script
 
Java script
Java scriptJava script
Java script
 
Java script
Java scriptJava script
Java script
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Slideshare.Com Powerpoint
Slideshare.Com PowerpointSlideshare.Com Powerpoint
Slideshare.Com Powerpoint
 

Similar to Introduction to my_sql

mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfmysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
pradnyamulay
 
MySQL Reference Manual
MySQL Reference ManualMySQL Reference Manual
MySQL Reference Manual
webhostingguy
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
ADARSH BHATT
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
PavithSingh
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
WrushabhShirsat3
 
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxhjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
EliasPetros
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
Axmed Mo.
 

Similar to Introduction to my_sql (20)

mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdfmysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
mysqlanditsbasiccommands-150226033905-conversion-gate02.pdf
 
sql.pdf
sql.pdfsql.pdf
sql.pdf
 
Sah
SahSah
Sah
 
SQL - RDBMS Concepts
SQL - RDBMS ConceptsSQL - RDBMS Concepts
SQL - RDBMS Concepts
 
Sql
SqlSql
Sql
 
LECTURE NOTES.pdf
LECTURE NOTES.pdfLECTURE NOTES.pdf
LECTURE NOTES.pdf
 
LECTURE NOTES.pdf
LECTURE NOTES.pdfLECTURE NOTES.pdf
LECTURE NOTES.pdf
 
Cassandra Tutorial | Data types | Why Cassandra for Big Data
 Cassandra Tutorial | Data types | Why Cassandra for Big Data Cassandra Tutorial | Data types | Why Cassandra for Big Data
Cassandra Tutorial | Data types | Why Cassandra for Big Data
 
MySQL Reference Manual
MySQL Reference ManualMySQL Reference Manual
MySQL Reference Manual
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
sql.pptx
sql.pptxsql.pptx
sql.pptx
 
unit-ii.pptx
unit-ii.pptxunit-ii.pptx
unit-ii.pptx
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
 
Mysql
MysqlMysql
Mysql
 
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptxhjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx
 
Managing user Online Training in IBM Netezza DBA Development by www.etraining...
Managing user Online Training in IBM Netezza DBA Development by www.etraining...Managing user Online Training in IBM Netezza DBA Development by www.etraining...
Managing user Online Training in IBM Netezza DBA Development by www.etraining...
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Sql Commands_Dr.R.Shalini.ppt
Sql Commands_Dr.R.Shalini.pptSql Commands_Dr.R.Shalini.ppt
Sql Commands_Dr.R.Shalini.ppt
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 

Recently uploaded

Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
YibeltalNibretu
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
Avinash Rai
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Accounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdfAccounting and finance exit exam 2016 E.C.pdf
Accounting and finance exit exam 2016 E.C.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 

Introduction to my_sql

  • 1. MySQL Popular Open Source Database www.PracticalCoding.inwww.PracticalCoding.in
  • 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
  • 4. MySQL Customers/Users ➢ Baidu.com ➢ Facebook ➢ Flickr ➢ LinkedIn ➢ Quora (http://www.quora.com/Why-does- Quora-use-MySQL-as-the-data-store- instead-of-NoSQLs-such-as-Cassandra- MongoDB-or-CouchDB ) ➢ StumbleUpon ➢ Tumblr ➢ Twitter (http://www.itnews.com.au/News/ 317811,twitter-paypal-reveal- database-performance.aspx ) ➢ Vimeo ➢ Wikipedia ➢ Wordpress ➢ Yelp ➢ Youtube Reference: http://www.mysql.com/customers/industry/?id=85 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
  • 15. Creating,Describing and Deleting Tables ➢ Creating tables ➢ Deleting tables ➢ Describing tables www.PracticalCoding.inwww.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

Editor's Notes

  1. MySQLi Procedural : <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?> PDO : <?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?>
  2. 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; -----------------------------------------------------------------------------------------------
  3. 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 '____';