C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Views in SQL
Sample table:Student_Detail
Student_Marks
1. Creating viewA view can be created using the CREATE VIEW statement. We can create a view from a single table or multiple tables. Syntax: CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE condition; 2. Creating View from a single tableIn this example, we create a View named DetailsView from the table Student_Detail. Query: CREATE VIEW DetailsView AS SELECT NAME, ADDRESS FROM Student_Details WHERE STU_ID < 4; Just like table query, we can query the view to view the data. SELECT * FROM DetailsView; Output:
3. Creating View from multiple tablesView from multiple tables can be created by simply include multiple tables in the SELECT statement. In the given example, a view is created named MarksView from two tables Student_Detail and Student_Marks. Query: CREATE VIEW MarksView AS SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student_Marks.MARKS FROM Student_Detail, Student_Mark WHERE Student_Detail.NAME = Student_Marks.NAME; To display data of View MarksView: SELECT * FROM MarksView;
4. Deleting ViewA view can be deleted using the Drop View statement. Syntax DROP VIEW view_name; Example: If we want to delete the View MarksView, we can do this as: DROP VIEW MarksView;
Next TopicDBMS SQL Index
|