Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Course : 20CS401- Database Management System

Module : Data Modeling and Relational Query Language

Topic : SQL Operations

Faculty : Sajeev Ram. A


SQL
 SQL is Structured Query Language, which is a computer language for storing, manipulating and

retrieving data stored in a relational database.

 SQL was the first commercial language introduced for E.F Codd’s Relational model.

 Today almost all RDBMS ( MySql, Oracle, Infomix, Sybase, MS Access) uses SQL as the standard

database language .

 Based on relational algebra, but not entirely identical.


– Relations  Tables
– Tuples  Rows
– Attributes  Columns
SQL - Categories
SQL

DDL DML TCL DCL

Drop Select Commit Grant

Rename Insert Rollback Revoke

Create Update Savepoint

Alter Delete

Truncate Merge
SQL - DDL

 Data Definition Language (DDL) statements are used to define the database structure or
schema.

 It simply deals with descriptions of the database schema and is used to create and modify the
structure of database objects in the database

 All DDL commands are auto-committed. That means it saves all the changes permanently in
the database.
SQL - DDL
SQL | DDL | CREATE
There are two CREATE statements available in SQL
1. CREATE DATABASE
2. CREATE TABLE

CREATE DATABASE
A Database is defined as a structured set of data. So, in SQL the very first step to store the data in a well structured
manner is to create a database.
The CREATE DATABASE statement is used to create a new database in SQL.
Syntax
Example Query:
CREATE DATABASE database_name; CREATE DATABASE my_database;
Where database_name: name of the database. This query will create a new database in SQL and
name the database as my_database
SQL - DDL
SQL | DDL | CREATE
CREATE TABLE
 The CREATE TABLE statement is used to create a table in SQL

 Table comprises of rows and columns

 While creating tables we have to provide all the information to SQL about the names of the columns, type of data to be stored
in columns, size of the data etc.
Example Query:
Syntax:
CREATE TABLE Students
CREATE TABLE table_name
(
(
ROLL_NO int(3),
column1 data_type(size),
NAME varchar(20),
column2 data_type(size), SUBJECT varchar(20),
column3 data_type(size), );
This query will create a table named Students with three
....
columns, ROLL_NO, NAME and SUBJECT.
);
SQL - DDL
SQL | DDL | DROP
 DROP is used to delete a whole database or just a table.

 The DROP statement destroys the objects like an existing database, table, index, or view.

 Table or Database deletion using DROP statement cannot be rolled back

Syntax:
DROP object object_name

Example: (drop a database) Example: (drop a table)


DROP DATABASE database_name; DROP TABLE table_name;
database_name: Name of the DB to be deleted. table_name: Name of the table to be deleted.
SQL - DDL
SQL | DDL | Truncate
 TRUNCATE statement is used to mark the extents of a table for deallocation (empty for reuse).

 The result of this operation quickly removes all data from a table, typically bypassing a number of integrity enforcing
mechanisms.
 Truncate >> delete all the tuples in a relation (keeps the structure)

Syntax:
TRUNCATE TABLE table_name;

Example:
TRUNCATE TABLE Student_details;
After running the above query Student_details table will be truncated, i.e, the
data will be deleted but the structure will remain in the memory for further
operations.
SQL - DDL
SQL | DDL | ALTER (ADD, DROP, MODIFY)
 ADD is used to add columns into the existing table

Syntax:
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,

Columnname_n datatype);
Example: (ADD 2 columns AGE and COURSE to table
Student)
ALTER TABLE Student
ADD (AGE number(3),
COURSE varchar(40));
SQL - DDL
SQL | DDL | ALTER (ADD, DROP, MODIFY)
 DROP COLUMN is used to drop column in a table

 Deleting the unwanted columns from the table

Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

Example: (Delete the columns COURSE from the table


Student)
ALTER TABLE Student DROP COLUMN COURSE;
SQL - DDL
SQL | DDL | ALTER (ADD, DROP, MODIFY)
 MODIFY is used to modify the existing columns in a table.

 Multiple columns can also be modified at once.

Syntax:
ALTER TABLE table_name
MODIFY column_name column_type;

Example: (Reduce the size columns COURSE from the table


Student (40 to 20)
ALTER TABLE Student
MODIFY COURSE varchar(20);
SQL - DDL
SQL | DDL | RENAME
 RENAME is used to Rename our table to give it a
more relevant name

Syntax: (Rename a Table)


ALTER TABLE table_name
RENAME TO new_table_name;

Syntax: (Rename a Column)


Example: (Reduce the size columns COURSE from the table
ALTER TABLE table_name
Student (40 to 20)
RENAME COLUMN old_name TO new_name;
ALTER TABLE Student
RENAME COLUMN NAME TO FIRST_NAME;
SQL - DML

 The SQL commands that deals with the manipulation of data present in the database belong to
Data Manipulation Language (DML)

 We could not change the structure of the table using DML


SQL - DML
SQL | DML | SELECT
 The SELECT Statement in SQL is used to retrieve or fetch data from a database
 We can fetch either the entire table or according to some specified rules
 The data returned is stored in a result table
 This result table is also called result-set

Syntax: (to select specific columns from table)


SELECT column1,column2 FROM table_name

Syntax: (to select all columns from table)


SELECT * FROM table_name
Example: (To fetch the fields ROLL_NO, NAME, AGE from
the table Student
SELECT ROLL_NO, NAME, AGE FROM Student;
SQL - DML
SQL | DML | INSERT
 The INSERT INTO statement of SQL is used to insert a new row in a table
 There are two ways of using INSERT INTO statement for inserting rows:
 Only values
 Column names and values both

Syntax: (Only values) Example: (To fetch the fields ROLL_NO, NAME, AGE from
INSERT INTO table_name the table Student
VALUES (value1, value2, value3,…); INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST
BENGAL’,’XXXXXXXXXX’,’19’)
Syntax: (Column names and values both)
INSERT INTO table_name (column1, column2,..)
VALUES ( value1, value2,..);
SQL - DML
SQL | DML | INSERT

Example:
INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST BENGAL’,’XXXXXXXXXX’,’19’)
SQL - DML
SQL | DML | INSERT

Example:
INSERT INTO Student (ROLL_NO, NAME, Age) VALUES (‘5′,’PRATIK’,’19’);
SQL - DML
SQL | DML | INSERT

Inserting all columns of a table


Syntax
INSERT INTO first_table SELECT * FROM second_table;
Eg: INSERT INTO Student SELECT * FROM LateralStudent;

Inserting specific columns of a table


Syntax
INSERT INTO first_table(names_of_columns1) SELECT names_of_columns2 FROM second_table;
Eg: INSERT INTO Student(ROLL_NO,NAME,Age) SELECT ROLL_NO, NAME, Age FROM LateralStudent;

Copying specific rows from a table


INSERT INTO table1 SELECT * FROM table2 WHERE condition;
Eg: INSERT INTO Student SELECT * FROM LateralStudent WHERE Age = 18;
SQL - DML
SQL | DML | INSERT

Inserting multiple rows in a table


Syntax
INSERT INTO table_name(Column1,Column2,Column3,.......)
VALUES (Value1, Value2,Value3,.....),
(Value1, Value2,Value3,.....),
(Value1, Value2,Value3,.....),
............................. ;

INSERT INTO STUDENT(ID, NAME,AGE,GRADE,CITY)


VALUES(1,"AMIT KUMAR",15,10,"DELHI"),
(2,"GAURI RAO",18,12,"BANGALORE"),
(3,"MANAV BHATT",17,11,"NEW DELHI"),
(4,"RIYA KAPOOR",10,5,"UDAIPUR");
SQL - DML
SQL | DML | UPDATE
 UPDATE statement is used to update the data of an existing table in database
 We can update single columns as well as multiple columns using UPDATE statement as per our requirement.

Syntax
UPDATE table_name SET column1 = value1, column2 = value2,...
WHERE condition;

UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;


SQL - DML
SQL | DML | UPDATE

Eg: (UPDATE Multiple Columns)


UPDATE Student SET NAME = 'PRATIK', ADDRESS = 'SIKKIM' WHERE ROLL_NO = 1;
SQL - DML
SQL | DML | D E L E T E
 The DELETE Statement in SQL is used to delete existing records from a table.
 We can delete a single record or multiple records depending on the condition we specify in the WHERE
clause.

Syntax:
DELETE FROM table_name WHERE some_condition;

Eg: (Deleting Single Record)


DELETE FROM Student WHERE NAME = 'Ram';
SQL - DML
SQL | DML | D E L E T E

Eg: (Deleting Multiple Records)


DELETE FROM Student WHERE Age = 20;

Eg: (Deleting All the Records)


DELETE * FROM Student;
SQL - DML
SQL | DML | MERGE
 MERGE Statement is a combination of three INSERT, DELETE and UPDATE statements
 If there is a Source table and a Target table that are to be merged, then with the help of MERGE
statement, all the three operations

Example
SQL - TCL

 Transaction Control Language

 TCL commands deal with the transaction within the database


 COMMIT– commits a Transaction.

 ROLLBACK– rollbacks a transaction in case of any error occurs.

 SAVEPOINT–sets a savepoint within a transaction.


SQL - DCL

 Data Control Language

 DCL includes commands such as GRANT and REVOKE which mainly deal with the rights,
permissions and other controls of the database system.
 GRANT-gives user’s access privileges to the database

 REVOKE-withdraw user’s access privileges given by using the GRANT command


SQL - DCL
SQL | DCL| GRANT
 SQL GRANT is a command used to provide access or privileges on the database objects to the users.

 privilege_name is the access right or privilege granted to the user. Some


Syntax
of the access rights are ALL, EXECUTE, and SELECT.
GRANT privilege_name  object_name is the name of an database object like TABLE, VIEW,
ON object_name
STORED PROC and SEQUENCE.
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];  user_name is the name of the user to whom an access right is being
granted.
 PUBLIC is used to grant access rights to all users.
 ROLES are a set of privileges grouped together.
 WITH GRANT OPTION - allows a user to grant access rights to other
users.
SQL - DCL
SQL | DCL| REVOKE
 The REVOKE command removes user access rights or privileges to the database objects

 privilege_name is the access right or privilege granted to the user. Some


Syntax
of the access rights are ALL, EXECUTE, and SELECT.
REVOKE privilege_name  object_name is the name of an database object like TABLE, VIEW,
ON object_name
STORED PROC and SEQUENCE.
FROM {user_name |PUBLIC |role_name}
 user_name is the name of the user to whom an access right is being
granted.
 PUBLIC is used to grant access rights to all users.
 ROLES are a set of privileges grouped together.
 WITH GRANT OPTION - allows a user to grant access rights to other
users.
Video Lectures

You might also like