0% found this document useful (0 votes)
80 views28 pages

Introduction To SQL: Dr. Sambit Bakshi

The document is a lecture on introduction to SQL by Dr. Sambit Bakshi from NIT Rourkela on April 8, 2020. It outlines what a database is, the different types of databases, and provides an introduction to database management systems and SQL. It describes the different types of SQL languages including DDL, DML, DCL, DQL and TCL and provides examples of SQL commands like CREATE, ALTER, SELECT and their usage.

Uploaded by

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

Introduction To SQL: Dr. Sambit Bakshi

The document is a lecture on introduction to SQL by Dr. Sambit Bakshi from NIT Rourkela on April 8, 2020. It outlines what a database is, the different types of databases, and provides an introduction to database management systems and SQL. It describes the different types of SQL languages including DDL, DML, DCL, DQL and TCL and provides examples of SQL commands like CREATE, ALTER, SELECT and their usage.

Uploaded by

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

Introduction to SQL

Dr. Sambit Bakshi

NIT Rourkela

April 8, 2020

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 1 / 28


Outline

1 What is Database?

2 Database Management System (DBMS)

3 Data Manipulation Language (DML)

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 2 / 28


What is Database?

What is Database?

Database is a organising collection of data


For example a database of a college would be having a
collection of data such as
Personal records of Students
Students performance history
Teachers data
Financial department data etc

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 3 / 28


What is Database?

Types of Databases

Basically there are two types of databases


Relational Database
Non-relational Database
Non-relational databases
Data is not organized in form of tables. Data is stored in
form of key and value pairs.
The examples of non-relational databases are: JSON and
XML.
N.B. We cannot interact with non-relational
databases using SQL
Relational Databases
In relational database, data is organized in form of tables. A
table contains rows and columns of data.
Table has a unique key to identify each row of the table.
Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 4 / 28
Database Management System (DBMS)

Database Management System (DBMS)

A database management system is a software application which


is used for managing different databases.
It helps us to create and manage database.
With the help of DBMS we take care following tasks.
Data Security.
Data Backup.
Manages huge amount of data.
Data export and import.
Serving multiple concurrent database requests.
Gives us a way to manage the data using programming languages.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 5 / 28


Database Management System (DBMS)

What is SQL ?

SQL stands for Structured Query Language, which is a standardised


language for interacting with RDBMS (Relational Database
Management System). Some of the popular relational database
example are: MySQL, Oracle etc.
SQL is used to perform C.R.U.D (Create, Retrieve, Update and
Delete) operations on relational databases.
SQL can also perform administrative tasks on database such as
database security, backup, user management etc.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 6 / 28


Database Management System (DBMS)

Types of SQL

SQL is a combination of the following different types of languages.


Data Definition Language (DDL)
Data Manipulate Language (DML)
Data Control Language (DCL)
Data Query Language (DQL)
Transaction Control Language (TCL)

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 7 / 28


Database Management System (DBMS)

Data Definition Language (DDL)

DDL is used to define the table schemas.


All DDL commands are auto-committed, which means it saves all
the changes permanently in the database.
Following are the DDL commands being used to define database
table.
Command Description
create to create new table or database
alter for alteration
truncate delete data from table
drop to drop a table
rename to rename a table

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 8 / 28


Data Manipulation Language (DML)

Data Manipulation Language (DML)

DML commands are used for manipulating the data stored in the
table and not the table itself.
DML commands are not auto-committed. It means changes are
not permanent to database, they can be rolled back.
The DML commands used are:
Command Description
insert to insert a new row
update to update existing row
delete to delete a row
drop to drop a table
merge merging two rows or two tables

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 9 / 28


Data Manipulation Language (DML)

Data Control Language (DCL)

Data control language are the commands to grant and take back
authority from any database user.
DCL commands in SQL are as follows:
Command Description
grant grant permission of right
revoke take back permission.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 10 / 28


Data Manipulation Language (DML)

Data Query Language (DQL)

DQL is used to fetch the information from the database which is


already stored there..
Select is the only one DQL commands used in SQL.
Command Description
select retrieve records from one or more table

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 11 / 28


Data Manipulation Language (DML)

Transaction Control Language (TCL)

These commands are to keep a check on other commands and


their affect on the database.
These commands can annul changes made by other commands
by rolling the data back to its original state.
It can also make any temporary change permanent.
TCL commands used in SQL are.
Command Description
commit to permanently save
rollback to undo change
savepoint to save temporarily

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 12 / 28


Data Manipulation Language (DML)

Datatypes used in SQL

In SQL, data types define what type of data a column can


contain.
Following are the few widely used datatypes in SQL.
Char.
Varchar
Boolean
Int
Real
Float
Double
Text
Date

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 13 / 28


Data Manipulation Language (DML)

SQL Syntax

As data need be stored in database in organized manner, there is


a requirement of SQL queries or statements to perform different
operations in the database.
SQL Statement:
SQL statement tells the database that what information you would like
to retrieve or what operation you want to perform on the data.
SQL statements are NOT case sensitive, but it can be made case
sensitive.
Semicolon at the end of the statement.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 14 / 28


Data Manipulation Language (DML)

Create Command

create is a DDL SQL command used to create a table or a


database in relational database management system.
Syntax: create database ¡database name¿: for creating
database
create table ¡table name¿
(
column name data type1,
column name data type2,
);
create table command will tell the database system to create a
new table with the given table name and column information.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 15 / 28


Data Manipulation Language (DML)

Create Command

Example :
CREATE TABLE Student
(
student id INT,
name VARCHAR(100),
age INT
);
The above command will create a table named Student in the
current database with 3 columns, namely student id, name, age
of datatypes integer, varchar that can hold upto 100 characters
and integer respectively.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 16 / 28


Data Manipulation Language (DML)

Alter Command

alter command is used for altering the table structure, such as


To add a column to existing table.
To rename any existing column or table name.
To change datatype of any column or to modify its size.
To drop a column from the table.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 17 / 28


Data Manipulation Language (DML)

Alter Command: Add new column

alter command is used for altering the table structure, such as


Using ALTER command we can add a column to any
existing table
Syntax: ALTER TABLE table name ADD(column name
datatype);
Example:ALTER TABLE student ADD(address
VARCHAR(200));

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 18 / 28


Data Manipulation Language (DML)

ALTER Command: Add multiple new Columns

alter command is used for altering the table structure, such as


ALTER TABLE table name ADDD(
column name1 datatype1,
column name2 datatype2,
column name3 datatype3
);
Example: ALTER TABLE student ADD(
father name VARCHAR(60),
mother name VARCHAR(60),
dob DATE);
The above command will add three new columns to the student
table.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 19 / 28


Data Manipulation Language (DML)

ALTER Command: Add multiple new Columns

ALTER command can add a new column to an existing table


with a default value too. The default value is used when no value
is inserted in the column.
Syntax: ALTER TABLE table name ADD(column-name1
datatype1 DEFAULT some value);
Example: ALTER TABLE student ADD(dob DATE DEFAULT
’01-Jan-99’);
The above command will add a new column with a preset default
value to the table student.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 20 / 28


Data Manipulation Language (DML)

ALTER Command: Modify an existing Column

ALTER command can also be used to modify data type of any


existing column.
Syntax: ALTER TABLE table name modify column name
datatype;
Example: ALTER TABLE student MODIFY address
varchar(300);
The above command will modify the address column of the
student table, to now hold up to 300 characters.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 21 / 28


Data Manipulation Language (DML)

ALTER Command: Rename a Column

Syntax: ALTER TABLE TABLE NAME RENAME


COLUMN NAME1 TO COLUMN NAME2;
ROLL NO NAME AGE
1 Ram 20
2 Abhi 21
ALTER TABLE Student RENAME COLUMN NAME TO
FIRST NAME;
ROLL NO FIRST NAME AGE
1 Ram 20
2 Abhi 21

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 22 / 28


Data Manipulation Language (DML)

AlterCommand: Drop and Modify Usage

Drop command : DROP COLUMN is used to drop column in a


table;
Syntax: ALTER TABLE table name DROP COLUMN
column name;
Modify command : It is used to modify the existing columns in a
table.
Syntax: ALTER TABLE table name MODIFY column name
column type;

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 23 / 28


Data Manipulation Language (DML)

DROP, TRUNCATE

DROP : delete a whole database or just a table.


Syntax: DROP object object name
where, an object can be a table or a database
TRUNCATE : is used to mark the extents of a table for
deallocation (empty for reuse).
Syntax: TRUNCATE TABLE table name;

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 24 / 28


Data Manipulation Language (DML)

DML: Insert Into command

The INSERT INTO statement of SQL is used to insert a new row


in a table.
Syntax:
INSERT INTO table name VALUES (value1, value2, value3,. . . );
table name: name of the table.
Syntax: TRUNCATE TABLE table name;value1, value2,.. :
value of first column, second column,. . . for the new record.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 25 / 28


Data Manipulation Language (DML)

DML: update

The UPDATE statement in SQL is used to update the data of an


existing table in database.
Syntax: UPDATE table name SET column1 = value1, column2
= value2,WHERE condition;
table name: name of the table column1: name of first , second,
third column.
value1: new value for first, second, third column.
condition: condition to select the rows for which the values of
columns needs to be updated.
WHERE clause is used to select the rows for which the columns
are.

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 26 / 28


Data Manipulation Language (DML)

DML: update (continues. . . )

Student
ROLL NO FIRST NAME AGE
1 Ram 20
2 Abhi 21
Example: UPDATE Student SET NAME = ’PRATIK’ WHERE
Age = 20;
ROLL NO FIRST NAME AGE
1 Pratik 20
2 Abhi 21

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 27 / 28


Data Manipulation Language (DML)

References

https://beginnersbook.com/2018/11/introduction-to-sql/
https://www.studytonight.com/dbms/alter-query.php
https://unacademy.com/lesson/introduction-to-sql-and-its-
syntaxinhindi/AHO6REIJ

Dr. Sambit Bakshi (NIT Rourkela) Introduction to SQL April 8, 2020 28 / 28

You might also like