Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Mysql

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

MySQL Tutorials

TISS
Data, Database and Database
Management System (DBMS)
 DBMS is a software application that interacts with the user, applications and the
database itself to capture, add, delete, update, retrieve and analyse the data
Types of DBMS
Types of Database Architecture
Types of Database Architecture – File Server
Types of Database Architecture – Client
Server
How MySQL is different?
 Atomicity: In a transaction involving two or more discrete pieces of information,
either all of the pieces are committed or none are.

 Consistency: A transaction either creates a new and valid state of data, or, if any
failure occurs, returns all data to its state before the transaction was started.

 Isolation: A transaction in process and not yet committed must remain isolated
from any other transaction.

 Durability: Committed data is saved by the system such that, even in the event of
a failure and system restart, the data is available in its correct state.
Relational Database Management System - MySQL
 SQL – Language for accessing and manipulating databases.
 Categories of SQL Commands
Basic concepts in SQL
 Tables in SQL: A Table is a database object which comprises of rows and columns
 Fields in SQL: A field provides specific information about data in the table (Columns)
 Records in SQL: A field provides specific information about data in the table (Rows)
Create, Use, and Drop Database in SQL
 CREATE DATABASE databasename;
 DROP database;
 USE database;

 Data Types in SQL:

Integer Character
Numerical Data Types
Character Data Types
Data and Time Data Types
Constraints in SQL
 Constraints are used to specify rules for data in table. There are four types of
contraints-
 Not Null: Not Null Constraint ensures that a column cannot have a NULL Value.
 Default: Default Constraint sets a default value for a column when no value is specified.
 Unique: Unique Constraint ensures that all values in a column are different.
 Primary Key: Primary Key constraint uniquely identifies each record in the table. It is
combination of Not NULL + Unique Constraint.
How to create a Table in SQL

 Syntax to create a Table Create table studentdb (


 CREATE TABLE table_name( S_Id int not null,
S_Name varchar (30),
 column1 datatype,
S_Age int not null,
 column2 datatype, S_city varchar (20),
S_Gender varchar (30),
 column3 datatype,
S_Salary int not null,
 ….. S_Specialisation varchar (40)
 columnN datatype, );

 PRIMARYKEY(column_x) );
 Note: A Table cannot have more than one PRIMARY KEY
How to insert values into the Table
 INSERT INTO table_name
 VALUES (value1, value2, value3,…valueN);

Insert into student.studentdb1 (


`S_id`, `S_Name`, `S_Age`, `S_city`, `S_Gender`, `S_Salary`,
`S_Specialisation`) values( 1,"Ram",22,"Mumbai","Male",470000,"Finance“
);

Insert into student.studentdb1 values


(2,"Shyam",24,"Delhi","Male",580000,"Marketing“
);
Import csv file to insert data
 Right click on database (student) and select Table Data Import Wizard.
 Select the csv file to import. (create 5 csv files from excel sheets).
 Refresh

 To view the inserted data, use command –


 select * from student.studentdb1;
 select * from student.studentdb2;
 select * from student.studentdb3;
 select * from student.studentdb4;
 select * from student.studentdb5;
Select Statement Syntax
 Select records of few fields from the database.
 Use the command –
 select `S_Name`, `S_Age` from student.studentdb1;
 select `S_Name`, `Sem_1_Total_Score` from student.studentdb2;
 select `S_Name`, `Sem_2_Total_Score` from student.studentdb3;
 select `S_Name`, `Sem_3_Total_Score` from student.studentdb4;
 select `S_Name`, `Sem_4_Total_Score` from student.studentdb5;
Select Distinct Syntax
 It is used to select only distinct values from our columns.
 Use command SELECT DISTINCT column1, column2,…column FROM table_name;

E.g. take Gender having two values (Male and Female). We need to extract only female
values from the column S_Gender
 select distinct S_Gender from student.studentdb1;
 select S_Gender from student.studentdb1;
Where clause Syntax
 It is used to extract records which satisfy a condition.
 Use command – SELECT column1, column2,… column FROM table_name WHERE
(condition).
 E.g. I want the records of all female students from the studentdb1.
 The command is –
 select * from student.studentdb1 where S_Gender="female";
 select * from student.studentdb1 where S_Specialisation="Marketing";
 select * from student.studentdb3 where Sem_2_Total_Score > 7.4;
AND Operator OR Operator NOT Operator
 AND Operator displays records if all the conditions separated by AND are TRUE.
 Use command – SELECT column1, column2, column FROM table_name WHERE
(condition1) AND (condition2)… AND (conditionN).
 E.g. Let’s take 2 conditions – Specialisation and Age
 Use command –
 select * from student.studentdb1 where S_specialisation="Marketing" AND S_Age > 25;
 select * from student.studentdb1 where S_specialisation="Marketing" AND S_Age > 25 AND
S_Salary > 2000000;
AND Operator OR Operator NOT Operator
 OR Operator displays records if any of the conditions separated by OR is TRUE.
 Use command – SELECT column1, column2, column FROM table_name WHERE
(condition1) OR (condition2)… OR (conditionN).
 Eg. Take Multiple specialisations
 Use command -
 select * from student.studentdb1 where S_Specialisation="Technology" OR
S_Specialisation="Marketing";
 select * from student.studentdb1 where S_Age < 30 OR S_Gender="Male";
AND Operator OR Operator NOT Operator
 NOT Operator displays a record if the condition is NOT TRUE.
 Extract records where the condition is NOT TRUE
 Use command – SELECT column1, column2, column FROM table_name WHERE NOT
(condition);
 E.g. Take S_Specialisation is NOT Marketing
 Use Command –
 select * from student.studentdb1 where not S_Specialisation="Marketing";
LIKE Operator
 LIKE Operator is used to extract records where a particular pattern is present.
 Eg. I want to select those records whose name starts with ‘R’. To extract all the
relevant records, SQL uses 2 wild Card Characters - % or _. % (represents zero or
multiple characters) while _ (represents single character).
 Use Command –
 select * from student.studentdb1 where S_Name LIKE 'R%’;
 select * from student.studentdb2 where Sem_1_Total_Score LIKE '6%';
BETWEEN Operator
 BETWEEN Operator is used to select values within a given range
 Use command –
 SELECT col_list FROM table_name WHERE column_N BETWEEN val1 AND val2;
 E,G. Students age between 27 – 32.
 Use command –

 select* from student.studentdb1 where S_Age BETWEEN 27


AND 32;
MIN Function
 It gives the smallest value.
 Use Command – SELECT MIN(col_name) FROM table_name;
 Eg. select min(S_Age) from student.studentdb1;

 select min(sem_1_total_score) from student.studentdb2;


MAX Function
 It gives the max value
 Use Command – SELECT MAX(col_name) FROM table_name;
 Eg. select max(S_Age) from student.studentdb1;

 select max(sem_1_total_score) from student.studentdb2;


COUNT Function
 Count function returns the number of rows that match a specific criteria
 Use command – SELECT COUNT(*) FROM table_name WHERE condition;
 Take – Gender (Male and Female)
 Use Command

 select count(*) from student.studentdb1 where S_Gender="male";


 select count(*) from student.studentdb1 where S_Gender="female";
SUM Function
 It gives the total sum of a numeric column
 Use Command – SELECT SUM(col_name) FROM table_name;
 E.g. Take Sum of Salary
 Use command -

 select sum(S_Salary) from student.studentdb1;


AVG Function
 It gives the average value of a numeric column
 Use command – SELECT AVG(col_name) FROM table_name;
 E.g. Take average age of students
 Use Command
 select avg(S_Age) from student.studentdb1;
STRING Functions – LOWER()
 It converts all characters to lower case letters
 E.g I AM GOD to i am god
 Use Command –

 select lower("I AM GOD");


STRING Functions – UPPER()
 It converts all characters to upper case letters
 E.g. i am god
 Use command –

 select upper("i am god");


STRING Functions – REVERSE()
 It reverses all characters in the string
 E.g REDRUM
 Use Command –

 select reverse("redrum");
STRING Functions – SUBSTRING()
 It gives a substring from the original string
 Eg. ‘I LOVE REDRUM’
 The substring that I want is REDRUM that starts from 7th space and contain 6
characters.
 Use Command –

 select substring("I LOVE REDRUM",7,7);


ORDER BY
 It is used to sort the data in ascending or descending order.
 Sort the records with ORDER BY clause.
 Use Command – SELECT column_list FROM table_name ORDER BY col1, col2,….ASC I
DESC
 E.g Salary in Ascending ORDER and then Descending ORDER

 select * from student.studentdb1 order by S_Salary;


 select * from student.studentdb1 order by S_Salary DESC;
TOP Clause
 Top clause is used to fetch the TOP N records
 Use command – SELECT TOP x column_list FROM table_name;
 Fetch top 3 records from the database.

Use command –

 select * from student.studentdb1 limit 3;


GROUP BY
 It is used to get aggregate result with respect to a group.
 E.g Gender and Salary
 Use Command –
 select avg(S_salary), S_Gender from student.studentdb1 GROUP BY
S_Gender;
 select Min(S_salary), S_Gender from student.studentdb1 GROUP BY S_Gender;
 select max(S_salary), S_Gender from student.studentdb1 GROUP BY S_Gender;
 select max(S_salary), S_Gender from student.studentdb1 GROUP BY S_Gender ORDER BY
max(S_Salary);
HAVING BY
 It is used in combination with GROUP By to impose conditions on groups
 E.g. Specialisation and average salary of students
 Use Command -

 selectS_Specialisation, avg(S_Salary) as avg_salary


from student.studentdb1group by S_Specialisation;
 selectS_Specialisation, avg(S_Salary) as avg_salary from
student.studentdb1group by S_Specialisation having
avg(S_Salary)>1500000;
INNER JOIN

 Use Command – SELECT Columns FROM table1 INNER JOIN table2 ON table1.column_x=table2.column_y
 E.g., Scores of sem 1 and sem 2 as per the common names of students.

 select student.studentdb2.S_Name,
student.studentdb2.Sem_1_Total_Score,
student.studentdb3.Sem_2_Total_Score From
student.studentdb2 INNER JOIN student.studentdb3 ON
student.studentdb2.S_Name=student.studentdb3.S_Name;
LEFT JOIN

 Example: Use S_Name from both the table1 and 2; Salary from table 1 and Sem 1 Total
Score from Table 2.
 Use command - select student.studentdb1.S_Name, student.studentdb1.S_Salary,
student.studentdb2.Sem_1_Total_Score From student.studentdb1 LEFT JOIN
student.studentdb2 ON student.studentdb1.S_Name=student.studentdb2.S_Name;
RIGHT JOIN

 Example – S_Name from table 1 and 2; Salary from table 1 and Sem_1_Total Score
from table 2.
 Use Command - select student.studentdb1.S_Name, student.studentdb1.S_Salary,
student.studentdb2.Sem_1_Total_Score From student.studentdb1 RIGHT JOIN
student.studentdb2 ON student.studentdb1.S_Name=student.studentdb2.S_Name;
FULL JOIN

 Example –
 Use Command – select student.studentdb1.S_Name, student.studentdb1.S_Salary,
student.studentdb2.Sem_1_Total_Score
 From student.studentdb1 LEFT JOIN student.studentdb2
 ON student.studentdb1.S_Name=student.studentdb2.S_Name
 union all
 select student.studentdb1.S_Name, student.studentdb1.S_Salary,
student.studentdb2.Sem_1_Total_Score
 From student.studentdb1 RIGHT JOIN student.studentdb2
 ON student.studentdb1.S_Name=student.studentdb2.S_Name;
FULL JOIN
 Joining Scores of all Semester from Multiple Sheets –

 select student.studentdb1.S_Name, student.studentdb1.S_Salary,


student.studentdb2.Sem_1_Total_Score,
student.studentdb3.Sem_2_Total_Score,
student.studentdb4.Sem_3_Total_Score,student.studentdb5.Sem_4_Tot
al_Score
 From student.studentdb1
 LEFT JOIN student.studentdb2 ON
student.studentdb1.S_Name=student.studentdb2.S_Name
 left join student.studentdb3 on
student.studentdb1.S_Name=student.studentdb3.S_Name
 left join student.studentdb4 on
student.studentdb1.S_Name=student.studentdb4.S_Name
 left join student.studentdb5 on
student.studentdb1.S_Name=student.studentdb5.S_Name;
UNION OPERATOR

 Use Command – SELECT column_list FROM table1


Union
SELECT column_list FROM table2
Example: Scores obtained by students in sem 1 and sem 2
Use command: select * from student.studentdb2 union select * from student.studentdb3;
Note: The results exhibit ‘error’ due to the fact that both the score columns have
different names.
INTERSECT OPERATOR

 Use Command – SELECT column_list FROM table1


Intersect
SELECT column_list FROM table2
Example: Scores obtained by students in sem 1 and sem 2
Use command: select * from student.studentdb2 intersect select * from
student.studentdb3;
Note: The results exhibit ‘error’ due to the fact that both the score columns have
different names.

You might also like