SUNBEAM
SUNBEAM
Institute of Information Technology
Course
Student NameSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD \
File
+ Collection of data/info on secondary storage device.
+ File = Contents (Data) + Information (Metadata).
+ Metadata - Fil
© Type,
ize, user, group, permissions, time-stamps
© Location of data on disk,
+ Files are stored in FileSystem.
+ FileSystem is on disk partition, logically divided into 4 sections
© Boot block, Super block, i-Node list, Data blocks.
DBMS - Database Management System
Is a program that store data, retrieve data & manipulate data.
CRUD operations
© Create -- Insert new record.
© Retrieve -- Select existing records.
© Update -- Modify existing record,
© Delete -- Delete existing record.
+ Generic program for any data eg. students, financial, employees, exams,
+ Example: Excel/Spreadsheets, Foxpro, Dbase,..
RDBMS - Relational DBMS
«Isa generic program that store data, retrieve data & manipulate data ie. perform CRUD
operations efficiently?
+ RDBMS organize data into Tables, Rows & Columns. These tables are related to each other -~
Relational DBMS.
DBMS RDBMS
Fields, Records, File Columns, Rows, Tables
Relations in file are program Built-in relational feature
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
DBMs
Heavy programming
Heavy network use (whole file)
Client side processing
RDBMS
Built-in CRUD & other operations
Less network use (only record)
Server side processing
Slower
Faster
Huge data is not supported(MB)
Huge data support (100s of GB)
No networking support
Client-server arch
|_Single-user systems
Multi-user systems
File locking
Row level locking
No distribted db support
Built-in support for clustering
No security
Db auth, object level security
Different RDBMS
+ MySQL: Michal - Sun - Oracle - Open Source (Community Ed).
‘9 Most used open source db.
© MySQL 5.x-- MySQL 80.15
+ MariaDb: Fully open source (clone of MySQL).
‘+ Oracle: Enterprise database -- Express edition (educational).
+ MSSQL server: Microsoft - Enterprise database -- Only windows.
«Informix: Fastest (Need assembly language knowledge).
+ Sybase: Taken by SAP.
+ PostreSQL: Open Source.
‘= DB2:IBM - Mainframe systems.
MsAccess, Paradox, SQLite: Small RDBMS (not all features).
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SQL - Structured Query Language
+ ROBMS data processing is done using SQL queries.
+ Client fre query on server, server process query, produce result and send result back to client.
+ ANSI standardised -- 1987, 1989, ., 2016.
+ Case-insensitive language.
© On Linux, table names & db names are case sensitive.
+ Categories:
‘© DDL Data Definition Language
‘+ CREATE, DROP, RENAME, ALTER
© DML: Data Manipulation Language*
+ INSERT, UPDATE, DELETE
© DQ: Data Query Language"
+ SELECT
© DCL: Data Control Language
+ CREATE USER, GRANT, REVOKE
© TCL: Transaction Control Language
+ Transaction management
+ Transaction is “set of queries executed as single unit”.
+ Ifany query fils effect of remaining queries is discarded.
+ START TRANSACTION, SAVEPOINT, COMMIT, ROLLBACK
Naming conventions
+ Table & column names can contain alphabets, digits and some special symbols eg. $, #,
+ Names must start with alphabets.
+ fname contains special symbol, then name must be enclosed in back-quotes eg, 118
+ Names can be max 30 chars long.
+ Names should be readable,
DATABASE TECHNOLOGIESNeen ee
‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
a
MySQL - RDBMS software
MySQL Installation - Ubuntu
cend> sudo apt-get install aysql-conmunity-server mysql-connunity-client
«This installs "MySQL server" ie. mysqld and MySQL client ie. mysql.
+ mysqld:
Server, No UL
© Implemented in C/C++.
Installed on RDBMS server machine.
© Process the data & generate result.
‘© Can be accessed from any MySQl client program.
+ MySQL CLI (mysql)
+ MySQL Workbench (GUI)
+ phpmyadmin (Web based)
+ mysqi
© Command line client for MySQL
cnd> sudo systenct status aysql
cnd> sudo systenctl stop eysql
md> sudo systenct] start mysql
cend> sudo netstat -tInp
cend> mysql -V
‘+ During installation of mysql server, its admin user is created with name root. User need to
specify the password.
end> mysql -u root -p
end> mysql -h localhost -u root -p
# client connect to mysql server of local machine.
# localhost can be replaced by ip addr of different server achine.
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Creating User & Database
+ Itis not recommended to use root login for creating tables & doing CRUD operations.
+ Better practice is to create a separate database/schema, db user and give permission to the
user on that db.
+ Alldb objects i. tables, constraint, relations, procedures, triggers, functions, etc should be
created under some database/schema,
+ Usually one project's all tables & other objects are kept in one database/schema.
cend> mysql -u root =p
CREATE DATABASE dact
SHON DATABASES
CREATE USER dacGlocalhost IDENTEFIED BY ‘dac*;
SELECT user, host FROM mysql user;
GRANT ALL PRIVILEGES ON dacd.* TO dacglocelhost;
FLUSH PRIVILEGES;
+= activate new permissions
ext;
cend> mysql -u dac -p
‘HOW DATABASES;
SELECT USER(), DATABASE();
USE dacdb;
exIT
cend>- mysql. -u dac -p
oR
cend> mysql. -u dac -pdac
oR
cend> mysql -u dac -p dacdb
on
‘end> mysql -u dac -pdac dacdd
Using MySQL
USE dacdb;
CREATE TABLE contacts (id INT, name VARCHAR(48), mobile VARCHAR(12), email VARCHAR(4@), age INT);
SHOW TABLES;
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
DESCRIBE contacts;
INSERT INTO contacts VALUES (1, ‘Nilesh Ghule', '9527331338', ‘
[email protected]’, 35),
INSERT INTO contacts VALUES (2, ‘Nitin Kudale', '9881208115', ‘nitingsunbeaninfo.con’, 40), (5
“Prashant Lad", "9881208114", ‘prashantgsunbeaninfo.con', 40); .
SELECT * FROM contacts;
INSERT INTO contacts (id, name, enail) VALUES (4, ‘Sandeep Kulange’,
* sandeepkulangedsunbeaninfo.coa'
INSERT INTO contacts (id, name) VALUES (5, ‘Rahul Kale");
INSERT INTO contacts VALUES (5, ‘Rahul Sansudi", NULL, NULL, NULL);
SELECT * FROM contacts;
SELECT id, nase, sobile FROM contacts;
s+ with “toot” login
CREATE DATABASE classvork;
GRANT ALL PRIVILEGES ON classwork.* TO dac@localhost;
FLUSH PRIVILEGES;
ur;
== with "dac™ login
‘SHOW DATABASES;
USE classworks
SOURCE /path/to/classwork-db.sql;
SHOW TABLES;
SELECT query (DQL)
‘SHOW TABLES;
SELECT * FROM BOOKS;
SELECT name, subject, price FROY BOOKS;
SELECT name, subject, price, price * 0.05 FROH BOOKS;
DESC BOOKS;
DATABASE TECHNOLOGIES=
‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Computed Columns
SELECT nane, subject, price, price * 0.05 AS gst FROM BOOKS;
igs keyword 1s optional
SELECT nane, price, price * 0.05 AS gst, price + price * 0.05 total FROH BOOKS;
SEtjet 6 totel are computed columns.
LIMIT clause
SELECT * FROM BOOKS;
SELECT * FROM BOOKS LIMIT 5;
se fetch first 5 rows
SELECT * FROM BOOKS LIMIT 4, 5;
SD skip first 4 rows and fetch next 5 rows
Database layout
Logical layout
+ How data is represenated?
database’ is ike a namespace/container that stores all db objects related to a project.
It contains tables, costraints, relations, stored procedures, functions, triggers, ..
There are some system databases e.g. mysql, performance_schema, information schema, sys, ..
They contains db internal info.
© SELECT user, host FROM mysqluser,
+ The database contains tables.
Tables have multiple columns. Each column is associated with a data-type & zero/more
cconstaints
The data in table is in multiple rows. Each row have multiple values (as per columns).
Physical layout
«How data is stored on db server hard disk?
«+ In MySQL, the data is stored on disk in its data directory ie. /var/lib/mysql
+ Each database/schema is a separate sub-directory in data dir.
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
rane anne a a
+ Each table in the db, isa fle on disk e.g. BOOKS table in classwork db is stored in fle
‘Nar flib/mysql/classwork/BOOKS ibd.
+ Note that data is stored in binary format
+ Afile is not contiguously stored on hard disk & hence all data rows are not contiguous, They
are scattered in the hard disk.
+ Inone row, all fields are consecutive.
+ When records are selected, they are selected in any order. Hence we cannot fetch first n rows
INSERT query - DML
USE elasswork;
Desc a00Ks;
INSERT INTO BOOKS VALUES (9001, ‘Atlas Shrugged’, ‘Ayn Rand", ‘Novell', 456.562);
INSERT INTO BOOKS VALUES (8002, ‘The Fountainhead", ‘Ayn Rand", ‘Novell", 423.123), (9003, “the
Alchenist', "Paulo Coelo', ‘Novell", 321.345);
CREATE TABLE SOOKS2 (Jd INT, name VARCHAR(SO), author VARCHAR(5), subject VARCHAR(S0), price
ovate);
INSERT INTO BOOKS2 SELECT * FROM BOOKS;
SELECT * FROY BOOKS2;
CREATE TABLE BOOKS3 (id INT, author VARCHAR(S®), nane VARCHAR(S®), price DOUBLE, subject
‘VARCHAR(5®));
INSERT INTO BOOKS3 SELECT id, author, name, price, subject FROM BOOKS;
SELECT * FROM BOOKS:
‘TRUNCATE B00KS3; -- delete all records
INSERT INTO 800KS3 (i nane, author subject, price) SELECT * FROM E00KS;
SELECT * FROM BOOKS3;
CREATE TABLE B0OKSS (id INT, price DOUBLE, nane VARCHAR(5®));
INSERT INTO BOOKSS SELECT id, price, name FROM BOOKS;
== OR
INSERT INTO BOOKSS (1d, price, name) SELECT id, price, name FROM BOOKS;
SELECT * FROM BOOKSS;
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
————————eeeee
CREATE TABLE - DDL
CREATE TABLE BOOKSS AS SELECT * FROM BOOKS;
scopy schesa & date
DESC BOOKS;
SELECT * FROM BOOKSS;
CREATE TABLE BOOKSS AS SELECT * FROM BOOKS LIMIT 6;
== copy only schema, not data
DESC BoOKS6;
SELECT * FROM BOOKSS;
SHOW TABLES;
DISTINCT values
SELECT * FROM EMP;
SELECT * FROM DEPTS
SELECT DISTINCT deptno FROM EMP;
SELECT DISTINCT job FROM ENP;
ORDER BY clause
+ Indb rows are scattered on disk. Hence not fetched in a fixed order.
SELECT * FROM EMP;
SELECT * FROM EMP ORDER BY sal;
SELECT * FROM EMP ORDER BY sal DESC;
SELECT * FROM EMP ORDER BY job ASC;
SELECT * FROM EMP ORDER BY deptno;
SELECT * FROM EHP ORDER BY deptno, sal DESC;
SELECT * FROM ENP ORDER BY deptno DESC, job, sal DESC;
DATABASE TECHNOLOGIESPUNE & KARAD
SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY,
+ When query is fired from client:
Db server will load the data from disk into its RAM.
ii, Using sorting algorithm data will be sorted there (on given col)
Processed result is sent back to the client.
«+ More are columns to sort, more is the time taken for sorting.
SELECT * FROM ENP
ORDER BY sal DESC
LIMIT 1
o> highest sal
SELECT * FROM ENP
ORDER BY sal ASC
LIMIT 2,35
== third lowest sal
Criteria - WHERE clause
Relational operators
<>
~ SELECT cols FROM table WHERE condition;
SELECT * FROM EHP WHERE enpno = 7900;
SELECT * FROM ENP WHERE sal > 2500;
SELECT enane, sal, com FROM EMP WHERE comm > 0.05
SELECT * FROM EMP AHERE comm = NULL; -- won't ork
NULL related operators
+ NULL values canriot be compared using above relational operators.
+ There are special operators for comparting NULL values:
o ISNULLor
o ISNOT NULL
SELECT * FROM ENP NHERE conn 1S NULL;
SELECT * FROM ENP KHERE conn <=> NULL;
10
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SELECT * FROM EMP WHERE comm IS NOT NULLS
Logical Operators
+ AND, OR &NOT
SELECT * FROW ENP WHERE job = "SALESHAN* AND sal <= 1500;
= find all salesman whose sal is below 1509
SELECT * FROM EHP WHERE job = “SALESMAN AND comm IS NULL;
s+ find all salesman who are not getting conn.
+ there are no such records
SELECT * FRON EMP KHERE sal
1000.00 AND sal <= 2000.00;
SELECT * FRON EMP NHERE job = ‘SALESMAN’ OR sal >= 2500;
“+ find all enps who are either salesman or have sal
2500.
SELECT * FRON EMP KHERE enpno = 7980 OR enpno = 7:
SELECT * FROM ENP KHERE job I= "SALESHAN' AND job I= "ANALYST";
s+ find all enps who are neither salesman nor analyst.
SELECT * FROM ENP MERE NOT (job = "SALESMAN" OR Job = ‘ANALYST*);
== find all enps who are neither salesman nor analyst.
BETWEEN operator
SELECT * FROM ENP KHERE sal BETHEEN 1100 AND 1600;
~ find enployee with sal greater than or equal 1108 & less than or equal 1600.
BETWEEN includes both ends of given range.
SELECT * FROM ENP KHERE hire BETWEEN "1982-01-01" AND ‘3982-12-31
~ find enployee joined in year 1982.
~ dates are in format "yyyy-nn-dd'
SELECT * FROM EMP ORDER BY ename;
SELECT * FROM EMP WHERE enane BETWEEN 'B" AND "F)
‘find enployees whose nane start fros 8 to F.
FORD is excluded (due to alphabetical order).
SELECT * FROM ENP WHERE enane BETWEEN "8" AND ‘6
SELECT * FROM ENP WHERE enane BETWEEN "B' AND
‘AND enane =
u
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
IN operator
SELECT * FROM ENP KHERE enpno = 7900 OR enpno = 7782 OR enpno = 78395
s+ find exployees whose enpno is 7920, 7782 or 7839.
SELECT * FROM EMP KHERE empno IN (7900, 7782, 7838);
find employees whose enpno is 7900, 7782 or 7839.
SELECT * FROM EMP WHERE job IN ("SALESHAN', ‘CLERK’, "PRESIDENT";
s+ find all salesman, clerk & president.
SELECT * FROM ENP KHERE deptno NOT IN (10, 20);
“= find all enps not in dept 10 & 28.
WHERE clause execution
‘+The condition in WHERE clause is checked for each row in table.
= Example:
SELECT * FROM ENP NHERE deptno = 30 AND sal > 15095
+ This query is executed similar to following C code:
Hnclude
struct emp {
struct exp arr[]
int 43
For(ie0; i <= 13; 444)
30 88 arr{i].sal > 1500)
if(are {i} deptno =
{
: printé("xd, Md, Xf, ...\n", arr[i]-enpno, arr[i].deptno, arr[i].sal);
r
return 0;
DATABASE TECHNOLOGIES
nSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
LIKE operator
+ %is any number of any characters.
any one character
SELECT * FROM ENP WHERE enane LIKE 'S%*;
sNfind enps whose name start with “S"
SELECT * FROM EMP WHERE enace LIKE 'XER
‘find enps whose nane end with “ER”
“SELECT * FROM EMP WHERE enane LIKE ‘Ux’
SX"find exps whose name contains "U".
SELECT * FROM EMP WHERE enane LIKE *
Sr" find emps whose nares are of 4 chars.
SELECT * FROM ENP WHERE enane LIKE "20_*;
Se"find enps whose name contains “O" & then tho chars.
CASE-WHEN statement
+ Used to generate a computed column in SELECT.
SELECT enane, deptno FROM EMP;
SELECT deptno, dnane FROM DEPT;
20 = ACCOUNTING, 20 - RESEARCH, 30 - SALES, 40 ~ OPERATIONS
SELECT enane, deptno,
case
WIEN deptno = 10 THEN ‘ACCOUNTING
WEN deptno = 28 THEN ‘RESEARCH’
WHEN deptno = 30 THEN ‘SALES"
ELSE ‘OPERATIONS®
et
FROM EMP;
SELECT enane, deptno,
case
WHEN deptno = 10 THEN ‘ACCOUNTING’
WHEN deptno = 20 THEN ‘RESEARCH’
WHEN deptno = 30 THEN "SALES"
ELSE "OPERATIONS"
END AS dnane
FROM EMP;
DATABASE TECHNOLOGIES
BSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
UPDATE query - DML
+ To change one or more rows in a table.
+ fnew value of afield needs same space as of old value, then record is updated in place (on
disk)
+ If size of new value is more than size of old value, then record cannot be updated in place
(because it will increase the size of whole record). In this case, rdbms server create a new copy
of the record with additional size requirement & then update that record. The record space is
‘marked as blank. Such updates increase fragmentation.
SELECT * FROM BOOKS;
syntax:
UPDATE table SET coli=valuel, ... WHERE condition;
UPDATE BOOKS SET price=150.123 WHERE id = 1004;
cchange price of book 1094 to 150.123.
UPDATE 800KS SET prices160.123, author="K & R', nane='C Prograsming Language’ WHERE id = 1004;
== change price of book 1064 to 160.123, author to "K & R” and nane to "C Progranming Language”,
sicetprice?9.10 WHERE subject
books by 16%.
‘Java Programming’;
UPDATE BOOKS SET price
=> increase price of all ja
UPDATE BOOKS SET [email protected];
== change all books price to 150.123;
UPDATE BOOKS SET id=1005, name='C Lang’, autho 23 WHERE id = 1004;
= change all columns --'not used conmonly.
DELETE query - DML
+ One or more rows of a table are deleted.
DELETE FROM BOOKS MHERE subjects "Java Programming’ ; .
== delete multiple rows
DELETE FROM 800KS;
== delete all rows
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
TRUNCATE query -- DDL
‘TRUNCATE TABLE BOOKS;
DELETE
TRUNCATE
Table structure is not changed
Table structure is not changed
Delete all rows (if no condition)
Delete all rows
Can have WHERE clause
No WHERE clause
DML query
DDL query
Can be rollbacked (using tx)
Cannot be rollbacked
File size does not change
File size shrink
Slower
Faster
DROP query -- DDL
‘DROP TABLE BOOKS3;
[DROP TABLE B0OKS7; -- error if table not present
[DROP TABLE IF EXISTS BOOKS7;
DROP
‘TRUNCATE
Table struct + data is deleted
Only data is deleted
DDL Query
DDL Query
Table file is deleted.
Table file size
hrinked
Data Types
+ Various data types are supported in MySQL.
+ Different db support different data types (they may be similar, but not same).
+ Categories:
© Numeric types
DATABASE TECHNOLOGIES
asSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
+ Integer types
+ TINYINT (1 byte)
+ SMALLINT (2 bytes)
+ MEDIUMINT (3 bytes)
+ INT(A bytes)
+ BIGINT (8 bytes)
+ integer types can signed (default) or unsigned.
+ BIT number of bits can be given.
+ Floating point types
+ Approx precision
+ FLOAT (4 bytes) - single precision
+ DOUBLE (8 bytes) - double precision
+ Exact precision
+ DECIMAL - size depend on given precision
+ (an): m digits & n digits ater decimal pt
© String types
+ Char-wise storage.
*+ Each char can be 1 bytes (ASCID, 2 bytes (UNICODE) & so on... depending
‘on char encoding
+ CHAR(a) -n can be max 255,
+ Fixed length.
+ Ifgiven small string, rest of space is not used.
+ Ifgiven bigger string, gives error.
+ Very fast access.
+ VARCHAR(n) - n can be max 65535 (64K)
+ Variable length,
+ Internally stores chars + length.
+ Depending on entered string, space is allocated.
+ Ifgiven smal string, wil take less space.
+ If given bigger string, gives error.
+ CHAR & VARCHAR are stored inline to the row.
+ TEXT are stored outside the record (its addr is in record).
+ Used for huge text data.
+ Very slow.
DATABASE TECHNOLOGIES |‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
+ Different sizes:
* TINYTEXT (max 255 chars)
+ TEXT (max 64K chars)
+ MEDIUMTEXT (max 16M chars)
+ LONGTEXT (max 4G chars)
ary types
+ CHAR -- BINARY
+ VARCHAR -- VARBINARY
+ Stored external to record,
+ Byte-wise storage.
+ Used for binary files e.g, images, audio, video, pdf, ..
* Using binary types increase db size quickly & slow down processing. Hence
usually not recommended
+ Different sizes:
+ TINYBLOB (max 255 bytes)
+ BLOB (max 64K bytes)
+ MEDIUMBLOB (max 16M bytes)
+ LONGBLOB (max 46 bytes)
© Miscellaneous types
+ BOL - Like TINYINT ie. 1 byte.
+ TRUE-1
+ FALSE-0
+ ENUM - Internally an int
+ Restrict values
+ eg. Gender: Male, Female.
+ eg. OrderStatus: Pending, Processing, Delived, Dispatched.
© Date/Time types
+ DATE - 3 bytes - num of days from 1-1-1000.
+ TIME - 3 bytes - timespan.
+ DATETIME - 8 bytes - store both date & time
+ TIMESTAMP - 4 bytes - num of secs from 1-1-1970
* auto updated on DML operation,
+ YEAR - 1 byte - year 1901 to 2155
v7
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
CREATE TABLE t1 (€1 INT, 2 INT UNSIGNED);
CREATE TABLE 2 (c1 INT, c2 INT(S), €3 TNT(5) ZEROFTLL);
s- size of c1, c2 8 c3 ds sane i.e, 4 bytes.
+> (5) represent expected max num of digits.
INSERT INTO t2 VALUES (12, 12, 12);
== 3 will be displayed as "00012".
INSERT INTO 2 VALUES (12345, 12345, 12345);
INSERT INTO t2 VALUES (1234567, 1234567, 1234567);
s+ no error, accepted by ab
SELECT * FROM 12;
CREATE TABLE t3 (€1 FLOAT, 2 DOUBLE, 3 DECIHAL(S,3))5
INSERT INTO t3 VALUES (1.2345, 1.2345, 1.2345);
SELECT cl, €2, €3 FROM 13;
SELECT c1/11.0, €2/11.0, €3/11.0 FROM t3;
CREATE TABLE t4 (cl CHAR(10), 2 VARCHAR(I0), 3 TEXT(10));
INSERT INTO t4 VALUES (‘abe', ‘abc", “abe”
INSERT INTO t4 VALUES (‘abcdefghij", ‘abedefghij", ‘abedefghij");
INSERT INTO t4 VALUES (“abcdefghijk', ‘abcdefghidk", ‘abedefghijk"); -- error
INSERT INTO t4 VALUES (‘abcdefghij", ‘abcdefghij', ‘abedefghijk' )
CREATE TABLE tS (name VARCHAR(20), birth DATETIME, modified TIMESTAMP);
DESCRIBE t5;
INSERT INTO tS (name, birth) VALUES (Nilesh, '1983-09-21
INSERT INTO tS (name, birth) VALUES (‘Vishal", '1980-01-c1
SELECT * FROM 85;
INSERT INTO t5 (name, birth, modified) VALUES (‘Rahul', '1980-12-31', NOW());
DUAL table
+ Adummyfin-memory a table having single row & single column,
+ Itis used for arbitrary calculations, testing functions ..
SELECT NOW() FROM DUAL;
8
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SELECT 2 + 3 * 4 FROM DUAL;
SELECT USER(), DATABASE() FROM DUAL;
+ Historically this table is first time is used by Oracle. At that time they develop table with two
rows & hence name given was DUAL. Later on table changed to single row, but name kept
same,
+ This keyword is accepted by ANSI But lot of db (including MySQL) keep it optional.
SELECT NOW();
SELECT 243* 4;
SELECT USER(), DATABASE);
Help
+ MySQL client provide help on commands & functions.
s+ syntax: HELP topic;
HELP INSERTS
HELP SELECT
HELP Functions;
HELP String Functions;
Single Row Functions
+ These functions work on each row of table.
«Input to fn is data from single row & then output is single row. Hence these functions are
called as "Single Row Functio
String Functions
eve concar;
SELECT CONCAT(“Sunbean’, ‘Infotech*
SELECT CONCAT(*Sunbean’, * *, ‘Infotech');
19
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SO
SELECT enane, job FROM EHP;
SELECT CONCAT(ename, ' - ', job) AS empjob FROM EMP;
s+ print “name of exp - job".
SELECT mane, author FROM BOOKS;
SELECT UPPER(name), LOWER(author) FROM BOOKS;
Eup suastRIN;
SELECT SUBSTAING(Sundeam Infotech', 4)3
SELECT SUBSTRING("SunBean Infotech*, 4, 2);
SELECT SUBSTRING( ‘Sunde: blank
Infotech', 4, 0):
SELECT SUBSTRING(‘SunBean Infotech’, 4, -2)3 -- blank
SELECT SUBSTRING( SunBean Infotech', -4);
SELECT SUBSTRING( SunBean Infotech*, -8, 4);
SELECT nare, SUBSTRING(author, 1, 4) FROM BOOKS;
+> print nane & first 4 letters of author name
SELECT UPPER(SUBSTRING(nane, -8)) FROM BOOKS;
++ print last 8 letters of name in captical case.
HELP. TRIM;
s+ removes leading & trailing white-spaces from string.
SELECT TRING? abe ys
SELECT * FROM BOOKS;
SELECT REPLACE(nane, ‘Complete’, Full") FROM BOOKS;
SELECT ASCIT(‘A'), ASCIT(‘a"), CHAR(8®), CHAR(122);
SELECT name, SUBSTRING(nane, 3, 2) FROM B00KS;
SELECT nane, ASCIT(SUBSTRING(nane, 3, 1)) FROM BOOKS;
SELECT * FROM BOOKS WHERE ASCIT(SUBSTRING(nane, 3, 1}) BETWEEN 65 AND 905
+ find books whose third letter is in capital.
SELECT enane, INSTR(enane, A") FROM EMP;
SELECT enane, INSTR(ename, ‘A'), SUBSTRING(enane, INSTR(enane, ‘A')+1) FROM ENP;
SELECT enane, INSTR(enase, ‘A'), INSTR(SUBSTRING(enane, INSTR(enane, ‘A')s1), "AY) FROM EMP;
SELECT enane FROM EMP KHERE INSTR(SUBSTRING(ename, INSTR(ename, ‘A')41), A) > 0;
0
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SELECT ename FROM ENP WHERE ename LIKE 'XAX';
‘SELECT ename, INSTR(SUBSTRING(enane, INSTR(enane, */
8
62), °A") FROM ENP WHERE TUSTR(enane, *
SELECT enase FROM EMP WHERE INSTR(enane, °A') > @ AND INSTR(SUBSTRING(ename, INSTR(enome, ‘A‘)#2),
or) er
Numeric Functions
HELP Nunerde Functions;
SELECT PORER(2, 10)5,
SELECT AB5(101.123), ABS(-101.123);
SELECT FLOOR(2.4), CEIL(2.4), FLOOR(-2.8), CETL(-2.8)
SELECT ROUND(5432.23456, 3), TRUNCATE(S432.23456, 3);
SELECT ROUND(S432.23456, 0), TRUNCATE(S432.23456, 0);
SELECT ROUND(4567.23456, -3), TRUNCATE(AS67.23456, -3);
SELECT STGN(123), STGN(0), STGN(-123);
SELECT mane, price, price - 580 FROM BOOKS;
SELECT nane, price, SIGN(price ~ 588) FROM BOOKS;
SELECT name, price,
case
WHEN SIQN(price ~ $00) = 1 THEN "High Cost!
WHEN SIGN(price ~ $00) = -1 THEN ‘Low Cost*
END AS resark
FROM B0OxS;
Date Functions
HELP bate and Tie Functions;
‘SELECT NOW(), SLEEP(5), SYSDATE();
SELECT DATE(WOW()), THME(NO());
SELECT hire, DAY(Rire), HONTH(hire), YEAR(hire) FROM EMP LIKIT 3;
HELP aTE_Fornars
SELECT hire,
DATE_FORNAT(hire, ‘Rd-Xn-¥") di,
a
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
DATE FORMAT(hire, “Xd-40-3
DATE_FORMAT(hire,
FROW ENP LIMIT 3}
SELECT enane, hire,
DATEDIFF(NOW(), hire) “exp in days",
TINESTANPDIFF(HONTH, hire, NON()) “exp in months"
TINESTANPOTFF(VEAR, hire, NOW()) “exp in years”
FROM EMP LIMIT 35
SELECT enane, hire FROM EP
WHERE YEAR(hire) IN (1981, 1983);
SELECT hire, LASTDAY(hire) FROM ENP LIMIT 3;
NULL Functions
+ NULL represent absence of value.
+ NULLis kept in record when value of Not Applicable or Not Available.
+ NULLis not zero or blank string.
+ When NULL value is used with most of operators & built-in functions, result is NULL.
+ ISNULL
IS NOT NULL are operators for NULL value.
+ ISNULLO
© returns 2 if value is NULL
© similar to IS NULL operator.
SELECT ename, sal, comm, TSNULL(comm) FROM EMP5
+ IFNULLO
© IFNULL(col, value): Returns value if col is NULL.
SELECT enane, sal, comm, TFMILL(comm, 6) FROM ENP;
SELECT enase, sal, conn, sal + conn, sal + IFNULL(comm,0) FRON EMP;
+ NULUIFO
© NULLIF(valt, val2) if values are same, returns NULL.
© Iftwo values are dif, then return first value.
R
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SELECT NULLIF(‘A', '8");
SELECT MULLTF(‘A', *A");
SELECT MULLIF(NULL, "B");
+ COALESCE)
© COALESCE(V1, v2, v3,..): Retums first non-null value.
SELECT COALESCE("A', 'B', ‘C');
SELECT COALESCE(NULL, NULL, ‘C");
SELECT COALESCE(WULL, NULL, NULL);
List Functions
+ Work with list of values
+ Examples: CONCATQ, COALESCE(), GREATESTO, LEASTO.
+ GREATESTO, LEASTO can be used with numbers, strings and dates.
SELECT GREATEST(34, 87, 12, 76, 43);
SELECT LEAST(34, 87, 12, 76, 43);
SELECT ename, sal, GREATEST(sal, 1000) FROM EMP;
s+ display name & sal of enp. If sal below 1000, show it as 1600.
Aggregate Functions
+ These functions work on group of rows of table.
+ Input to fn is data from multiple rows & then output is single row. Hence these functions are
called as "Multi Row Function".
+ These fns are also called as "Group Functions".
+ These fns are used to perform aggregate ops like sum,
Hence these fns are also called as “Aggregate Function:
+ Example: SUMO, AVG, MAXO, MIN, COUNTO.
+ NULL values are ignored by group fns.
3, Max, min, count or std dev, ete.
SELECT COUNT(s@1), COUNT(coms) FROM ENP;
SELECT SUM(sal), SUM(coma) FROM ENP;
23
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SELECT AVG(sa1), AVG(comm) FROM ENP;
SELECT MAX(sa1), MAX(comm) FROM ENP;
SELECT MIN(sa2), MIN(comm) FROM EHP;
SELECT SUM(sa1) + SUM(comn) FROM, EMP;
SELECT SUN(sal + TFNULL(conm, @)) FROM ENP;
=- find total incone of all employees
+ Cannot select group fn along with a column.
+ Cannot select group fn along with a single row fn.
+ Cannot use group fn in WHERE clause/condition.
+ Cannot nest a grop fn in another group fn.
SELECT enane, SUM(sa1) FROM ENP; -- error
SELECT LOWER(enane), SUM(sal) FROM EMP;
SELECT * FROM EMP WHERE sal > AVG(sal); -- error
SELECT MAX(SUN(Sa1)) FROM ENP; -- error
GROUP BY clause
+ syntax:
= SELECT group_col, agg_fn, ... FROM table GROUP BY group_col;
SELECT deptno, COUNT(enpno) FROM EMP
GROUP BY deptno;
SELECT deptno, SUN(sal) FROM EMP
GROUP BY deptho;
SELECT deptno, COUNT(empno), SUM(sal), MAX(sal), MIN(sal), AVG(sal) FROM EMP
GROUP BY deptno;
o"Find num of emps, total sal, max sal, min sal, avg sal for each dept.
SELECT DISTINCT job FROM EHP;
SELECT Job, SUN(sa1), AVG(sal) FROM EMP
GROUP BY job;
SELECT DISTINCT YEAR(hire) FROM ENP
SELECT YEAR(hire), COUNT(empno) FROM EMP
GROUP BY YEAR(hire);
<= number of enp joined in each year.
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD_
SELECT deptno, job, enpno FROM ENP ORDER BY deptno, Job;
SELECT deptno, Job, COUNT(enpna) FROM EMP
GROUP BY deptno, Jobs
SELECT deptno, Job, COUNT(empno) FROM EMP
GROUP BY deptno, Job
(ORDER BY deptno, Job;
== find num of emps in each dept for each job.
SELECT deptno, COUNT(enpno) FROM EHP
GROUP BY deptne
‘ORDER BY deptno;
+ These queries are used for analysis of data ie, generating reports & charts.
+ When GROUP BY single column, generated output can be used to plot 2-D chart. When
GROUP BY two column, generated output can be used to plot 3-D chart and so on.
+ GROUP BY queries are also called as Multi-dimensional / Spatial queri
+ Syntactical Characteristics:
2. Ifacolumn is used for GROUP BY, then it may or may not be used in SELECT clause.
However, this is rae requirement.
© Ifa column is in SELECT, it must be in GROUP BY.
+ Internals:
i, Load data from server disk into server RAM.
Sort data on group by columns.
Group similar records by group columns.
iv. Perform given aggregate ops on each column.
v. Send result to client.
HAVING clause
+ HAVING clause cannot be used without GROUP BY clause.
+ HAVING clause is used to specify condition on aggregate values.
SELECT deptno, SUM(sal) FROM EMP
GROUP BY deptnos
SELECT deptno, SUM(sal) FROM ENP
GROUP BY deptno
HAVING SUN(sal) > 9000;
s+ find depts which are spending core than 9000 on emp sals.
23
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION: ‘TECHNOLOGY, PUNE & KARAD |
SELECT job, HAX(sal) FROM ENP
GROUP By job
HAVING MAX(sa1) > 2500;
== find Jobs which are giving max sal eore than 2500.
+ Syntactical Characteristics:
© WHERE clause executed for each record; while HAVING is executed for each group.
HAVING clause can be used to specify condition on group fn or grouped columns.
2 However using HAVING to specify condition of group col reduce the performance. Use
‘WHERE clause for the same,
SELECT deptno, SUM(sal) FROM ENP
‘GROUP aY deptne
HAVING deptno = 205
== find total sal of deptno 20 (unefficient)
SELECT deptno, SUM(sal) FROM ENP
WHERE deptno = 20
GROUP BY deptnos
== find total sal of deptno 20 (efficient)
+ SELECT Syntax
SELECT expt, expr2,
WHERE condition
GROUP BY expr, «
‘ORDER BY expr, «
LIAIT 1,93
SELECT deptno, SUM(sal) FROM ENP
GROUP BY deptno
(ORDER BY SUM(sal) DESC
LIMIT 13
== find dept that spend max on enp's sals.
FRON table
HAVING condition
SELECT deptno, SUM(sal) FROM EHP
WHERE jobs"CLERK™
GROUP BY deptno
‘ORDER BY SUM(sal) DESC
LIMIT 45
== find dept that spend max on clerk's sals.
SELECT deptno, SUM(sal) FROM EHP
WHERE Job = "CLERK
GROUP BY deptno
HAVING SUM(sal) > 15005
S- find dept that spend more than 1580 on clerk's sals.
%
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD.
Joins
Use tests
(CREATE TABLE EMP(enpno INT, ename VARCHAR(2@), deptno INT);
INSERT INTO ENP VALUES (1,’*Sarang', 109);
INSERT INTO ENP VALUES (2, ‘Nitin’, 108);
INSERT INTO ENP VALUES (3, ‘anit', "209);
INSERT INTO EMP VALUES (4, ‘Rahul’, 200);
INSERT INTO ENP VALUES (5) ‘Nilesh’, 300);
CREATE TABLE DEPT(deptno INT, dame VARCHAR(20));
INSERT INTO DEPT VALUES (200, DEV");
INSERT INTO DEPT VALUES (300, ‘Q4");
INSERT INTO DEPT VALUES (428, ‘ADHIH");
INSERT INTO DEPT VALUES (500, ‘0PS");
enpno | ename | deptno |
1 | Sarang | 108 |
2 | Nitin | 100 |
3 | anit | 200 |
4 | Rahul | 200 |
5 | Nilesh | 320 |
|
| 200 | bev
| 380 | 8 |
| 490 | aon |
| 520 | ops |
struct emp { enpno, enane, deptno };
struct dept { deptno, dnane };
struct emp emps(] = { ... }
struct dept depts(] = { ... };
Cross Join
+ Join in two different tables.
+Join without any condition,
+ Output rows = Num of rows in tablel * Num of rows in table2
+ For each row of tablet, all rows of table2 are scanned & printed,
SELECT e.ename, d.dname FROM EMP e CROSS JOIN DEPT d;
@ display enp name & all possible depts he can be.
DATABASE TECHNOLOGIES
7‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
foreach(e in e=ps)
for(d in depts)
t
y
print evenane, d.dname;
‘ename | dname
Sarang | DEV
sarang | QA
Sarang | ADMIN
Inner Join
SELECT e.ename, d.dname FROM EMP e INNER JOIN DEPT d ON edeptno = d.deptno;
Q. display enp nane & dept nane. Do not display non-macthing rows.
foreach(e in exps)
{
for(d An depts)
t
if (e.deptno == d.deptno)
print e.enane, d.dnane;
y
d
®
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
ename | dname
Amit | DEV
Rahul DEV
Nilesh_| QA
Left Outer Join
SELECT e¢name, ddname FROM EMP e LEFT OUTER JOIN DEPT d ON e.deptno = dideptno;
Q. display emp nane & de
foreach(e in exps)
{
Pt mane. If dept not found, display WLL.
found = 0;
for(d in depts)
{
i#Ce-deptna x= dept)
found =
print e.enane, d.dnane;
)
Af(found == 0);
print e.enase, NULL;
ename | dname
Sarang | NULL
Nitin NULL
Amit DEV
Rahul DEV
Nilesh QA
Right Outer Join
SELECT e.ename, d.dname FROM EMP e RIGHT OUTER JOIN DEPT d ON edeptno = d.deptno;
23
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
aa as Sn
ename | dname
NULL ADMIN
NULL ops
Amit DEV ;
Rahul DEV
Nilesh | QA
Full Outer J
+ Full Outer Join is not supported in MySQL.
+ But its result can be achieved by combi
ig Left & Right joins.
Set operators
+ In MySQL results of two queries can be combined using SET operators.
+ There are two set operators in MySQL: UNION, UNION ALL
UNION operator
(SELECT e.enase, d.dnane FROM ENP ©
LEFT OUTER JOIN’ DEPT d ON e.deptno = d.deptno)
UNION
(SELECT e.enane, didnaze FROM EHP
RIGHT OUTER 30IN DEPT d ON e.deptno = d.deptno);
+ Common records are displayed only once.
UNION ALL operator
(SELECT e.ename, d.dnane FROM EMP e
LEFT OUTER JOIN DEPT d ON e.deptno = d.deptno)
UNTON ALL
(SELECT e.enane, d.dname FROM EMP @
RIGHT OUTER JOIN DEPT d ON e.deptno = d.deptno);
+ Common records are displayed duplicated.
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Join on multiple tables.
SELECT 0.onum, o.ant, o.odate, c.cname FROM ORDERS ©
TAMER 3OIN CUSTOMERS ¢ ON o.enum = c.cnum;
SELECT o.onun, o.ant, o.adate, s.snane FROM ORDERS 0
INVER JOIN SALESPEOPLE $ ON o.snun = $.snun;
SELECT o.onun, o.ant, o.odate, c.cnane, s.snane FROM ORDERS ©
INNER JOIN CUSTOMERS '¢ ON o.cnun = c.chum
ITWNER JOIN SALESPEOPLE 5 ON o.snun = s.snuns
SELECT 0.onun, o.ant, o.odate, c.cnane, s.sname FROM ORDERS ©
INVER 3OIN CUSTOMERS’ ON o.cnum = c.chum
INNER JOIN SALESPEOPLE s ON o.snum = s.snun
WHERE ovamt > 5000;
Cross Join vs Inner Join
+ Cross Join:
© Two different tables.
‘© Doesn't have any condition,
© Tofind all possible combinations.
© Num of output rows = Num of rows in tl * Num of rows in t2
+ Inner Join:
© Usually two different tables.
© Have some condition (usually t1.pk = t2f%).
© Tofind rows of tl related to rows of t2.
+ Unrelated rows from tl & t2 are skipped
Inner Join vs Outer Join
+ Inner Join:
© Usually two different tables,
‘2 Have some condition (usually tl pk = t2k
© Tofind rows of t1 related to rows of t2,
* Unrelated rows from tl & t2 are skipped
+ Outer Join:
© Usually two different tables.
© Have some condition (usually t.pk = t2k).
31
DATABASE TECHNOLOGIES“SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
To find rows of t1 related to rows of t2 along with additional rows in t2 (LEFT JOIN) oy,
12 (RIGHT JOIN) or both (FULL JOIN).
Joins on multiple tables
SELECT o.cnum, s.sname, ¢.cname FROM SALESPEOPLE
INNER JOIN CUSTOMERS ¢ ON c.snum = s.snum
INNER JOIN ORDERS 0 ON o.Snum = s.snun;
crrong result due to wrong relation used in this query.
SELECT o-onum, o.cnum, 0-snium FROM ORDERS 0;
SELECT o.onum, s.snane, ¢.cnane FROM ORDERS 0
INNER 30TN CUSTOMERS c’ON o.cnum = c.cnum
WNER JOIN SALESPEOPLE s ON 0.snun = s.snun;
== correct & standard.
SELECT o.onum, s.snane, ¢.cnane
FRON ORDERS 0, CUSTOMERS ¢, SALESPEOPLE s
WHERE o.cnum © c.cnum AND O.snum = s.snum;
‘sane result (only applicable for inner joins)
Self Join
+ Inner join or Outer join between same table. The PK & FK both belong to the same table or
columns in which relation is to be made are from same table.
+ Foreach row of a table, all rows of the same table are scanned.
+ Q Find emp name & his manager name.
i. Assume we have two tables
+E (employees): empno(PK), ename, mgr(FK)
+ M (managers): empno(PK), ename
Implement Join in two tables.
1ce we don't have two separate table, consider same table having two copies ie. E&
M.
fn.enane “ngr_nane’ FROM EHP ©
npn}
SELECT e-enane “enp_nane”
INNER JOIN EMP w ON e.ngr
SELECT e.enane “enp_naze’, m.enane “mgr_nane’ FROM ENP ©
LEFT 3OIN EHP m ON e.mgr = m-enpno;
+ Q White a query that produces al pats of salespeople who are living in the same city. Exclude
tions of salespeople with themselves as well as duplicate rows with the order reversed.
2
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SELECT cl.cnum, cl.cnane, c2.enum,
FROM CUSTOMERS ci
INNER JOIN CUSTONERS 2 ON 1.
WHERE c1.cnum
cLicnane, cl.city
scity = c2.city
2.cnum AND ¢1.cnum > €2.¢nun;
Query Performance
+ Evaluating whether quer
take?
+ To get cost of query, based on o}
used by database internally.
+ When a query is submitted to the db server,
has certain cost.
"Y execution will be faster or not. How much time (not exact time) it will
erations performed internally by database and algorithms
an execution plan is made by db server. That plan
+ More'is the cost, higher will be time taken for execution of query.
+ Tose the execution in MySQL. there is EXPLAIN statement,
EXPLAIN query;
This gives high-level info about query execution,
EXPLAIN FORMAT=ISON query;
This gives Jow-
level info (detailed) about query execution.
EXPLAIN FORNAT=2S0N
SELECT * FROM EMP WHERE enpno=7900; -- 1.65
EXPLAIN. FORMAT=3S0N
SELECT * FROM EMP WHERE Job="ANALYST'; -- 1.65
EXPLAIN FORMAT=2S0N
SELECT * FROM EMP MHERE mgr=7839; -- 1.65
EXPLAIN FORMAT=ISON
SELECT e.enane, d.dnane FROM ENP ¢
INNER JOIN DEPT d ON e.deptno = d.deptno; -- 6.50
+ Note that the query costing will vty in MySQL versions as well as machine configuration.
+ The queries can be optimized by changing optimization options of database. Changing these
options will force db to use different algorithm to solve the same query.
+ These optimization options will change from db to db.
33
DATABASE TECHNOLOGIES
Ee‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Indexes
+ Indexes are used to improve searching speed.
CREATE INDEX 4x Job OU ENP( ob);
EXPLAIN FORMATEISON
SELECT * FROM ENP WHERE Job="ANALYST*; -- 0.70
‘CREATE INDEX Sx _egr ON EMP(mgr DESC);
EXPLAIN FORMAT=3S0N
SELECT * FROM EHP HERE mgr=7839; -- 0.00
‘CREATE INDEX 4x edeptno ON EHP(deptno) ;
CREATE INDEX Axcddeptno ON DEPT(deptno);
EXPLAIN FORMAT=3SON
SELECT e.enane, d.dnane FRON ENP ©
INVER JOIN DEPT d ON e.deptno = d.deptno;
4.52
(CREATE INDEX 5 empno ON ENP(enpno ASC);
EXPLAIN FORMAT=2S0N
SELECT * FROM EMP WHERE enpno=7900; -- 0.35
DESCRIBE ENP
‘SHOW INDEXES FROM EMP;
[DROP INDEX ix_empno ON ENP;
+ Internally indexes stored addresses of rows corresponding to each value of the column on
which indexis created,
«These addresses are interally maintained in B-Tree or Hash data structure in MySQL
+ Inindex values of indexed column are arranged asc or desc order depending on how indexis
created.
+ Ifindex is Unique index, it cannot contain duplicate values.
(CREATE UNIQUE TNDEX Sx enpno ON EHP(enpno);
DESCRIBE EMP;
INSERT TNTO EMPCenpno, enase) VALUES (7908, ‘ANOY");
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
+ Composite index is created on multiple columns.
+ When searching is done on all those columns (with AND), searching will be faster.
CREATE INDEX 4x,
ene_job ON EHP(deptno, Job);
EXPLAIN FORMAT=25ON
SELECT * FROM ENP WHERE deptno=30 AND Job=" SALESMA!
0.98
+ When indexes are created SELECT operations on indexed columns will be very fast.
+ However on each DML operation respecting index table is also modified (along with main data
table) by db server. Hence DML operations will slow down,
Constraints
‘+ Constraints are applied on columns to ensure that correct data is entered in those columns (for
each row).
«The constraints are given while creating table or can be modified later (using ALTER statement).
«Types of constraints
© Primary Key
© Foreign Key
© Unique Key
© Not Null
© Check
+ Types of declaring constraints
‘© Column level
+ Primary Key, Foreign Key, Unique Key, Not Null & Check constraints can be given
Column level.
+ In MySQL, Foreign Key constraints at column doesn't work.
+ In MySQL CHECK constraint at column level doesn’t work
© Table level
+ Primary Key, Foreign Key, Unique Key & Check constraints can be given Table
level
+ Tn MySQL, CHECK constraint at table level doesn’t work
35
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Primary Key
‘+ Unique identity of each record in the table. e.g. empno in EMP table.
+ Pk is always unique and cannot be null.
+ PK shouldn't be changed.
(Natural) Primary Key
+ Acol which represent natural identity of row, should be taken as PK. e.g, Users table PK can be
email.
‘+ Internally a UNIQUE index is auto created for PK column.
CREATE TABLE users (
rane VARCHAR(S®),
fenail VARCHAR(50) PRIMARY KEV,
Password VARCHAR(5®),
age INT
4
= on
CREATE TABLE users (
mame VARCHAR(5®),
fmail VARCHAR(50),
password VARCHAR(5®),
ge INT,
PRIMARY’ KEY (email)
‘
Composite Primary Key
+ Sometimes a combination of multiple columns is unique (instead of single column). In this case
we create composite PK.
+ Internally a UNIQUE Composit
+ Example: STUDENTS table:
lex is auto created for PK columns.
roll | std | name | ..
1 ji ja
2 {1 |B
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
roll | std | name | .
a [2 [¢
RERTE TALE students
roll INT,
std INT,
ave VARCHAR(S0),
PRIMARY KEY (roll, std)
u
surrogate Primary Key
+ When no column can be used as unique identity, neither combination columns is available to
use as unique identity; a separate column is added into table to use as PK.
+ Such PK is called as "Surrogate Primary Key".
+ Such unique nums are usually generated using sequences in oracle or autoi
MySQL.
CREATE TABLE registrations(
regno INT AUTO_INCREMENT,
rane VARCHAR(S@) ,
nail VARCHAR(S®),
batch INT,
PRIMARY KEY(regno)
u
INSERT INTO registrations(nane, enail, batch) VALUES (*A', ‘[email protected]', 108);
INSERT INTO registrations(nane, email, batch) VALUES ('8", ‘bGsun.con", 161);
regno will be 2
+ regno will be 2
Add or remove PK
+ Iftable is already created, its PK can be added or removed,
USE sales;
ALTER TABLE CUSTOMERS ADD PRIMARY KEY (enum);
ALTER TABLE ORDERS ADD PRIMARY KEY (onus)
ALTER TABLE SALESPEOPLE ADO PRIMARY KEY (snum);
DESC cusrowERS;
DESC ORDERS;
DESC SALESPEOPLE;
37
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Deciding Primary Key
+ Primary is decided when table is
reated
+ The table design of a project is done using a process, called as "Normalization"
+ Pksis by-product of Normalization process,
Unique Key
+ Cannot be duplicated.
+ Can be NULL
+ There can be multiple Unique keys ina table.
‘+ Internally UNIQUE index is created on that column.
CREATE TABLE users (
name VARCHAR(S®),
fenail VARCHAR(5@),
password VARCHAR(5®),
age INT,
‘mobile CHAR(20) UNIQUE,
PRIMARY KEY (email)
Ms
NOT NULL Constraint
‘+The value of col cannot be NULL.
CREATE TABLE users (
name VARCHAR(5®) NOT NULL,
fenail VARCHAR(50),
password VARCHAR(S®) NOT NULL,
age INT,
rnobile CHAR(20) UNIQUE,
PRIMARY KEY (email)
ds
Foreign Key Constraint
+ Acolumn (or combn of column) that refer to another col into another/same table.
+ Usually FK col refer to the PK col of another table.
«+ If corresponding Pk in another table is not available, then Fk entry cannot be done. It raise
error.
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
+ For example: If DEPT table contains deptnos 10,20, 30 & 40; then a row EMP table cannot have
deptno=50.
+ Internally it create an index for Fk column.
+_Having indexes (auto created for PK & FK) improve speed of join
cqente TABLE CUSTORERS
0 a)
ene VAesAR(20),
fey vancian(ze),
rating 1,
Sun i,
FORTEGN KEY (Srun) REFERENCES SALESPEOLE(snue)
7
on
ALTER TABLE CUSTOMERS ADD FOREIGN KEY (snun) REFERENCES SALESPEOPLE snus);
EXPLAIN FORNAT=3S0N
SELECT cucnane, s.snase FROM CUSTOMERS ¢
TAMER JOIN SALESPEOPLE s OW c.snum = $.snun;
SELECT * FROM SALESPEOPLE;
INSERT INTO CUSTOMERS (cnus, chase, snun) VALUES (9001, ‘Ay2", 1009)5
s+ error (no salesman in SALESPEOPLE with snume1009).
DELETE FROM SALESPEOPLE WHERE snum = 1001;
error (it will make corresponding CUSTOMER records orphan).
‘PORTE SALESPEOPLE SET snum = 2001 HERE snane = “Peel;
= error (Jt will make corresponding CUSTOVER records orphan).
+ In MySQL if parent row is deleted/updated corresponding child rows can be deleted/updated;
is mentioned while creating table
CREATE TABLE CUSTOMERS (
«run TXT,
rane VARCHAR(20),
city VARCHER(20),,
rating INT,
snus INT,
FORTEGN KEY (snun) REFERENCES SALESPEOPLE(smum) ON DELETE CASCADE
hi
on
CREATE TABLE CUSTOMERS,
‘nun IN,
mane VARCHAR( 20),
city vaRcHAR(20),,
rating INT,
sun Txt,
FORIEGN KEY (snun) REFERENCES SALESPEOPLE(smum) ON UPDATE CASCEDE
9
DATABASE TECHNOLOGIES 5SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
a
Disabling Foreign Key checks
+ We can disable foreign key checks temporarily
+ This may loose integrity of data,
+ This feature can be helpful while migrating data into new database, to speed up data transfer
SELECT @@foreign.key_checks;
Tf 1, FK checks are done,
Tf ©, FR checks are skipped.
SET @@foreign_key_checks = 0}
SELECT @@foreign_key_checks;
INSERT INTO CUSTOMERS (enum, cnane, snun) VALUES (9001, *Xy2", 1009);
CHECK Constraint
‘+ MySQL support CHECK constraint from 8.0.16 onwards.
+ Itis used to validate values to be inserted/updated in column.
© eg. sal must be greater than 500.
‘© eg. age must be in range of 18 to 58.
© eg. Name must be entered in cap case,
© eg. sal + comm should not exceed 10000.
+ MySQL can emulate CHECK constraints using Triggers (BEFORE INSERT & BEFORE UPDATE), |
SELECT GOversion;
CREATE TABLE EHP
enpno INT,
fenane VARCHAR(28) CHECK (enane = UPPER(enane)),,
‘age INT CHECK (age BETWEEN 10 AND 58),
81 DOUBLE CHECK (sal > 580),
con DOUBLE,
CHECK (sal + comm) <= 10000)
%
«+ Is query inside query. Typically SELECT query within SELECT query.
+ For each row of outer query,
Sub-query |
|
ner query is executed once.
DATABASE TECHNOLOGIES 2973.21;
= Q, find all enps whose sal is greater
SELECT * FROM ENP WHERE sal» (setect a
SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Ifdb is not doing any optimization, then Sub-queries are usually slower than joins.
Single row sub-query
Sub-query returns single row,
A As greater than avg sal.
than avg sal (in a query).
VS(sal) FROM ENP);
+ Q, find emp with 3rd highest sal.
SELECT DISTINCT sal FROW ENP ORDER BY sal DESC;
SELECT DISTINCT sal FROM EN> ORDER BY
SELECT * FROM EMP WHERE sal = 2975.00;
os step
sal DESC LINIT 2,2; -- step?
= @: find ecp with 3rd highest sal (in a query).
SELECT * FRON EMP MNERE sall= (SELECT DISTINCT sal FROM ENP ORDER BY sal DESC LIMIT 2,2);
+= Q. find erps whose sal is greater than all salesnan.
SELECT * FROM EMP MMERE jobs"SALESMAN'; ~~ step!
SELECT MAX(Sal) FROM ENP AHERE jobs"SALESNWW's =~ step2
SELECT * FROM EMP HERE sal > 1800.09; = ansver
zz, G: Find enps hose sal is greater than all salesean (in a query)
SELECT * FROM EMP WHERE sal > (SELECT MAX(sal) FROM EMP WHERE Jobe:
SALESMAN");
Multi-row sub-query
Sub-query returns multiple rows.
ALL operator is used to compare (relational op) result of multi-row sub-query into outer query.
It compare will all values retumed by sub-query.It is equivalent to logical AND.
ANY operator is used to compare (relational op) result of multi-row sub-query into outer
query. It compare will all values retumed by sub-query. It is equivalent to logical OR,
c= Q. Find ecps whose sal is greater than all salesman.
SELECT sal FROM EMP WHERE jot
SELECT * FROM EMP
WHERE sal > ALL(SELECT sal FROM EMP HNERE job="SALESMAN'); ~~ ans
SALESMAN"; ~~ step
o= Q. Find enps whose sal is greater than any’ salesman.
SELECT * FROW EMP
WSERE sal > ANV(SELECT sal FROM EMP KHERE job="SALESHAN")
AND job [= "SALESMAN"; ~~ ans
DATABASE TECHNOLOGIES.
|
|
|
|SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
s+ Q. find all depts who contain at least one exp.
SELECT * FROM DEPT; -- step
SELECT * FROM ENP;'-- step2
SELECT DISTINCT deptno FROM ENP; -- steps
SELECT * FROM DEPT
WHERE deptno = ANV(SELECT DISTINCT deptno FROM ENP);
s+ Q, find all depts who contain at least one enp.
SELECT * FROM DEPT
WHERE deptno IN (SELECT DISTINCT deptno FROM EMP);
ALL vs ANY
ALL ANY
Used with multi-ow sub-qry | Used with multi-row sub-ary
Can be used with any reln op | Can be used with any reln op
Like logical AND Like logical OR
IN vs ANY
IN
Used with/without sub-query
Like logical OR Like logical OR
Can be used to check equality | Can be used with any reln op
Faster (for equi
) Slower (for equality)
Correlated Sub-query
+ For each row of outer query, inner query is executed once,
+ To improve the performance of sub-queries, inner query should return min number of rows.
+ This can be done by giving criteria (WHERE clause) in inner query based on value of row in
outer query.
+ Such sub-queries where result of inner query depends on current row of outer query, is called
as "Correlated Sub-query”.
DATABASE TECHNOLOGIES
2SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
-- Q. find all depts who contain at least one
SELECT * FROM DEPT aE sees
WHERE deptno TN (SELECT deptno FROM EHP}
+ subgry return 14 rows
Q. find all depts who contain at least one exp.
Tl EXPLAIN FORMAT=IS0N ”
SELECT * FROM DEPT
WHERE d.deptno
IW (SELECT e.deptno FROM EMP e WHERE e.deptno
- subqry returns 3 rows when
jnd 0 rows when d.deptno = 40.
+ reduce num of rows returned by subgry - to inprove performance
Q. find all depts who contain at least one enp.
EXPLAIN FORNAT=350N, ”
SELECT * FROM DEPT d WHERE
{SELECT COUNT(e.deptno) FROM EMP © WHERE e.deptno = d.deptno) > 0;
leptno);
d.deptno = 10, 5 rows when d.deptno = 20, 6 rows when d.deptno =
- Q. find all depts who contain at least one enp.
EXPLAIN FORMAT=3S0N
SELECT * FROM DEPT d WHERE
EXISTS (SELECT e.deptno FROM EMP e HERE e.deptno
4.deptno);
<= Q, find all depts who doesn't contain any esp.
SELECT * FROM DEPT WERE
(SELECT COUNT(e.deptno) FROM EMP © WHERE e.deptno =
eptno)
<= Q find all depts who doesn't contain any exp.
SELECT * FROM DEPT d WHERE
NOT EXISTS (SELECT e.deptno FROM EHP e WHERE e.deptno = d.deptno);
SELECT within UPDATE/DELETE
+ You cannot delete/update from the same table, on which you are firing inner query.
-- Delete all enps whose sal is greater than avg sal of enps.
SELECT * FROM EMP MHERE sal > (SELECT AVG(sal) FROM EMP)
step
DELETE FROM EMP WHERE sol > (SELECT AVG(sal) FROM EMP); -- ans
+ You can delete/update from different table than the table on which inner query is fired.
Correlated sub-query is not allowed.
<= Delete all depts, who doesn't contain exp.
DELETE FROM DEPT
WHERE deptno NOT IN (SELECT deptno FROM EHP);
a
DATABASE TECHNOLOGIES.SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Transactions
* Set of DML queries executed as single unit. f any one query is failed, effect of all queries in tis
discarded,
‘Tx very useful on real world applications eg. Funds transfer, Placing order, Reservation, ete
+ TCL command:
2 START TRASACTION;
© COMMIT;
© ROLLBACK;
© SAVEPOINT x;
© ROLLBACK TO x
‘+ START TRANSACTION; command is used to begin a new tx
* COMMIT; command is used to commit current tx All changes done by queries with this te are
saved permanently in db.
+ ROLLBACK: command is used to rollback current tx All changes done by queries with this tx
are discarded permanently from db. |
* Once commit/roliback is done, current tx will finish. For further changes start new t%
Savepoint
+ Savepoints internally store a state within tx,
+ Itis used rollback to that state, if we want to discard tx partially.
+ We can only rollback to savepoint. We cannot commit to savepoint.
+ When commit/rollback is done fora tx, all savepoints in that tx are deleted
START TRANSACTION;
ant qt
dal a2
SAVEPOINT sas
nl 93
dnl a
SAVEPOINT $b;
nl 9s
nl 96
ROLLBRCK TO sa; -- discard changes of d3, qt, 45 & 96
£
DATABASE TECHNOLOGIES[sunbeam smu oF nromamion TECHNOLOGY, PUNE &KARAD
Rate ue OF nromearanerggignen mene ||
ype rhe tee wep gr
SO TeansscrTON; :
‘ne
oa
jen 285
‘a ©
dnl a
Sjuonit =;
an 98
dal 96
comIT 10 sb; =~ connit to savept is not allowed
foUeAck TO sb; -- instead rollback to savept
ComiIT; -- and then conit.
autocommit variable
«There isa global config variable autocommit.
+ In MySQL. by default itis 1... all changes are auto commited.
+ To begin ty alternative way isto set autocommit to 0.
+ Asa beginner do not use this variable,
SELECT @@autoconnit;
SET @Gautoconnit =
DELETE FROM DEPT;
ROLLBACK;
SET @Bautoconnit
DDL queries in tx
+ DDL queries are not part of tx.
+ When DDL query is executed, current tx is automatically commited.
+ So first commit/roliback current tx & then only fire DDL queries.
START TRANSACTION;
DELETE FROM DEPT}
Hod TABLES;
45
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
DROP TABLE DUMMY;
RoLLaAcK;
SELECT * FROM DEPT;
Transaction characteristics
Tx follow ACID properties.
A: Atomic
© At the end of tx all queries in tx will succeed or will discard.
© Partial success/discard is not allowed
C: Consistent
© At the end of tx changes saved, willbe visible to all clients.
© Different clients do not see different state of data
I:lsolated
© Num of clients can execute txs simultenously.
© They should be able to execute tx without affecting each other, as if they are done one
after another.
© Internally db server maintains a req queue, that stores all ops from all clients. Then they
are executed one after another.
D: Durable
2 Atthe end of txall changes must be saved permanently on db server disk.
‘© Data changes should not be loss (even in case of server crash).
Transaction internals
+ For each txa separate temp table is created
+ Allchanges done by user in that tx are recorded in temp table, but not updated in main table.
+ When user fetch records in that tx, combined result of main table & tx temp table is shown to
him.
+ However these changes are not visible to other users, until txis commited by the first user.
== root login
SELECT user, host FROM aysql.user;
CREATE USER dacBlocalhost IDENTIFIED BY *
DATABASE TECHNOLOGIES
6gant ALL PRIVILEGES 0 classvork,
TO dacélocelhost)
qusw PRIVILEGES;
sot GRANTS FOR ntlesheocathosty
wns
‘changes in atx of client are not visible to another client.
+ And willbe discarded, it tis rollbacked by user.
= (terminal) end> aysql -u ntlesh -pnitesh classwerk
ALTER TABLE DEPT ADD PRIMARY KEV(deptno);
°
START TRANSACTION;
SELECT * FROM DEPT; ~~ 2
DELETE FROM DEPT WHERE deptnoeco;
ROULBACK; ~~ 6
++ (terminal2) end> aysql -u dac -pdac elasswork
4
SELECT * FROM DEPT; =.
SELECT * FROM DEP
3
5 (changes in 4 are not visible here)
SELECT * FROM DEPT; -- 7
(changes in 4 are discarded)
changes
a tx of client are not visible to another client.
+ And will be visible to user, if tx is commited by user1,
+ (terminal) cnd> mysql -u nilesh -pnilesh classwork
START TRANSACTION; == 2
SELECT * FROM DEPTS -- 2
WPOATE DEPT SET loce"PUNE' HERE deptno=40; -- 4
commit; -- 6
s+ (terminal2) end> mysql -u dac -pdac classvwork
SELECT * FROM DEPT; =~ 3
SELECT * FROM DEPT; -- S (changes in 4 are not visible here)
SELECT * FROM DEPT; -- 7 (changes in 4 are visible here)
Optimistic Locking.
+ (terminals) end> aysql -u nilesh -pnilesh classvork
START TRANSACTION; =~ 1
SELECT * FROM DEPT; -- 2
PATE OEPT SET loc="PUNE" WHERE deptno:
a
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
SELECT * FROM DEPT;
COMMIT; -- 8
== (terminal2) cnd> mysql -u dac -pdac classwork
SELECT * FROM DEPT; -- 3
SELECT * FROM DEPT} -- 6 (changes in 4 are not visible here)
DELETE FROM DEPT WHERE deptno=4@; -- 7 (blocked until 8 is done)
SELECT * FROM DEPT; -- 9 (record 40 is deleted)
Pessimistic Locking.
=> (Rerminal1) cnd> aysql -u ndlesh -pnilesh classwork
START TRANSACTIONS =~ 2
SELECT * FROM DEPT WHERE deptno=30 FOR UPDATES
UPDATE DEPT SET loc="KARAD" WHERE deptno:
SELECT * FROM DEPT; -- 5
Commit; -- 6
=> (Rerminal2) ced> mysql -u dac -pdac classwork
SELECT * FROM DEPT WHERE deptn
12 FOR UPDATE; -- 3 (will block unt{l 6)
Views
‘+ View is limited/restricted view of the data.
+ View is used for frequently required SELECT queries. SELECT queries can be with few cols, few
rows, grouped, joins or sub-queries.
«Ita
need of rewriting same query multiple times.
+ There is no memory/space allocated to view. Working on view internally is working underlying
table(s).
+ View doesn't increase speed of execution. Because itis merely executing same SELECT query.
+ Two types of views
© Simple view
© Complex view
Simple view
+ Doesn't contain computed column, group by, sub-queries or joins.
+ On which we can perform SELECT as well as DML operations.
‘+ DML operations are allowed as long as no constraint on table is violated.
a
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
«Ifyou want to allow DML operations only for selected rows (as given in WHERE clause), then
that view should created WITH CHECK OPTION.
use 2
ion TABLES
sevecr * FROM v_custoners;
caeaTe VIEW vcustoner_detalls
GE SELECT Crum, cnane, city, rating FROM CUSTONERS;
sftect * FROM v_custoner details;
wOaTE v.custoner_details SET citys'Paris* WERE cnane="Hoffnan’
INSERT INTO v.customer_details VALUES (2610, ‘BL11", ‘karad", 508)3
cgEATE VIEW v_toprated.custoners
IS SELECT * FROM CUSTOVERS WHERE rating >= 200;
SELECT * FROM v_toprated_custoners
CREATE VIEH v_small_orders
JS SELECT * FROM ORDERS WHERE ant <= 1000 WITH CHECK OPTION;
SELECT * FROM v_snall_ orders;
INSERT INTO v_snall.orders VALUES (6001, 9000, "2019-03-09", 2018, 1001);
INSERT INTO v_small_orders VALUES (6002, 8000, '2019-03-09", 2010, 1001);
Complex view
+ Can perform only SELECT operations.
+ Cannot perform DML operations
+ Contain computed column, group by, sub-queries or joins.
CREATE VIEW v_year_orders
AS SELECT onun, ant, odate, snum, cnun, YEAR(odate) oyear FROM ORDERS;
SELECT * FROM v_year_orders WHERE oye:
2019;
CREATE VIEW v_custoner_naxant
AS SELECT chun, MAXant) FROM ORDERS GROUP BY cnun;
SELECT * FROM v_custoner_saxanty
«
DATABASE TECHNOLOGIES 8‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
error
“+ INSERT INTO v_custoner_saxant VALUES (2010, 20000)
SELECT + FROM v_custoner_maxont MERE enum IN (2001, 2062, 2003);
CREATE VIEW v_toporders
AAS SELECT * FROM ORDERS
WHERE ant > (SELECT AVG(ant) FROM ORDERS);
SELECT * FROM v_toporders;
(CREATE VIEW v_orders
AS SELECT o.cnun, o.ant, o.odate, o.cnum, c.cname, c.rating crating, o.snun, $.sname, $.CORn Scom
FROM ORDERS 0
INNER JOIN CUSTOMERS ¢ ON o.cnum = c.cnum
JINNER JOIN SALESPEOPLE 5 ON o.snun = s.snun;
SELECT * FROM vorders;
SELECT onum, snun, ant, scom, scom * ant FROM v_orders;
Applications
+ Simplifies some complex queries
+ Enables reuse of queries
+ Can provide restricted view of data to the different users.
+ Hide source code of table.
+ Even though table structure is changed, applications can continue to execute.
DCL - Data Control Language
+ Itcommon requirement to provide limited to access of the data to certain users,
+ Limited number of rows, limited number of columns or limited privileges of table/view.
+ Thisis done using views & DCL.
+ DCL.Commands
© GRANT
REVOKE
© SHOW GRANTS
‘+ When db is installed, one user is created by default. It is admin user. In MySQL admin user
login name is "root".
+ By default "root" have all permissions.
DATABASE TECHNOLOGIES= syntax
_—
Basic commands
SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
0k DATABASES
ot BLES
quer user, DOSE FON nyse users
cqeate USER usernaneBhostnane TOENTIFIED BY “password;
yo GUNTS FOR usermaneGhostnane;
squect USER(), DATABASE();
privileges
«Two types of privileges (MySQL):
© Global privileges
+ Not specific to db or table.
+ 9, privileges to CREATE USER, CREATE DATABASE, DROP DATABASE, changing
system variables, ..
+ These privileges are given on +.*.
© Object level privileges
+ Privileges inside db/schema on table or views.
+ These privileges are given on dbnane.*.
+ Different privileges (MySQU:
© USAGE: No perm
© SELECT: DQL permissions
© INSERT,UPDATE,DELETE: DML permssions
© CREATEALTER,DROP: DDL permssions
© AlLall permissions except GRANT OPTION
© GRANT OPTION: permission to give permissions to others.
‘+ You can only give those permissions to others, which you have.
© SUPER: admin permission to change system variables.
© EXECUTE: permission to execute stored procedure.
GRNT pernissions ON dbnane.* TO userghosts
GRINT permissions ON tablenane.* TO useréhost;
DATABASE TECHNOLOGIES
51‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
‘GRANT permissions ON viewnane.* TO userghost;
=> root login
don't do this.
‘GRANT ALL PRIVILEGES ON *.* TO nilesh@localhost WITH GRANT OPTION;
Will make nilesh similar to adnin.
Strictly prohibited for security reasons.
GRANT ALL PRIVILEGES ON hr.* TO nilesh@localhost WITH GRANT OPTION;
will make nilesh adain for hr db.
FLUSH PRIVILEGES
++ nilesh login
USE hes
GRANT SELECT ON departments TO dacGlocalhost;
GRANT TNSERT,UPOATE,DELETE, SELECT ON employees To dac@localhost;
=> dae login
SHOW DATABASES;
USE hes
stow TABLES;
‘HOM GRANTS FOR dacBlocathost;
(CREATE TABLE temp(id INT);
== not allowed
SELECT * FROM employees;
INSERT INTO enployees VALUES (.
SELECT * FROM departments;
s+ INSERT INTO departments VALUES (...
~~ ndlesh login
USE hrs
REVOKE UPDATE, DELETE ON enployees FROM dacGlocalhost;
+ dae login
SHOW GRANTS;
2
DATABASE TECHNOLOGIESr
SSS
_SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
ene Wess
ysl Variables i. 7
|, There are tWo types of variables
System variables
o User defined variabless
system variables
are pre-defined in Nysou.
Gre accessed using @@ si
variables:
Siow vaRTAsLes;
1 ol VAREABLES LIKE “Rforesgnt;
few variables can be modified in user session scope, while few can be modified globally.
“pet An stzion scope are sie only to caren eer (ot pacing ter 345)
* eng- @leutoconait, Qeforeign key-checks, Buoptinizer switchs oso)
5 SET eoatocomie gj Sheesh optinszer seth
= SELECT @version;
= changes An global scope are alloyed only to user with SUPER privileges (1ike root).
~ SET GLOBAL EeForeign key checks = 0;
“qanges in global scope are visible ail users;
(SELECT ‘AVG(sal) FROM EMP) ORDER BY d.dnane; "
== OR
DELCLARE v_avgsal DOUBLE;
SELECT AVG(sal) INTO v_avgsal FROM EHP;
DECLARE v_cur CURSOR SELECT e.enpno, d.dnane FROM EHP ¢ INNER JOIN DEPT d ON d.deptno =
‘e-deptno KHERE e501 > v_avgsal ORDER BY’ d.dnane;
Functions
User defined functions are routines stored on server and are used in queries (OQL or OMY).
UDFs are written to provide functionalities which are not available in built-in SQL functions, |
In MySQL, UDFs are single row functions. (Implementing group UDF is out of scope). |
+ Note that UDFs are slower than built-in functions,
+ UDF are similar to SP, but UDF return value,
‘+ In MySQL, UDF cannot have OUT or INOUT params. Here all params are IN params by defaut,
+ Functions are not called using CALLO statement. They are called in SQL queries. They willbe
executed for each row,
‘+ There are two types of functions in MySQL:
© Deterministic
+ The return value is only dependent on its params.
+ Not dependent on current time or state of table,
+ MySQL stores/cache params & result, so that if fn is called again with same
params result returned quickly (without actually calling fn)
© Non-deterministic
+The return value is dependent on its params as well as some more factors.
+ These factors are current time or state of table.
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
+ MySQL doesn't cache its results, Function is always called.
CREATE FUNCTION fn_name(pl DATATYPE, p2 OATATYPE, ---)
RETURNS DATATYPE
[Nor] veteRManrstic
BecmN
fn body; -- PL syntax
RETURN ret_value;
EN;
+ Allstored proc & functions are stored into system table information_schema.routines;
DESC information_schema. routines;
SELECT * FROM infornation_schena.routines;
SELECT * FROM information_schena.routines \¢
SELECT ROUTINE_SCHEMA, ROUTINE_TYPE, ROUTINE NAME, DEFINER, ROUTINE DEFINITION FROM
information_schesa.routines \¢
Triggers
+ Triggers are also MySQL programs. Hence written in same PL syntax and stored on server. They
execute faster than SQL queries.
+ Triggers are never called explicitly. Their execution is triggered on some events.
+ Triggers can be invoked before (pre) or after (post) DML operations.
© BEFORE INSERT
‘2 AFTER INSERT
© BEFORE UPDATE
© AFTER UPDATE
© BEFORE DELETE
© AFTER DELETE
+ You should not write TCL statements in triggers. Triggers are always executed in the tx. in
which DML operation is in progress.
+ Iftrigger fails, the current tris rolibacked.
+ Triggers don't have parameters. In MySQL, triggers have two special keywords "NEW" and.
"OLD", which represent new row and old row respectively.
+ INSERT trigger have only NEW keyword (OLD is not applicable).
DELETE trigger have only OLD keyword (NEW is not applicable).
a
DATABASE TECHNOLOGIES.SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
+ UPDATE trigger have NEW & OLD keyword.
+ Trigger is executed for each row (inserted, updated or deleted),
CREATE TRIGGER trig_nane
‘trigger_type ON table nane
FoR Each ROM
BEGIN
trigger bodys
Use OLD & NEW keywords here as appropriates
+ Triggers can be cascaded,
Circular cascading of trigger will stop execution of triggers. Itis called as "Mutating table
error".
+ Triggers are stored in information_schema.triggers table.
SELECT + FROM information schena.triggers;
SHOW TRIGGERS FROM classworks
+ Applications:
‘© DML operations logging (Audit tails).
© Tosstore old data (History table).
© Tocreate backup table (Shadow table).
© To replicate data on other server.
© Data cleansing before inserting/updating data
Temporary Tables
«+ View is limited view of data, as per given SELECT statement. But internally no memory/space
allocated to view. Any operation done on view will execute on main table. Advantages of view
ate security, hiding source code and simplifying queries View doesn't affect performance of
query.
+ Feature of MySQL.
+ Can create new temporary table per user session to store/cache the result of SELECT query.
«They are not visible & accessible to other user. Even you cannot use GRANT/REVOKE for doing
so.
«This table is auto deleted when user session is completed (MySQL terminal is closed).
+ Using this we can simplify some complex queries and at the same time we can make it more
efficient.
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
+ Any changes done in main table after creation of temp table are not available into temp table.
+ Any changes done in temp table are not available main temp table.
+ Temp table can have same name as of existing db table. In this case you access temp table. To
access main table, first delete temp table. It is not good practice to give same name to avoid
confusion.
CREATE TEMPORARY TABLE t_deptemp
AS SELECT e.enpno, e.enane, e.sal, d.deptno, d.dnane
FRON EMP e INVER JOTW DEPT’ ¢ ON e.deptno = d.deptno;
SHOW TABLES;
SELECT * FROM _deptenp;
DROP TEMPORARY TABLE t_deptenp;
Codd's Rules
+ Rule-0: For any system that is advertised as, or claimed to be, a relational data base
management system, that system must be able to manage data bases entirely through its
relational capabilities.
+ Rule-1: All information in a relational data base is represented explicitly at the logical level and.
in exactly one way - by values in tables. (Tables + Rows + Columns.)
+ Rule-2: Each and every datum (atomic value) in a relational data base is guaranteed to be
logically accessible by resorting to a combination of table name, primary key value and column
name. (Primary Key for each Row).
+ Rule-3: Null values (distinct from the empty character string or a string of blank characters and
distinct from zero or any other number) are supported in fully relational DBMS for representing
missing information and inapplicable information in a systematic way, independent of data
type.
+ Rule-4: The data base description (metadata) is represented atthe logical level in the same way
as ordinary data, so that authorized users can apply the same relational language to its
interrogation as they apply to the regular data, (Stored into system tables e.g, mysqluser,
information_schema.routines,information_schema.tiggers)
+ Rule-5: A relational system may support several languages and various modes of terminal use
(for example, the fl-in-the-blanks mode). However, there must be atleast one language
8
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
whose statements are expressible, per some well-defined syntax, as character strings. (SQL
language).
+ Rule-6: All views that are theoretically updatable are also updatable by the system.
+ Rule-7: The capability of handling a base relation or a derived relation as a single operand
applies not only tothe retrieval of data but also to the insertion, update and deletion of data,
+ Rule-8: Application programs and terminal activities remain logically unimpaired whenever any
changes are made in either storage representations or access methods. (Storage engine)
Rule-9: Application programs and terminal activities remain logically unimpaired when
information-preserving changes of any kind that theoretically permit unimpairment are made
to the base tables. (Views)
+ Rule-10: Integrity constraints specific to @ particular relational data base must be definable in
the relational data sublanguage and storable in the catalog, notin the application programs,
(oreign-key constraint)
Rule-11: The end-user must not be able to see that the data is distributed over various
locations. Users should always get the impression that the data is located at one site only.
(Clustering)
Rule-12: If a relational system has a low-level (single-record-at-a-time) language, that low leve|
cannot be used to subvert or bypass the integrity rules and constraints expressed in the higher
level relational language (multiple-records-at-a-time). (Snapshot)
Normalization
+ Concept of table design --> Table, Structure, Data Types, Width, Constraints, Relations.
+ Goals:
© Effi
‘9 Avoid data redundancy ie. unnecessary duplication of data (to save disk space)
© Reduce problems of insert, update & delete,
t table structure
+ Done from input perspective.
DATABASE TECHNOLOGIES
ie iene-
SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE 8 KARAD
+ Based on user requirements.
| + Part of software design phase.
transaction basis & then normalize each transaction separately.
‘+ View entire application on per
+ Transaction Examples:
© Banking: Open New Account, Deposit Amount, Withdraw Amount.
© Rail Reservation: Reservation, Cancellation.
© Online Shopping: Customer Order, Stock Update, New Product, ~~
Getting ready for Normalization:
‘+ For given transaction make list ofall the fields.
«+ Strive for atomicity.
+ Get general description of all field properties.
For all practical purposes we can have a single table with all the columns. Give meaningful
names to the table.
Assign datatypes and widths to all columns on the basis of general description of fields
properties
+ Remove computed columns.
‘+ Assign primary key to the table.
+ Atthis stage data isin unnormalised form.
UNF --> starting point of normalization.
+ Delete anomaly
+ Update anomaly
+ Insert anomaly
Normalization step:
1. Remove repeating group into a new table.
2. Key elements will be PK of new table.
DATABASE TECHNOLOGIES 6sY,
SUNBEAM INSTITUTE OF InFORMATION TECHNOLOSY
in yosite PK.
3. (Optional) Add PK of original table to new table to give us Comp:
" into new tables.
Repeat steps 1-3 infinitely -- to remove all repeating JYOUPS realtonsied bet
«This 1-NF. No repeating groups present here: One to Many 2 tg
tables.
4, Only table with composite PK to be examined.
- to be
5. Those columns that ae not dependent on te entre composi PK. they are to be remove
into anew table.
ginally dependent, its to be addeg
6. The key elements on which the non-key elements were Ori
to the new table, and it will be the PK of new table.
«© Repeat steps 4-6 infinitely - to separate all non-key elements from al tables with
composite primary key.
© This is 2-NF. Many-to-Many relationship.
7. Only non-key elements are examined for inter-dependencies.
8. Inter-dependent columns that are not directly elated to PK, they are to be removed into anew
table.
9. (a) Key element will be PK of new table.
(b) The PK of new table is to be retained in original table for relationship purposes.
© Repeat steps 7-9 infinitely to examine all non-key elements from all tables and separate
them into new table if not dependent on PK.
© Thisis 3-NF.
+ To ensure data consistency (no wrong data entered by end user).
+ Separate table to be created of well-known data, So that min data will be entered by the end
user.
+ This is BCNF or 4-NF.
MySQL client software's
1. mysql SQL shell
6
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
2. mysql workbench (GUI desktop based tool)
© sudo apt-get install mysql-workbench
3. phpmyadmin (GUI web based tool)
sudo apt-get install phpmyadmin
MySQL Architecture
Physical architecture
+ Configuration files
© fetc/mysqV/my.cnt
+ Installed Files
© Executable files
+ mysqld
+ mysaladmin
+ mysql
+ mysqldump
© Log files
+ pid files
+ socket files
+ document files
+ libraries
+ Data Files
© Data directory
+ Server logs
+ Status files
+ Innodb tablespaces & log buffer
© Database directory files
+ Data & Index files (ibd)
+ Object structure files (frm, .opt)
DATABASE TECHNOLOGIES
o&KARAD
SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE
5 8
tsa OS wIORY
CUENT
sromoemncne
Fie yale nel Storage Database Files MNNUNNNNNT
—_
= Client
+ Server (mysqld)
© Accept & process client requests
© Multi-threaded process
‘2 Dynamic memory allocation
+ Global allocation
*+ Session allocation
+ Parser
© SQL syntax checking.
© Generate sql.id for query,
© Check user authentication.
+ Optimizer
© Generate efficient query execution plan
© Make use of appropriate indexes
© Check user authorization,
+ Query cache
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
© Server level (global) cache
© Speed up execution if identical query is executed previously
+ Key cache
© Cache indexes
© Only for MyiSAM engine
+ Storage engine
‘© Responsible for storing data into files.
‘© Features like transaction, storage size, speed depends on engine.
© Supports multiple storage engines
© Can be changed on the fly (table creation)
© Important engines are InnoDB, MyiSAM, NDB, Memory, Archive, CSV.
InnoDB engine
+ Fully transactional ACID.
+ Row-level locking.
+ Offers REDO and UNDO for transactions.
+ Shared fil to store objects (Data and Index in the same file - ibd)
+ InnoDB Read physical data and build logical structure (Blocks and Rows)
+ Logical storage called as TABLESPACE,
+ Data storage in tablespace
© Multiple data files
© Logical object structure using InnoDB data and log buffer
MyIsaM
‘+ Non-transactional storage engine
+ Table-level locking
+ Speed for read
+ Data storage in files and use key, metadata and query cache
© rm for table structure
table index
© myd for table data
69
DATABASE TECHNOLOGIESp——
8 KARAD
‘SUNBEAM INSTITUTE OF INFORMATION recuot0ct, PUNE BY
cel dia —
*+ Fully Transactional and ACID Storage engine.
+ Row-level locking,
*+ Offers REDO and UNDO for transactions.
+ NDB use logical data with own buffer for each NDB engine.
+ Clustering: Distribution execution of data and using multiple mysald
Clustering in MySQL
+ Using multiple machines to store & process data.
+ All machines are connected through high-speed network.
+ Two types of clusters are supported.
© InnoDB cluster
© NDB cluster
InnoDB cluster
+ Designed for High-Avaiability
‘+ System continue to function even if one of the server goes down
‘+ Consists of multiple servers running mysqld and a router.
+ Group replication
© Multiple servers maintains same copy of the data,
© Usually one of the servers primary (ie. handles write+tead)
© Remaining all servers are secondary (ie. only read)
© If primary fails, one ofthe secondary is elected as primary.
+ Router
© Usually installed on same machine as of client application.
© Client fre SQL queries to router.
© Router send them to appropriate server from replication group.
© Actas load balancer.
© Use separate ports for
+ MySQL shell
© Used for creating & monitoring cluster
& read (in config files),
DATABASE TECHNOLOGIES\
‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
© Itis different from SQL shell -~ mysqlsh
dbacreateCluster))
© cluster = dba.getCluster)
© clusterstatus)
NDB cluster
‘+ Designed for High-Availability (99.99% up
+ Management node (ndb_mgmd)
© manage the other nodes within the NDB Cluster
© providing configuration data, starting and stopping nodes, and running backups.
© node of this type should be started first
+ Data node (ndbd)
© stores cluster data
© multiple nodes for replica per fragment
© minimum two replicas per fragment for HA
© cluster tables are stored completely in memory rather than on disk
+ SQUAPI node (mysqld)
© accesses the cluster data
© a specialized type of API node
© minimum two replicas are recommended
re)
MySQL Snapshots
+ Snapshots state of database. It includes table metadata & data,
+ Applications:
‘©. Porting existing appin on new server.
© Recovering from database crash/corruption.
+ Backup & Restore.
+ Methods of snapshots:
‘© MySQL workbench -- export & import
© PhpMyAdmin -- export & import
© MySQL utilities -- mysqldump & mysql
© Raw files backup & restore -- very fast, difficult
n
DATABASE TECHNOLOGIESSUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
mysqldump & mysql
mysqldump -u [username] -p[password] [database_name] > [dump_file.sql]
mysqldump -u [username] -p[password] [dbname1,dbnane2...] > [dunp_file.sql]
ysqldump -v [usernane] -p[password] --all-database > [dump_file-sql)
‘ysql -u [uname] -p[pass) [db_to_restore] < [backupfile.sql]
DATABASE TECHNOLOGIES
n‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Mongo DBMS
+ Performs CRUD operations on data.
+ In RDBMS, data is stored in tables in rows. Schema is defined by its columns.
+ In Mongo, data is stored in collections in documents. Schema is flexible. Each document can
have multiple/different fields.
‘+ Mongo documents are in JSON (ava Script Object Notation) format.
+ JSON is a text format to represent the data. It is used heavily nowdays at many places (where
earlier people were using XML).
+ JSON example:
35,
address"
| )
| Neontact: {
“email”: “[email protected]",
mobile": "9527381338"
»
Srating": 3.8,
“isTrainer": true,
skills": [ "Database", "Hadoop", “Spark”,
"birth": 1SoDate("1983-09-28")
jevice Drivers, “Micro-controllers” ],
+ JSON is internally converted into BSON (Binary JSON) before storing into mongo db. For
efficient storage & processing. In BSON, each data type is represeted as a number eg. array is.
4, null is 10,
+ In Mongo, max size of one document is 16 MB.
+ Each mongo doc should have an unique *_id”. User can provide unique id for each record (like
PK in RDBMS).
+ Ifuser have not given " id", mongo will auto generate it of type Objectid of 12 bytes.
© counter -- arbitrary counter.
© timestamp -- current client time.
© process id -- client (OS) process id.
2 machine -- client machine id.
B
DATABASE TECHNOLOGIES‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
‘command> mongo
show databases;
use kdac;
show collections;
ab.persons.insert({
Tenail": "[email protected]",
ceobile": "9527331338",
‘cnane": "Nilesh",
wage": 35
De
db.persons.insert({
name": “Nitin”,
age": 40,
“nobile":
Ds
4b.persons.insert({
‘Sandeep",
"Katraj, Pune”
Da
db. persons.find();
11 Cursor is returned by find() method of collection.
I Cursor methods: pretty(), sort(), linit(), skip(), «
b.persons.find().pretty()5
b.persons.find()-Limit(2);
4b. persons. find().skip(2) .1imit(1)5
db. persons.find().sort({"nane": -1));
I ase order of nane
71 copy all Lines from enpdept_separate.js on mongo shell
show collections;
b.enp.find();
db.enp.find().sort({*deptno: 1});
4.enp.find()-sort({"deptno": 1, “sal: -1));
1/ multi-level sorting
DATABASE TECHNOLOGIES
”SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
//db.colnane. Find(criteria, projection);
ab.enp.find({}, {"enane":1, “sal":1});
db.emp.find({}, ("Job":0, “mgr*:0, “hire”: 0})3
db-enp.find({}, (Tenane":1, “sal":1, “deptno":0}); //error
//db.colnane.Find(criteria, projection);
db.enp.find({"_d": 7300});
db.emp.find({"deptno": 10);
1/9. find enp with max sal in dept 20.
db.enp.find({*deptno": 20);
// relational operators: $eq, $ne, $gt, Sete, $1t, $lte, $in, $nin, ...
J db.colnane.find({ “fieldnane™ : { "Soper" : value } 3);
db-enp.find({"sal": ( $Ite : 1000} });
1 logical operators: Sand, $or, $nor
1 db.colnane.find({ "Slog oper*
u { “fieldname" : { “sreln_oper*
” { “fieldname” : { “$relnoper
WI):
value } },
: value } }
J/ Q. find a11 salesman with sal >= 1400,
db.enp.find({
Sand
{ “Job : “SALesman™ ),
{ "sal": ( $gte : 1490) }
De
// Q. find a1] salesman or sal >= 3000.
4.enp.Find( {
sort [
{ dob": “saLEsman y,
{ "501": (Sgre : 3000} }
De
// Q. find all salesman & clerks.
ab.enp.Find({
sort [
{ Job": "saLesman ),
{ "Job" : *cLenK™ }
De
11 Q Find all salesman & clerks using $in.
db.enp.Find({
“Jeob": { Sin : [ “satesmaw-, cterK> J >
DATABASE TECHNOLOGIES
78SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
»;
4b emp. insert((*_td
db.enp insert((* id
db.emp- insert ({"2!
1, “ename": "Soham", "sal": 3400))
“Rohan”, "sal": 2400});
; Nename": “Ashwin®, "sal": 2600});
db.emp.Find();
1/db.colnane.renove( criteria);
db.enp-renove({"_id": 3})3
db.emp.remove({}); // delete all emp
do.persons.drop(); // delete all records + collection
show collections;
//db.colnane update (criteria, new record);
db.emp-update({"_id": 2}, { “sal”? 2500 });
//db.colnane-update(eriteria, changes);
db.emp.update({"_id": 1}, {
» ‘$set: {Tsal" : 3500)
7/db.colnane-update(criteria,
db.emp.update({*_id": 4, “enant
$set: { “sal”: 3200, “deptno’
}, false);
71 no record found, hence no record modified.
ddb.emp. update({"_id'
$set
}, true);
11 Sf record is found, it will be updated.
11 4# record not found, record of given criteria is inserted & then its given fields will be
edited. This is called as “upsert”.
4, “enane": "Vishal"), {
3208, “deptno": se}
b.enp-find();
11 aggregation pipeline
11 db.colnane. aggregate ({
u { stagel },
u { stage? },
u { stage3 },
u tee
Ws
17 sages /operators: Seroup, Snatch, $sort, Sprofect, $lockup, Sout, ...
gbaenpcoeereaste(t
seroup + {
DATABASE TECHNOLOGIES
8‘SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Mongo Indexes
To speedup execution of find) ie. searching is faster.
+ By default all collections are indexed on "_id" field.
+ Types of indexes:
o Index
© Unique index
© Composite index
© Geo-spatial index
© THLindex
.enp.createTndex({“deptno": 1})3
db. enp -createrndex(
"sal": -1))5
emp .createlndex({“deptno
4, "fob": 1))5
db.dept.createIndex({"dname": 1}, {“unique": true});
11 collections are auto created, while insert record.
// they can be created explicitly using createCollection().
db.createcollection("stud");
>. stud. insert(
Mongo Data Modeling
+ How data is organized into mongo database.
+ Two modeling methods:
© Embedded data model
Referenced data model (Normalized data model)
Embedded data model
+The related objects are embedded into some parent object.
+ example:
© emp collection:
id, ename, sal, mgr, comm, job, hire, dept
dept is a nested object that contains id, dname, loc
DATABASE TECHNOLOGIES ”SUNBEAM INSTITUTE OF INFORMATION TECHNOLOGY, PUNE & KARAD
Reference data model
Data is in separate collections.
The documents are connected by some field.
‘example:
© dept collection
+ _id, dname, loc
© emp collection
+ id, ename, sal, mgr, comm, job, hire, deptno
+ Here “deptno” field is mapped to “id” of dept.
+ Reduces data redundancys
Mongo Consistency
+ Each record editing is always atomic.
© Multi-row tx are not straight forward.
© Specialized design patterns can be used for such tx e.g. two phase commit.
+ Changes done by one client are available to all clients.
+ Number of clients can work simultaneously.
+ All changes are saved on disk.
DATABASE TECHNOLOGIES