SlideShare a Scribd company logo
Query
 A Language which is used to store and retrieve data from database is
known as query language. For example – SQL
 There are two types of query language:
1.Procedural Query language
2.Non-procedural query language
Nahida Nazir
Procedural Query language
 In procedural query language, user instructs the system to perform a
series of operations to produce the desired results. Here users tells
what data to be retrieved from database and how to retrieve it.
Nahida Nazir
Non-procedural query language:
 n Non-procedural query language, user instructs the system to
produce the desired result without telling the step by step process.
Here users tells what data to be retrieved from database but doesn’t
tell how to retrieve it.
Nahida Nazir
Relational Algebra:
 Relational algebra is a conceptual procedural query language used on relational model
 Relational Calculus:
 Relational calculus is a conceptual non-procedural query language used on relational
model.
 SQL is a practical implementation of relational algebra and relational calculus.
Nahida Nazir
Relational Algebra
 Relational algebra is a procedural query language that works on relational model. The
purpose of a query language is to retrieve data from database or perform various
operations such as insert, update, delete on the data.
Nahida Nazir
Types of operations in relational
algebra
 1. Basic Operations
2. Derived Operations
Nahida Nazir
Basic/Fundamental Operations:
 1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Nahida Nazir
Derived Operations:
 1. Natural Join (⋈)
2. Left, Right, Full outer join (⟕, ⟖, ⟗)
3. Intersection (∩)
4. Division (÷)
Nahida Nazir
Select Operator (σ)
 Select Operator is denoted by sigma (σ) and it is used to find the tuples (or rows)
in a relation (or table) which satisfy the given condition.
 Predicates are keywords that specify a relationship between two expressions.
 Syntax: σ Condition/Predicate(Relation/Table name)
Nahida Nazir
Example
Nahida Nazir
Project Operator (∏)
 Project operator is denoted by ∏ symbol and it is used to select desired
columns (or attributes) from a table (or relation).
 Syntax of Project Operator (∏)
 ∏ column_name1, column_name2, ...., column_nameN(table_name)
Nahida Nazir
Example
Nahida Nazir
Union Operator (∪)
 Union operator is denoted by ∪ symbol and it is used to select all the
rows (tuples) from two tables (relations).
 Lets say we have two relations R1 and R2 both have same columns and we want to select
all the tuples(rows) from these relations then we can apply the union operator on these
relations.
 Note: The rows (tuples) that are present in both the tables will only appear once in the union
set. In short you can say that there are no duplicates present after the union operation.
 There are no duplicate names present in the output
 Syntax of Union Operator (∪)
 table_name1 ∪ table_name2
Nahida Nazir
Intersection Operator (∩)
 Intersection operator is denoted by ∩ symbol and it is used to select common rows
(tuples) from two tables (relations).
 Lets say we have two relations R1 and R2 both have same columns and we want to
select all those tuples(rows) that are present in both the relations, then in that case
we can apply intersection operation on these two relations R1 ∩ R2.
 Note: Only those rows that are present in both the tables will appear in the result set.
 Syntax of Intersection Operator (∩)
 table_name1 ∩ table_name2
Nahida Nazir
Set Difference (-)
 Set Difference is denoted by – symbol. Lets say we have two relations R1 and
R2 and we want to select all those tuples(rows) that are present in Relation R1
but not present in Relation R2, this can be done using Set difference R1 – R2.
 Syntax of Set Difference (-)
 table_name1 - table_name2
Nahida Nazir
Cartesian product (X)
 Cartesian Product is denoted by X symbol. Lets say we have two relations R1 and
R2 then the cartesian product of these two relations (R1 X R2) would combine each
tuple of first relation R1 with the each tuple of second relation R2. I know it sounds
confusing but once we take an example of this, you will be able to understand this.
 Syntax of Cartesian product (X)
 R1 X R2
Nahida Nazir
Rename (ρ)
 Rename (ρ) operation can be used to rename a relation or an attribute of a
relation.
Rename (ρ) Syntax:
ρ(new_relation_name, old_relation_name)
Nahida Nazir
What is SQL?
SQL stands for Structured Query Language.
SQL is used to communicate with a database.
SQL lets you access and manipulate databases.
Software to be used: Oracle
Nahida Nazir
Polling Question
Nahida Nazir
What SQL can do?
What Can SQL do?
-SQL can execute queries against a database
-SQL can retrieve data from a database
-SQL can insert records in a database
-SQL can update records in a database
-SQL can delete records from a database
-SQL can create new databases
-SQL can create new tables in a database
-SQL can create stored procedures in a database
-SQL can create views in a database
-SQL can set permissions on tables, procedures, and views
Nahida Nazir
DDL
Data Definition Language (DDL) statements are used to define the database
structure or schema. Some examples:
•CREATE - to create objects in the database
•ALTER - alters the structure of the database
•DROP - delete objects from the database
•TRUNCATE - remove all records from a table, including all spaces allocated
for the records are removed
•RENAME - rename an object
Nahida Nazir
DML
Data Manipulation Language (DML) statements are used for managing data
within schema objects. Some examples:
•SELECT - retrieve data from the a database
•INSERT - insert data into a table
•UPDATE - updates existing data within a table
•DELETE - deletes all records from a table, the space for the records remain
Nahida Nazir
CREATING A TABLE
The CREATE TABLE statement is used to create a table in a database.
Tables are organized into rows and columns; and each table must have a
name.
Nahida Nazir
CREATING A TABLE
Syntax:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
Nahida Nazir
CREATING A TABLE
Example:
CREATE TABLE Student
(
StudentID int,
LastName varchar(25),
FirstName varchar(25),
CGPA float,
Address varchar(25)
);
Nahida Nazir
Describe table
Nahida Nazir
INSERTING INTO A
TABLE
The INSERT INTO statement is used to insert new records or rows in a table.
It is possible to write the INSERT INTO statement in two forms.
Nahida Nazir
Inserting first row
Nahida Nazir
Inserting 2nd Row
Nahida Nazir
Inserting 3rd Row
Nahida Nazir
Select command
Nahida Nazir
Nahida Nazir
Union
Nahida Nazir
INSERTING INTO A
TABLE
The first form does not specify the column names where the data will be
inserted, only their values:
Syntax:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
Nahida Nazir
INSERTING INTO A
TABLE
Example:
INSERT INTO Student VALUES (1, ‘Kumar’, ‘Ravi’, 8.7, ‘Jal’ );
INSERT INTO Student VALUES (2, ‘Sharma’, ‘Partha’, 6.8, ‘Phagwara’);
INSERT INTO Student VALUES (3, ‘Singh’, ‘Gurpreet’, 9.5, ‘Ldh’ );
INSERT INTO Student VALUES (4, ‘Jain’, ‘Neharika’, 7.2, Jal );
Nahida Nazir
INSERTING INTO A
TABLE
The second form specifies both the column names and the values to be
inserted
Syntax:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Nahida Nazir
INSERTING INTO A
TABLE
Example:
INSERT INTO Student (StudentID, FirstName, CGPA) VALUES (1, ‘Ravi’, 8.7);
INSERT INTO Student (StudentID, FirstName, CGPA) VALUES (2, ‘Partha’, 6.8);
INSERT INTO Student (StudentID, FirstName, CGPA) VALUES (3, ‘Gurpreet’,
9.5);
INSERT INTO Student (StudentID, FirstName, CGPA) VALUES (4, ‘Neharika’,
7.2);
Nahida Nazir
SELECT STATEMENT
The SELECT statement is used to select data from a database.
For retrieving all the columns and rows:
Syntax
SELECT * FROM table_name;
Example
Ques: Show the records of all the students from ‘Student’ table.
Ans.
SELECT * FROM Student;
Nahida Nazir
 create table ki(id int, roll_no int, salary float, address
varchar(20));
 insert into ki values(21, 56,67.89, 'hj');
 select*from ki;
 alter table ki add email varchar(23);
ALTER TABLE ki
DROP COLUMN Email;
 update ki set email='nahida@12.gmail.com’
Nahida Nazir
create table ki(id int, roll_no int, salary float, address
varchar(20));
insert into ki values(21, 56,67.89, 'hj');
select*from ki;
alter table ki add try varchar(23);
ALTER TABLE Customers
DROP COLUMN Email;
update ki set try='89' where id=21;
Nahida Nazir
SELECT STATEMENT
For retrieving specific columns:
Syntax
SELECT column_name1, column_name2
FROM table_name;
Example
Ques: Show the columns StudentID, LastName and City from ‘Student’
table.
Ans.
SELECT StudentID, LastName, City
FROM Student;
Nahida Nazir
SELECT STATEMENT
For retrieving records using condition:
Syntax
SELECT *
FROM table_name
WHERE condition;
Example
Ques: Show record of student having StudentID=2 from ‘Student’ table.
Ans.
SELECT *
FROM Student
WHERE StudentID=2;
Nahida Nazir
UPDATING A TABLE
The SQL UPDATE Query is used to modify the existing records in a table.
You can use WHERE clause with UPDATE query to update selected rows
otherwise all the rows would be affected.
Syntax
UPDATE table_name
SET column1=value1, column2=value2,...
WHERE some_column=some_value;
Nahida Nazir
UPDATING A TABLE
Example
Ques: Update Lastname of student having StudentID=4 .
Ans.
UPDATE Student
SET LastName = ‘Verma’
WHERE StudentID = 4;
Ques: Update Address of all the students.
Ans.
UPDATE Student
SET Address = ‘Jal’;
Ques: Update CGPA of student having StudentID=3.
Ans.
UPDATE Student
SET CGPA = 9.7
WHERE StudentID = 3;
Nahida Nazir
Update
Nahida Nazir
Updated Single Value
Nahida Nazir
Updated multiple values
Nahida Nazir
ALTERING A TABLE
The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
Syntax
To add a column:
ALTER TABLE table_name
ADD column_name datatype;
To delete a column:
ALTER TABLE table_name
DROP COLUMN column_name;
Nahida Nazir
Alter table
Nahida Nazir
DELETING FROM TABLE
The SQL DELETE Query is used to delete the existing records from a
table.
You can use WHERE clause with DELETE query to delete selected rows,
otherwise all the records would be deleted.
Syntax
DELETE FROM table_name
WHERE some_column=some_value;
Nahida Nazir
DELETING FROM TABLE
Example
Ques: Delete the record of student having StudentID=2.
Ans.
DELETE FROM Student
WHERE StudentID=2;
Ques: Delete the records of all students living in ‘Jalandhar’.
Ans.
DELETE FROM Student
WHERE Address=‘Jal’;
Ques: Delete the records of all the students.
Ans.
DELETE FROM Student;
Nahida Nazir
ALTERING A TABLE
Example
Ques: Add a column FathersName into ‘Student’ table.
Ans.
ALTER TABLE Student
ADD FathersName varchar(30);
Ques: Delete column Address from ‘Student’ table.
Ans.
ALTER TABLE Student
DROP COLUMN Address;
Nahida Nazir
Altering a table
Nahida Nazir
Inserting data into created
columns
Nahida Nazir
RENAMING A COLUMN
Syntax
rename ki to ui;
ALTER TABLE table_name
RENAME COLUMN column1 TO column2;
Example
Ques: Change name of the column StudentID to ID.
Ans.
ALTER TABLE Student
RENAME COLUMN StudentID TO ID;
Nahida Nazir
DROP TABLE
The SQL DROP TABLE statement is used to remove a table definition and all
data, triggers, constraints, and permission specifications for that table.
NOTE: You have to be careful while using this command because once a table
is deleted then all the information available in the table would also be lost
forever.
Nahida Nazir
DROP TABLE
Syntax
DROP TABLE table_name;
Example
Ques: Drop the ‘Student’ table.
Ans.
DROP TABLE Student;
Nahida Nazir
Delete syntax
 delete from ui where id=21;
Nahida Nazir
TRUNCATE TABLE
The SQL TRUNCATE TABLE command is used to delete complete data from an
existing table.
You can also use DROP TABLE command to delete complete table but it would
remove complete table structure form the database and you would need to
re-create this table once again if you wish you store some data.
Nahida Nazir
TRUNCATE TABLE
Syntax
TRUNCATE TABLE table_name;
Example
Ques: Truncate the ‘Student’ table.
Ans.
TRUNCATE TABLE Student;
Nahida Nazir
Difference among
DELETE, DROP, and
TRUNCATE
DELETE:
The DELETE command is used to remove rows from a table.
A WHERE clause can be used to only remove some rows. If no WHERE
condition is specified, all rows will be removed.
After performing a DELETE operation you need to COMMIT or ROLLBACK the
transaction to make the change permanent or to undo it.
Note that this operation will cause all DELETE triggers on the table to fire.
Nahida Nazir
Difference among
DELETE, DROP, and
TRUNCATE
TRUNCATE:
TRUNCATE removes all rows from a table. The operation cannot be rolled
back and no triggers will be fired. As such, TRUNCATE is faster.
DROP:
The DROP command removes a table from the database. All the tables' rows,
indexes and privileges will also be removed. No DML triggers will be fired.
The operation cannot be rolled back.
Nahida Nazir

More Related Content

PDF
23. SQL and Database.pdf
UtkarshGAUTAM49
 
PPTX
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
PPTX
23. SQL and Database.pptx this ppt file is important for class 12
jayedhossain1519
 
PDF
23. SQL and Database.pdf
AmaanRizvi6
 
PPT
Sql basics and DDL statements
Mohd Tousif
 
PPTX
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
PPT
Db1 lecture4
Sherif Gad
 
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
23. SQL and Database.pdf
UtkarshGAUTAM49
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
23. SQL and Database.pptx this ppt file is important for class 12
jayedhossain1519
 
23. SQL and Database.pdf
AmaanRizvi6
 
Sql basics and DDL statements
Mohd Tousif
 
ADVANCE ITT BY PRASAD
PADYALAMAITHILINATHA
 
Db1 lecture4
Sherif Gad
 
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 

Similar to DDL and DML statements.pptx (20)

PPT
relational algebra and it's implementation
dbmscse61
 
PPTX
Relational Algebra in DBMS power ppoint pesenetation
AshokRachapalli1
 
PPTX
introduction to database system concepts 2
Rajasekhar364622
 
PPTX
SQL commands powerpoint presentation. Ppt
umadevikakarlapudi
 
PPTX
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
PPTX
BASICS OF STRUCTURED QUEERY LANGUAGE.PPT
Uma Kakarlapudi
 
PPTX
RDBMS
NilaNila16
 
PPTX
SQl data base management and design
franckelsania20
 
PPTX
Sql 
statements , functions & joins
baabtra.com - No. 1 supplier of quality freshers
 
PPT
04 Introduction to SQ(Structural Query Language)L.ppt
ShishirOyshi20143346
 
PPTX
Lab
neelam_rawat
 
PPTX
SQL
Shyam Khant
 
PPT
ch3.ppt
Ashwini Rao
 
PDF
BCS4L1-Database Management lab.pdf
KeerthanaP37
 
PPTX
Data Manipulation Language.pptx
EllenGracePorras
 
PDF
SQL Overview
Stewart Rogers
 
PPTX
Statements,joins and operators in sql by thanveer danish melayi(1)
Muhammed Thanveer M
 
PPTX
Introduction to SQL- Database management System
Shabanam Shikalgar
 
PPTX
Fundamentals of Database management system Lab Manual.pptx
Getnet Tigabie Askale -(GM)
 
PPTX
REC-UNIT-2-DATABASEMANAGEMENTSYSTEMS.pptx
Uma Kakarlapudi
 
relational algebra and it's implementation
dbmscse61
 
Relational Algebra in DBMS power ppoint pesenetation
AshokRachapalli1
 
introduction to database system concepts 2
Rajasekhar364622
 
SQL commands powerpoint presentation. Ppt
umadevikakarlapudi
 
DBMS and SQL(structured query language) .pptx
jainendraKUMAR55
 
BASICS OF STRUCTURED QUEERY LANGUAGE.PPT
Uma Kakarlapudi
 
RDBMS
NilaNila16
 
SQl data base management and design
franckelsania20
 
Sql 
statements , functions & joins
baabtra.com - No. 1 supplier of quality freshers
 
04 Introduction to SQ(Structural Query Language)L.ppt
ShishirOyshi20143346
 
ch3.ppt
Ashwini Rao
 
BCS4L1-Database Management lab.pdf
KeerthanaP37
 
Data Manipulation Language.pptx
EllenGracePorras
 
SQL Overview
Stewart Rogers
 
Statements,joins and operators in sql by thanveer danish melayi(1)
Muhammed Thanveer M
 
Introduction to SQL- Database management System
Shabanam Shikalgar
 
Fundamentals of Database management system Lab Manual.pptx
Getnet Tigabie Askale -(GM)
 
REC-UNIT-2-DATABASEMANAGEMENTSYSTEMS.pptx
Uma Kakarlapudi
 
Ad

More from Karthick Panneerselvam (6)

PPT
Cursors.ppt
Karthick Panneerselvam
 
PPT
Concurrent Transactions.ppt
Karthick Panneerselvam
 
PPTX
Risk Analysis.pptx
Karthick Panneerselvam
 
PDF
standards1.pdf
Karthick Panneerselvam
 
PDF
Security.pdf
Karthick Panneerselvam
 
DOCX
computer Network
Karthick Panneerselvam
 
Concurrent Transactions.ppt
Karthick Panneerselvam
 
Risk Analysis.pptx
Karthick Panneerselvam
 
standards1.pdf
Karthick Panneerselvam
 
computer Network
Karthick Panneerselvam
 
Ad

Recently uploaded (20)

PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
easa module 3 funtamental electronics.pptx
tryanothert7
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
easa module 3 funtamental electronics.pptx
tryanothert7
 
Information Retrieval and Extraction - Module 7
premSankar19
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 

DDL and DML statements.pptx

  • 1. Query  A Language which is used to store and retrieve data from database is known as query language. For example – SQL  There are two types of query language: 1.Procedural Query language 2.Non-procedural query language Nahida Nazir
  • 2. Procedural Query language  In procedural query language, user instructs the system to perform a series of operations to produce the desired results. Here users tells what data to be retrieved from database and how to retrieve it. Nahida Nazir
  • 3. Non-procedural query language:  n Non-procedural query language, user instructs the system to produce the desired result without telling the step by step process. Here users tells what data to be retrieved from database but doesn’t tell how to retrieve it. Nahida Nazir
  • 4. Relational Algebra:  Relational algebra is a conceptual procedural query language used on relational model  Relational Calculus:  Relational calculus is a conceptual non-procedural query language used on relational model.  SQL is a practical implementation of relational algebra and relational calculus. Nahida Nazir
  • 5. Relational Algebra  Relational algebra is a procedural query language that works on relational model. The purpose of a query language is to retrieve data from database or perform various operations such as insert, update, delete on the data. Nahida Nazir
  • 6. Types of operations in relational algebra  1. Basic Operations 2. Derived Operations Nahida Nazir
  • 7. Basic/Fundamental Operations:  1. Select (σ) 2. Project (∏) 3. Union (∪) 4. Set Difference (-) 5. Cartesian product (X) 6. Rename (ρ) Nahida Nazir
  • 8. Derived Operations:  1. Natural Join (⋈) 2. Left, Right, Full outer join (⟕, ⟖, ⟗) 3. Intersection (∩) 4. Division (÷) Nahida Nazir
  • 9. Select Operator (σ)  Select Operator is denoted by sigma (σ) and it is used to find the tuples (or rows) in a relation (or table) which satisfy the given condition.  Predicates are keywords that specify a relationship between two expressions.  Syntax: σ Condition/Predicate(Relation/Table name) Nahida Nazir
  • 11. Project Operator (∏)  Project operator is denoted by ∏ symbol and it is used to select desired columns (or attributes) from a table (or relation).  Syntax of Project Operator (∏)  ∏ column_name1, column_name2, ...., column_nameN(table_name) Nahida Nazir
  • 13. Union Operator (∪)  Union operator is denoted by ∪ symbol and it is used to select all the rows (tuples) from two tables (relations).  Lets say we have two relations R1 and R2 both have same columns and we want to select all the tuples(rows) from these relations then we can apply the union operator on these relations.  Note: The rows (tuples) that are present in both the tables will only appear once in the union set. In short you can say that there are no duplicates present after the union operation.  There are no duplicate names present in the output  Syntax of Union Operator (∪)  table_name1 ∪ table_name2 Nahida Nazir
  • 14. Intersection Operator (∩)  Intersection operator is denoted by ∩ symbol and it is used to select common rows (tuples) from two tables (relations).  Lets say we have two relations R1 and R2 both have same columns and we want to select all those tuples(rows) that are present in both the relations, then in that case we can apply intersection operation on these two relations R1 ∩ R2.  Note: Only those rows that are present in both the tables will appear in the result set.  Syntax of Intersection Operator (∩)  table_name1 ∩ table_name2 Nahida Nazir
  • 15. Set Difference (-)  Set Difference is denoted by – symbol. Lets say we have two relations R1 and R2 and we want to select all those tuples(rows) that are present in Relation R1 but not present in Relation R2, this can be done using Set difference R1 – R2.  Syntax of Set Difference (-)  table_name1 - table_name2 Nahida Nazir
  • 16. Cartesian product (X)  Cartesian Product is denoted by X symbol. Lets say we have two relations R1 and R2 then the cartesian product of these two relations (R1 X R2) would combine each tuple of first relation R1 with the each tuple of second relation R2. I know it sounds confusing but once we take an example of this, you will be able to understand this.  Syntax of Cartesian product (X)  R1 X R2 Nahida Nazir
  • 17. Rename (ρ)  Rename (ρ) operation can be used to rename a relation or an attribute of a relation. Rename (ρ) Syntax: ρ(new_relation_name, old_relation_name) Nahida Nazir
  • 18. What is SQL? SQL stands for Structured Query Language. SQL is used to communicate with a database. SQL lets you access and manipulate databases. Software to be used: Oracle Nahida Nazir
  • 20. What SQL can do? What Can SQL do? -SQL can execute queries against a database -SQL can retrieve data from a database -SQL can insert records in a database -SQL can update records in a database -SQL can delete records from a database -SQL can create new databases -SQL can create new tables in a database -SQL can create stored procedures in a database -SQL can create views in a database -SQL can set permissions on tables, procedures, and views Nahida Nazir
  • 21. DDL Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples: •CREATE - to create objects in the database •ALTER - alters the structure of the database •DROP - delete objects from the database •TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed •RENAME - rename an object Nahida Nazir
  • 22. DML Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples: •SELECT - retrieve data from the a database •INSERT - insert data into a table •UPDATE - updates existing data within a table •DELETE - deletes all records from a table, the space for the records remain Nahida Nazir
  • 23. CREATING A TABLE The CREATE TABLE statement is used to create a table in a database. Tables are organized into rows and columns; and each table must have a name. Nahida Nazir
  • 24. CREATING A TABLE Syntax: CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... ); Nahida Nazir
  • 25. CREATING A TABLE Example: CREATE TABLE Student ( StudentID int, LastName varchar(25), FirstName varchar(25), CGPA float, Address varchar(25) ); Nahida Nazir
  • 27. INSERTING INTO A TABLE The INSERT INTO statement is used to insert new records or rows in a table. It is possible to write the INSERT INTO statement in two forms. Nahida Nazir
  • 34. INSERTING INTO A TABLE The first form does not specify the column names where the data will be inserted, only their values: Syntax: INSERT INTO table_name VALUES (value1,value2,value3,...); Nahida Nazir
  • 35. INSERTING INTO A TABLE Example: INSERT INTO Student VALUES (1, ‘Kumar’, ‘Ravi’, 8.7, ‘Jal’ ); INSERT INTO Student VALUES (2, ‘Sharma’, ‘Partha’, 6.8, ‘Phagwara’); INSERT INTO Student VALUES (3, ‘Singh’, ‘Gurpreet’, 9.5, ‘Ldh’ ); INSERT INTO Student VALUES (4, ‘Jain’, ‘Neharika’, 7.2, Jal ); Nahida Nazir
  • 36. INSERTING INTO A TABLE The second form specifies both the column names and the values to be inserted Syntax: INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...); Nahida Nazir
  • 37. INSERTING INTO A TABLE Example: INSERT INTO Student (StudentID, FirstName, CGPA) VALUES (1, ‘Ravi’, 8.7); INSERT INTO Student (StudentID, FirstName, CGPA) VALUES (2, ‘Partha’, 6.8); INSERT INTO Student (StudentID, FirstName, CGPA) VALUES (3, ‘Gurpreet’, 9.5); INSERT INTO Student (StudentID, FirstName, CGPA) VALUES (4, ‘Neharika’, 7.2); Nahida Nazir
  • 38. SELECT STATEMENT The SELECT statement is used to select data from a database. For retrieving all the columns and rows: Syntax SELECT * FROM table_name; Example Ques: Show the records of all the students from ‘Student’ table. Ans. SELECT * FROM Student; Nahida Nazir
  • 39.  create table ki(id int, roll_no int, salary float, address varchar(20));  insert into ki values(21, 56,67.89, 'hj');  select*from ki;  alter table ki add email varchar(23); ALTER TABLE ki DROP COLUMN Email;  update ki set email='[email protected]’ Nahida Nazir
  • 40. create table ki(id int, roll_no int, salary float, address varchar(20)); insert into ki values(21, 56,67.89, 'hj'); select*from ki; alter table ki add try varchar(23); ALTER TABLE Customers DROP COLUMN Email; update ki set try='89' where id=21; Nahida Nazir
  • 41. SELECT STATEMENT For retrieving specific columns: Syntax SELECT column_name1, column_name2 FROM table_name; Example Ques: Show the columns StudentID, LastName and City from ‘Student’ table. Ans. SELECT StudentID, LastName, City FROM Student; Nahida Nazir
  • 42. SELECT STATEMENT For retrieving records using condition: Syntax SELECT * FROM table_name WHERE condition; Example Ques: Show record of student having StudentID=2 from ‘Student’ table. Ans. SELECT * FROM Student WHERE StudentID=2; Nahida Nazir
  • 43. UPDATING A TABLE The SQL UPDATE Query is used to modify the existing records in a table. You can use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected. Syntax UPDATE table_name SET column1=value1, column2=value2,... WHERE some_column=some_value; Nahida Nazir
  • 44. UPDATING A TABLE Example Ques: Update Lastname of student having StudentID=4 . Ans. UPDATE Student SET LastName = ‘Verma’ WHERE StudentID = 4; Ques: Update Address of all the students. Ans. UPDATE Student SET Address = ‘Jal’; Ques: Update CGPA of student having StudentID=3. Ans. UPDATE Student SET CGPA = 9.7 WHERE StudentID = 3; Nahida Nazir
  • 48. ALTERING A TABLE The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. Syntax To add a column: ALTER TABLE table_name ADD column_name datatype; To delete a column: ALTER TABLE table_name DROP COLUMN column_name; Nahida Nazir
  • 50. DELETING FROM TABLE The SQL DELETE Query is used to delete the existing records from a table. You can use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted. Syntax DELETE FROM table_name WHERE some_column=some_value; Nahida Nazir
  • 51. DELETING FROM TABLE Example Ques: Delete the record of student having StudentID=2. Ans. DELETE FROM Student WHERE StudentID=2; Ques: Delete the records of all students living in ‘Jalandhar’. Ans. DELETE FROM Student WHERE Address=‘Jal’; Ques: Delete the records of all the students. Ans. DELETE FROM Student; Nahida Nazir
  • 52. ALTERING A TABLE Example Ques: Add a column FathersName into ‘Student’ table. Ans. ALTER TABLE Student ADD FathersName varchar(30); Ques: Delete column Address from ‘Student’ table. Ans. ALTER TABLE Student DROP COLUMN Address; Nahida Nazir
  • 54. Inserting data into created columns Nahida Nazir
  • 55. RENAMING A COLUMN Syntax rename ki to ui; ALTER TABLE table_name RENAME COLUMN column1 TO column2; Example Ques: Change name of the column StudentID to ID. Ans. ALTER TABLE Student RENAME COLUMN StudentID TO ID; Nahida Nazir
  • 56. DROP TABLE The SQL DROP TABLE statement is used to remove a table definition and all data, triggers, constraints, and permission specifications for that table. NOTE: You have to be careful while using this command because once a table is deleted then all the information available in the table would also be lost forever. Nahida Nazir
  • 57. DROP TABLE Syntax DROP TABLE table_name; Example Ques: Drop the ‘Student’ table. Ans. DROP TABLE Student; Nahida Nazir
  • 58. Delete syntax  delete from ui where id=21; Nahida Nazir
  • 59. TRUNCATE TABLE The SQL TRUNCATE TABLE command is used to delete complete data from an existing table. You can also use DROP TABLE command to delete complete table but it would remove complete table structure form the database and you would need to re-create this table once again if you wish you store some data. Nahida Nazir
  • 60. TRUNCATE TABLE Syntax TRUNCATE TABLE table_name; Example Ques: Truncate the ‘Student’ table. Ans. TRUNCATE TABLE Student; Nahida Nazir
  • 61. Difference among DELETE, DROP, and TRUNCATE DELETE: The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire. Nahida Nazir
  • 62. Difference among DELETE, DROP, and TRUNCATE TRUNCATE: TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUNCATE is faster. DROP: The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back. Nahida Nazir