introduction to my_sql

22
MySQL Popular Open Source Database www.PracticalCoding. in www.PracticalCoding. in

Upload: basavaraj-hampali

Post on 07-Apr-2017

156 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Introduction to my_sql

MySQLPopular Open Source Database

www.PracticalCoding.inwww.PracticalCoding.in

Page 2: Introduction to my_sql

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

Page 3: Introduction to my_sql

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

Page 5: Introduction to my_sql

Connecting to MySQL

➢ MySQLi extension (the "i" stands for improved)

➢ PDO (PHP Data Objects)

www.PracticalCoding.inwww.PracticalCoding.in

Page 6: Introduction to my_sql

Get started with MySQLLet'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

Page 7: Introduction to my_sql

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

Page 8: Introduction to my_sql

Viewing Databases and Tables

➢# show databases;

➢# use <dbName>;

➢# show tables;

www.PracticalCoding.inwww.PracticalCoding.in

Page 9: Introduction to my_sql

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

Page 10: Introduction to my_sql

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

Page 11: Introduction to my_sql

Databases➢ Creating Databases

mysql> CREATE DATABASE <dbName>;

➢ Deleting Databasesmysql> drop database <dbName>;

➢ Using Databasesmysql> use <dbName>;

www.PracticalCoding.inwww.PracticalCoding.in

Page 12: Introduction to my_sql

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

Page 13: Introduction to my_sql

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.

VARCHARThe 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

Page 14: Introduction to my_sql

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’.

DATETIMEThe 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.

YEARThe 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

Page 15: Introduction to my_sql

Creating,Describing and Deleting Tables

➢ Creating tables

➢ Deleting tables

➢ Describing tables

www.PracticalCoding.inwww.PracticalCoding.in

Page 16: Introduction to my_sql

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', '[email protected]');

mysql> INSERT INTO SiteUsers values ('','Johny', 'Doepp', '[email protected]','');

www.PracticalCoding.inwww.PracticalCoding.in

Page 17: Introduction to my_sql

Show table data-- List all the rows of the specified columnsSELECT 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

Page 18: Introduction to my_sql

Altering tablesALTER TABLE tableName ... -- Modify a table, e.g., ADD COLUMN and DROP COLUMNALTER TABLE tableName ADD columnDefinitionALTER TABLE tableName DROP columnNameALTER 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

Page 19: Introduction to my_sql

Alter tables continued...ALTER COLUMNUsed 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 COLUMNUsed 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 COLUMNUsed 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

Page 20: Introduction to my_sql

Updating tablesUPDATE 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

Page 21: Introduction to my_sql

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

Page 22: Introduction to my_sql

Learn More @

www.PracticalCoding.in