SlideShare a Scribd company logo
A database is a collection of interrelated data. It’s a collection of information
that is organised so that it can be easily accessed, managed and updated.
e.g. A school’s database
Database
Advantages of Database
● Reduces redundancy.
● Controls data inconsistency.
● Facilitates data sharing.
● Ensures data security
● Integrity can be maintained.
DBMS(DataBase Management System) is a software which helps us establish
and implement a database. It consists of a group of programs which helps in
storing, retrieving and manipulating data while considering appropriate
safety measures.
e.g. MySQL, Microsoft SQL Server, Oracle, MongoDB, PostgreSQL, etc.
DBMS
Relational Data Model
In relational data models, data is organised in tables(rows and columns).
These tables are called relations.
Name Roll_No Marks
Harsh 1 100
Raghav 3 99
Abhinandan 23 01
Bhanupriya 7 00
Hemant 2 73
Terminologies
1. Relation : A table storing logically related data.
2. Domain : Pool of values from which the actual values appearing in
a given column are drawn.
3. Tuple : row
4. Attribute : Column
5. Degree : no. of rows/tuples.
6. Cardinality : no. of columns/attributes.
number of attribute or column
number of tuples or row
1. View : A virtual table that doesn’t really exist, but it’s used to view the
base table or tables derived from one or more base tables.
2. Primary : One or more attributes that can uniquely identify tuples within
key the table.
3. Candidate : All attribute combinations inside a table that are candidates to
key become primary key.
4. Alternate : A candidate key which is not primary key.
key
5. Foreign : A non-key attribute whose values are derived from the primary
key key of some other table, is called foreign key in its current table.
Terminologies
SQL(Structured Query Language) is a database language that is recognised by nearly
all RDBMSs(Relational DataBase Management System).
It contains set of commands that enables us to create and operate on relational
databases.
Why we use SQL?
SQL is fast, very high level, platform independent, standardized, portable language which
requires almost no coding skills.
SQL
DDL Commands
DDL or Data Definition Language
It contains set of commands that allows us to perform tasks related to data definition.
It specifies the storage structure and access methods for database system.
DDL consists of :
● Creating, altering & dropping.
● Granting, revoking privileges and roles.
● Maintenance commands.
DML Commands
DML or Data Manipulation Language
It consists of commands that allows you to perform data manipulation.
DML consists of :
● Insertion
● Deletion
● Modification
● Retrieval
TCL Commands
TCL or Transaction Control Language
It consists of commands that allows us to manage and control the transactions
(a transaction is one complete unit of work involving many steps)
TCL consists of :
● Making changes to database, permanent.
● Undoing changes to database, permanent.
● Creating savepoints.
● Setting properties for current transactions.
*DCL and DQL are beyond scope of syllabus, we’ll study it later sometime.
Important Data Types in MySQL
Data Type Description
CHAR string(0-255)
VARCHAR string(0-255)
TINYINT integer(-128 to 127)
SMALLINT integer(-32768 to 32767)
INT integer(-231
to 231
-1)
BIGINT integer(-263
to 263
-1)
FLOAT Decimal(precise to 23 digits)
Data Type Description
DOUBLE Decimal(24 to 53 digits)
DECIMAL ‘DOUBLE’ stored as string
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS
TIMESTAMP YYYYMMDDHHMMSS
TIME HH:MM:SS
Varchar
The difference between a char and varchar is of fixed length and variable length.
When a column is given datatype char(n), then values under this attribute are
strings of n bytes. If a value is less than n bytes, then empty spaces are added till
nth byte.
In varchar(n), values are still strings of n bytes. No spaces are added even if size is
smaller than n bytes.
‘Hey’ char(5) varchar(5)
‘Hey ’ ‘Hey’
Varchar Varchar2
● It can identify NULL and empty
string separately.
● It’s an internal data type.
● Minimum size - 1
Maximum size - 2000
● NULL and empty string are same.
● It’s an external data type.
● Minimum size - 1
Maximum size - 4000
Creating table
CREATE TABLE <table-name> ( <column-name> <data type>, .. , .. , .. )
CREATE TABLE student
( name char(20),
rollno int,
marks int );
Name rollno marks
Inserting data into table
INSERT INTO <tablename> (<column list>)
VALUES (<value>, <value2>, ....);
INSERT INTO student
VALUES (‘Harsh’, 141, 97);
INSERT INTO student(Name, marks)
VALUES (‘Raghav’, 99);
INSERT INTO student
VALUES (‘Hemant’, 146, NULL);
Name rollno marks
Harsh 141 97
Raghav null 99
Hemant 146 null
CREATE TABLE <table-name>( <column-name> date, .. , .... )
CREATE TABLE transac(Amount int, Date date)
INSERT INTO transac
VALUES (10000, '2021-17-01');
INSERT INTO transac(Amount,Date)
VALUES (-3000, '2021-19-01');
INSERT INTO transac
VALUES (2, '2021-24-01');
Using DATE
Amount Date
10000 2021-17-01
-3000 2021-19-01
2 2021-24-01
The SELECT command is used to pull information from a table.
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;
Making Queries
CREATE TABLE student
( Name char(20) NOT NULL,
Rollno int,
Marks int );
Name rollno marks
Harsh 141 97
Raghav null 99
Hemant 146 null
Name
Harsh
Raghav
Hemant
SELECT Name
FROM student
SELECT Name, rollno
FROM student
Making Queries
Name rollno marks
Harsh 141 97
Raghav null 99
Hemant 146 null
Name rollno
Harsh 141
Raghav null
Hemant 146
SELECT Name
FROM student
where marks>95
Name
Harsh
Raghav
SELECT *
FROM student
Name rollno marks
Harsh 141 97
Raghav null 99
Hemant 146 null
SELECT ALL Name
FROM student
ALL, DISTINCT, BETWEEN
Name rollno marks
Harsh 141 97
Raghav 143 99
Hemant 146 97
Name
Harsh
Raghav
Hemant
SELECT DISTINCT marks
FROM student
marks
97
99
SELECT *
FROM student
WHERE marks
BETWEEN 90 AND 98
Name rollno marks
Harsh 141 97
Hemant 146 97
90 & 98 are inclusive
SELECT Name, rollno
FROM student
WHERE marks IS null
Null, ORDER BY
Name rollno marks
Harsh 141 97
Raghav null 99
Hemant 146 null
Name rollno
Hemant 146
SELECT Name, rollno
FROM student
WHERE marks IS not null
SELECT *
FROM student
ORDER BY marks ASC
Name rollno marks
Hemant 146 null
Harsh 141 97
Raghav null 99
Name rollno
Harsh 141
Raghav null
SELECT marks, count(*)
FROM student
GROUP BY marks
GROUP BY, count,
DESCRIBE, DISTINCT
Name rollno marks
Harsh 141 97
Raghav 143 99
Hemant 146 97
marks count(*)
97 2
99 1
SELECT DISTINCT marks
FROM student
marks
97
99
DESCRIBE student
Field Type NULL Key Default Extra
Name char(20) N PRI
rollno int
marks int
This is just an example table, real output might differ.
SELECT rollno, marks
FROM student
WHERE Name IN (‘Harsh’, ‘Suraj’, ‘Khushi’)
IN, HAVING
rollno marks
141 97
SELECT marks, count(marks)
FROM student
GROUP BY marks
HAVING count(marks)>1
marks count(marks)
97 2
Name rollno marks
Harsh 141 97
Raghav 143 99
Hemant 146 97
SELECT name
FROM student
WHERE name
LIKE ‘H%’
LIKE
Name rollno marks
Harsh 141 97
Raghav 143 99
Hemant 146 97
SELECT name
FROM student
WHERE name
LIKE ‘H_m%’
Name
Harsh
Hemant
Name
Hemant
SELECT name
FROM student
WHERE name LIKE ‘%t’
OR name LIKE ‘%gh%
SELECT name
FROM student
WHERE name
LIKE ‘%a_s%’
Name
Raghav
Hemant
Name
Harsh
SELECT SUM(marks)
FROM student
SQL functions
sum(marks)
293
SELECT AVG(marks)
FROM student
avg(marks)
97.666666….
Name rollno marks
Harsh 141 97
Raghav 143 99
Hemant 146 97
SELECT productid, max(quantity),
min(quantity)
FROM Orders
GROUP BY productid having
avg(quantity)>100
An SQL Join is a query that fetches data from two or more tables whose
records are joined with one another based on a condition.
SELECT <attributes>
FROM <table1>,<table2>,<table3>, . . . .
WHERE <join condition of tables>
e.g. SELECT Name, marks, AdmNo
FROM student, list
WHERE student.name=list.name
This will join tables student and list on the basis of condition
student.name=list.name
SQL Joins
Types of Joins
1. Cartesian Product/Cross Join :
Without any condition, returns all joined records.
2. Equi Join :
Joins two or more tables based on a condition using equality operator.
3. Natural Join :
A type of equi join where the join condition compares all the same names
columns in both tables.
A primary key is used to uniquely identify each row in the table. It can be either
one attribute or an artificial field(like serial number).
Primary key
Name rollno marks
Harsh 141 97
Raghav 143 99
Hemant 146 97
CREATE TABLE student
( Name char(20)
Rollno int PRIMARY KEY
Marks int );
INSERT INTO student
VALUES (‘Bhanu’, 141, 33)
A composite key or primary key composition is a combination of two or more
columns in a table that can be used to uniquely identify each row in the table.
Composite key
Name Class RollNo
Harsh 9 80
Raghav 10 80
Hemant 10 87
CREATE TABLE student
( Name char(20)
Class int
Marks int
PRIMARY KEY(Class, Marks );
FOREIGN key
Name rollno marks class
Harsh 141 97
Raghav 143 99
Hemant 146 97
rollno Maths Science Hindi English
141 100 100 100 100
143 99 93 97 85
146 80 87 81 33
CREATE TABLE subject marks
( rollno int,
Maths int,
Science int,
Hindi int,
English int,
FOREIGN KEY (rollno)
REFERENCES student(rollno) );
ALTER TABLE <tablename>
Name rollno marks class
Harsh 141 97
Raghav 143 99
Hemant 146 97
CREATE TABLE student
( Name char(20)
Rollno int
Marks int );
ALTER TABLE student
ADD PRIMARY KEY (Rollno);
ADD class int
DROP COLUMN Name
MODIFY COLUMN class char(5)
23. SQL and Database.pdf

More Related Content

Similar to 23. SQL and Database.pdf

ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsgADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
zmulani8
 
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
Class XII-UNIT III - SQL and MySQL Notes_0.pdfClass XII-UNIT III - SQL and MySQL Notes_0.pdf
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
rohithlingineni1
 
Ankit
AnkitAnkit
12 SQL
12 SQL12 SQL
12 SQL
12 SQL12 SQL
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
Balqees Al.Mubarak
 
sql.pptx
sql.pptxsql.pptx
sql.pptx
slavskrillex
 
SQL
SQLSQL
sql_data.pdf
sql_data.pdfsql_data.pdf
sql_data.pdf
VandanaGoyal21
 
Sql Commands_Dr.R.Shalini.ppt
Sql Commands_Dr.R.Shalini.pptSql Commands_Dr.R.Shalini.ppt
Sql Commands_Dr.R.Shalini.ppt
DrRShaliniVISTAS
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
PriyaPandey767008
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
pranav kumar verma
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
Shakila Mahjabin
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
amitabros
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
RickyLidar
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
SANTOSH RATH
 
Lab
LabLab
Database Overview
Database OverviewDatabase Overview
Database Overview
Livares Technologies Pvt Ltd
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
EllenGracePorras
 
Sql12
Sql12Sql12
Sql12
tacex1
 

Similar to 23. SQL and Database.pdf (20)

ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsgADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
 
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
Class XII-UNIT III - SQL and MySQL Notes_0.pdfClass XII-UNIT III - SQL and MySQL Notes_0.pdf
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
 
Ankit
AnkitAnkit
Ankit
 
12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
sql.pptx
sql.pptxsql.pptx
sql.pptx
 
SQL
SQLSQL
SQL
 
sql_data.pdf
sql_data.pdfsql_data.pdf
sql_data.pdf
 
Sql Commands_Dr.R.Shalini.ppt
Sql Commands_Dr.R.Shalini.pptSql Commands_Dr.R.Shalini.ppt
Sql Commands_Dr.R.Shalini.ppt
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
SQL.ppt
SQL.pptSQL.ppt
SQL.ppt
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
 
Lab
LabLab
Lab
 
Database Overview
Database OverviewDatabase Overview
Database Overview
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Sql12
Sql12Sql12
Sql12
 

Recently uploaded

Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
Celine George
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
indexPub
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
Steve Thomason
 

Recently uploaded (20)

Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17How to Predict Vendor Bill Product in Odoo 17
How to Predict Vendor Bill Product in Odoo 17
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
A Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two HeartsA Visual Guide to 1 Samuel | A Tale of Two Hearts
A Visual Guide to 1 Samuel | A Tale of Two Hearts
 

23. SQL and Database.pdf

  • 1.
  • 2. A database is a collection of interrelated data. It’s a collection of information that is organised so that it can be easily accessed, managed and updated. e.g. A school’s database Database
  • 3. Advantages of Database ● Reduces redundancy. ● Controls data inconsistency. ● Facilitates data sharing. ● Ensures data security ● Integrity can be maintained.
  • 4. DBMS(DataBase Management System) is a software which helps us establish and implement a database. It consists of a group of programs which helps in storing, retrieving and manipulating data while considering appropriate safety measures. e.g. MySQL, Microsoft SQL Server, Oracle, MongoDB, PostgreSQL, etc. DBMS
  • 5. Relational Data Model In relational data models, data is organised in tables(rows and columns). These tables are called relations. Name Roll_No Marks Harsh 1 100 Raghav 3 99 Abhinandan 23 01 Bhanupriya 7 00 Hemant 2 73
  • 6. Terminologies 1. Relation : A table storing logically related data. 2. Domain : Pool of values from which the actual values appearing in a given column are drawn. 3. Tuple : row 4. Attribute : Column 5. Degree : no. of rows/tuples. 6. Cardinality : no. of columns/attributes. number of attribute or column number of tuples or row
  • 7. 1. View : A virtual table that doesn’t really exist, but it’s used to view the base table or tables derived from one or more base tables. 2. Primary : One or more attributes that can uniquely identify tuples within key the table. 3. Candidate : All attribute combinations inside a table that are candidates to key become primary key. 4. Alternate : A candidate key which is not primary key. key 5. Foreign : A non-key attribute whose values are derived from the primary key key of some other table, is called foreign key in its current table. Terminologies
  • 8. SQL(Structured Query Language) is a database language that is recognised by nearly all RDBMSs(Relational DataBase Management System). It contains set of commands that enables us to create and operate on relational databases. Why we use SQL? SQL is fast, very high level, platform independent, standardized, portable language which requires almost no coding skills. SQL
  • 9. DDL Commands DDL or Data Definition Language It contains set of commands that allows us to perform tasks related to data definition. It specifies the storage structure and access methods for database system. DDL consists of : ● Creating, altering & dropping. ● Granting, revoking privileges and roles. ● Maintenance commands.
  • 10. DML Commands DML or Data Manipulation Language It consists of commands that allows you to perform data manipulation. DML consists of : ● Insertion ● Deletion ● Modification ● Retrieval
  • 11. TCL Commands TCL or Transaction Control Language It consists of commands that allows us to manage and control the transactions (a transaction is one complete unit of work involving many steps) TCL consists of : ● Making changes to database, permanent. ● Undoing changes to database, permanent. ● Creating savepoints. ● Setting properties for current transactions. *DCL and DQL are beyond scope of syllabus, we’ll study it later sometime.
  • 12. Important Data Types in MySQL Data Type Description CHAR string(0-255) VARCHAR string(0-255) TINYINT integer(-128 to 127) SMALLINT integer(-32768 to 32767) INT integer(-231 to 231 -1) BIGINT integer(-263 to 263 -1) FLOAT Decimal(precise to 23 digits) Data Type Description DOUBLE Decimal(24 to 53 digits) DECIMAL ‘DOUBLE’ stored as string DATE YYYY-MM-DD DATETIME YYYY-MM-DD HH:MM:SS TIMESTAMP YYYYMMDDHHMMSS TIME HH:MM:SS
  • 13. Varchar The difference between a char and varchar is of fixed length and variable length. When a column is given datatype char(n), then values under this attribute are strings of n bytes. If a value is less than n bytes, then empty spaces are added till nth byte. In varchar(n), values are still strings of n bytes. No spaces are added even if size is smaller than n bytes. ‘Hey’ char(5) varchar(5) ‘Hey ’ ‘Hey’
  • 14. Varchar Varchar2 ● It can identify NULL and empty string separately. ● It’s an internal data type. ● Minimum size - 1 Maximum size - 2000 ● NULL and empty string are same. ● It’s an external data type. ● Minimum size - 1 Maximum size - 4000
  • 15. Creating table CREATE TABLE <table-name> ( <column-name> <data type>, .. , .. , .. ) CREATE TABLE student ( name char(20), rollno int, marks int ); Name rollno marks
  • 16. Inserting data into table INSERT INTO <tablename> (<column list>) VALUES (<value>, <value2>, ....); INSERT INTO student VALUES (‘Harsh’, 141, 97); INSERT INTO student(Name, marks) VALUES (‘Raghav’, 99); INSERT INTO student VALUES (‘Hemant’, 146, NULL); Name rollno marks Harsh 141 97 Raghav null 99 Hemant 146 null
  • 17. CREATE TABLE <table-name>( <column-name> date, .. , .... ) CREATE TABLE transac(Amount int, Date date) INSERT INTO transac VALUES (10000, '2021-17-01'); INSERT INTO transac(Amount,Date) VALUES (-3000, '2021-19-01'); INSERT INTO transac VALUES (2, '2021-24-01'); Using DATE Amount Date 10000 2021-17-01 -3000 2021-19-01 2 2021-24-01
  • 18. The SELECT command is used to pull information from a table. SELECT what_to_select FROM which_table WHERE conditions_to_satisfy; Making Queries CREATE TABLE student ( Name char(20) NOT NULL, Rollno int, Marks int ); Name rollno marks Harsh 141 97 Raghav null 99 Hemant 146 null Name Harsh Raghav Hemant SELECT Name FROM student
  • 19. SELECT Name, rollno FROM student Making Queries Name rollno marks Harsh 141 97 Raghav null 99 Hemant 146 null Name rollno Harsh 141 Raghav null Hemant 146 SELECT Name FROM student where marks>95 Name Harsh Raghav SELECT * FROM student Name rollno marks Harsh 141 97 Raghav null 99 Hemant 146 null
  • 20. SELECT ALL Name FROM student ALL, DISTINCT, BETWEEN Name rollno marks Harsh 141 97 Raghav 143 99 Hemant 146 97 Name Harsh Raghav Hemant SELECT DISTINCT marks FROM student marks 97 99 SELECT * FROM student WHERE marks BETWEEN 90 AND 98 Name rollno marks Harsh 141 97 Hemant 146 97 90 & 98 are inclusive
  • 21. SELECT Name, rollno FROM student WHERE marks IS null Null, ORDER BY Name rollno marks Harsh 141 97 Raghav null 99 Hemant 146 null Name rollno Hemant 146 SELECT Name, rollno FROM student WHERE marks IS not null SELECT * FROM student ORDER BY marks ASC Name rollno marks Hemant 146 null Harsh 141 97 Raghav null 99 Name rollno Harsh 141 Raghav null
  • 22. SELECT marks, count(*) FROM student GROUP BY marks GROUP BY, count, DESCRIBE, DISTINCT Name rollno marks Harsh 141 97 Raghav 143 99 Hemant 146 97 marks count(*) 97 2 99 1 SELECT DISTINCT marks FROM student marks 97 99 DESCRIBE student Field Type NULL Key Default Extra Name char(20) N PRI rollno int marks int This is just an example table, real output might differ.
  • 23. SELECT rollno, marks FROM student WHERE Name IN (‘Harsh’, ‘Suraj’, ‘Khushi’) IN, HAVING rollno marks 141 97 SELECT marks, count(marks) FROM student GROUP BY marks HAVING count(marks)>1 marks count(marks) 97 2 Name rollno marks Harsh 141 97 Raghav 143 99 Hemant 146 97
  • 24. SELECT name FROM student WHERE name LIKE ‘H%’ LIKE Name rollno marks Harsh 141 97 Raghav 143 99 Hemant 146 97 SELECT name FROM student WHERE name LIKE ‘H_m%’ Name Harsh Hemant Name Hemant SELECT name FROM student WHERE name LIKE ‘%t’ OR name LIKE ‘%gh% SELECT name FROM student WHERE name LIKE ‘%a_s%’ Name Raghav Hemant Name Harsh
  • 25. SELECT SUM(marks) FROM student SQL functions sum(marks) 293 SELECT AVG(marks) FROM student avg(marks) 97.666666…. Name rollno marks Harsh 141 97 Raghav 143 99 Hemant 146 97 SELECT productid, max(quantity), min(quantity) FROM Orders GROUP BY productid having avg(quantity)>100
  • 26. An SQL Join is a query that fetches data from two or more tables whose records are joined with one another based on a condition. SELECT <attributes> FROM <table1>,<table2>,<table3>, . . . . WHERE <join condition of tables> e.g. SELECT Name, marks, AdmNo FROM student, list WHERE student.name=list.name This will join tables student and list on the basis of condition student.name=list.name SQL Joins
  • 27. Types of Joins 1. Cartesian Product/Cross Join : Without any condition, returns all joined records. 2. Equi Join : Joins two or more tables based on a condition using equality operator. 3. Natural Join : A type of equi join where the join condition compares all the same names columns in both tables.
  • 28. A primary key is used to uniquely identify each row in the table. It can be either one attribute or an artificial field(like serial number). Primary key Name rollno marks Harsh 141 97 Raghav 143 99 Hemant 146 97 CREATE TABLE student ( Name char(20) Rollno int PRIMARY KEY Marks int ); INSERT INTO student VALUES (‘Bhanu’, 141, 33)
  • 29. A composite key or primary key composition is a combination of two or more columns in a table that can be used to uniquely identify each row in the table. Composite key Name Class RollNo Harsh 9 80 Raghav 10 80 Hemant 10 87 CREATE TABLE student ( Name char(20) Class int Marks int PRIMARY KEY(Class, Marks );
  • 30. FOREIGN key Name rollno marks class Harsh 141 97 Raghav 143 99 Hemant 146 97 rollno Maths Science Hindi English 141 100 100 100 100 143 99 93 97 85 146 80 87 81 33 CREATE TABLE subject marks ( rollno int, Maths int, Science int, Hindi int, English int, FOREIGN KEY (rollno) REFERENCES student(rollno) );
  • 31. ALTER TABLE <tablename> Name rollno marks class Harsh 141 97 Raghav 143 99 Hemant 146 97 CREATE TABLE student ( Name char(20) Rollno int Marks int ); ALTER TABLE student ADD PRIMARY KEY (Rollno); ADD class int DROP COLUMN Name MODIFY COLUMN class char(5)