SlideShare a Scribd company logo
Introduction to MySQL
Presented by:
Amit Kumar Gupta
Flowchart-of-topic
 Database and Database Management System
 Introduction to MySQL
 Connecting and Disconnecting
 Entering Basic Queries
 Creating and Using a Database
Database and Database Management System
Student_id Student_name Branch Year
101 Rms yadav CS 3
102 Shubham Kumar CS 3
103 Sujeet Kumar CS 3
104 Rakesh Kumar ME 3
Database Management System (DBMS) is software to maintain and utilize
the collections of data (Oracle, DB2, MySQL)
MySQL Introduction
 MySQL is a database management system
 SQL stands for the Structured Query Language. It defines
how to insert, retrieve, modify and delete data
 Free from www.mysql.com
 Why are we using MySQL?
 Free (much cheaper than Oracle!)
 Each student can install MySQL locally.
 Easy to use Shell for creating tables, querying tables, etc.
 Easy to use with Java JDBC
Basic MySQL Operations
 Insert records
 Create table
 Load data
 Retrieve records
 Update records
 Delete records
 Modify table
 Join table
 Drop table
 Optimize table
 Count, Like, Order by, Group by
 More advanced ones (sub-queries, stored procedures, triggers, views …)
How MySQL stores data (by default)
 A MySQL server can store several databases
 Databases are stored as directories
 Default is at /usr/local/mysql /var/
 Tables are stored as files inside each database (directory)
 For each table, it has three files:
 table.FRM file containing information about the table structure
 table.MYD file containing the row data
 table.MYI containing any indexes belonging with this table, as well as some
statistics about the table.
Login
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 241 to server version: 3.23.49
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql>
 To exit the MySQL Shell, just type QUIT or EXIT:
mysql> QUIT
mysql> exit
Basic Queries
 Once logged in, you can try some simple queries.
 For example:
mysql> SELECT VERSION(), CURRENT_DATE;
+-----------+--------------+
| VERSION() | CURRENT_DATE |
+-----------+--------------+
| 3.23.49 | 2002-05-26 |
+-----------+--------------+
1 row in set (0.00 sec)
 Note that most MySQL commands end with a semicolon (;)
 MySQL returns the total number of rows found, and the total time to execute the
query.
Create Database
What are the current databases at the server?
mysql> show databases;
+--------------+
| Database |
+--------------+
| mysql | mysql is a database (stores users’ password …) used by system.
| test |
+--------------+
Create a database (make a directory) whose name is MyDB
mysql> create database MyDB;
Select database to use
mysql> use MyDB;
Database changed
What tables are currently stored in the MyDB database?
mysql> show tables;
Empty set (0.00 sec)
Create Table
 CREATE TABLE Table_Name (column_specifications)
 Example
mysql> CREATE TABLE student
-> (
-> Student_id INT UNSIGNED NOT NULL,
-> Student_name VARCHAR(20) NOT NULL,
-> Branch VARCHAR(5),
-> Year VARCHAR(5)
-> );
Query OK, 0 rows affected (0.00 sec)
Student_id Student_name Branch Year
Display Table Structure
mysql> show tables;
+--------------------+
| Tables_in_MyDB |
+--------------------+
| student |
+--------------------+
1 row in set (0.00 sec)
mysql> describe student;
+---------------+----------------------+------+------+----------+--------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------------+-------+-----+-----------+-------+
| Student_id | int(10) unsigned | | | 0 | |
| Student_name | varchar(20) | | | | |
| Branch | varchar(50) | YES | | NULL | |
| Year | varchar(5) | YES | | NULL | |
+---------------+----------------------+-------+------+----------+-------+
4 rows in set (0.00 sec)
this command use for show table in current database
this this comand use for display table structure in current
database
Insert Record
 INSERT Syntax:-
 INSERT INTO table_name values(column1,column2,column3,…..);
 Example
mysql> INSERT INTO student values( 101, ‘Rms Yadav', ‘CS', ‘3’);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO student values( 102, ‘Shubham Kumar', ‘CS’,
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO student values( 103, ‘Santosh Kumar', ‘CS', ‘3’);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO student values( 104, ‘Rakesh Kumar’, ‘ME', ‘3’);
Query OK, 1 row affected (0.00 sec)
SELECT Statement
 The SELECT statement is used to select data from a database.
 The data returned is stored in a result table, called the result-set.
 SELECT Syntax
SELECT column1, column2, ...
FROM table_name;
 Here, column1, column2, ... are the field names of the table you want to
select data from. If you want to select all the fields available in the table,
 use the following syntax:
 SELECT * FROM table_name;
+---------------+--------------------- +-------+------+----------+--+
| Student_id| Student_name | Branch | Year |
+---------------+----------------------+-------+------+----------+--+
| 101 | Rms Yadav | CS | 3 |
| 102 | Shubham Kumar | CS | 3 |
| 103 | Santosh Kumar | CS | 3 |
| 104 | Rakesh Kumar | ME | 3 |
+---------------+----------------------+-------+------+-----------+---+
Selecting Particular Rows
 You can select only particular rows from your table.
 For example, if you want to verify the change that you made to Bowser's birth date,
select Bowser's record like this:
 For example:-
mysql> SELECT * FROM Student WHERE name = “Rms Yadav";
+---------------+--------------------- +-------+------+----------+--+
| Student_id| Student_name | Branch | Year |
+---------------+----------------------+-------+------+----------+--+
| 101 | Rms Yadav | CS | 3 |
+---------------+----------------------+-------+------+----------+--+
Selecting Particular Rows using Relational operator
 To find all Student record in third Year
SELECT * FROM student WHERE Year >= “3";
+---------------+--------------------- +-------+------+----------+--+
| Student_id| Student_name | Branch | Year |
+---------------+----------------------+-------+------+----------+--+
| 101 | Rms Yadav | CS | 3 |
| 102 | Shubham Kumar | CS | 3 |
| 103 | Santosh Kumar | CS | 3 |
| 104 | Rakesh Kumar | ME | 3 |
+---------------+----------------------+-------+------+-----------+---+
 To find all Student Record, use a logical AND
SELECT * FROM student WHERE Year = ‘3’ AND Branch= “CS";
 To find all Student Record, use a logical OR
SELECT * FROM student WHERE Year = “3" OR Branch=“ME”;
Selecting Particular Columns
 If you don’t want to see entire rows from your table, just name the columns in which you are
interested, separated by commas.
 For example, if you want to know when your pets were born, select the name and birth columns.
 for example:-
mysql> select Student_id, Student_name from student;
D
+---------------+--------------------- +-------+-
| Student_id | Student_name |
+---------------+----------------------+-------+
| 101 | Rms Yadav |
| 102 | Shubham Kumar |
| 103 | Santosh Kumar |
| 104 | Rakesh Kumar |
+---------------+----------------------+-------+--
Group By
 Cluster query results based on different groups
Example:-
mysql> select Branch, count(*) from student GROUP BY Branch;
+---------+----------+
| Branch | count(*)|
+---------+-----------+
| CS | 3 |
| ME | 1 |
+---------+-----------+
Different type of Key
• Super Key
• Candidate Key
• Primary Key
• Alternate key
• Composite/Compound Key
• Unique Key
• Foreign Key
Super Key and Primary Key
 1. Super Key
 Super key can be defined as a set of one or more than one keys that can be used to identify a
record/data uniquely in a table. This key includes only those fields which includes unique value as if we
take an example of Employee than Employee_Id will be the field which includes unique value and it
become easy to identify the employee from Employee_Id field.
 For Example : -Keys which can be the subset of Super Key are Primary key, Unique key and Alternate
key. As right now we don’t know about these keys so further we will discuss these keys.
 2. Primary Key
 Primary key can be defined as a set of one or more fields/columns of a table that uniquely identify a
record in database table. Record can be uniquely identify when the column which includes unique value
like Employee_Id of employee from an organization. It will not accept null values and duplicate values.
Only one primary key can be exist in a single table not more than one.
 For Example:- Suppose a table consist of Employee data with fields Employee_Name,
Employee_Address,Employee_Id and Employee_Designation so in this table only one field is there which
is used to uniquely identify detail of Employee that is Employee_Id.
Unique Key and Alternate Key
 3. Unique Key
 Unique key can be defined as a set of one or more fields/columns of a table that have the capability to
uniquely identify a record in database table. We can have other fields also in a table beyond primary key
which are also able to uniquely identify the record. It can accept only one null value and it can not have
duplicate values in it.
 For Example:-Suppose a table consist of Employee data with fields Employee_Name,
Employee_Address,Employee_Id , Employee_Designation and Employee_PhoneNo so in this table except
Employee_Id we also have an another field named Employee_PhoneNo which is can also be used to
uniquely identify the record and can termed as Unique Key.
 4. Alternate key
 Alternate key can be defined as a key that can be work as a primary key if required. We can also
understand this key as a candidate for primary key as candidate key but right now it is not a primary key.
 For Example:- Suppose a table consist of Employee data with fields Employee_Name,
Employee_Address,Employee_Id , Employee_Designation and Employee_PhoneNo in this case
Employee_PhoneNo can be the alternate key as it is also suitable to identify the record uniquely but right
now it is not primary key.
table like primary key or we can also say that other fields than primary key which can become primary key and a
table can have more than one candidate key. Each candidate key can work as primary key if required in any
case.
 For Example:- Suppose a table consist of Employee data with fields Employee_Name,
Employee_Address,Employee_Id , Employee_Designation.Employee_PANNo and Employee_PhoneNo in this table
Employee_PhoneNo and Employee_PANNo are Candidate Keys as these two fields can also work as candidate key.
 6. Composite/Compound Key
 Composite Key can be defined as a combination of more than one fields/columns of a table to uniquely identify
the record. Fields which can be combine to make composite key can be candidate, primary key.
 For Example:-Suppose a table consist of Employee data with fields Employee_Name,
Employee_Address,Employee_Id , Employee_Designation, Employee_PANNo and Employee_PhoneNo in this table
to build a composite key we combine Employee_Id and Employee_PhoneNo to fetch data from table.
 7. Foreign Key
 Foreign Key can be defined as a field/column in the Company table that is Primary key in Employee table. It can
also accept multiple null values and duplicate values. This can be easily understand with the help of example
given below.
 For Example:- We can have a Employee_Id column in the Company table which is pointing to Employee_Id
column in a Employee table where it a primary key. So with the help of foreign key we can easily identify the
data from tables.
Modify Table Structure
 ALTER TABLE table_name Operations
mysql> alter table student add primary key (Student_id);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;
+---------------+--------------------- +-------+------+----------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------------+-------+------+----------+-------+
| Student_id | int(10) unsigned | | PRI | 0 | |
| Student_name | varchar(20) | | | | |
| Branch | varchar(7) | YES | | NULL | |
| Year | varchar(5) | YES | | NULL | |
+---------------+----------------------+-------+------+-----------+-------+
4 rows in set (0.00 sec)
Update Record
 UPDATE table_name
SET which columns to change
WHERE condition
 Example
mysql> UPDATE student SET Year=‘4' WHERE Student_name='Shannon';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM student WHERE Student_name=‘Shannon’;
+------------+---------------+--------+--------+--------+
| Student_name | student_ID | Branch |Year |
+------------+---------------+--------+--------+--------+
| Rms Yadav | 101 | CS | 4 |
+------------+---------------+--------+--------+--------+
1 row in set (0.00 sec)
Delete Record
 DELETE FROM table_name WHERE condition
 Example
mysql> DELETE FROM student WHERE Student_name=‘Rms Yadav';
Query OK, 1 row affected (0.00 sec)
Mysql> DELETE FROM student;
Will delete ALL student records
Drop Table
 DROP TABLE table_name
 Example
mysql> drop table student;
Query OK, 0 rows affected (0.00 sec)
 Logout MySQL
mysq> quit;
SQL JOIN
OrderID CustomerID OrderDate
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
Let's look at a selection from the "Orders" table:
Customer
ID
CustomerName ContactName Country
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo
Emparedados y
helados
Ana Trujillo Mexico
3 Antonio Moreno
Taquería
Antonio Moreno Mexico
Customer
ID
CustomerName ContactName Country
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo
Emparedados y
helados
Ana Trujillo Mexico
3 Antonio Moreno
Taquería
Antonio Moreno Mexico
Then, look at a selection from the "Customers" table:
OrderID CustomerName OrderDat
e
10308 Ana Trujillo Emparedados y helados 9/18/199
6
10365 Antonio Moreno Taquería 11/27/19
96
10383 Around the Horn 12/16/19
96
10355 Around the Horn 11/15/19
96
10278 Berglunds snabbköp 8/12/199
6
Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the
"Customers" table. The relationship between the two tables above is the "CustomerID" column.
Then, we can create the following SQL statement (that contains an INNER JOIN), that selects
records that have matching values in both tables:
Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID= Customers.CustomerID;
Different Types of SQL JOINs
 Different Types of SQL JOINs
 Here are the different types of the JOINs in SQL:
1.(INNER) JOIN: Returns records that have matching values in both tables
SQL LEFT JOIN Keyword
 The LEFT JOIN keyword returns all records from the left table (table1), and the matched
records from the right table (table2). The result is NULL from the right side, if there is no
match.
 LEFT JOIN Syntax
 SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
SQL RIGHT JOIN Keyword
 The RIGHT JOIN keyword returns all records from the right table
(table2), and the matched records from the left table (table1). The
result is NULL from the left side, when there is no match.
 RIGHT JOIN Syntax
 SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
SQL FULL OUTER JOIN Keyword
 SQL FULL OUTER JOIN Keyword
 The FULL OUTER JOIN keyword return all records when there is a
match in either left (table1) or right (table2) table records.
 Note: FULL OUTER JOIN can potentially return very large result-sets!
Thank You
By Ramashankar Kumar

More Related Content

PPT
PPT
mysqlHiep.ppt
PPT
Applied Partitioning And Scaling Your Database System Presentation
PPT
MySQL Functions
DOC
Best sql plsql material
PDF
Moving to the NoSQL side: MySQL JSON functions
PDF
Character Encoding - MySQL DevRoom - FOSDEM 2015
DOCX
Relational DB Course
mysqlHiep.ppt
Applied Partitioning And Scaling Your Database System Presentation
MySQL Functions
Best sql plsql material
Moving to the NoSQL side: MySQL JSON functions
Character Encoding - MySQL DevRoom - FOSDEM 2015
Relational DB Course

What's hot (20)

PDF
MySQL partitions tutorial
PDF
New features in Performance Schema 5.7 in action
PDF
My SQL Idiosyncrasies That Bite OTN
PDF
Mysql basics1
DOCX
12c Database TEMPORAL VALIDITY & FLASHBACK ARCHIVE Testing
PPTX
Lecture3 mysql gui by okello erick
PDF
MySQL for business developer - Titouan BENOIT
PPTX
MySQL constraints
PDF
Performance Schema for MySQL Troubleshooting
PDF
Performance Schema for MySQL Troubleshooting
PDF
w_dzon05
PPT
Intro to my sql
TXT
Cat database
ODP
Pro PostgreSQL
PDF
MySQL Query tuning 101
PDF
Why Use EXPLAIN FORMAT=JSON?
PPT
My sql with querys
PPTX
DB2 Basic Commands - UDB
TXT
Scripts related to temp tablespace
PDF
Empowering Users with Analytical MDX
MySQL partitions tutorial
New features in Performance Schema 5.7 in action
My SQL Idiosyncrasies That Bite OTN
Mysql basics1
12c Database TEMPORAL VALIDITY & FLASHBACK ARCHIVE Testing
Lecture3 mysql gui by okello erick
MySQL for business developer - Titouan BENOIT
MySQL constraints
Performance Schema for MySQL Troubleshooting
Performance Schema for MySQL Troubleshooting
w_dzon05
Intro to my sql
Cat database
Pro PostgreSQL
MySQL Query tuning 101
Why Use EXPLAIN FORMAT=JSON?
My sql with querys
DB2 Basic Commands - UDB
Scripts related to temp tablespace
Empowering Users with Analytical MDX
Ad

Similar to DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018 (20)

PDF
Advance MySQL Training by Pratyush Majumdar
PPT
MySQL Database System Hiep Dinh
PPT
My sql1
PPT
4. Data Manipulation.ppt
PPT
MySql slides (ppt)
DOCX
Bt0075, rdbms and my sql
PPT
Basic Commands using Structured Query Langauage(SQL)
DOCX
Bt0075, rdbms and my sql
PPT
Distributed databases systems-3-2015.ppt
PPT
Introduction To Lamp P2
PDF
Mysqlfunctions
PDF
Performance Schema for MySQL Troubleshooting
PDF
MySQL Kitchen : spice up your everyday SQL queries
PPT
mySQL and Relational Databases
PPTX
Introduction databases and MYSQL
PPTX
PHP mysql Introduction database
PPT
PDF
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
PPTX
Optimizing queries MySQL
Advance MySQL Training by Pratyush Majumdar
MySQL Database System Hiep Dinh
My sql1
4. Data Manipulation.ppt
MySql slides (ppt)
Bt0075, rdbms and my sql
Basic Commands using Structured Query Langauage(SQL)
Bt0075, rdbms and my sql
Distributed databases systems-3-2015.ppt
Introduction To Lamp P2
Mysqlfunctions
Performance Schema for MySQL Troubleshooting
MySQL Kitchen : spice up your everyday SQL queries
mySQL and Relational Databases
Introduction databases and MYSQL
PHP mysql Introduction database
MariaDB 10.5 new features for troubleshooting (mariadb server fest 2020)
Optimizing queries MySQL
Ad

Recently uploaded (20)

PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Chapter 2 Digital Image Fundamentals.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Advanced IT Governance
PPTX
Cloud computing and distributed systems.
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
NewMind AI Monthly Chronicles - July 2025
Big Data Technologies - Introduction.pptx
Understanding_Digital_Forensics_Presentation.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Sensors and Actuators in IoT Systems using pdf
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced Soft Computing BINUS July 2025.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectral efficient network and resource selection model in 5G networks
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Chapter 2 Digital Image Fundamentals.pdf
Electronic commerce courselecture one. Pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Advanced IT Governance
Cloud computing and distributed systems.
madgavkar20181017ppt McKinsey Presentation.pdf

DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018

  • 1. Introduction to MySQL Presented by: Amit Kumar Gupta
  • 2. Flowchart-of-topic  Database and Database Management System  Introduction to MySQL  Connecting and Disconnecting  Entering Basic Queries  Creating and Using a Database
  • 3. Database and Database Management System Student_id Student_name Branch Year 101 Rms yadav CS 3 102 Shubham Kumar CS 3 103 Sujeet Kumar CS 3 104 Rakesh Kumar ME 3 Database Management System (DBMS) is software to maintain and utilize the collections of data (Oracle, DB2, MySQL)
  • 4. MySQL Introduction  MySQL is a database management system  SQL stands for the Structured Query Language. It defines how to insert, retrieve, modify and delete data  Free from www.mysql.com  Why are we using MySQL?  Free (much cheaper than Oracle!)  Each student can install MySQL locally.  Easy to use Shell for creating tables, querying tables, etc.  Easy to use with Java JDBC
  • 5. Basic MySQL Operations  Insert records  Create table  Load data  Retrieve records  Update records  Delete records  Modify table  Join table  Drop table  Optimize table  Count, Like, Order by, Group by  More advanced ones (sub-queries, stored procedures, triggers, views …)
  • 6. How MySQL stores data (by default)  A MySQL server can store several databases  Databases are stored as directories  Default is at /usr/local/mysql /var/  Tables are stored as files inside each database (directory)  For each table, it has three files:  table.FRM file containing information about the table structure  table.MYD file containing the row data  table.MYI containing any indexes belonging with this table, as well as some statistics about the table.
  • 7. Login Enter password: ***** Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 241 to server version: 3.23.49 Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql>  To exit the MySQL Shell, just type QUIT or EXIT: mysql> QUIT mysql> exit
  • 8. Basic Queries  Once logged in, you can try some simple queries.  For example: mysql> SELECT VERSION(), CURRENT_DATE; +-----------+--------------+ | VERSION() | CURRENT_DATE | +-----------+--------------+ | 3.23.49 | 2002-05-26 | +-----------+--------------+ 1 row in set (0.00 sec)  Note that most MySQL commands end with a semicolon (;)  MySQL returns the total number of rows found, and the total time to execute the query.
  • 9. Create Database What are the current databases at the server? mysql> show databases; +--------------+ | Database | +--------------+ | mysql | mysql is a database (stores users’ password …) used by system. | test | +--------------+ Create a database (make a directory) whose name is MyDB mysql> create database MyDB; Select database to use mysql> use MyDB; Database changed What tables are currently stored in the MyDB database? mysql> show tables; Empty set (0.00 sec)
  • 10. Create Table  CREATE TABLE Table_Name (column_specifications)  Example mysql> CREATE TABLE student -> ( -> Student_id INT UNSIGNED NOT NULL, -> Student_name VARCHAR(20) NOT NULL, -> Branch VARCHAR(5), -> Year VARCHAR(5) -> ); Query OK, 0 rows affected (0.00 sec) Student_id Student_name Branch Year
  • 11. Display Table Structure mysql> show tables; +--------------------+ | Tables_in_MyDB | +--------------------+ | student | +--------------------+ 1 row in set (0.00 sec) mysql> describe student; +---------------+----------------------+------+------+----------+--------+ | Field | Type | Null | Key | Default | Extra | +---------------+----------------------+-------+-----+-----------+-------+ | Student_id | int(10) unsigned | | | 0 | | | Student_name | varchar(20) | | | | | | Branch | varchar(50) | YES | | NULL | | | Year | varchar(5) | YES | | NULL | | +---------------+----------------------+-------+------+----------+-------+ 4 rows in set (0.00 sec) this command use for show table in current database this this comand use for display table structure in current database
  • 12. Insert Record  INSERT Syntax:-  INSERT INTO table_name values(column1,column2,column3,…..);  Example mysql> INSERT INTO student values( 101, ‘Rms Yadav', ‘CS', ‘3’); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO student values( 102, ‘Shubham Kumar', ‘CS’, Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO student values( 103, ‘Santosh Kumar', ‘CS', ‘3’); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO student values( 104, ‘Rakesh Kumar’, ‘ME', ‘3’); Query OK, 1 row affected (0.00 sec)
  • 13. SELECT Statement  The SELECT statement is used to select data from a database.  The data returned is stored in a result table, called the result-set.  SELECT Syntax SELECT column1, column2, ... FROM table_name;  Here, column1, column2, ... are the field names of the table you want to select data from. If you want to select all the fields available in the table,  use the following syntax:  SELECT * FROM table_name; +---------------+--------------------- +-------+------+----------+--+ | Student_id| Student_name | Branch | Year | +---------------+----------------------+-------+------+----------+--+ | 101 | Rms Yadav | CS | 3 | | 102 | Shubham Kumar | CS | 3 | | 103 | Santosh Kumar | CS | 3 | | 104 | Rakesh Kumar | ME | 3 | +---------------+----------------------+-------+------+-----------+---+
  • 14. Selecting Particular Rows  You can select only particular rows from your table.  For example, if you want to verify the change that you made to Bowser's birth date, select Bowser's record like this:  For example:- mysql> SELECT * FROM Student WHERE name = “Rms Yadav"; +---------------+--------------------- +-------+------+----------+--+ | Student_id| Student_name | Branch | Year | +---------------+----------------------+-------+------+----------+--+ | 101 | Rms Yadav | CS | 3 | +---------------+----------------------+-------+------+----------+--+
  • 15. Selecting Particular Rows using Relational operator  To find all Student record in third Year SELECT * FROM student WHERE Year >= “3"; +---------------+--------------------- +-------+------+----------+--+ | Student_id| Student_name | Branch | Year | +---------------+----------------------+-------+------+----------+--+ | 101 | Rms Yadav | CS | 3 | | 102 | Shubham Kumar | CS | 3 | | 103 | Santosh Kumar | CS | 3 | | 104 | Rakesh Kumar | ME | 3 | +---------------+----------------------+-------+------+-----------+---+  To find all Student Record, use a logical AND SELECT * FROM student WHERE Year = ‘3’ AND Branch= “CS";  To find all Student Record, use a logical OR SELECT * FROM student WHERE Year = “3" OR Branch=“ME”;
  • 16. Selecting Particular Columns  If you don’t want to see entire rows from your table, just name the columns in which you are interested, separated by commas.  For example, if you want to know when your pets were born, select the name and birth columns.  for example:- mysql> select Student_id, Student_name from student; D +---------------+--------------------- +-------+- | Student_id | Student_name | +---------------+----------------------+-------+ | 101 | Rms Yadav | | 102 | Shubham Kumar | | 103 | Santosh Kumar | | 104 | Rakesh Kumar | +---------------+----------------------+-------+--
  • 17. Group By  Cluster query results based on different groups Example:- mysql> select Branch, count(*) from student GROUP BY Branch; +---------+----------+ | Branch | count(*)| +---------+-----------+ | CS | 3 | | ME | 1 | +---------+-----------+
  • 18. Different type of Key • Super Key • Candidate Key • Primary Key • Alternate key • Composite/Compound Key • Unique Key • Foreign Key
  • 19. Super Key and Primary Key  1. Super Key  Super key can be defined as a set of one or more than one keys that can be used to identify a record/data uniquely in a table. This key includes only those fields which includes unique value as if we take an example of Employee than Employee_Id will be the field which includes unique value and it become easy to identify the employee from Employee_Id field.  For Example : -Keys which can be the subset of Super Key are Primary key, Unique key and Alternate key. As right now we don’t know about these keys so further we will discuss these keys.  2. Primary Key  Primary key can be defined as a set of one or more fields/columns of a table that uniquely identify a record in database table. Record can be uniquely identify when the column which includes unique value like Employee_Id of employee from an organization. It will not accept null values and duplicate values. Only one primary key can be exist in a single table not more than one.  For Example:- Suppose a table consist of Employee data with fields Employee_Name, Employee_Address,Employee_Id and Employee_Designation so in this table only one field is there which is used to uniquely identify detail of Employee that is Employee_Id.
  • 20. Unique Key and Alternate Key  3. Unique Key  Unique key can be defined as a set of one or more fields/columns of a table that have the capability to uniquely identify a record in database table. We can have other fields also in a table beyond primary key which are also able to uniquely identify the record. It can accept only one null value and it can not have duplicate values in it.  For Example:-Suppose a table consist of Employee data with fields Employee_Name, Employee_Address,Employee_Id , Employee_Designation and Employee_PhoneNo so in this table except Employee_Id we also have an another field named Employee_PhoneNo which is can also be used to uniquely identify the record and can termed as Unique Key.  4. Alternate key  Alternate key can be defined as a key that can be work as a primary key if required. We can also understand this key as a candidate for primary key as candidate key but right now it is not a primary key.  For Example:- Suppose a table consist of Employee data with fields Employee_Name, Employee_Address,Employee_Id , Employee_Designation and Employee_PhoneNo in this case Employee_PhoneNo can be the alternate key as it is also suitable to identify the record uniquely but right now it is not primary key.
  • 21. table like primary key or we can also say that other fields than primary key which can become primary key and a table can have more than one candidate key. Each candidate key can work as primary key if required in any case.  For Example:- Suppose a table consist of Employee data with fields Employee_Name, Employee_Address,Employee_Id , Employee_Designation.Employee_PANNo and Employee_PhoneNo in this table Employee_PhoneNo and Employee_PANNo are Candidate Keys as these two fields can also work as candidate key.  6. Composite/Compound Key  Composite Key can be defined as a combination of more than one fields/columns of a table to uniquely identify the record. Fields which can be combine to make composite key can be candidate, primary key.  For Example:-Suppose a table consist of Employee data with fields Employee_Name, Employee_Address,Employee_Id , Employee_Designation, Employee_PANNo and Employee_PhoneNo in this table to build a composite key we combine Employee_Id and Employee_PhoneNo to fetch data from table.  7. Foreign Key  Foreign Key can be defined as a field/column in the Company table that is Primary key in Employee table. It can also accept multiple null values and duplicate values. This can be easily understand with the help of example given below.  For Example:- We can have a Employee_Id column in the Company table which is pointing to Employee_Id column in a Employee table where it a primary key. So with the help of foreign key we can easily identify the data from tables.
  • 22. Modify Table Structure  ALTER TABLE table_name Operations mysql> alter table student add primary key (Student_id); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> describe student; +---------------+--------------------- +-------+------+----------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+----------------------+-------+------+----------+-------+ | Student_id | int(10) unsigned | | PRI | 0 | | | Student_name | varchar(20) | | | | | | Branch | varchar(7) | YES | | NULL | | | Year | varchar(5) | YES | | NULL | | +---------------+----------------------+-------+------+-----------+-------+ 4 rows in set (0.00 sec)
  • 23. Update Record  UPDATE table_name SET which columns to change WHERE condition  Example mysql> UPDATE student SET Year=‘4' WHERE Student_name='Shannon'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> SELECT * FROM student WHERE Student_name=‘Shannon’; +------------+---------------+--------+--------+--------+ | Student_name | student_ID | Branch |Year | +------------+---------------+--------+--------+--------+ | Rms Yadav | 101 | CS | 4 | +------------+---------------+--------+--------+--------+ 1 row in set (0.00 sec)
  • 24. Delete Record  DELETE FROM table_name WHERE condition  Example mysql> DELETE FROM student WHERE Student_name=‘Rms Yadav'; Query OK, 1 row affected (0.00 sec) Mysql> DELETE FROM student; Will delete ALL student records
  • 25. Drop Table  DROP TABLE table_name  Example mysql> drop table student; Query OK, 0 rows affected (0.00 sec)  Logout MySQL mysq> quit;
  • 26. SQL JOIN OrderID CustomerID OrderDate 10308 2 1996-09-18 10309 37 1996-09-19 10310 77 1996-09-20 A JOIN clause is used to combine rows from two or more tables, based on a related column between them. Let's look at a selection from the "Orders" table:
  • 27. Customer ID CustomerName ContactName Country 1 Alfreds Futterkiste Maria Anders Germany 2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico 3 Antonio Moreno Taquería Antonio Moreno Mexico Customer ID CustomerName ContactName Country 1 Alfreds Futterkiste Maria Anders Germany 2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico 3 Antonio Moreno Taquería Antonio Moreno Mexico Then, look at a selection from the "Customers" table:
  • 28. OrderID CustomerName OrderDat e 10308 Ana Trujillo Emparedados y helados 9/18/199 6 10365 Antonio Moreno Taquería 11/27/19 96 10383 Around the Horn 12/16/19 96 10355 Around the Horn 11/15/19 96 10278 Berglunds snabbköp 8/12/199 6 Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column. Then, we can create the following SQL statement (that contains an INNER JOIN), that selects records that have matching values in both tables: Example SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID= Customers.CustomerID;
  • 29. Different Types of SQL JOINs  Different Types of SQL JOINs  Here are the different types of the JOINs in SQL: 1.(INNER) JOIN: Returns records that have matching values in both tables
  • 30. SQL LEFT JOIN Keyword  The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.  LEFT JOIN Syntax  SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
  • 31. SQL RIGHT JOIN Keyword  The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side, when there is no match.  RIGHT JOIN Syntax  SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
  • 32. SQL FULL OUTER JOIN Keyword  SQL FULL OUTER JOIN Keyword  The FULL OUTER JOIN keyword return all records when there is a match in either left (table1) or right (table2) table records.  Note: FULL OUTER JOIN can potentially return very large result-sets!