Unit 2 Dml & Views

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

DML commands in SQL:

1.SELECT Command

2.INSERT Command

3.UPDATE Command

4.DELETE Command
• SELECT :
• SELECT column_Name_1, column_Name_2, ….., column_Name_N
FROM Name_of_table;
SELECT * FROM table_name;

SELECT * FROM Student;


Student_ID Student_Name Student_Marks
BCA1001 Abhay 85
BCA1002 Anuj 75
BCA1003 Bheem 60
BCA1004 Ram 79
BCA1005 Sumit 80
• SELECT Emp_Id, Emp_Salary FROM Employee;

Emp_Id Emp_Salary

201 25000

202 45000

203 30000

204 29000

205 40000
Student_ID Student_Name Student_Marks

BCA1001 Abhay 80

BCA1002 Ankit 75

BCA1003 Bheem 80

BCA1004 Ram 79

BCA1005 Sumit 80
WHERE clause with the SELECT DML command.

SELECT * FROM Student WHERE Stu_Marks = 80;

Student_ID Student_Name Student_Marks

BCA1001 Abhay 80

BCA1003 Bheem 80

BCA1005 Sumit 80
UPDATE DML Command
Product_Id Product_Name Product_Price Product_Quantity

P101 Chips 20 20

P102 Chocolates 60 40

P103 Maggi 75 5

P201 Biscuits 80 20

P203 Namkeen 40 50
UPDATE Table_name SET [column_name1= value_1,
….., column_nameN = value_N] WHERE CONDITION
;

• UPDATE Product SET Product_Price = 80 WHERE Product


_Id = 'P102' ;
DELETE DML Command

• DELETE FROM Table_Name WHERE condition;

• TO DLETE SINGLE RECORD

• DELETE FROM Product WHERE Product_Id = 'P202' ;

• TO DELETE MULTIPLE RECORDS

• DELETE FROM Student WHERE Stu_Marks > 70 ;


VIEWS: view is a virtual table
• A view contains rows and columns, just like a real table.

• View can be created with the required no of attributes.

• Changes made in table reflects on view ,and changes made in view reflects on
table

• SQL CREATE VIEW Statement:

• CREATE VIEW view_name AS


SELECT column1, column2, ...
FROM table_name
WHERE condition;
STAUDENT DETAILS
STUDENT MARKS TABLE
CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;
SELECT * FROM DetailsView
•we will create a view named StudentNames from the table
StudentDetails.
Query:

• CREATE VIEW StudentNames AS


SELECT S_ID, NAME
FROM StudentDetails
ORDER BY NAME;
SELECT * FROM StudentNames;
Creating View from multiple tables:

CREATE VIEW MarksView AS


SELECT StudentDetails.NAME, StudentDetails.ADDRESS,
StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
SELECT * FROM MarksView;
DELETING VIEWS

DROP VIEW view_name;

view_name: Name of the View which we want to


delete.

DROP VIEW MarksView;


UPDATING VIEWS
• There are certain conditions needed to be satisfied to update a view. If
any one of these conditions is not met, then we will not be allowed to
update the view.
1.The SELECT statement which is used to create the view should not
include GROUP BY clause or ORDER BY clause.
2.The SELECT statement should not have the DISTINCT keyword.
3.The View should have all NOT NULL values.
4.The view should not be created using nested queries or complex queries.
5.The view should be created from a single table. If the view is created
using multiple tables then we will not be allowed to update the view.
We can use the CREATE OR REPLACE
VIEW statement to add or remove fields from a view.

REATE OR REPLACE VIEW view_name AS


C

SELECT column1,column2,..
FROM table_name
WHERE condition;
If we want to update the view MarksView and add the field
AGE to this View from StudentMarks Table, we can do this
as:

CREATE OR REPLACE VIEW MarksView AS


SELECT StudentDetails.NAME,StudentDetails.ADDRESS,
StudentMarks.MARKS, StudentMarks.AGE
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
SELECT * FROM MarksView;
Inserting a row in a view:
INSERT INTO view_name(column1, column2 , column3,..)
VALUES(value1, value2, value3..);

INSERT INTO DetailsView(NAME, ADDRESS)


VALUES("Suresh","Gurgaon");
Deleting a row from a View:

DELETE FROM view_name WHERE condition;

DELETE FROM DetailsView WHERE NAME="Suresh";

You might also like