Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 58

Structured Query Language

Structured query language (SQL) is a language


that is used for interaction between the users
and the RDBMS. SQL is also used to create,
update and maintain the database.
Structured Query Language
Characteristics of SQL:
• SQL is a non-procedural language i.e., the user needs
to specify only what data to get without specifying
how to get those data.
• SQL is easy to learn because it has a small set of
commands.
• SQL is simple and user-friendly because, the syntax
of its commands is as English-like sentences.
• SQL is highly flexible. A query in SQL can be written
in a number of ways.
• Each SQL query is parsed by RDBMS to check its
syntax.
• Each SQL query is optimized prior to execution.
BASIC DATA TYPES

• Char (size) upto 255 charachetrs


• Varchar(size)/Varchar2(size) upto 4000
chanraccetrs
• Date: DD-MON-YYYY
• Number (P,S)
SUBDIVISIONS OF SQL

• Data definition language (DDL)


• Data manipulation language (DML)
• Data control language (DCL)
Data Definition Language
DDL includes the commands that are used to create,
update and delete the structure of tables. The list of
DDL commands is as follows:
• Create table
• Alter table
• Drop table
• Create view
• Drop view
• Rename
Data Manipulation Language
DML includes the commands that are used to
access or manipulate the data in the tables.
DML commands are listed as follows:

• Insert
• Update
• Delete
• Select
Data Control Language
DCL includes the commands that related to the
security of the database. The commands of
DCL are:

• Grant
• Revoke
Create Table Command
Create table command is used to define a new
table.

Syntax:
Create table table-name(column1 datatype (size),
column2 datatype (size),
------,
columnN datatype (size));
Constraints in Create Table
Command

• Primary key constraint


• Unique constraint
• Not null constraint
• Foreign key constraint
• Check constraint
Primary Key Constraint

Primary key is applied to a column so that this


column can be used to uniquely identify each
row in the table. A primary key has following
features:

• Primary key does not allow duplicate values.


• Primary key does not allow null values.
Primary Key Constraint

Syntax:

Column-name datatype (size) Primary Key


Unique Constraint

A unique key has following characteristics:

• Unique key will not allow duplicate values.


• Unique key can accept null values.
• A table can have many unique constraints.
Unique Constraint

Syntax:

Column-name datatype (size) Unique


Not Null Constraint

Not Null column constraint ensures that a table


column cannot be left empty.

Syntax:
Column-name datatype (size) Not Null
Foreign Key Constraint
Foreign key is a column whose values are derived from the
primary key of other table. Foreign key has following
characteristics:
• Foreign key can contain only those values that are present in
the primary key of the master table.
• The values in the foreign key can be null or duplicate values.
• In foreign key, if we insert a value that is not present in the
primary key of master table then, that record will be rejected
by Oracle engine.
• A record in the master table can not be deleted if the
corresponding record in the foreign table exists.
Foreign Key Constraint

Syntax:

Column-name datatype (size)


References
Master-table-name (column-name)
Check Constraint

Check constraint is used to specify the data


validation for a column. Check constraint is a
Boolean expression that is evaluated to either
True or False.
Check Constraint

Syntax:
Column-name Datatype (size) Check (Boolean
Expression)
Alter Table Command

Alter table command can be used to modify the


structure of this already existing table. Alter
command does not change the contents of a
table. We can add or delete columns, change
the data type of existing columns, add or
remove the constraints by using alter table
command.
Alter Table Command

• Adding a New Column to an existing Table


• Modifying an existing Column
Alter Table Command
• Adding a New Column to an existing Table

Syntax:

Alter table table-name ADD(Column-name1 datatype (size),


Column-name2 datatype (size),
|
|
Column-nameN datatype (size));
Alter Table Command
• Modifying an existing Column:

1. To increase or decrease a column’s width


and to change the data type.
2. To change a column definition from null to
not-null or from not-null to null.
3. Adding a constraint to an existing column.
4. Dropping the constraints.
5. Dropping a column from a table.
Increase or Decrease Column
Depth

• Syntax:

Alter table table-name MODIFY


(column-name New-datatype (New
Size));
Null to Not Null or Not Null To
Null

• Syntax:

Alter table table-name MODIFY


(column-name datatype (size) Null/Not Null);
Adding a constraint to an existing
column

• Syntax:
– To add primary key:
Alter table table-name ADD
Primary Key (column-name);
– To add other constraints:
Alter table table-name ADD
(Constraint-definition);
Dropping the Constraints
• Syntax:

-To remove primary key:


Alter table table-name DROP Primary Key;
-To remove other constraints:
Alter table table-name DROP
constraint constraint-name;
Dropping a column from a table

• Syntax:
Alter table table-name DROP
column column-name;
Drop Table Command

When a table is no longer needed then, the drop


table command can be used to remove the
structure of the table from the database.
Drop Table Command

Syntax:
Drop table table-name;
Rename Command

Rename command is used to change the name of


an existing table.
Syntax:
Rename table-name TO new-table-name;
Insert Command
Syntax:
• Insert into table-name(Column-Name1,.....,
Column-NameN)
Values
(Value1,...., ValueN);
• Insert into table-name Values(Value1,
Value2, ..... ValueN);
• Insert into table-name Values(‘&Column1’,
‘&Column2’, ----, ‘&ColumnN’);
Update Command

The update command is used to modify the


already existing data values in a table. Update
command can be used to update either:

• All the rows from a table or


• A selected set of rows from a table
Update Command
The syntax for updating all the rows is as
follows:
• Syntax:

Update table-name
SET
Column-Name1 = Value1,...,
Column- NameN = ValueN;
Update Command
The syntax for updating a selected set of rows is as
follows:
• Syntax:

Update table-name
SET
Column-Namel = Value1,....,
Column-NameN = ValueN
Where Condition;
Delete Command

Delete command is used to delete the rows from


a table. Delete command is used to delete
either:
• All the rows from a table or
• A selected set of rows from a table
Delete Command

The syntax for deleting all the rows is as


follows:
• Syntax:
Delete from table-name;
Delete Command

The syntax for deleting specific rows is as


follows:
• Syntax:
Delete from table-name Where Condition;
Select Command
Select command is used to view or retrieve the
rows from one or more tables. The general
form of select statement is as follows:
Syntax:
Select C1, C2,..., Cn
From R1, R2,..., Rn
Where P;
Select Command

Select command is used to access the data in


various ways and they are:
• To access all rows and all columns.
• To access specific columns and all rows.
• To access specific rows and all columns.
• To access specific columns and specific rows.
Select command with Distinct
clause

To view only the unique values from a column,


distinct clause can be used. The distinct clause
eliminates the duplicate values from the result.
The syntax of the distinct clause is as follows:
Syntax:
Select DISTINCT column-name
From table-name;
Select command with Order By
Clause
Order by clause can be used to view the data in a
sorted order (either ascending or descending).
The syntax for order by clause is as follows:
Syntax:
Select * from table-name
Order By column-name Sort-order;

• The sort-order may be either, Asc (for


ascending) or Desc (for descending).
Alias with Select command
An alias is used to rename the column name
while displaying the data. But, the actual
column name in the structure does not change.
The syntax for using the alias is as follows:
• Syntax:
Select column-name1 alias-name1,
column-name2 alias-name2
From table-name;
Select command with BETWEEN
Operator

BETWEEN operator is used to select the data that is


within a specified range of values. The result also
includes the lower and upper limit (means the range is
inclusive). The lower and upper limit values are linked
with keyword AND. The syntax for between operator is
as follows:
Syntax:
Select * from table-name
Where column-name BETWEEN LL AND UL;
Select command with LIKE
Operator

LIKE operator is used for string or pattern matching. It


compares one string value with another string value.
LIKE operator uses two wild card characters:
• Percent (%) character to match any string of any
length (0-N).
• Underscore (_ ) character to match a single character.
One point is to remember that the pattern is case
sensitive.
Select command with LIKE
Operator

Examples: Consider the following queries:


• Show the details of employees whose name
starts with R.
Query:
Select * from employee
Where ename LIKE ‘R%’;
Select command with SET
Operations

Followings are the three set operations:


• Union
• Intersect
• Minus
Select command with SET
Operations

Syntax:
Select-Statement1
UNION/INTERSECT/MINUS
Select-Statement2;
Select command with DUAL
Table

Dual is the part of the data dictionary. It is a


small work-table owned by SYS. It consists of
only one row and one column. The column-
name is D, and the value in this column is x.
Sometimes, the simple calculations are to be
done (like 3*3 etc.). Then, the select statement
uses the dual table to do the calculations.
Select command with DUAL
Table
Query 1:

Select 4+4 from dual;


Output 1:
4+4
-----------
8
Query 2: To view today’s date.

Select Sysdate from dual;


Output 2:

Sysdate
-------------
20-Jan-09
Select command with Aggregate
Functions
Aggregate functions are the Oracle functions that take a
collection of values as an input and produce a single
value as an output. Aggregate functions can be
applied on any column of numeric type. There are
five aggregate functions and they are:
• Avg
• Min
• Max
• Sum
• Count
Select command with Group By
clause
Group by clause creates a data set that contains several
sets of records grouped together based on a condition.
The syntax for group by clause is as follows:
Syntax:
Select C1, C2,..., Cn
Aggregate-Function(Column-Name)
From Table-Name
Where condition
Group By C1, C2,..., Cn;
Select command with Having
clause

Having clause is used to specify a condition on


the group by clause. Having clause is
equivalent to where clause except that it acts
on record set but not on the individual record.
DATA CONTROL LANGUAGE

• DCL includes the commands that are related to


the security of the database. These commands
are used to restrict the operations on a
database object for a particular user. Database
administrator grants and revokes the user
privileges by using grant command and revoke
command.
DATA CONTROL LANGUAGE
Various object privileges are explained as under:
• Alter: allows the user to change the table definition with
ALTER TABLE command.
• Delete: allows the user to delete the records from the table with
DELETE command.
• Insert: allows the user to add records to the table with
INSERT command.
• Select: allows the user to query the table with SELECT
command
• Update: allows the user to modify the records in a table with
UPDATE command.
• Index: allows the user to create an index on a table with
CREATE INDEX command.
• With grant option: allows the user to grant the privileges to
some other users.
DATA CONTROL LANGUAGE
Grant

The owner of a database object can grant the privileges


to a user by using GRANT command. The database
object can be a table, a view or a sequence.
Syntax:
Grant privileges
ON object-name
TO user-name
WITH GRANT OPTION;
DATA CONTROL LANGUAGE
Revoke
The privileges granted to a user can be revoked
(taken back) with REVOKE command.
Syntax:
Revoke privileges
ON object-name
From user-name;
VIEW
• View is an Oracle object that is used to implement the
security. A view is an imaginary table that is created
from some other base table.
Syntax:
Create view view-name
AS select column-list
From table-name
Where condition;
VIEW

View can be dropped when no longer needed.


Syntax:
Drop view view-name;

You might also like