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
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.
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!