Mysql
Mysql
Mysql
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;
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
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);
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 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 –
Use command –
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 –