Today SQL Views CREATE VIEW Command
Total Page:16
File Type:pdf, Size:1020Kb
Today IT420: Database Management and SQL Views Organization SQL Views (Chapter 7) 1 Kroenke, Database Processing 2 SQL Views CREATE VIEW Command SQL view is a virtual table that is constructed from other CREATE VIEW command: tables or views CREATE VIEW view_name It has no data of its own, but obtains data from tables or AS other views select_statement It only has a definition Use the view: SELECT statements are used to define views In SELECT statements A view definition may not include an ORDER BY clause Sometimes in INSERT statements Views can be used as regular tables in SELECT Sometimes in UPDATE statements statements Sometimes in DELETE statements Kroenke, Database Processing 3 Kroenke, Database Processing 4 1 CREATE VIEW Command Uses for SQL Views CREATE VIEW Security: hide columns and rows command: CREATE VIEW CustomerNameView Display results of computations AS Hide complicated SQL syntax SELECT CustName AS CustomerName Provide a level of isolation between actual FROM CUSTOMER; data and the user’s view of data To use the view: three-tier architecture SELECT * Assign different processing permissions to FROM CustomerNameView ORDER BY CustomerName; different views on same table Kroenke, Database Processing 5 Kroenke, Database Processing 6 Security: hide columns and rows Display results of computations MIDS database, Midshipmen table Faculty (EmpID , LName, FName, Department, AreaCode, LocalPhone) View for faculty – all mids with IT major Create a view to display 2 columns: View for students – all mids, no grades Name = Fname LName Midshipmen (Alpha , Name, DateOfBirth, GPA, Phone = (AreaCode) LocalPhone Major) SELECT, INSERT, UPDATE, DELETE? Exercise: Write the SQL to create the views SELECT, INSERT, UPDATE, DELETE? Kroenke, Database Processing 7 Kroenke, Database Processing 8 2 Provide a level of isolation between Hide complicated SQL syntax actual data and application Mid(Alpha , LName, FName, Class, Age) CREATE VIEW CustomerV AS Course(CourseID , Description, Textbook) SELECT * Enroll(Alpha , CourseID , Semester , Grade) FROM Customers Applications use CustomerV Create a view to display the student alpha, Can change the underlying table without name, CourseID and description of courses they changing the application are/were enrolled ALTER VIEW CustomerV AS SELECT, INSERT, UPDATE, DELETE? SELECT * FROM New_Customers Kroenke, Database Processing 9 Kroenke, Database Processing 10 Updating Views Updateable Views CREATE VIEW CustomerV AS Views based on a single table SELECT * FROM Customers No computed columns SELECT, INSERT, DELETE, UPDATE? All non-null columns present in view Faculty (EmpID, LName, FName, Department, AreaCode, LocalPhone) CREATE VIEW FacultyPhone AS SELECT FName + ‘ ’ + LName AS Name, ‘(’ + AreaCode + ‘)’ + LocalPhone AS Phone Views based on a single table, primary key in FROM Students view, some non-null columns missing from view UPDATE FacultyPhone Updates for non-computed columns ok SET Phone = ‘(410)-293-6822’ Works? Deletes ok WHERE Name=‘Adina Crainiceanu’ Inserts not ok Kroenke, Database Processing 11 Kroenke, Database Processing 12 3 Summary – SQL Views CREATE VIEW view_name AS select_statement Virtual table It only has a definition Data is computed at run-time from base tables All views can be used in SELECT Some views can be used in INSERT, DELETE, UPDATE Kroenke, Database Processing 13 4.