0% found this document useful (0 votes)
195 views51 pages

DBMS Module 3 PPT

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

DBMS Module 3 PPT

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

Introduction to Structured

Query Language (SQL)


Ms. Himani Tiwari

4/21/2022 DBMS : III : Introduction to Structured Query Language 1


Contents
• Overview of SQL
• Data Definition Language Commands
• Data Manipulation Language Commands
• Data Control Language Commands
• Transaction Control Language Commands
• Constraints
• Set and String Operations
• Aggregate Functions
• Group By and Having Clause

4/21/2022 DBMS : III : Introduction to Structured Query Language 2


Overview of SQL
• SQL is a structured query language. Query language is nothing but language
used to deal with database.

• It is used for storing and managing data in Database Management System.

• SQL allows users to query the database in a number of ways, using English-
like statements

4/21/2022 DBMS : III : Introduction to Structured Query Language 3


Overview of SQL (Cont…)
Data Transaction
Data Definition Data Control
Manipulation Control language
Language (DDL) Language (DCL)
Language (DML) (TCL)

Create Select Grant Commit

Alter Insert Revoke Rollback

Save
Drop Update
point

Truncate Delete

Rename

4/21/2022 DBMS : III : Introduction to Structured Query Language 4


Overview of SQL (Cont…)
• Writing SQL statements

• SQL statements are not case sensitive. Generally, keywords of SQL are written
in uppercase.

• SQL statements can be on one or more line.

• Keywords cannot be abbreviated or split across lines.

• Clauses are usually placed on separate lines.

4/21/2022 DBMS : III : Introduction to Structured Query Language 5


Overview of SQL (Cont…)

SQL Databases Applications of SQL


• MySQL • Access Data
• Oracle • Describe Data
• MSAccess • Manipulate Data
• PostgresDB • Create & Drop Database
• Create view, Stored
Procedures, Function
• Set permissions on tables,
view
4/21/2022 DBMS : III : Introduction to Structured Query Language 6
DDL Commands
• DDL changes the structure of the table like creating table, deleting
table, altering table etc.
• All the commands of DDL are auto-committed that means it
permanently save all the changes in the database
• Here are some commands that come under DDL
• CREATE
• ALTER
• DROP
• TRUNCATE: Used to remove all of the rows from a table, regardless of whether or not any conditions
are met and resets the table definition.
• RENAME

4/21/2022 DBMS : III : Introduction to Structured Query Language 7


DDL Commands (Cont…) Create command
• CREATE TABLE
Syntax table_name(column
data_type (data_size));
• create table student(name
Example
varchar2(10), rno number(10));
• CREATE is used to create a new
Use
table in the databse
4/21/2022 DBMS : III : Introduction to Structured Query Language 8
DDL Commands (Cont…) Create command

LONG
Varchar2(size) Char(size) Number(p,s) Date

• Variable • Fixed length • Variable • Date and • variable


length character length time values length
character data numeric data character
data data upto
2GB

4/21/2022 DBMS : III : Introduction to Structured Query Language 9


DDL Commands (Cont…) Alter command
• It is used to
• Add new column
• Modify existing column
• Drop column

4/21/2022 DBMS : III : Introduction to Structured Query Language 10


DDL Commands (Cont…) Alter command
• ALTER TABLE table_name ADD column_name data_type;
Add new column • alter table student add (department varchar2(10) );

• ALTER TABLE table_name


Modify existing ALTER COLUMN column_name datatype;
column • alter table student
• ALTER COLUMN name varchar(30);

• ALTER TABLE table_name


DROP COLUMN column_name;
Drop Column • ALTER TABLE Customers
DROP COLUMN Email;

4/21/2022 DBMS : III : Introduction to Structured Query Language 11


DDL Commands (Cont…) Drop command
Syntax • DROP TABLE table_name;

Example • drop table student;

• All data structure and data


present in the table is deleted
Use • Can not add data again as
structure is removed.
4/21/2022 DBMS : III : Introduction to Structured Query Language 12
DDL Commands (Cont…) Truncate command
Syntax • TRUNCATE TABLE table_name;

Example • truncate table student;

• All data present in the table is deleted


and free the space containing the
Use table.
• You can again add data using same
data structure
4/21/2022 DBMS : III : Introduction to Structured Query Language 13
DDL Commands (Cont…) Rename command

•RENAME old_table_name to
Syntax
new_table_name;

Example •rename student to stud;

Use •To change the tablename

4/21/2022 DBMS : III : Introduction to Structured Query Language 14


DML Commands
• DML commands are used to modify the database.
• The command of DML is not auto-committed that means it can’t
permanently save all the changes in the database. They can be
rollback
• Commands come under DML
• SELECT
• INSERT
• UPDATE
• DELETE

4/21/2022 DBMS : III : Introduction to Structured Query Language 15


DML Commands (Cont…) Select
• To select all data from table
select * from student;
OR
select name, rno from student ;

• To select whole column from table


select name from student;

• To select data with condition


select name, rno from student where name=“Sam”;

• To select data with comparison condition


select name, rno from student where rno>10;
4/21/2022 DBMS : III : Introduction to Structured Query Language 16
DML Commands (Cont…) Select
• Column Alias
• Renames a column heading.
• Is useful with calculations
• Immediately follows the column name – there can also be the optional AS
keyword between the column name and alias.
• Requires double quotation marks if it contains space or special characters or
is case sensitive.
• e.g select last_name AS name from employees;

select last_name “Name” from employees;

4/21/2022 DBMS : III : Introduction to Structured Query Language 17


DML Commands (Cont…) Select Distinct
• The SQL DISTINCT command is used with SELECT key word to retrieve
only distinct or unique data.
• Suppressed dulicates
• E.g
select distinct name from student;

4/21/2022 DBMS : III : Introduction to Structured Query Language 18


DML Commands (Cont…) Select

Comparison Other Comparison


Logical Condition
Conditions Condition
• Grater than > • Is like • AND
• Less than < • Is not like • OR
• Not equal <> • Between AND • NOT
• Grater than equal >= • IN
• Less than equal <= • Not IN

4/21/2022 DBMS : III : Introduction to Structured Query Language 19


DML Commands (Cont…) Select
select name, marks from student where marks between 70 and 90;

select name, marks from student where marks not between 70 and 90;

select name, rno from student where rno in (5,6,7);

select name, rno from student where rno not in (5,6,7);

4/21/2022 DBMS : III : Introduction to Structured Query Language 20


DML Commands (Cont…) Select
• Using Like condition
• Search conditions can contain either literal characters or numbers.
• % denotes zero or many characters.
• _ denote one character.

select name from student where name like ‘s%’;

select name from student where name like ‘%s’;

select name from student where name like ‘_s%’;

select name from student where name like ‘s_’;

4/21/2022 DBMS : III : Introduction to Structured Query Language 21


DML Commands (Cont…) Select
• Using Null condition

select name, rno from student where name is null;

select name, rno from student where name is not null;

4/21/2022 DBMS : III : Introduction to Structured Query Language 22


DML Commands (Cont…) Select
• Logical AND

select name, address from student where id>=1 and name like ‘%E%’;

4/21/2022 DBMS : III : Introduction to Structured Query Language 23


DML Commands (Cont…) Insert
• Add new rows to a table by using the INSERT statement.
insert into table_name values(value1,value2,…valuen);
insert into student values(1,’Ram’);
• Only one row is inserted at a time with this syntax.
• Insert a new row containing values for each column.
• List values in the default order of the columns in the table
• Enclose character and date value within single quotation marks.

4/21/2022 DBMS : III : Introduction to Structured Query Language 24


DML Commands (Cont…) Update
• Modify existing rows with the UPDATE statement.
• Update more than one row at a time, If required.
update table set column =value where condition;

• Specific row or rows are modified if you specify the WHERE clause.
update student set address=“Mumbai” where rno=5;

• All rows in the table are modified if you omit the WHERE clause.
update student set address=“Mumbai”;
4/21/2022 DBMS : III : Introduction to Structured Query Language 25
DML Commands (Cont…) Delete
We can remove existing rows by using the DELETE statement.
delete from table where condition;

Specific rows are deleted if you specify the WHERE clause.


delete from student where rno=5;

All rows in the table are deleted if you omit the where clause.
delete from student;

4/21/2022 DBMS : III : Introduction to Structured Query Language 26


Drop, Truncate &Delete

4/21/2022 DBMS : III : Introduction to Structured Query Language 27


Data Control Language Commands
• Create User
Syntax: create user user_name identified by password;
Example: create user admin identified by admin@123;

• Grant
Syntax:grant privileges to user;
Example: grant create, select to admin;

• Revoke
revoke select on student from admin;

4/21/2022 DBMS : III : Introduction to Structured Query Language 28


Data Control Language Commands
Role: It is group of related privileges. It becomes easier to grant and
revoke privileges in bulk.
create role HOD;
grant create table, create view to HOD;
grant HOD to admin, clerk;

• Now, admin, clerk have create table, create view privileges.

4/21/2022 DBMS : III : Introduction to Structured Query Language 29


Transaction Control Language
• TCL commands can only use with DML commands like INSERT,DELETE
and UPDATE only
• These operations are automatically committed in the database that’s
why cannot be used while creating tables or dropping them
• Some commands that come under TCL
• COMMIT
• ROLLBACK
• SAVEPOINT

4/21/2022 DBMS : III : Introduction to Structured Query Language 30


Transaction Control Language - COMMIT
• COMMIT command is used to save all the transactions to the
database
• Syntax:
COMMIT;
• Example:
delete from student where rno=5;
commit;

4/21/2022 DBMS : III : Introduction to Structured Query Language 31


Transaction Control Language - ROLLBACK
• Rollback command is used to undo transaction that have not already
been saved to the database.
• Syntax:
ROLLBACK;
• Example:
delete from student where rno=5;
rollback;

4/21/2022 DBMS : III : Introduction to Structured Query Language 32


Transaction Control Language - SAVEPOINT
• It is used to roll the transactions back to a certain point without
rolling back the entire transaction.
• Syntax:
SAVPOINT savepoint_name;
• Example:
update student set address=“Mumbai” where rno=5;
savepoint s1;
delete from student where rno=5;
rollback to s1;

4/21/2022 DBMS : III : Introduction to Structured Query Language 33


Constraints
• Constraints are enforce rules at the table
• Types of constraints
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK

4/21/2022 DBMS : III : Introduction to Structured Query Language 34


Constraints – NOT NULL
• The NOT NULL constraints ensures that the column contains no null
values. It is defined at the column level.
create table employee(
Employee_id NUMBER(6),
Last_name VARCHAR2(25) NOT NULL);

4/21/2022 DBMS : III : Introduction to Structured Query Language 35


Constraints – UNIQUE
• Every value must be unique that is not same
create table employee(
emp_id number(6),
name varchar2(20) not null,
salary number(10),
email varchar2(20) unique);
• Apply constraint at Table level
create table employee(
emp_id number(6),
name varchar2(20) not null,
salary number(10),
email varchar2(20),
constraint emp_email_uk UNIQUE(email));
4/21/2022 DBMS : III : Introduction to Structured Query Language 36
Constraints – PRIMARY KEY
• Only one primary key can be created for each table.
• No column that is part of the primary key can contain a null value.

create table department(


department_id NUBER(4),
department_nameVARCHAR2(30),
constraint dept_id_pk_primary_key (department_id);

4/21/2022 DBMS : III : Introduction to Structured Query Language 37


Constraints – FOREIGN KEY
• Only one primary key can be created for each table.
• No column that is part of the primary key can contain a null value.

create table department(


department_id NUBER(4),
department_nameVARCHAR2(30),
constraint dept_id_pk_primary_key (department_id);

• FOREIGN KEY : Defines the column in the child table at the table constraint level.
• REFERENCES : Identifies the table and column in the parent table.
• ON DELETE CASCADE : Deletes the dependent rows in the child table when a row in the
parent table is deleted.
4/21/2022 DBMS : III : Introduction to Structured Query Language 38
Constraints – CHECK
• Defines a condition that each row must satisfy.
create table branch (
branch_name varchar(20),
branch_city varchar(20),
assets integer,
primary key (branchname),
check (bcity in ‘Pune’,’Mumbai’)
check (assets > 0));

4/21/2022 DBMS : III : Introduction to Structured Query Language 39


Set Operations

4/21/2022 DBMS : III : Introduction to Structured Query Language 40


Set Operations - Union
• Returns result from both queries without duplicates.
• The number of columns and their data types should be same.

Eg. select emp_id, job_id from employee


UNION
select emp_id, job_id from departments;

4/21/2022 DBMS : III : Introduction to Structured Query Language 41


Set Operations – UNION ALL
• All rows selected by either query, including all duplicates.
Eg. select emp_id, job_id from employee
UNION ALL
select emp_id, job_id from departments;

4/21/2022 DBMS : III : Introduction to Structured Query Language 42


Set Operations - MINUS
• All distinct rows that are selected by the first SELECT statement and
not selected in the second SELECT statement.
Eg. select emp_id from employee
MINUS
select emp_id from departments;

4/21/2022 DBMS : III : Introduction to Structured Query Language 43


String Operations

4/21/2022 DBMS : III : Introduction to Structured Query Language 44


Aggregate Functions (Cont…)
• Aggregate functions work on set of rows.
• It gives one result per group.
• Types of group functions: avg, count,max,min, stddev, sum,variance
• Order by clause arranges output of query either in ascending order or
descending order, by default order is ascending

4/21/2022 DBMS : III : Introduction to Structured Query Language 45


Aggregate Functions (Cont…)

4/21/2022 DBMS : III : Introduction to Structured Query Language 46


Aggregate Functions (Cont…)
select name, avg(salary), min(salary),max(salary) from employee where id=1;

select count(*) from employee;

Select student_id, avg(marks)


from students
group by student_id;

select avg(salary)
from departments
group by department_id;

4/21/2022 DBMS : III : Introduction to Structured Query Language 47


Group by Clause
• Divide rows in a table into smaller groups by using the GROUP BY
clause.
• We can’t use the column alias in the GROUP BY clause.
• Must include columns in the GROUP BY clause.

4/21/2022 DBMS : III : Introduction to Structured Query Language 48


Group by Clause (Cont…)
SELECT column,group_function(column)
FROM table
[WHERE condition]
[GROUP BY group by_expression]
[OREDER BY column];

SELECT department_id, AVG(salary)


FROM employees
GROUP BY department_id;

• All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.
SELECT AVG(salary)
FROM employees
GROUP BY department_id;
4/21/2022 DBMS : III : Introduction to Structured Query Language 49
Having Clause
• Rows are grouped
• The group function is applied
• Group matching the HAVING clause are displayed.
• If you include group function in a select clause you can not select
individual results unless the individual columns appears in group by
clause.
• Any column or expression in the select list that is not an aggregate
function must be in group by clause.
• Can’t use column alias in this clause.

4/21/2022 DBMS : III : Introduction to Structured Query Language 50


Having Clause (Cont…)
SELECT column,group_function
FROMtable
[WHERE condition]
[HAVING group_condition]
[ORDER BY column];

SELCT department_id, MAX (salary)


FROM employees
GROUP BY department_id
HAVING MAX(salary)>10000;
4/21/2022 DBMS : III : Introduction to Structured Query Language 51

You might also like