Sie sind auf Seite 1von 37

SQL Concepts Part-1

Prepared By:Akanksha Mishra

SQL CONCEPTS
Basics of SQL,
DDL,DML,DCL,
Structure creation, alteration,
Defining constraints Primary key, foreign key, unique,
not null, check, IN operator,
Functions - aggregate functions,
Built-in functions numeric, date, string functions,
Set operations
Sub-queries, correlated sub-queries,
Use of group by, having, order by, join and its types, Exist,
Any, All , view and its types.
Transaction Control commands Commit,
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra
Savepoint

Rollback,

INTRODUCTION
When a user wants to get some information from a
database file, he can issue a Query.
A query is a userrequest to retrieve data or information
with a certain condition.
SQL is a query language that allows user to specify
the conditions. (instead of algorithms).
The user specifies certain condition.
The program will go through all the records in the
database file and select those records that satisfy the
condition.(searching).
Statistical information of the data.
The result of the query will then be stored in form of a
table.
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

INTRODUCTION TO
QUERY
LANGUAGE
Language
:- A language
in which a user

Query
requests
information from the database. These languages are higher
level than that of a standard programming language. Query
Language are classified as :Procedural

Non-Procedural

Requires a programmer to
specify in detail the steps that
are required to complete a
programming task.
User instructs the system to
perform sequence of operations
to compute desired result.
Typical procedural languages
may have 100 lines of coding
instructions.

Extract
information
by
simply telling the system
what information you need
without telling the system
how to perform the data
retrieval.
No detailed programming
instructions required.
So they are very short.

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

SQL BASICS

SQL stands for Structured Query Language, which is computer


language used for querying relational databases.

SQL was designed by IBM during 1970s.

SQL is a non-procedural
Sequel.

SQL is a comprehensive database language. Prior to SQL, there was


no standard data access languages.

ANSI & ISO both adopted SQL as the standard language for
RDBMS.

language. SQL is derived from

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

WHY SQL ???


Allows users to create tables and drop tables
and other database objects.
Allows users to store the data in database and
modify data.
Allows users to access/retrieve data in RDBMS.
Allows users to create view, stored procedure,
functions in a database.
Allows users to set permissions on tables,
procedures, and views
Allows to embed within other languages using
SQL modules, libraries & pre-compilers.
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

SQL COMMANDS :- DDL,


DCL
Standard DML,
SQL commands
to interact with relational databases
are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP.

These commands are classified into groups based on their


nature: DDL - Data Definition Language

CREATE, ALTER, DROP, TRUNCATE,RENAME


DML - Data Manipulation Language

INSERT,SELECT, DELETE,UPDATE
DCL - Data Control Language

GRANT,REVOKE
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

CONTD

DDL Data Definition Language (at Table Level)


DDL is a language used by DBMS which allows users to define
database & its objects, specify data types, structures &
constraints on the data.

DML Data Manipulation Language (at Record Level)


DML is a language used by DBMS that enables users to
manipulate existing data objects as organized by data
model. Theses are most frequently used SQL commands.

DCL Data Control Language


DCL is a language which provides users with privilege
commands. It is used for controlling access to data in the
database. Thus we can control users privileges using DCL.
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

DDL

DML

Data Definition
Language

Data Manipulation
Language

CREATE - Creates a new SELECT


Retrieves
table, a view of a table, certain records from one
or
other
object
in or more tables
database
INSERT creates a record
ALTER - Modifies an
existing
database UPDATE modifies a
object, such as a table.
record
DROP - Deletes an DELETE deletes a record
entire table, a view of a
table or other object in
the database.. All the
tables' rows, indexes
and privileges will also
be removed.
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

TRUNCATE Removes

DCL
Data Control
Language

GRANT

gives
privilege to the user
REVOKE takes back
privilege granted to
user.

INTRODUCTION TO ORACLE

Oracle is most widely used RDBMS. Oracle Corporation is second


largest software company next to Microsoft.

The significant features of Oracle are:-

Security mechanisms

Backup & Recovery

Space Management

Open Connectivity

Tools & Application

Oracle versions

Standard Edition, Enterprise Edition,

Personal Edition, Lite Edition


BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

ORACLE SQL*PLUS

First company to release RDBMS software that used


English-based structured query language.

SQL*Plus provides a very simple command-line


interface that can be used to enter SQL
statements and view results of execution.

Using SQL*Plus, one can display data dictionary


information, write and run SQL script files, execute
any SQL statements to create or alter database
objects and access database data.

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

Table
Name

TABLES IN SQL

Attribut
e Name

Enroll_No

Fname

Lname

DOB

E001

Riya

Patel

12-04-1996

E002

Rohan

Mehta

02-07-1996

E003

Sagar

Shah

24-10-1996

E004

Sima

Mistry

04-11-1996

Students

Tuple
Name

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

DATA TYPES
Char:-

Used for storing fix length character string.


Maximum 255 no. of Character is hold.
Data is right padded with spaces to whatever
length specified.
It will waste lot of Disk Space
Varchar/Varchar2( ):-

Used for storing variable length of alpha numeric


data.
More flexible then char data type.
Maximum byte stored is 4000 character.
Ex:- varchar(10) i.e svmit
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

CONTD
Date:-

Used for represent date and time.


Standard format is DD-Mon-YY.
Date time stored in 24 hour format.
To enter other standard format appropriate function
can be used.
Ex:- '01-july-2015'

Number (P,S):-

Used for storing number (Fixed or floating point).


P (precision) determine maxi. length of data.
S (scale) determine the no. of places to the right
of decimal number.
Ex:- number(4,2) i.e 10.00
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

CONTD
Long:-

Used to store variable length character string.


Only one long value can be defined per table.
Cannot used in sub querys function
expression.

Raw/Long Raw:-

Used to store binary data such as digital


picture or image.

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

BASIC STRUCTURE OF AN SQL QUERY

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

DDL:- CREATE
The CREATE
TABLE statement is used to create a table
COMMAND
in a database.

Tables are organized into rows and columns and each


table must have a name.

General Form:-

CREATE
CREATE TABLE
TABLE
Tablename
Tablename
(column_name1
(column_name1
data_type(size),
data_type(size),
column_name2
column_name2
data_type(size),
data_type(size),
xample:- Insert a new
student to the database:
column_name3
column_name3
CREATE
(EnrollNo
varchar(5),
Fname
CREATE TABLE
TABLE Students
Students
(EnrollNo
varchar(5),
Fname
data_type(size),
data_type(size),
varchar(10),
varchar(10),
varchar(10), Lname
Lname....)
varchar(10), DOB
DOB date);
date);
....)
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

CONTD

column_name :- specifies columns name of table.

data_type :- specifies what type of data column can hold (e.g.


varchar, integer, decimal, date, etc.).

size :- specifies maximum length of column of table.

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

DML:- INSERT COMMAND


The INSERT INTO statement is used to insert new
records in a table.
It is possible to write the INSERT INTO statement in
two forms:General Form:-

INSERT
INSERT INTO
INTO Tablename
Tablename (A1,.,
(A1,., An)
An) VALUES
VALUES (v1,.,
(v1,., vn
vn
OR

INSERT
INSERT INTO
INTO Tablename
Tablename VALUES
VALUES (v1,
(v1,
.,
., vn)
vn)
xample:- Insert a new student to the database:
INSERT
INSERT INTO
INTO Students
Students VALUES
VALUES (E005,
(E005, Shreya,
Shreya,
Shah,28-05-1996)
Shah,28-05-1996)
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

DDL:- ALTER COMMAND

It is used for alteration of table structures.

There are various uses of alter command, such as,

to add column to existing table


to rename any existing column
to change datatype of any column or modify
column size.
to drop a column.

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

ALTER TO ADD MULTIPLE COLUMN


TO ALREADY EXISTING TABLE

ALTER TABLE table_name


ADD column_name datatype(size);

For Example: To add a column "experience" to employee


table, the query would be like
ALTER TABLE employee ADD experience number(3));
For Example: To add multiple column "experience, city,
hiredate to employee table, the query would be :ALTER TABLE employee
ADD (experience number(3), city varchar2(10), hiredate date));

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

ALTER TO DROP_COLUMN
ALTER TABLE table_name DROP column column_name;

For Example: To drop the column "location" from the


employee
table, the query would be
ALTER TABLE employee DROP column location;

ALTER TO MODIFY_COLUMN
ALTER TABLE table_name MODIFY column_name
datatype;

For Example: To modify the column salary in the employee


table,
the query would
be
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra
ALTER TABLE employee MODIFY salary number(15,2);

ALTER TO RENAME_COLUMN

ALTER TABLE table_name


RENAME oldcolumn_name to newcolumn_name;

For Example: To rename the column from "location" to


address in employee table, the query would be
ALTER TABLE employee RENAME location to address;

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

DML :- UPDATE
The SQL UPDATE
Query is used to modify the existing records in a table.
COMMAND
We can use WHERE clause with UPDATE query to update selected
rows otherwise all the rows would be affected.

Syntax: The basic syntax of UPDATE query with WHERE clause is -

UPDATE table_name
SET column1 = value1, column2 = value2....,
columnN = valueN
WHERE [condition];
We can combine N number of conditions using AND/OR operators.
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

EXAMPLE:
Consider the CUSTOMERS table having the following
records:

Following is an example, which would update ADDRESS for a


customer whose ID is 6:
SQL> UPDATE
CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6;
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

DML:- DELETE
COMMAND
The SQL DELETE Query is used to delete the existing records
from a table.
You can use WHERE clause with DELETE query to delete
selected rows, otherwise all the records would be
deleted.
Syntax: The basic syntax of DELETE query with WHERE clause is :

DELETE FROM table_name WHERE [condition];

You can combine N number of conditions using AND/OR operators.

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

QUERY

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof.


Akanksha A. Mishra

DROP TABLE
The SQL DROP TABLE statement is used to remove a
table definition and all data, indexes, triggers, constraints,
and permission specifications for that table.
NOTE: Be careful while using this command because
once a table is deleted then all the information
available in the table would also be lost forever.

Syntax: DROP TABLE table_name;


If we want to drop a table employee query will be

DROP TABLE employee;


BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

ORDER BY COMMAND

The SQL ORDER BY clause is used to sort the data in


ascending or descending order, based on one or more
columns. Some database sorts query results in ascending
order by default.
Syntax:
SELECT column-list FROM table_name
WHERE condition
ORDER BY column1, column2, .. columnN ASC | DESC;
You can use more than one column in the ORDER BY clause.
Make sure whatever column you are using to sort, that
column should be in column-list.
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof.


Akanksha A. Mishra

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

WHERE CLAUSE
The SQL WHERE clause is used to specify a condition while fetching
the data from single table or joining with multiple tables.
If the given condition is satisfied then only it returns specific value
from table. WHERE clause to filter the records and fetching only
necessary records.
The WHERE clause is not only used in SELECT statement, but it is
also used in UPDATE, DELETE statement, etc.,

Syntax: The basic syntax of SELECT statement with WHERE clause is as


follows:
SELECT column1, column2, columnN
FROM table_name WHERE [condition]
We can specify a condition using comparison or logical operators
like >, <, =, LIKE, NOT, etc. Below examples would make this concept
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra
clear.

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof.


Akanksha A. Mishra

AND OPERATOR
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.
AND Operator: allows the existence of multiple
conditions in an
SQL statement's WHERE clause.
Syntax:

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof.


Akanksha A. Mishra

OR OPERATOR
The OR operator is used to combine multiple conditions in
an SQL statement's WHERE clause.
Syntax:
SELECT column1, column2, columnN FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN]

We 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.
BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof. Akanksha A. Mishra

BE.COMP - 3rd SEM - DBMS- SQL Concepts - By Prof.


Akanksha A. Mishra

Das könnte Ihnen auch gefallen