180 likes | 197 Views
This recap covers advanced SQL commands including INSERT, UPDATE, and DELETE statements in a database management system. Learn about various types of INSERT statements for single or multiple rows, different types of UPDATE statements, and the two types of DELETE statements. Explore examples and actions for referenced rows in SQL.
E N D
Recap of SQL Lab no 8 Advance Database Management System
Outline • Revision of SQL Commands • Insert • Update • Delete
INSERT Statement • There are two types of INSERT statements • One row at a time can be inserted in tables. Specify values for each column with the VALUES Clause • Multiple rows can be inserted in a single INSERT Statement.
INSERT INTO Customer ( CustID, Name, Address, Phone ) VALUES ( 101, ‘Ali Khan’, ’21 Sabzazar Karachi’, ’092-021-8311865’ )
Example – One row at a time INSERT INTO Customer ( CustID, Name, Address, Phone ) VALUES ( 101, ‘Ali Khan’, ’21 Sabzazar Karachi’, ’092-021-8311865’ )
Example – One row at a time INSERT INSERT INTO Customer VALUES ( 101, ‘Ali Khan’, ’21 Sabzazar Karachi’, ’092-021-8311865’ )
Example – Multiple rows INSERT INSERT INTO DBMSStudent SELECT * FROM Student WHERE StdMajor = ‘DBMS’
UPDATE Statement • There can be 2 types of UPDATE statements • Single column update • Multiple column update
Single Column UPDATE UPDATE Employee SET EmpSalary=EmpSalary*5 WHERE EmpDept=‘Finance’ Note: All Employees of Finance Dept got five times raise in salary
Multiple Columns UPDATE UPDATE Employee SET EmpLastName=‘Homer’, EmpPhone=’92-51-223123’, EmpDept=‘Accounts’ WHERE EmpDept=‘Finance’
Multiple Columns UPDATE UPDATE Student SET StdMajor=‘Computer Engineering’, StdSession= ‘2k3-CP’ Notice there is no WHERE condition
DELETE Statement • There are 2 types of DELETE statements • Selected rows DELETE • All rows DELETE
Example - Selected Rows DELETE DELETE FROM Student WHERE StdMajor=‘IS’ AND StdSession=‘Fall04’
Example - Selected Rows DELETE • DELETE statement Using Join Operator DELETE Offering.* FROM Offering INNER JOIN Faculty ON Offering.FacID=Faculty.FacID WHERE FacFisrtName=‘John’ AND FacLastName=‘Barron’ Note: Used when rows of a table reference other tables
Example – All rows DELETE DELETE FROM DBMSStudent
DELETE and UPDATE actions for Referenced Rows • Deleting a referenced row : What happens to related rows (in foreign key table) when referenced rows are deleted • Updating the PK of a referenced row: What happens to related rows when the primary key of the referenced row is updated
Things to done … • Some ACTIONS must be applied on referenced rows of the tables in CREATE TABLE statements • Restrict (NO ACTION) • Cascade (CASCADE) • Nullify (SET NULL) • Default (SET DEFAULT)
Example CREATE TABLE Enrollment ( EnrID INTEGER NOT NULL, Stdid CHAR(11) NOT NULL, Enrgrade DECIMAL(3,2) CONSTRAINT PKEnrID PRIMARY KEY(EnrID), CONSTRAINT FKStdid FOREIGN KEY(STDid) REFERENCES Student ON DELETE NO ACTION ON UPDATE CASCADE )