Presentation On SQL

You might also like

Download as odp, pdf, or txt
Download as odp, pdf, or txt
You are on page 1of 33

SQL

SQL

SQL is the abbreviation of Structured Query Language.


Used for assessing, managing and retreiving the data held in a
RDBMS
SQL is used to manipulate data in several database systems
Database systems like MySQL, SQL server, Microsoft Access,
Oracle, Sybase, DB2.

THINGS ABOUT SQL

Not case-sensitive.

Every statement ends with the semi-colon ( ; ).

SQL was developed in ANSI standard.

CLASSIFICATION

SQL was classified, based on the keywords as follows:

DDL (Data Definition Language)

DML (Data Manipulation Language)

DCL (Data Control Language)

TCL (Transition Control Language)

DQL (Data Query Language).

DDL (Data Definition Language):


Create
Alter
Drop

DML (Data Manipulation Language):


Insert
Update
Delete

DQL (Data Query Language):

Select

DCL (Data Control Language):

Grant

Revoke

TCL (Transition Control Language):


Commit
Rollback
Savepoint

Creation of Database:
Database can be created by using the keyword
'CREATE DATABASE'.
Syntax: create database database_name;
Eg:
create database dbase;

Creation of Table:
Table can be created with the keyword
'CREATE TABLE'.
Syntax:
Eg:

create table table_name;


create table customer;

Every table contains:


Fields along with data type and length.
Constraints.

Create table customer


(
Id int not null identity(1,1) primary key,
Name varchar(35) not null,
Age int not null check ( age > 18),
Ph bigint not null unique,
City varchar(20) default 'bangalore',
Postal int not null
);
Create table orders
(
o_id int not null primary key,
o_name varchar(100) not null,
c_id int not null foreign key references customer(id),
o_quantity int not null
);

To insert values into table:


Syntax: insert into table_name (field1,field2,..n) values
(value1, value2,..n);
Eg:
insert into customer (name, age, ph, city, postal)
values ('arun', 25, 9784512451, 'bangalore', 560012);

To update a record in a table:


Syntax: update customer set field1=value1,...fieldn=valuen
where condition;
Eg:
update customer set name = 'varun' where id = 1;

To select the records of the table:


Case 1:
to select all the records of a table.
Syntax: select * from table_name;
Eg:
select * from customer;
Case 2:
to select the selected fields from the table.
Syntax: select field1, field2, fieldn from customer;
Eg:
select id, name, age from customer;

Select statement:

~ Using 'Order by':


Syntax: select * from table_name where condition order
by field_name asc/desc;
Eg:
select * from customer order by name desc;
~ Using 'AS' keyword:
Eg:
select id as [customer id], name as [customer
name] , city+,',+postal as [customer address]
from customer order by name asc;
~ Using 'Top n':
Eg:
select top 3 * from customer order by name asc;
~ Using 'distinct' keyword:
Eg:
select distinct city from customer ;

~ using 'LIKE' patterns:


to select the city begins with 'B'.
Eg: select * from customer where city like 'B%';
to select the name ends with 'e'.
Eg: select * from customer where name like '%e';
to select the name, where the in between characters are sh.
Eg: select * from customer where city like '%sh%';
~ using 'AND' and 'OR' statements :
using OR:
Eg: select * from customer where city = 'bangalore' OR
name = 'arun';
using AND:
Eg: select * from customer where city = 'bangalore'
AND name = 'arun';

~ Using 'IN' keyword:


Eg:

select * from customer where city in


('chennai','bangalore','mumbai');

~ Using 'BETWEEN' keyword:


Eg:
select * from customer where id
between 2 and 5;

Deletion of record in the table:


Eg:

delete from table where name = 'arun';

JOINS IN SQL

JOIN clause is used to combine rows from two or more tables,


based on a common field between them.
Types of joins:

Inner join

Left outer join

Right outer join

Full join

Self join.

Inner join:
Inner join selects all the fields that is getting
match with both the tables
Eg: select c.id, c.name, c.ph, o.o_id, o.o_quantity from
customer as c inner join orders as o on c.id = o.c_id;

Left outer join:


Left outer join selects all the fields from the left table and
matching fields from the right table.
Eg: select c.id, c.name, c.ph, o.o_id, o.o_quantity from
customer as c left outer join orders as o on c.id =
o.c_id;

Right outer join:


Right outer join selects all the fields from the right table and
matching fields from the left table.
Eg: select c.id, c.name, c.ph, o.o_id, o.o_quantity from
customer as c right outer join orders as o on c.id =
o.c_id;

Full join:
Full join selects all the fields from both the tables irrespective
of the condition and shows the null fields with the term 'null'.
Eg: select c.id, c.name, c.ph, o.o_id, o.o_quantity from
customer as c full join orders as o on c.id = o.c_id;

Self join:

self join is a special purpose join which is used to compare


the table with same table by assuming it as a second table by
temporarily giving it a name.

FUNCTIONS IN SQL

SQL having some default functions to perform calculation on


data.
Functions are broadly classified into two types:

Aggregate functions,

Scalar funtions.

AGGREGATE FUNCTIONS:
~ These functions will returns a single value based on the
opertations performed on the fields of the table.
~ Some of the aggregate functions are:
Numerals alone:
-> AVG()

Returns the average value

-> SUM()

Returns the sum.

Numeral and Alphanumerals:


-> COUNT() -

Returns the number of rows

-> MAX()

Returns the largest value

-> MIN()

Returns the smallest value.

SCALAR FUNCTIONS:
These are another set of functions to process the values of the
fields.
UPPER()

- Converts a field to upper case

LOWER()

- Converts a field to lower case

SUBSTRING() - Extract characters from a text field


LEN()

- Returns the length of a text field

ROUND()

- Rounds a numeric field to the number


of decimals specified.

DATE FUNCTIONS:
These are another set of functions to process the felds
containing the data type of date and time.
GETDATE()

- Returns the current date and time

DATEPART() - Returns a single part of a date/time


DATEADD()

Adds or subtracts a specified time


interval from a date

DATEDIFF()

- Returns the time between two


dates

CONVERT()

Displays date/time data in different


formats.

OUTPUT OF DATE
FUNCTIONS

ALTER:
~ To add some fields to a existing table:
Syntax: ALTER TABLE table_name ADD COLUMN
data_type;
Eg:

ALTER TABLE customer ADD Ld bigint;

~ To change the datatype of the existing field in a table:


Syntax:

ALTER TABLE table_name ALTER COLUMN


column_name datatype;

Eg:

ALTER TABLE customer ALTER COLUMN


Ld int;

~ To delete a column from the table:


Syntax: ALTER TABLE table_name DROP COLUMN
column_name;
Eg:
ALTER TABLE customer DROP COLUMN Ld;

DELETE:
~ To delete a record from the table:
Syntax: DELETE FROM table_name where
CONDITION;
Eg:
DELETE FROM customer where id = 2;
~ To delete all the records of the table:
Syntax: DELETE FROM table_name;
Eg:
DELETE FROM customer;
(Note: if where condition is not mentioned, it will
delete entire contents of the table).

DROP:
~ To delete the schema of the table (i.e.) to delete the table,
we can go for 'DROP TABLE' keywords.
Syntax: DROP TABLE table_name;
Eg:
DROP TABLE customer;
~ To delete the database, we can go for
'DROP DATABASE' keywords.
Syntax: DROP DATABASE table_name;
Eg:
DROP DATABASE dbase;

COMMIT:

It is the transactional command used to save changes


invoked by a transaction to the database.
Eg:

COMMIT;

ROLLBACK:

It is the transactional command used to undo transactions


that have not already been saved to the database.
Eg:

ROLLBACK | ROLLBACK TO
SAVEPOINT_NAME;

SAVEPOINT:

It is a point in a transaction when you can roll the


transaction back to a certain point without rolling back the entire
transaction.
Syntax: SAVEPOINT SAVEPOINT_NAME;
To release savepoint: RELEASE SAVEPOINT SAVEPOINT_NAME;

DATA CONTROL LANGUAGE:

GRANT:
It is a command used to provide access or privileges on the
database objects to the users.
Syntax: GRANT privilege_name ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
Eg:
GRANT select ON customer TO PUBLIC;

REVOKE:
It removes user access rights or privileges to the database
objects.
Syntax: REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
Eg:
REVOKE select on customer from PUBLIC;

TUNING IN SQL
SQL can be made tuned by following some normal procedures.
Tuning is the process by which the optimum performance is
gained.
Some procedures are:
Always select the records using the WHERE clause.
Select only the required fields of the table.
Avoid function on the search conditions.

Avoid foreign key constrainsts if possible.

Instead of deletion of a record, try to modify it .

Avoid too many index for a table.

Use temporary tables to insert records into the table.

SOME ADVANCED TOPICS IN


SQL

PIVOT:
It transform data from row-level to
columnar data.
STORED PROCEDURE
TRY AND CATCH
A TRYCATCH construct catches all execution errors.

ERROR MESSAGES.

TEMPORARY TABLES.

TRIGGERS

PIVOT:
SELECT * FROM ( SELECT o_id, o_name, c_id FROM
customer) as c
PIVOT ( SUM(o_quantity) FOR orders IN (name, age, ph,
city, postal) )AS piv ;

Eg of a coding:
CREATE PROCEDURE sp_emp
AS BEGIN
SELECT * FROM employ
END
BEGIN TRY
EXECUTE sp_emp
END TRY
BEGIN CATCH
IF ( error_number() = 208)
RAISEERROR ('this table is not exist', 0, 1)
ELSE SELECT error_message() AS errormessage
END CATCH

?
S
E
I
R
E
U
ANY Q

THANK YOU

You might also like