+ All Categories
Home > Documents > MySQL and SQLamitabh/pubs/Roorkee/sqlmysql.pdf · 2005-05-14 · SQL: Structured... DML: Queries...

MySQL and SQLamitabh/pubs/Roorkee/sqlmysql.pdf · 2005-05-14 · SQL: Structured... DML: Queries...

Date post: 22-Mar-2019
Category:
Upload: ngothuan
View: 215 times
Download: 0 times
Share this document with a friend
53
What is MySQL? SQL: Structured . . . DML: Queries Home Page Title Page Page 1 of 53 Go Back Full Screen Close Quit MySQL and SQL Amitabh Trehan [email protected] September 11, 2003
Transcript

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 1 of 53

Go Back

Full Screen

Close

Quit

MySQLandSQL

Amitabh [email protected]

September 11, 2003

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 2 of 53

Go Back

Full Screen

Close

Quit

MySQL

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 3 of 53

Go Back

Full Screen

Close

Quit

1. What is MySQL?

• General Purpose Open Source RDBMS

• Optimized for speed, reliability and high load - nar-rower, deeper feature set.

• Incredibly well-supported and documented - easy toinstall, configure and manage

• Extensively used worldwide (more than 10 000 serverdownloads per day)

• Flexible: Transactional/Non-transactional storage en-gines

• Low cost of ownership - Dual-licensed under GPL andCommercial licenses

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 4 of 53

Go Back

Full Screen

Close

Quit

1.1. Design Philosophy

• Modular design

• Never pay in performance for unused features

• Easy to install and use

• Write documentation while writing code

• Use regression and limit tests

• Occam’s Razor: No complexity beyond necessity

• Optimize, optimize, optimize

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 5 of 53

Go Back

Full Screen

Close

Quit

1.2. The Numbers

• Installation Base

– Difficult to judge

– Included in major Linux distributions

– Example: 1 million copies has been shipped withMacOS X

• >10000 server downloads per day

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 6 of 53

Go Back

Full Screen

Close

Quit

• Links according to Google.com

MySQL 30,100Oracle 15,500Postgres 6,890

• Mentions according to Google.com

MySQL 4,850,000Oracle 5,930,000PostgreSQL 1,760,000

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 7 of 53

Go Back

Full Screen

Close

Quit

High-Profile Users

• NASA

• Yahoo: Finance, News in Asia & Europe,Sports...

• Slashdot.org, Freshmeat.net & Linux.com

• Nortel (InSight) / Ericsson / Alcatel / Telia/ Nokia

• Motorola

• Compaq

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 8 of 53

Go Back

Full Screen

Close

Quit

MySQL Features

• An extended subset of ANSI SQL89.

• Numerous extensions like SELECT .... LIMIT 10,10

• Very few limits: disk, cpu, memory and OS set thelimits

• Localized to many languages (Japanese, Chinese, Ger-man)

• Powerful security system

• Master-/Slave Replication for High Availability

• Per table choice of backend (with/without transac-tions)

• Can be extended with User Defined Functions (UDF)

• MyISAM has very compact tables

• Embeddable engine (no client server)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 9 of 53

Go Back

Full Screen

Close

Quit

APIs

ADA CC++ Common LispDelphi DylanGuile JDBCMatLab ODBCPHP PikePython TCL. . .And many more

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 10 of 53

Go Back

Full Screen

Close

Quit

Storage Engine: MyISAM

• Developed by MySQL AB (replaced original ISAM)

• Static, dynamic and compressed (read-only) row for-mats but no transactions

• Text and compressed indexes

• Data and indexes in separate files

• Fast read/write performance but low r/w concurrency

• Extremely good concurrency in the select and insertat end case (logs)

• External check and repair program (myisamchk)

• Especially useful for websites & logging

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 11 of 53

Go Back

Full Screen

Close

Quit

Why use Free/Open Software

• Independence

• Access to all technical information and notonly a vendor chosen subset

• Any good technical person has the samechance to become a expert as company em-ployees

• Competing commercial services available

• Expanded use because of lower investmentin time and money before

• No extra fees for advanced features likereplication/free text search

• Result: Low Total Cost of Ownership

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 12 of 53

Go Back

Full Screen

Close

Quit

Free/Open is Better

• Community

– Additional testing, development, business intelli-gence, etc. resources

– Find bugs faster, on more platforms

– Everyone wins - original developers, end users,other software projects

– Trains and exposes skilled developers

• Strong advantages for all users - including proprietaryusers

– Less risk

– Deep integration is much easier

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 13 of 53

Go Back

Full Screen

Close

Quit

SQL

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 14 of 53

Go Back

Full Screen

Close

Quit

2. SQL: Structured Query Lan-guage

• Originally called SEQUEL (SEnglishQL)

• Data Definition : DDL

• Data Manipulation : DML

• Data Control : DCL

• SQL-86(ANSI): SQL1, SQL-92: SQL2, SQL3 underdevelopment

• MySQL (Latest: MySQL4.1): Extended Entry-levelSQL-92. ODBC levels 0-3.51Doesn’t support Procedures and Triggers yet

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 15 of 53

Go Back

Full Screen

Close

Quit

Data Definition

Databases=directories; Tables=files(.myd,.myi,.frm)

Create Database

• CREATE DATABASE [IF NOT EXISTS] db namemysql> create database university

Accessing a Database

• USE db namemysql> use university;

Dropping a Database

• DROP DATABASE [IF EXISTS] db namemysql> drop database if exists olduniversity;

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 16 of 53

Go Back

Full Screen

Close

Quit

Create Table

• CREATE [TEMPORARY] TABLE [IF NOT EXISTS]tbl name [(create definition,...)][table options] [select statement]

• CREATE [TEMPORARY] TABLE [IF NOT EXISTS]tbl nameLIKE [(] old tbl name [)] ;

create definition:col name type [NOT NULL | NULL] [DEFAULTdefault value] [AUTO INCREMENT][[PRIMARY]KEY] [COMMENT ’string’] [reference definition]| PRIMARY KEY (index col name,...)| UNIQUE [INDEX] [index name] (index col name,...)| [CONSTRAINT symbol] FOREIGN KEY[index name] (index col name,...)| CHECK (expr)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 17 of 53

Go Back

Full Screen

Close

Quit

MySQL Datatypes

Numeric Data TypesColumn type Storage requiredTINYINT 1 byteSMALLINT 2 bytesINT 4 bytesINTEGER 4 bytesBIGINT 8 bytesFLOAT(X) 4 (X<=24) or 8 (25<=X<=53)FLOAT 4 bytesDOUBLE 8 bytesREAL 8 bytesDECIMAL(M,D) M+2 or M+1 or D+2 bytesNUMERIC(M,D) M+2 or M+1 or D+2 bytes

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 18 of 53

Go Back

Full Screen

Close

Quit

Date and Time Types

Column type Storage requiredDATE 3 bytesDATETIME 8 bytesTIMESTAMP 4 bytesTIME 3 bytesYEAR 1 byte

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 19 of 53

Go Back

Full Screen

Close

Quit

String Types

Column type Storage requiredCHAR(M) M bytes, 1 <= M <= 255VARCHAR(M) L+1 bytes,L <= MTINYBLOB, TINYTEXT L+1 bytes, L < 28

BLOB, TEXT L+2 bytes, L < 216

LONGBLOB, LONGTEXT L+4 bytes, L < 232

ENUM(’value1’,’value2’,. . . ) 1 or 2 bytes, 65535 values maxSET(’value1’,’value2’,. . . ) 1, 2, 3, 4 or 8 bytes, 64 members max

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 20 of 53

Go Back

Full Screen

Close

Quit

The Great Indian University

3 tables:

StudentRoll No 10 Characters Primary keyName 20 Characters, necessaryAddress 20 CharactersCity 20 charactersPhone no numeric

ExamRoll No 10 Characters, necessaryCourse CodeName 10 Characters, necessaryMarks numeric between 0 and 100

CourseCourse code 10 Characters, necessaryCourse Name 20 Characters, necessaryfaculty codeMarks 10 Characterspr text book 20 Charactersteach slot 10 characters

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 21 of 53

Go Back

Full Screen

Close

Quit

Write the SQL Statements to create the

University tables.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 22 of 53

Go Back

Full Screen

Close

Quit

mysql> create table student-> ( roll_no char(10) Primary Key,-> Name VarChar(20) not null,-> Address Varchar(40),-> City VarChar(20),-> Phone_no Integer );

Query OK, 0 rows affected (0.07 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 23 of 53

Go Back

Full Screen

Close

Quit

mysql> describe student;+----------+-------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+----------+-------------+------+-----+---------+-------+

| roll_no | varchar(10) | | PRI | | |

| Name | varchar(20) | | | | |

| Address | varchar(40) | YES | | NULL | |

| City | varchar(20) | YES | | NULL | |

| Phone_no | int(11) | YES | | NULL | |

+----------+-------------+------+-----+---------+-------+

5 rows in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 24 of 53

Go Back

Full Screen

Close

Quit

mysql> create table Exam-> ( roll_no char(10) not null references student.roll_no,

-> course_code char(10) not null references course.course_code,

-> Marks TinyInt Unsigned default 0,

-> Primary Key(roll_no,course_code),

-> Constraint marks_check check (Marks <= 100));

Query OK, 0 rows affected (0.08 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 25 of 53

Go Back

Full Screen

Close

Quit

mysql> create table course-> ( Course_code Char(10) Primary Key,

-> faculty_code char(10),

-> pr_text_book varchar(20),

-> teach_slot char(10) );

Query OK, 0 rows affected (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 26 of 53

Go Back

Full Screen

Close

Quit

Alter and Rename Table

• ALTER TABLE tbl name alter specification[, alter specification ...]

• alter_specification:ADD [COLUMN] create_definition [FIRST | AFTER column_name ]

| ADD PRIMARY KEY (index_col_name,...)

| ADD UNIQUE [index_name] (index_col_name,...)

| ADD [CONSTRAINT symbol] FOREIGN KEY [index_name]

(index_col_name,...) [reference_definition]

| ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}

| CHANGE [COLUMN] old_col_name create_definition

[FIRST | AFTER column_name]

| DROP [COLUMN] col_name

| DROP PRIMARY KEY

| RENAME [TO] new_tbl_name

| table_options

• RENAME TABLE tbl name TO new tbl name[, tbl name2 TO

new tbl name2,...]

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 27 of 53

Go Back

Full Screen

Close

Quit

3. DML: Queries

The SELECT statement• SELECT [DISTINCT | DISTINCTROW | ALL]

select_expression,...

[INTO {OUTFILE | DUMPFILE} ’file_name’ export_options]

[FROM table_references

[WHERE where_definition]

[GROUP BY <Grouping Attributes> [ASC | DESC], ...]

[HAVING where_definition]

[ORDER BY <Attribute List> [ASC | DESC] ,...]

• MYSQL extensions. . .

[LIMIT [offset,] row_count | row_count OFFSET offset]

[PROCEDURE procedure_name(argument_list)]

[FOR UPDATE | LOCK IN SHARE MODE]]

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 28 of 53

Go Back

Full Screen

Close

Quit

Student table

+------------+---------------+--------------+-----------+------------+------------+| roll_no | Name | Address | City | Phone_no | DOB |+------------+---------------+--------------+-----------+------------+------------+| 2001MBA001 | Bimal Jalan | Parliament | New Delhi |NULL | 1960-01-05 || 2001MBA007 | Harshad Mehta | Central Jail | Mumbai | 2147483647 | 1971-12-24 || 2001MCA011 | Sachin | Fiat Motors | Mumbai | 221111001 | 1984-04-24 |+------------+---------------+--------------+-----------+------------+------------+

Course Table

+-------------+-----------------+--------------+--------------------+------------+| Course_code | Course_name | faculty_code | pr_text_book

| teach_slot |+-------------+-----------------+--------------+--------------------+------------+| MBA001001 | Business Ethics | MBAfac007 | ArthaShastra

| C || MBA007007 | Share Swindling | MBAfac007 | SEBI Manual

| A || MBA007009 | Cyber Fraud | MCAfac001 | The Crackers Guide | B |+-------------+-----------------+--------------+--------------------+------------+

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 29 of 53

Go Back

Full Screen

Close

Quit

Exam Table+------------+-------------+-------+| roll_no | course_code | Marks |+------------+-------------+-------+| 2001MBA001 | MBA001001 | 75 || 2001MBA007 | MBA001001 | 100 || 2001MCA011 | MBA001001 | 90 || 2001MBA007 | MBA007007 | 30 || 2001MBA007 | MBA007009 | 100 || 2001MCA011 | MBA007009 | 90 || 2001MCA011 | MBA001009 | 55 |+------------+-------------+-------+

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 30 of 53

Go Back

Full Screen

Close

Quit

Insertion example

mysql> insert into Exam values-> ("2001MBA001","MBA001001",75),-> ("2001MBA007","MBA001001",100),-> ("2001MCA011","MBA001001",90),-> ("2001MBA007","MBA007007",30),-> ("2001MBA007","MBA007009",100),-> ("2001MCA011","MBA007009",55);

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 31 of 53

Go Back

Full Screen

Close

Quit

Display the Examination Data.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 32 of 53

Go Back

Full Screen

Close

Quit

mysql> select * from Exam;+------------+-------------+-------+| roll_no | course_code | Marks |+------------+-------------+-------+| 2001MBA001 | MBA001001 | 75 || 2001MBA007 | MBA001001 | 100 || 2001MCA011 | MBA001001 | 90 || 2001MBA007 | MBA007007 | 30 || 2001MBA007 | MBA007009 | 100 || 2001MCA011 | MBA007009 | 90 || 2001MCA011 | MBA001009 | 90 |+------------+-------------+-------+7 rows in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 33 of 53

Go Back

Full Screen

Close

Quit

List the names of all the students.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 34 of 53

Go Back

Full Screen

Close

Quit

mysql> select name from student-> ;

+---------------+| name |+---------------+| Bimal Jalan || Harshad Mehta || Sachin |+---------------+3 rows in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 35 of 53

Go Back

Full Screen

Close

Quit

List the names of all the students with-

out repeating any name.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 36 of 53

Go Back

Full Screen

Close

Quit

mysql> select distinct name from student;+---------------+| name |+---------------+| Bimal Jalan || Harshad Mehta || Sachin |+---------------+3 rows in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 37 of 53

Go Back

Full Screen

Close

Quit

List all the Primary Text books display-

ing the header as ”Primary Text Books”

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 38 of 53

Go Back

Full Screen

Close

Quit

mysql> select c.pr_text_book as"Primary Text Books" from course c ;

+--------------------+| Primary Text Books |+--------------------+| ArthaShastra || SEBI Manual || The Crackers Guide |+--------------------+3 rows in set (0.01 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 39 of 53

Go Back

Full Screen

Close

Quit

Suppose Average marks for the Course

with code ”MBA001001” is 65. List

the deviation of marks from the average,

with a meaningful header and with the

roll number of students for that course.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 40 of 53

Go Back

Full Screen

Close

Quit

mysql> select roll_no, marks-65 as"Deviation from Average" fromExam where course_code="MBA001001";

+------------+------------------------+| roll_no | Deviation from Average |+------------+------------------------+| 2001MBA001 | 10 || 2001MBA007 | 35 || 2001MCA011 | 25 |+------------+------------------------+3 rows in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 41 of 53

Go Back

Full Screen

Close

Quit

List Roll Numbers of those students who

have got second division (50 to 60) in

any subject.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 42 of 53

Go Back

Full Screen

Close

Quit

mysql> select distinct roll_no from Examwhere marks between 50 and 60;

+------------+| roll_no |+------------+| 2001MCA011 |+------------+1 row in set (0.01 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 43 of 53

Go Back

Full Screen

Close

Quit

List courses taken by faculties with fac-

ulty codes - mbafac007 or mcafac007 or

mcafac001.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 44 of 53

Go Back

Full Screen

Close

Quit

mysql> select course_code from coursewhere faculty_code in("mbafac007","mcafac007","mcafac001");

+-------------+| course_code |+-------------+| MBA001001 || MBA007007 || MBA007009 |+-------------+3 rows in set (0.02 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 45 of 53

Go Back

Full Screen

Close

Quit

List the students by their name in al-

phabetic order .

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 46 of 53

Go Back

Full Screen

Close

Quit

mysql> select name from student order by name asc;+---------------+| name |+---------------+| Bimal Jalan || Harshad Mehta || Sachin |+---------------+3 rows in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 47 of 53

Go Back

Full Screen

Close

Quit

How many people have scored 100 marks

in any subect.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 48 of 53

Go Back

Full Screen

Close

Quit

mysql> select count(distinct roll_no) from Examwhere marks=100;+-------------------------+| count(distinct roll_no) |+-------------------------+| 1 |+-------------------------+1 row in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 49 of 53

Go Back

Full Screen

Close

Quit

What are the average marks in each sub-

ject.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 50 of 53

Go Back

Full Screen

Close

Quit

mysql> select course_code,avg(marks) from Examgroup by course_code;

+-------------+------------+| course_code | avg(marks) |+-------------+------------+| MBA001001 | 88.3333 || MBA007007 | 30.0000 || MBA007009 | 77.5000 |+-------------+------------+3 rows in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 51 of 53

Go Back

Full Screen

Close

Quit

Give 10% grace marks to all those who

are failing (<40) in any subject, in that

subject.

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 52 of 53

Go Back

Full Screen

Close

Quit

mysql> update Exam set marks=marks*1.1-> where marks < 40;

Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from Exam;+------------+-------------+-------+| roll_no | course_code | Marks |+------------+-------------+-------+| 2001MBA001 | MBA001001 | 75 || 2001MBA007 | MBA001001 | 100 || 2001MCA011 | MBA001001 | 90 || 2001MBA007 | MBA007007 | 33 || 2001MBA007 | MBA007009 | 100 || 2001MCA011 | MBA007009 | 55 |+------------+-------------+-------+6 rows in set (0.00 sec)

What is MySQL?

SQL: Structured . . .

DML: Queries

Home Page

Title Page

JJ II

J I

Page 53 of 53

Go Back

Full Screen

Close

Quit

Acknowledgements

• My Colleague, Kaushal Kishore Tripathi for providingmaterial on MySQL.

• Prof. Akshay Kumar, IGNOU for his advice(s).

• Priti Sinha, In-Charge, LILA for allowing time-out toteach the course BM852 at I.I.T. Roorkee for whichthese slides were prepared.

• The cvr (CV RadhaKrishnan) for giving pdfscreen.


Recommended