Lecture #8 To 9
Lecture #8 To 9
الرحمن
الرحیم
3
OUTCOMES
• DDL
• DQL
• DCL
• DML etc
6
1. DATA DEFINITION LANGUAGE (DDL)
• DDL(Data Definition Language) : DDL or Data Definition Language
actually consists of the SQL commands that can be used to define the
database schema. It simply deals with descriptions of the database
schema and these commands are used to create, alter, and drop
tables etc. and they are covered first in this chapter.
7
TYPES OF CREATE STATEMENT
• There are two CREATE statements available in SQL:
• 1. CREATE DATABASE
• 2. CREATE TABLE
• Syntax:
• Example Query:
This query will create a new database in SQL and name the database
as my_database.
8
CREATE STATEMENT
• 2. The CREATE TABLE statement is used to create a table. We know that
a table comprises of rows and columns. Syntax:
9
EXAMPLE QUERY:
• This query will create a table named Students with three columns, ROLL_NO,
NAME and SUBJECT.
• (
• ROLL_NO int(3),
• NAME varchar(20),
• SUBJECT varchar(20),
• );
• This query will create a table named Students. The ROLL_NO field is of type int and
can store an integer number of size 3. The next two columns NAME and SUBJECT
are of type varchar and can store characters and the size 20 specifies that these two
fields can hold maximum of 20 characters.
10
DROP QUERY
• DROP is used to delete a whole database or just a table. The DROP statement
destroys the objects like an existing database, table, index, or view.
A DROP statement in SQL removes a component from a relational database
management system (RDBMS)
• Syntax:
• Examples:
11
2. DATA QUERY LANGUAGE (DQL) :
• The SELECT Statement in SQL is used to retrieve or fetch data from a
database. We can fetch either the entire table or according to some
specified rules. The data returned is stored in a result table.
• Example of DQL:
12
BASIC SYNTAX OF (DQL) :
• SELECT column1,column2 FROM table_name
• This query will return all the rows in the table with fields column1 ,
column2.
• Query to fetch the fields ROLL_NO, NAME, AGE from the table Student:
13
EXAMPLE OF (DQL) :
Output:
1 Ram 18
2 RAMESH 18
3 SUJIT 20
4 SURESH 18
14
EXAMPLE OF (DQL) :
• To fetch all the fields from the table Student:
• SELECT * FROM Student;
• Output:
15 SELECT STATEMENT
16
USING THE WHERE CLAUSE:
17
USING THE WHERE CLAUSE:
• Examples of DML:
• INSERT – is used to insert data into a table.
• UPDATE – is used to update existing data within a table.
19
BEFORE INSERT STATEMENT OF SQL
20
INSERT STATEMENT OF SQL
• The INSERT INTO statement of SQL is used to insert a new row
in a table. There are two ways of using INSERT INTO statement
for inserting rows:
21
OUTPUT: THE TABLE STUDENT WILL NOW LOOK LIKE:
22
23
OUTPUT:THE TABLE STUDENT WILL NOW LOOK LIKE:
24
DELETE STATEMENT
• The DELETE Statement in SQL is used to delete existing records from
a table. We can delete a single record or multiple records depending on
the condition we specify in the WHERE clause.
• Basic Syntax:
25
BEFORE DELETE STATEMENT TABLE
26
DELETE STATEMENT
• Deleting single record: Delete the rows where NAME = ‘Ram’. This will delete
only the first row.
• Output:
• The above query will delete only the first row and the table Student will now
look like,
27
AFTER DELETE STATEMENT TABLE
ROLL_NO NAME ADDRESS PHONE Age
28
UPDATE STATEMENT
• The UPDATE statement in SQL is used to update the data of an existing table
in database. We can update single columns as well as multiple columns using
UPDATE statement as per our requirement: Basic Syntax
• WHERE condition;
29
BEFORE UPDATE STATEMENT
30
UPDATE STATEMENT
• Updating single column: Update the column NAME and set the value
to ‘PRATIK’ in all the rows where Age is 20.
31
BEFORE UPDATE STATEMENT
ROLL_NO NAME ADDRESS PHONE Age
• SQL is a database computer language designed for the retrieval and management of
data in a relational database.
• DDL or Data Definition Language actually consists of the SQL commands that can
be used to define the database schema.
• The purpose of DQL Command is to get some schema relation based on the query
passed to it.
• The SQL commands that deals with the manipulation of data present
in the database belong to DML
33
THE END