Views in SQL
Views in SQL
o Views in SQL are considered as a virtual table. A view also contains rows and
columns.
o To create the view, we can select the fields from one or more tables present
in the database.
o A view can either have specific rows based on certain condition or all the rows
of a table.
Advantages of View:
1. Complexity: Views help to reduce the complexity. Different views can be created on the
same base table for different users.
2. Security: It increases the security by excluding the sensitive information from the view.
3. Query Simplicity: It helps to simplify commands from the user. A view can draw data
from several different tables and present it as a single table.
4. Consistency: A view can present a consistent, unchanged image of the structure of the
database. Views can be used to rename the columns without affecting the base table.
5. Data Integrity: If data is accessed and entered through a view, the DBMS can
automatically check the data to ensure that it meets the specified integrity constraints.
6. Storage Capacity: Views take very little space to store the data.
Sample table:
Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
A view can be created using the CREATE VIEW statement. We can create a view from
a single table or multiple tables.
Syntax:
Query:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
Query:
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
A view can be deleted using the Drop View statement.
Syntax
Example:
If we want to delete the View MarksView, we can do this as:
Sequences in MySQL
In MySQL, we use the AUTO_INCREMENT attribute to generate unique
integer values (sequences) for a column. By default, the sequence starts with
an initial value of 1 and increments by 1 for each new row.
Syntax
Following is the syntax of AUTO_INCREMENT attribute in MySQL −
Example
In the following example, we are creating a table named CUSTOMERS. In
addition to that, we are defining AUTO_INCREMENT on ID column of the table.
Here, we are adding some records into the above created table –
INSERT INTO CUSTOMERS VALUES
(NULL, 'Ramesh', 32, 'Ahmedabad', 2000.00),
(NULL, 'Khilan', 25, 'Delhi', 1500.00),
(NULL, 'Kaushik', 23, 'Kota', 2000.00),
(NULL, 'Chaitali', 25, 'Mumbai', 6500.00),
(NULL, 'Hardik', 27, 'Bhopal', 8500.00),
(NULL, 'Komal', 22, 'Hyderabad', 4500.00),
(NULL, 'Muffy', 24, 'Indore', 10000.00);
As we can see in the above table, the values in the ID column are auto
incremented.
Starting a Sequence at a Particular Value in MySQL
By default, MySQL sequences start from 1. To start a sequence with a
different value, we use the AUTO_INCREMENT in combination with
the ALTER statement.
Syntax
Following is the syntax to start the sequence with different value −
Here, we are making the sequence start with 100 using the ALTER Statement
as shown below −
Now, we are adding records into the BUYERS table using the INSERT INTO
statement −
As observed in the table above, the values in the "ID" column begin with 100
instead of 1.
Here,
We are inserting some records in the above-created table using INSERT INTO
statement as shown in the query below −
(7 rows affected)
Verification
Let us verify whether is sequence is updated in the ID column of the table or
not using the following query −
Synonyms
Understanding Synonyms:
This query will retrieve data from the Employees table, but it
refers to it using the synonym Emp.
Note: The syntax for creating indexes varies among different databases.
Therefore: Check the syntax for creating indexes in your database.
CREATE INDEX Example
The SQL statement below creates an index named "idx_lastname" on the
"LastName" column in the "Persons" table:
If you want to create an index on a combination of columns, you can list the
column names within the parentheses, separated by commas:
MS Access:
MySQL:
In SQL, a nested query involves a query that is placed within another query.
Output of the inner query is used by the outer query. A nested query has two
SELECT statements: one for the inner query and another for the outer query.
Syntax of Nested Queries
The basic syntax of a nested query involves placing one query inside of
another query. Inner query or subquery is executed first and returns a set of
values that are then used by the outer query. The syntax for a nested query
is as follows:
Correlated subqueries are executed once for each row of the outer query.
They use values from the outer query to return results.
This operator checks if a column value in the outer query's result is present in
the inner query's result. The final result will have rows that satisfy the IN
condition.
NOT IN Operator
This operator checks if a column value in the outer query's result is not
present in the inner query's result. The final result will have rows that satisfy
the NOT IN condition.
ALL Operator
This operator compares a value of the outer query's result with all the values
of the inner query's result and returns the row if it matches all the values.
ANY Operator
This operator compares a value of the outer query's result with all the inner
query's result values and returns the row if there is a match with any value.
Execution Order in Co-related Nested Queries
In correlated nested queries, the inner query uses values from the outer
query, and the execution order is different from that of independent nested
queries.
EXISTS Operator
ANY Operator
This operator compares a value of the outer query's result with one or more
values returned by the inner query. If the comparison is true for any one of
the values returned by the inner query, the row is included in the final result.
ALL Operator
This operator compares a value of the outer query's result with all the values
returned by the inner query. Only if the comparison is true for all the values
returned by the inner query, the row is included in the final result.
These operators are used to create co-related nested queries that depend on
values from the outer query for execution.
Examples
emp_i
emp_name dept_id
d
1 John 1
2 Mary 2
3 Bob 1
4 Alice 3
5 Tom 1
dept_i
dept_name
d
1 Sales
2 Marketing
3 Finance
Table: sales table
1 1 1000
2 2 2000
3 3 3000
4 1 4000
5 5 5000
6 3 6000
7 2 7000
Required query
SELECT emp_name
FROM employees
WHERE dept_id IN (SELECT dept_id
FROM departments
WHERE dept_name = 'Sales');
Output
emp_name
John
Bob
Tom
Example 2: Find the names of all employees who have made a sale
Required query
SELECT emp_name
FROM employees
WHERE EXISTS (SELECT emp_id
FROM sales
WHERE employees.emp_id = sales.emp_id);
Output
emp_name
John
Mary
Bob
Alice
Tom
This query selects all employees from the "employees" table where there
exists a sale record in the "sales" table for that employee.
Example 3: Find the names of all employees who have made sales greater
than $1000.
Required query
SELECT emp_name
FROM employees
WHERE emp_id = ALL (SELECT emp_id
FROM sales
WHERE sale_amt > 1000);
Output
emp_name
John
Mary
Bob
Alice
Tom
This query selects all employees from the "employees" table. With the
condition that where their emp_id equals all the emp_ids in the "sales" table
where the sale amount is greater than $1000. Since all employees have
made a sale greater than $1000, all employee names are returned.
SQL Commands
o SQL commands are instructions. It is used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
DCL commands are used to grant and take back authority from any database user.
DCL commands include GRANT and REVOKE , which are used to control
access to the database.
i. GRANT
In SQL, the GRANT statement gives users access privileges to the database.
For example,
ii. REVOKE
In SQL, the REVOKE statement withdraws access privileges given by
the GRANT statement.
Let's look at an example.
Here, the SQL query revokes SELECT permission on the Customers table
from user1 .
Transaction Control Language (TCL)
In SQL, TCL commands manage changes affecting the database.
i. COMMIT
In SQL, the COMMIT command is used for saving the changes made in the
database. For example,
UPDATE Customers
SET country = 'UK'
WHERE customer_id = 4;
COMMIT;
Here, the query deletes all records from the Orders table but then rolls back
the transaction.
SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back
the entire transaction.
Syntax:
1. SAVEPOINT SAVEPOINT_NAME;
In the above syntax, SAVEPOINT_NAME is the name given to savepoint.
1. Rollback TO <save_point_name>
Example:
1. DELETE FROM CUSTOMERS WHERE AGE = 15;
2. SAVEPOINT A;
3. DELETE FROM CUSTOMERS WHERE AGE = 35;
4. ROLLBCAK TO A;