You are on page 1of 7

Unit – 3

SQL: Introduction – Types of SQL commands – Tables: Creating, Deleting and


Modifying a Table – Views: Creating, Deleting view – advantages of view - Indexes:
Creating Index – Types – Dropping Index.

SQL

SQL stands for Structured Query Language use for storing, manipulating and retrieving
relational database data.SQL queries to retrieve data from database same as you can adding and
manipulating database data.

SQL is a very powerful and diverse database language use to storing data into databases.
SQL is loosely typed language so you can learn easily.

Type of SQL Commands (DDL, DML, DCL, TCS, SCS Commands)

SQL statements are divided into five different categories:

 Data definition language (DDL)


 Data manipulation language (DML)
 Data Control Language (DCL)
 Transaction Control Statement (TCS)
 Session Control Statements (SCS).

Data Definition Language (DDL) Statements

Data definition statement are use to define the database structure or table.

Statement Description
CREATE Create new database/table.
ALTER Modifies the structure of database/table.
DROP Deletes a database/table.
TRUNCATE Remove all table records including allocated table spaces.
RENAME Rename the database/table.

Data Manipulation Language (DML) Statements

Data manipulation statement are use for managing data within table object.

Statement Description
SELECT Retrieve data from the table.
INSERT Insert data into a table.
UPDATE Updates existing data with new data within a table.
DELETE Deletes the records rows from the table.
MERGE (also called UPSERT) statements to INSERT new records or
MERGE
UPDATE existing records depending on condition matches or not.
MERGE (also called UPSERT) statements to INSERT new records
MERGE
or UPDATE existing records depending on condition matches or not.
LOCK TABLE statement to lock one or more tables in a specified
LOCK TABLE mode. Table access denied to a other users for the duration of your
table operation.
CALL Statements are supported in PL/SQL only for executed dynamically.
EXPLAIN PLAN CALL a PL/SQL program or EXPLAIN PATH access the data path.

Data Control Language (DCL) Statements

Data control statement are use to give privileges to access limited data.

Statement Description
GRANT Gives privileges to user for accessing database data.
REVOKE Take back for given privileges.
ANALYZE statement to collect statistics information about index,
ANALYZE
cluster, table.
To track the occurrence of a specific SQL statement or all SQL
AUDIT
statements during the user sessions.
COMMENT Write comment to the data table.

Transaction Control Statement (TCS)

Transaction control statement are use to apply the changes permanently save into database.

Statement Description
COMMIT Permanent work save into database.
ROLLBACK Restore database to original form since the last COMMIT.
SAVEPOINT Create SAVEPOINT for later use ROLLBACK the new changes.
SET SET TRANSACTION command set the transaction properties such as
TRANSACTION read-write/read only access.

Session Control Statements (SCS)

Session control statement are manage properties dynamically of a user session.

Statement Description
ALTER SESSION statement to modify conditions or parameters that
ALTER SESSION
are affect to your database connection.
SET ROLE statement to enable or disable the roles that are currently
SET ROLE
enabled for the session.
Tables
Tables are basic building blocks in any relational database management system. They
contain the rows and columns of your data. We can create, modify and delete the tables using the
data definition language.
Data Definition Language
Data Definition Language is a part of SQL that is responsible for the creation, updating
and deletion of tables. It is responsible for creation of views and indexes also. The list of DDL
commands is given below:
1.create table 2.alter table 3.drop table 4.create view 5. create index
Create table
The create table statement creates a new base table.
Fomat-1 Syntax
create table table_name(
Column-1-definiiton,
Column-2-definiiton,
…………..
Column-n-definiiton,
Primary-key-definition,
Alternate-key-definiiton,
Foreign –key-definition );
Where column-definition takes the form
column_name datatype [NULL|NOT NULL [WITH DEFAULT \ UNIQUE]]
The keywords NULL and NOT NULL are optional. The default is to permit nulls.
NULL
 If you specify the NULL option, then the RDBMS will insert a null in that column if
the user does not specify a value.
NOT NULL
 If the user specifies NOT NULL option, you can either specify the WITH DEFAULT
option or UNIQUE option.
 If you specify the NOTULL WITH DEFAULT then the RDBMS will substitute the
default values.
 IF NOT NULL UNIQUE is specified, the RDBS will ensure that the values for the
columns that are
Fomat-2
The second format of the create table statement allows the user to create a base table that
is having the same structure as existing table.
Syntax: Create table base_table_name like table_name;
Modifying a table
An existing table can be modified by using the ALTER TABLE statement.
Syntax Alter table table_name
Add column data type [NULL|NOT NULL WITH DEFAULT];
Example: alter table stud
add address char(10); Table altered.
Using the alter table statement new columns can be added and primary and foreign key
specification can be added or removed. But alternate key specifications cannot be changed sung
the alter table command. Since the alter table statement does not provide any facility to delete a
column from a table.
Deleting a table
An existing base table can be deleted at any time by using the drop table command.
Syntax: drop table table_name;
View
A view is a virtual table in the database whose contents are defined by a query. To the
database user, the view appears just like a real table, with a set of named columns and rows of
data. But unlike a real table, a view does not exist in the database as a stored set of data values.
SQL creates the illusion of the view by giving the view a name like a table name and storing the
definition of the view in the database.
Creating view
A view can be created using the create view statement.
Syntax: Create view view_name{column1,column2….]
as subquery
[with check option]
The subquery cannot include either union or order by. The clause with check option
indicates that update an insert operations against the view are to be checked to ensure that the
updated or inserted row satisfies the view defining condition.
Book Table
id title author year price
1 FOC Leon 2001 100
2 C Bala 2002 200
3 DBMS Leon 2003 300
4 VB Bala 2004 400
5 Java Bala 2005 500
6 Webtech Rajkamal 2006 600
Example:create view Bookv view
bookv(id,title,author,year,price) id title author year price
4 VB Bala 2004 400
as select Id,title,author,year,price 5 Java Bala 2005 500
from book 6 Webtech Rajkamal 2006 600
where year>2003;

If the column names are not specified explicitly in the view definition statement then the
view inherits the column names of the source of the view. Column names must be specified
explicitly for all columns of the view if:
1. Any column of the view is derived from a function, an operational expression or literal.
2. Two or more columns of the view otherwise have the same name.
3.
Examplecreate view Author max(price)
maxprice(author,max(price) Leon 300
Bala 500
as select author,max(price) Rajkamal 600
from book
group by author;
Data query and Manipulation operations with views
 A view derives its from a base table, the RDBMS manipulates the data of the base
table when the user queries, insert into , updates or deletes from a view.
 The RDBMS converts your query into an equivalent operation on the underlying
table.
 The conversion is done by merging the query issued by the user with the subquery
that the catalog when the view was defined.
Select statement
Select * from book; Select * from book
Where year>2003;
Update statement
update bookv update book
set price=price*1.5; set price=price*1.5;
where author=’rajkamal’; where author=’rajkamal’ and year>2005;

Insert statement
insert into bookv(id,title,author,year,price) insert into book(id,title,author,year,price)
values(7,’flash’,’rajkamal’,2007,700); values(7,’flash’,’rajkamal’,2007,700);
Delete statement
delete from bookv delete from book
where id=2; where id=2 and year>2003;
Check option
All the inserts and updates operations on the views quill be checked to ensure that the
newly inserted or updated rows do not violate the view defining conditions. If there are some
values, do not satisfy the view condition, then the insert and update will not take place.
Insert operation Update operation
insert into bookv(id,title,author,year,price) update bookv
values(8,’msoffice’,’leon’,400,2000); set year=2003
The above will be inserted as it is disappear from where author=’rajkamal’;
the view because the row does not satisfy the It never satisfy the viewing condition
viewing condition where year>2003 year>2003
Views involving multiple tables
We can create a view joining the two tables by using the create view statement.
Example: create view bookpub
as select id,title,author,publisher,year,price,city
from book,publisher
where book.publisher=publisher.publisher.name;
Advantage
 Data security: Views are allows to set up different security levels for the same table,
thus protecting certain data from unauthorized people.
 The views allow the same data to be seen by different users in different ways at the
same time.
 Views can be used to present additional information like derived columns.
 Views can be used to hide complex queries
Dropping a view
An existing view can be deleted at any time by using the drop view command.
drop view view_name {RESTRICT|CASCADE}
If RESTRICT is specified and if the view is referenced in any other view definition, the
drop view will fail. If CASCADE is specified always succeed any referencing views and
integrity constraints will automatically be dropped too.
Ex: Drop view bookv;

Index
An index is a structure that provides fast access to the rows of a table based on the values
of one or more columns. The index stores data values and pointer to the rows where those data
values occur. In the index the data values are sorted and stored in the ascending and descending
order. So the RDBMS can quickly search the index to find a particular data value.

Advantage
 Searching an index is must faster than searching tables. Finding the rows of a table
from an index is also faster, because the index tells the RDBMS, where exactly the
rows are stored.
Disadvantage
 Index is a separate database objects and needs additional desk space.
 The index must be updated every time a row is inserted, deleted or modified.
Creating an Index
Indexes are created using the CREATE INDEX statement.
Syntax
CREATE [UNIQUE] INDEX index_name
ON base-table (column [order] [, column [order]]……)
Each ‘order’ specification is ASC (ascending order) or DESC (descending order). Asc is being
default. The ‘Unique’ option specifies that no two rows in the indexed base table will be allowed
to take same value for the indexed column.
Types of Indexes
Composite Indexes
When index is made up of more than one column it is called a composite index.
Composite indexes are used when two or more columns are best searched as a unit because of
their logical relationship.
Example: create index index1
on stud(rno,name);

Unique Indexes
 Unique index is one in which no two rows are permitted or in which no duplicate
values are allowed for the same index value.
 Unique indexes are usually created on the primary key of a table. We specify the
index as a unique index by using the keyword ‘unique’.
 For unique indexes, the RDBMs checks for duplicate values when the index is created
and each time new data is added.
Example: create unique index ind_id on stud(rno);
Clustered Index Non clustered index
When you use the clustered index, the rows in In a non-clustered index, the physical order of
the table will be in the same order as that of the the rows is not same as their indexes order.
index.
Since clustered index controls the physical There can be many non-clustered indexes per
location of data, there can be only one table as you wish.
clustered index table, most often created on the
primary key.
Clustered indexes are much faster than non-
clustered index.
Dropping an index
Index can be dropped using the drop index command. Once the command is executed, the
index is removed. drop index index_name;

You might also like