You are on page 1of 29

Hello!

I’m
Nitesh Sharma
1
Presentation
ON
SQL & COMMANDS

2
About SQL
SQL (Structured Query Language) is a
standard language for assessing and
manipulating databases.
By using SQL commands one can create
data bases and perform other functions
like create tables , add records , modify
3
software . It provides
some MySQL
useful features like
creating ,
sorting manipulating
and
assessing data stored in
the
SQL COMMANDS

DDL DQL DML

• CREATE • SELECT • INSERT

• DROP • UPDATE

• ALTER • DELETE
5
Creating databases
SYNTAX
CREATE DATABASE [if not exist] <database_name>

Opening databases
Use <database_name> ;
To check databases
Show <database_name> ;

6
CREATE Command
SYNTAX
Create <table_name>
(
<column_name> <datatype> [(<size>)] ,
<column_name> <datatype> [(<size>)]...
);

7
Write queries for the following questions :

1. Reema wants to create a table Employee with


these values . Write a Query for her.

Query ~ CREATE TABLE Employe


(
Emp_Name Varchar (10),
Emp_ID Char (4),
Phone_No Int,
City_Country Varchar(15)
);
8
INSERT
SYNTAX
Insert into <table_name> Values
(<Value1> , <Value2>... ) ;
OR
Insert into <table_name> (<column1>, <column2>,
<column3>, <column4>) Values (<Value1>, <Value2>,
<Value3> , <Value4> ) ;

9
Write queries for the following questions :

1. Reema wants to create a table Employee with


these values . Write a Query for her.

Query ~ INSERT into Employee Values


( ‘Atul‘, 1001,931845XXXX,
’Delhi,India’ ) , ( ‘Harsh’, 1002,
836821XXXX, ’Mysore,India’ ) ,
‘Mohit‘, 1003, 876631XXXX,
‘Jaipur, India’ ) , ( ‘Nikhil’, 1004,
881038XXXX, ‘Indore,India’ ) ;
10
Output , Result

Emp_Name Emp_ID Phone_No City_Country

Atul 1001 931845XXXX Delhi, India


Harsh 1002 836821XXXX Mysore, India
Mohit 1003 876631XXXX Jaipur, India
Nikhil 1004 881038XXXX Indore, India

11
ALTER Command
ALTER TABLE – ALTER/MODIFY COLUMN
SYNTAX
ALTER TABLE <table_name>
ALTER COLUMN <column_name> <datatype> <(size)>;

ALTER TABLE – ADD Column


SYNTAX
ALTER TABLE <table_name>
ADD <column_name> <datatype> <(size)>;
12
Write queries for the following questions :

1. Reema wants to Add a column For salary in the


table . Write a Query for her.

Query ~ ALTER TABLE Employee


ADD Emp_Salary Int

13
UPDATE Command
SYNTAX
UPDATE <table_name> SET <column1> = <value1> ,
<column2> = <value2>
WHERE condition ;

14
Write queries for the following questions :

1. Reema wants to change the City_Country of Mohit


with Bhopal, India. Write a Query for her.

Query ~ UPDATE Employee


SET City_Country = Bhopal, India
WHERE name = Mohit ;

15
Output , Result

Emp_Name Emp_ID Phone_No City_Country Emp_Salary

Atul 1001 931845XXXX Delhi, India


Harsh 1002 836821XXXX Mysore, India
Mohit 1003 876631XXXX Bhopal, India
Nikhil 1004 881038XXXX Indore, India

16
SELECT
To see the entire table or records in the table
SYNTAX
Select * <table_name> ;
OR
Select <column1> , <column2>
from <table_name> ;

17
SELECT with WHERE Clause
To view certain records or rows from table
SYNTAX
Select * from <table name>
Where <condition> ;

Relational Operators
= , > , < , >= , <= , <> ( not equal to)

18
Write queries for the following questions :
1. Reema wants to see the records of those whose id is
1004 .

Query ~ SELECT * from Employee


WHERE Emp_Id = 1004 ;

2. Reema wants to see the Name and Phone_No


column .

Query ~ SELECT Emp_Name , PhonePhone_No from


Employee ;
19
Output , Result
1. Emp_Name Emp_ID Phone_No City_Country Emp_Salary

Nikhil 1004 881038XXXX Indore, India

Emp_Name Phone_No
2.
Atul 931845XXXX
Harsh 836821XXXX
Mohit 876631XXXX
Nikhil 881038XXXX
20
DELETE Command
To delete the existing records from the table
SYNTAX
DELETE FROM <table_name> WHERE <condition> ;

DELETE Command
To delete All the records from the table
SYNTAX
DELETE FROM <table_name> ;
21
Example , Database – Emp1db
Table Name ~ Employee
Emp_Name Emp_ID Phone_No City_Country

Atul 1001 931845XXXX Delhi, India


Harsh 1002 836821XXXX Mysore, India
Mohit 1003 876631XXXX Jaipur, India
Nikhil 1004 881038XXXX Indore, India

22
Write queries for the following questions :

1. Simar didn’t want the record with the name


Harsh. Write a query for her.
Query ~ DELETE from Employee
WHERE Emp_Name = “Harsh” ;

2. She wants to clear all the records. Write a query


for her.
Query ~ DELETE from Employee ;
23
Output , Result
1. Emp_Name Emp_ID Phone_No City_Country

Atul 1001 931845XXXX Delhi, India


Mohit 1003 876631XXXX Bhopal, India
Nikhil 1004 881038XXXX Indore, India

Emp_Name Emp_ID Phone_No City_Country


2.

24
Example , Database – Emp1db
Table Name ~ Employee
Emp_Name Emp_ID Phone_No City_Country

Atul 1001 931845XXXX Delhi, India


Harsh 1002 836821XXXX Mysore, India
Mohit 1003 876631XXXX Jaipur, India
Nikhil 1004 881038XXXX Indore, India

25
DROP Command
To Delete the whole table
SYNTAX

DROP TABLE <table_name> ;

DROP Command
To delete the database
SYNTAX
DROP DATABASE <database_name> ;
26
Write queries for the following questions :

3. Now She have no use of that table ,So Delete the


table . Write a Query for her.
Query ~ DROP TABLE Employee ;

4. She wants to Delete the Database . Write a


Query for her.

Query ~ DROP DATABASE Emp1db ;

27
Output , Result 3,4 :
Following the query , You will not
find any database with name
Emp1db and table with the name
Employee .
That means the table employee has
been deleted from the database
Emp1db and the database too.
28
Thanks !
29

You might also like