Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views19 pages

IV SQL

Uploaded by

rramya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views19 pages

IV SQL

Uploaded by

rramya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

IV-SQL

Introduction to SQL:

 SQL was invented and developed by IBM in early 1970’s. SQL stands for
structured query language.
 IBM was able to demonstrate SQL which could be used to control relational
database. The SQL implemented by ORACLE CORPORATION is 100%
compliant with the ANSI / ISO standard SQL data language.
 Oracle database language is SQL, which is used for storing and retrieving
information in oracle.
 A table is a primary database object of SQL which is used to store data.
 A table hold data in the form of rows and columns to communicate with the
database.

SOL supports the following commands.

1. Data definition language (DDL).


2. Data Manipulation Language (DML).
3. Transaction control Language (TCL).

The Following are the benefits of SQL:

 Non-procedural language, because more than one record can be accessed


rather then one record at a time.
 It is the common language for all relation database (i, e) portable and it requires
only small modification to make use of in other database.
 Very simple commands for querying, inserting, deleting and modifying data and
objects.

Data Definition Language:

DDL is used to create an object (i,e table), alter the structure of an


object and also to drop the object created.

Table Definition:
A Table is a unit of storage which holds data in the form of rows and
columns. The Data definition language used for table definition can be classified into the
following for categories.

 Create
 Alter
 Drop
 Truncate

In a table we should specify a unique column name.


-We should specify proper data type along with its width.
-We can include ‘NOT NULL’ condition when needed, by default ‘NULL’,

1
Create Table:
Create command is used to create the new table.
Syntax;
Create table <table name> (column1 data type, column2 data type…….)
Example:
Create table customer(custid number(5), Name varchar(20), address varchar(20));

Example:
Following is an example, which creates a CUSTOMERS table with ID as primary key and NOT NULL are
the constraints showing that these fields can not be NULL while creating records in this table:
SQL> CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
You can verify if your table has been created successfully by looking at the message displayed by
the SQL server, otherwise you can use DESC command as follows:
SQL> DESC CUSTOMERS;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | | |
| NAME | varchar(20) | NO | | | |
| AGE | int(11) | NO | | | |
| ADDRESS | char(25) | YES | | NULL | |
| SALARY | decimal(18,2) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
The NOT NULL constraint enforces a field to always contain a value. This means that you
cannot insert a new record, or update a record without adding a value to this field.

The table name in the above example should adhere strictly to the following norms.

1. While naming a table the first letter should be an alphabet.

2. Oracle reserved words cannot be used to name a table.


3. Maximum length of the table name is 30 characters.
4. Two different table should not have the same name.
5. underscore, numerals and letters are allowed but not blank space and single
quotes.

Alter table:
The Alter table command used to create the need of the following situations.

1. When a user wants to add a new column.


2. When a user wants to modify the existing column definition, say from ‘null’
to ‘not null’ or to charge the width of the data type itself.
2
3. To include or drop integrity constraints.
4. We can decrease the length of an existing column data type provide it is
empty. No such restrictions, while increasing the length of existing columns.

Example:

SQL> create table stu2(regno number(5), name varchar(20));

Table created.

SQL> desc stu2


Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO NUMBER(5)
NAME VARCHAR2(20)

Syntax : alter table tablename add(field name, datatype);


SQL> alter table stu2 add(gender varchar(2));

Table altered.

SQL> desc stu2


Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO NUMBER(5)
NAME VARCHAR2(20)
GENDER VARCHAR2(2)

By using add gender field (column) is added

Syntax: alter table table name modify( field name, datatype);


SQL> alter table stu2 modify(gender varchar(1));

Table altered.

SQL> desc stu2


Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO NUMBER(5)
NAME VARCHAR2(20)
GENDER VARCHAR2(1)
3
The gender field width is modified from 2 to 1

Drop table:
Drop command is used to drop the table from the database.

Syntax:
Drop table <table name>;
Example:
Drop table customer;

Truncate table:
Truncate table is used to delete all the rows associated with the table.

Syntax:
Truncate table <table name>;
Example:
Truncate table customer;

Data Manipulation Commands:


Data manipulation commands are the most frequently used SQL commands.
They are the follows.

1. Insert
2. Select
3. Update
4. Delete

Insert command:
The Insert command is used to add me or more rows to a table. While using this
command the values are separated by common and the data type char and date are
enclosed in apostrophes.
The values must be entered in the some order as they are defined in the table.

Syntax:
Insert into <table name> values( a list of values);

Example:
Insert into order values(12, ’12-jan-74’ , 3, null);

The following example illustrates the concept of inserting more then the record using a
single insert command.

Example:
Insert into order into values(&no, ’&ord_date’ , ’&comm’, total);

Inserting date values:


To_date function to insert date values other then the standard from at for date.

4
Insert into order_into values(‘14/12/88 9:30’ ‘dd/mm/yy 44:mi’);

Example:

SQL> create table list(


2 proid number(5), proname varchar(15), quantity number(5), primary key(p

Table created.

SQL> desc list;


Name Null? Type
----------------------------------------- -------- ----------------------------
PROID NOT NULL NUMBER(5)
PRONAME VARCHAR2(15)
QUANTITY NUMBER(5)
PRICE NUMBER(10,2)

SQL> insert into list values(101,'soap', 300);

1 row created.

SQL> insert into list values(102,'shampoo',500);

1 row created.

SQL> insert into list values(103,'toothpaste',200);

1 row created.

Select command:
Select command is used to retrieve the data from the table.
Syntax:
Select column_name…… from table name…;
Example:
The following example shows how to retrieve the all the information from a table

SQL> select * from list;

PROID PRONAME QUANTITY


---------- --------------- ---------- ---------------
101 soap 300
102 shampoo 500
103 toothpaste 200

5
The following example shows how to select specific fields from a table

SQL> select proname from list;

PRONAME
---------------
soap
shampoo
toothpaste

SQL> select proid from list


2 ;

PROID
----------
101
102
103
;
Selecting distinct rows:
Distinct clause is used to prevent the selection of duplicate rows.
Example:
Select distinct repid from customer;

Where clause:
Where clause is used to select specific row from a table. It can appear
only the from clause,
Example:

SQL> select price, quantity from list where proname ='soap';

PRICE QUANTITY
---------- ----------
30 300
50 200

Order by clause:
It is used to arrange rows in descending or ascending order.
Example:

SQL> select proid, proname from list order by proid asc;

PROID PRONAME
6
---------- ---------------
101 soap
102 shampoo
103 toothpaste
104 soap
106 jam

We want to arrange in descending order:

SQL> select proid, proname from list order by proid desc;

PROID PRONAME
---------- ---------------
106 jam
104 soap
103 toothpaste
102 shampoo

101 soap

Select command to create a table:


The following example will create a new table cust from the existing table
customer along with its record.

Example:
Create table cust as select * from customer;

Update Command:
Update command is used to alter the column values in a table. The update
command consist of a ‘set’ clause and an optional ‘where’ clause.

Syntax:
Update <table name> set field=value… where condition;

SQL> alter table list add(price number(10,2));

Table altered.

SQL> update list set price=30 where proid=101;

1 row updated.

SQL> update list set price=40 where proid=102;

1 row updated.

7
SQL> update list set price=20 where proid=103;

1 row updated.

SQL> update list set price=50 where proid=104;

1 row updated.

SQL> select * from list;

PROID PRONAME QUANTITY PRICE


---------- --------------- ---------- ------------------- ----------
101 soap 300 30
102 shampoo 500 40
103 toothpaste 200 20
104 soap 200 50

Update list
Set quantity= quantity+100
Where quantity < 500

Update list
Set quantity= (select max(quantity)
From list
Where quanity<500

Delete command:
Delete command is used to delete the specific record from the table.
Syntax:
Delete from <table name> where condition;
Example:
Delete from customer where custid=105;

Note:
It we want to delete several rows from a table, select the rows with appropriate
conditions in a ‘where’ clause.

The SQL AND and OR operators are used to combine multiple conditions to narrow data in an SQL statement. These two
operators are called conjunctive operators.

These operators provide a means to make multiple comparisons with different operators in the same SQL statement.

8
The AND Operator:
The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.

Syntax:
The basic syntax of AND operator with WHERE clause is as follows:

SELECT column1, column2, columnN


FROM table_name
WHERE [condition1] AND [condition2]...AND [conditionN];

You can combine N number of conditions using AND operator. For an action to be taken by the SQL statement, whether it
be a transaction or query, all conditions separated by the AND must be TRUE.

Example:
Consider the CUSTOMERS table having the following records:

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Following is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS table where salary is
greater than 2000 AND age is less tan 25 years:

SQL> SELECT ID, NAME, SALARY


FROM CUSTOMERS
WHERE SALARY > 2000 AND age < 25;

This would produce the following result:

+----+-------+----------+
| ID | NAME | SALARY |
+----+-------+----------+
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+-------+----------+

The OR Operator:
The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.

Syntax:
The basic syntax of OR operator with WHERE clause is as follows:

SELECT column1, column2, columnN


FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN]

You can combine N number of conditions using OR operator. For an action to be taken by the SQL statement, whether it
be a transaction or query, only any ONE of the conditions separated by the OR must be TRUE.

9
Example:
Consider the CUSTOMERS table having the following records:

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Following is an example, which would fetch ID, Name and Salary fields from the CUSTOMERS table where salary is
greater than 2000 OR age is less tan 25 years:

SQL> SELECT ID, NAME, SALARY


FROM CUSTOMERS
WHERE SALARY > 2000 OR age < 25;

This would produce the following result:

+----+----------+----------+
| ID | NAME | SALARY |
+----+----------+----------+
| 3 | kaushik | 2000.00 |
| 4 | Chaitali | 6500.00 |
| 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 |
| 7 | Muffy | 10000.00 |
+----+----------+----------+

Transaction control language:


The following commands referred to as a transaction commands.

1. Commit
2. Roll back
3. Save point

Commit:

Example:
Consider the CUSTOMERS table having the following records:

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |

10
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Following is the example which would delete records from the table having age = 25 and then COMMIT the
changes in the database.

SQL> DELETE FROM CUSTOMERS


WHERE AGE = 25;
SQL> COMMIT;

As a result, two rows from the table would be deleted and SELECT statement would produce the following
result:

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

 This commands is used to end a transaction.


 Only with the help of the commit command transaction charges can be made
permanent to the database.
 This command also erases all save point.

Syntax:
Commit work; (or) Commit;

Roll back:
A roll back command is used to undo the word done in the current transaction.
Example:
Consider the CUSTOMERS table having the following records:

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

Following is the example, which would delete records from the table having age = 25 and then ROLLBACK
the changes in the database.

SQL> DELETE FROM CUSTOMERS


WHERE AGE = 25;
SQL> ROLLBACK;

11
As a result, delete operation would not impact the table and SELECT statement would produce the following
result:

+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

- We can either roll back the entire transaction so that all charges made by SQL
statement are undone.
- Roll back a transaction to a save point so that the SQL statement after the save
point are rolled back.

Syntax: Rollback
SQL> DELETE FROM CUSTOMERS
WHERE AGE = 25;
SQL> ROLLBACK;

Save point:

The syntax for SAVEPOINT command is as follows:

SAVEPOINT SAVEPOINT_NAME;

This command serves only in the creation of a SAVEPOINT among transactional statements. The
ROLLBACK command is used to undo a group of transactions.

The syntax for rolling back to a SAVEPOINT is as follows:

ROLLBACK TO SAVEPOINT_NAME;

Following is an example where you plan to delete the three different records from the CUSTOMERS table.
You want to create a SAVEPOINT before each delete, so that you can ROLLBACK to any SAVEPOINT at
any time to return the appropriate data to its original state:

Integrity Constraints:
It is used to prevent invalid data entry into the table. It is nothing but
enforcing rule for the columns in a table.

1. Domain
12
2. Entity
3. Referential

1.Domain Integrity Constraints:

Maintains the value according to the specification like ‘not null’ and ‘check’.

 Not null.
 Check.

Not null constraints:


 We Know that by default all columns in a table allow null values.
 Not null is used to not allow null values enter into the table.
 Not null constrain defined using create table or alter table command.

Example:
Create table cust(custied number(6) not null,…);

Check constraints:
 These are rule governed by logical expressions or Boolean expression.
 Check conditions cannot contain sub queries.

Example:
Create table parts( price number(4) check (price >= 0.0);

2. Entity integrity constraints:


 It is used to maintains uniqueness in a record.
 To identity each row in a table uniquely we need to use the following
constraints.

 Unique.
 Primary Key.

Unique constraints:
13
It is used to prevent the duplication of values within the rows of a
specified column or a set of column in a table. It unique key constraints is defined in
more than one column (i,e) combination of columns, it is said to be composite unique
key. Maximum combination of columns that a composite unique key can contain is 16.

Example:

Create table price(prodid number(6), start_date date unique, end_date date);

Primary key constraints:


 This constraint avoids duplication of rows and does not allow null values.
 It is used to identity a row.
 Table can have only one primary key.
 If a primary key constraint is assigned to more than the column (i.e.)
combination of columns it is said to be a composite primary key,Which can
contain maximum of 16.
 Primary key constraint can’t be defined in an alter table command when the
table constraints rows having null values.

Example:
Create table customer (custid number(10) primary key,…);

3. Referencial integrity constraint:


 It is used to enforces relationship between tables.
 To establish a ‘parent-child’ relationship between two tables having a
common column.

Foreign Key:
A column or combination of columns included in the definition of referential
integrity which would refer to a referenced key.

Referenced Key:
It is a unique or primary key which is defined on a column belong to the parent
table.

Child table:
This table depends upon the values present in the referenced key of the parent
table, which is referred by a foreign key.

Parent table:
This table determines whether insertion or updation of data can be done in child
table. This table would referred by child’s T\table foreign key.

Example:
Parent table:
Create table bank(acc no number(10) primary key……);
Child table:
Create table Account(acc_no number(10) references bank(acc_no) on delete cascade…..);

14
On delete cascade clause:
If all the rows under the referenced key column in a parent table are deleted, then
all rows in the child table with depend foreign key column will also be deleted
automatically.

Sub Queries

SQL Subquery
Subquery or Inner query or Nested query is a query in a query. A subquery is usually added in the WHERE
Clause of the sql statement. Most of the time, a subquery is used when you know how to search for a value
using a SELECT statement, but do not know the exact value in the database. Subqueries are an alternate
way of returning data from multiple tables. Subqueries can be used with the following sql statements along
with the comparision operators like =, <, >, >=, <= etc.

 SELECT
 INSERT
 UPDATE
 DELETE

Subquery Example:
1) Usually, a subquery should return only one record, but sometimes it can also return multiple records
when used with operators like IN, NOT IN in the where clause. The query would be like,

SELECT first_name, last_name, subject


FROM student_details
WHERE games NOT IN ('Cricket', 'Football');

The output would be similar to:


first_name last_name subject
------------- ------------- ----------
Shekar Gowda Badminton
Priya Chandra Chess
2) Lets consider the student_details table which we have used earlier. If you know the name of the students
who are studying science subject, you can get their id's by using this query below,

SELECT id, first_name


FROM student_details
WHERE first_name IN ('Rahul', 'Stephen');

but, if you do not know their names, then to get their id's you need to write the query in this manner,

15
SELECT id, first_name
FROM student_details
WHERE first_name IN (SELECT first_name
FROM student_details
WHERE subject= 'Science');

Output:
Id first_name
-------- -------------
100 Rahul
102 Stephen
In the above sql statement, first the inner query is processed first and then the outer query is processed.

SQL GROUP Functions


Group functions are built-in SQL functions that operate on groups of rows and return one value
for the entire group. These functions are: COUNT, MAX, MIN, AVG, SUM, DISTINCT

SQL COUNT (): This function returns the number of rows in the table that satisfies the
condition specified in the WHERE condition. If the WHERE condition is not specified, then the
query returns the total number of rows in the table.
For Example: If you want the number of employees in a particular department, the query
would be:
SELECT COUNT (*) FROM employee
WHERE dept = 'Electronics';

The output would be '2' rows.

If you want the total number of employees in all the department, the query would take the
form:

SELECT COUNT (*) FROM employee;

The output would be '5' rows.

SQL DISTINCT(): This function is used to select the distinct rows.


For Example: If you want to select all distinct department names from employee table, the
query would be:
SELECT DISTINCT dept FROM employee;

To get the count of employees with unique name, the query would be:

SELECT COUNT (DISTINCT name) FROM employee;

SQL MAX(): This function is used to get the maximum value from a column.

To get the maximum salary drawn by an employee, the query would be:

16
SELECT MAX (salary) FROM employee;

SQL MIN(): This function is used to get the minimum value from a column.

To get the minimum salary drawn by an employee, he query would be:

SELECT MIN (salary) FROM employee;

SQL AVG(): This function is used to get the average value of a numeric column.

To get the average salary, the query would be

SELECT AVG (salary) FROM employee;

SQL SUM(): This function is used to get the sum of a numeric column

To get the total salary given out to the employees,

SELECT SUM (salary) FROM employee;

The SQL UNION Operator


The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns.
The columns must also have similar data types. Also, the columns in each SELECT statement
must be in the same order.

SQL UNION Syntax


SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

Note: The UNION operator selects only distinct values by default. To allow duplicate values,
use the ALL keyword with UNION.

SQL UNION ALL Syntax


SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

CustomerID CustomerName ContactName Address City PostalCod Country


e

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 German


y

2 Ana Trujillo Emparedados y Ana Trujillo Avda. de la México 05021 Mexico


helados Constitución 2222 D.F.

3 Antonio Moreno Taquería Antonio Mataderos 2312 México 05023 Mexico

17
Moreno D.F.

And a selection from the "Suppliers" table:

SupplierID SupplierName ContactName Address City PostalCode Country

1 Exotic Liquid Charlotte Cooper 49 Gilbert St. Londona EC1 4SD UK

2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA

3 Grandma Kelly's Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA

SELECT City FROM Customers


UNION
SELECT City FROM Suppliers
ORDER BY City;

SQL GROUP BY Clause


The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped according to one
or more columns.

For Example: If you want to know the total amount of salary spent on each department, the query would
be:
SELECT dept, SUM (salary)
FROM employee
GROUP BY dept;

The output would be like:


dept salary
---------------- --------------
Electrical 25000
Electronics 55000
Aeronautics 35000
InfoTech 30000
NOTE: The group by clause should contain all the columns in the select list expect those used along with the
group functions.
SELECT location, dept, SUM (salary)
FROM employee
GROUP BY location, dept;

The output would be like:


location dept salary
------------- --------------- -----------
Bangalore Electrical 25000
Bangalore Electronics 55000
Mysore Aeronautics 35000
Mangalore InfoTech 30000

18
SQL Views
A view is a table that derived from other views and the base tables are created using
create table staement
A VIEW is a virtual table, through which a selective portion of the data from one or more tables can be seen.
Views do not contain data of their own. They are used to restrict access to the database or to hide data
complexity. A view is stored as a SELECT statement in the database. DML operations on a view like INSERT,
UPDATE, DELETE affects the data in the original table upon which the view is based.

The Syntax to create a sql view is


CREATE VIEW view_name
AS
SELECT column_list
FROM table_name [WHERE condition];
 view_name is the name of the VIEW.
 The SELECT statement is used to define the columns and rows that you want to display in the view.
For Example: to create a view on the product table the sql query would be like
CREATE VIEW view_product
AS
SELECT product_id, product_name
FROM product;

SQL Dropping a View


You can delete a view with the DROP VIEW command.

SQL DROP VIEW Syntax


DROP VIEW view_name

19

You might also like