The document discusses different types of query languages used to retrieve data from a database. It states that there are two main types: procedural query languages and non-procedural query languages. Procedural query languages require the user to specify the step-by-step process to retrieve data, while non-procedural languages only require the user to specify what data to retrieve without how. Relational algebra is provided as an example of a procedural query language, while relational calculus is given as a non-procedural language. SQL is described as a practical implementation of both relational algebra and relational calculus used to interact with relational databases.
1 of 62
Download to read offline
More Related Content
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 doesnt
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
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
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='nahida@12.gmail.com
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
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
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
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