Featured Post

15 Python Tips : How to Write Code Effectively

Image
 Here are some Python tips to keep in mind that will help you write clean, efficient, and bug-free code.     Python Tips for Effective Coding 1. Code Readability and PEP 8  Always aim for clean and readable code by following PEP 8 guidelines.  Use meaningful variable names, avoid excessively long lines (stick to 79 characters), and organize imports properly. 2. Use List Comprehensions List comprehensions are concise and often faster than regular for-loops. Example: squares = [x**2 for x in range(10)] instead of creating an empty list and appending each square value. 3. Take Advantage of Python’s Built-in Libraries  Libraries like itertools, collections, math, and datetime provide powerful functions and data structures that can simplify your code.   For example, collections.Counter can quickly count elements in a list, and itertools.chain can flatten nested lists. 4. Use enumerate Instead of Range     When you need both the index ...

3 SQL Query Examples to Create Views Quickly

There are three kinds of Views in SQL. The three views are Read-only, Force, and Updatable. Views real usage is to hide data. And you need to ensure base tables are present before you create a View.


You can call views as logical tables. The advantage of Views is you can show only some of the fields of base tables.


What is a View in SQL
  • A view can be constructed with another view so it is called a nested view.
  • You can create or replace an existing view
  • A view can be created without having base tables. This is possible with the FORCE option.
#1: Read-Only Views

The standard syntax for the view is as follows:

CREATE OR replace VIEW invoice_summary AS
SELECT vendor_name count(*) AS invoice_count,
SUM(invoice_total) AS invoice_total_sum
FROM vendor
JOIN invoices
ON vendors.vendor_id*invoices.vendor_id
GROUP BY vendor_name;

Notes: You cannot update Read-only Views


#2: Force Views

CREATE FORCE VIEW products_list
AS
SELECT product_description,
product_price
FROM products;

Notes: Without base Table you can create a FORCE View.


#3: Updatable Views

A view can be updatable if a view follows certain rules.

A view when it is created for update purpose, you can give INSERT, UPDATE and DELETE operations.

A read-only view should contain WITH  READ ONLY clause

While updating a view, it is possible to update only one base table at a time. When you created a view from more than one table, then it is not possible to update two tables at a time.


How to Manipulate Views 

ALTER View

  • CREATE OR REPLACE statement you can use to ALTER the View.

Drop View

  • Drop view vendor_sw

Summary

A view with CHECK OPTION restricts to update. When condition satisfied it updates.

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)