0% found this document useful (0 votes)
23 views

SQL Queries

Uploaded by

mugeshmsd5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

SQL Queries

Uploaded by

mugeshmsd5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Unit- II

SQL
Structured query language
• SQL is a programming language for storing and
processing information in a relational database.

• A relational database stores information in tabular form,


with rows and columns representing different data
attributes and the various relationships between the data
values.

• Structured query language (SQL) is a popular query


language that is frequently used in all types of
applications.
History of SQL
• SQL was invented in the 1970s based on the relational
data model.
• It was initially known as the structured English query
language (SEQUEL).
• The term was later shortened to SQL.

• Some common relational database management systems


that use SQL are: Oracle, Sybase, Microsoft SQL Server
• Structured query language (SQL) implementation
involves a server machine that processes the database
queries and returns the results.
• The American National Standards Institute (ANSI)
and International Organization for Standardization
(ISO) adopted the SQL standards in 1986.
Database Languages

• Data Definition Language (DDL)

• Data Manipulation Language (DML)

• Data Control Language (DCL)

• Transaction Control Language (TCL)

• Data Query Language (DQL)


• DDL is used to define database structure
• Create: It is used to create objects in the database.

• Alter: It is used to alter the structure of the database.

• Drop: It is used to delete objects from the database.

• Truncate: It is used to remove all records from a table.

• Rename: It is used to rename an object.


• DML is used for accessing and manipulating data in a
database.
• Insert: It is used to insert data into a table.

• Update: It is used to update existing data within a table.

• Delete: It is used to delete all records from a table.


• DCL is used to retrieve the stored or saved data.
• Grant: It is used to give user access privileges to a database.

• Revoke: It is used to take back permissions from the user.


• TCL is used to run the changes made by the DML statement.
• Commit: It is used to save the transaction on the database.
• Rollback: It is used to restore the database to original since the last Commit.
• Savepoint: It is used to roll the transaction back to a certain point without rolling
back the entire transaction.
• DQL – It is used to retrieve data from a database.

• Select - It is used to select the attribute based on the condition described by WHERE
clause.
1. Data Definition Language (DDL)
• DDL are normally not used by end-users.
• The syntax of the SQL CREATE
DATABASE statement is:

• CREATE DATABASE DB_NAME;


• To create a new table :

• CREATE TABLE table_name ( column_1


DATATYPE, column_2 DATATYPE, column_n
DATATYPE );
• create a backup table from the existing table
Customers

• CREATE TABLE CustomersBackup AS SELECT *


FROM Customers;
Create a table with a Primary Key
• CREATE TABLE Companies ( id int, name
varchar(50), address text, email varchar(50),
phone varchar(10), PRIMARY KEY (id) );
• To delete an existing table :

• DROP TABLE table_name;


• Drop table student;
• To add new column(s) in a table :

• ALTER TABLE table_name ADD ( column_1 DATATYPE,

column_2 DATATYPE, column_n DATATYPE );

• Alter table guru99 add subject varchar;

• To change the datatype of a column in a table :

• ALTER TABLE table_name MODIFY column_name DATATYPE;

• To remove a column from a table :

• ALTER TABLE table_name DROP COLUMN column_name;


• delete country column from Customers table
• ALTER TABLE Customers DROP COLUMN
country;
• add phone column to Customers table
• ALTER TABLE Customers ADD phone
varchar(10);
• add phone and age columns to Customers
table
• ALTER TABLE Customers ADD phone
varchar(10), age int;
• To remove data present inside a table :

• TRUNCATE TABLE table_name;


• TRUNCATE table students;
• To rename a table :

• RENAME old_table_name TO new_table_name;

• To rename a column of a table :

• ALTER TABLE table_name RENAME COLUMN


old_Column_name to new_Column_name;
• rename Customers table to New_customers

• ALTER TABLE Customers RENAME TO


New_customers;
2. Data Manipulation Language (DML)
• DML statements deal with the user requests.
• To fetch an entire table :
• SELECT * FROM table_name;

• To fetch particular columns from a table :


• SELECT column_1, column_2 FROM
table_name;
• To fetch particular columns from a table
based on a condition:

• SELECT column_1, column_2 FROM


table_name WHERE <condition>;

• SELECT FirstName FROM Student WHERE


RollNo > 15;
• select first_name from Customers table
• SELECT first_name FROM Customers;

• select first_name and last_name columns


from Customers table
• SELECT first_name, last_name FROM
Customers;
• select all columns from Customers table
SELECT * FROM Customers;
• select all columns from the customers table
with last_name 'Doe' SELECT * FROM
Customers WHERE last_name = 'Doe';
• select age and country columns from
customers table where the country is 'USA'
• SELECT age, country FROM Customers WHERE
country = 'USA';
• select all columns from Customers table with
first name 'John'
• SELECT * FROM Customers WHERE first_name
= 'John';
• select all columns from Customers table with
age greater than 25
• SELECT * FROM Customers WHERE age > 25;
• select all columns from Customers table with
last_name 'Doe' and country 'USA'
• SELECT * FROM Customers WHERE last_name
= 'Doe' AND country = 'USA';
SQL Subquery
• use a subquery to select the first name of
customer -- with the highest age
• SELECT first_name FROM Customers WHERE
age= (SELECT MAX(age) FROM CUSTOMERS );
• select all the rows from the Customers table
-- with the minimum age
• SELECT * FROM Customers WHERE age =
( SELECT MIN(age) FROM Customers );
• To insert values according to the table
structure :
• INSERT INTO table_name VALUES value1,
value2, value3;
• To insert values based on the columns :
• INSERT INTO table_name column1, column2,
column3 VALUES value1, value2, value3;
• INSERT INTO students (RollNo, FIrstName,
LastName) VALUES ('60', 'Tom', Erichsen');
• insert a row in the Customers table

• INSERT INTO Customers(customer_id,


first_name, last_name, age, country) VALUES
(7, 'Ron', 'Weasley', 31, 'UK');
• To update the columns of a table based on a
condition (General UPDATE statement) :

• UPDATE table_name SET column_1 = value1,


column_2 = value2, column_3 = value3,
[WHERE condition]

• UPDATE students SET FirstName = 'Jhon',


LastName= 'Wick' WHERE StudID = 3;
• To delete rows from a table based on a
condition :

• DELETE FROM table_name [WHERE


condition];

• DELETE FROM students WHERE FirstName =


'Jhon';
• delete a row from the Customers table if
its customer_id is 4.
• DELETE FROM Customers WHERE customer_id
= 4;
3. Data Control Language (DCL)
• The user privileges include ALL, CREATE,
SELECT, INSERT, UPDATE, DELETE, EXECUTE,
etc.
• DCL commands are transactional.
• To grant user privilege to specified users on a
database object :

• GRANT <privilege> ON <object> TO user1,


user2;

• GRANT SELECT ON Users TO'Tom'@'localhost;


• To revoke user privilege to specified users on
a database object :

• REVOKE <privilege> ON <object> FROM user1,


user2;

• REVOKE SELECT, UPDATE ON student FROM


BCA, MCA;
4. Transaction Control Language (TCL)
• DBMS software implicitly uses the COMMIT
command before and after every DDL
command to save the change permanently in
the database.

• COMMIT;

• DELETE FROM Students WHERE RollNo =25;


COMMIT;
• To undo a group of transactions since last
COMMIT or SAVEPOINT

• ROLLBACK;
• To create a SAVEPOINT :

• SAVEPOINT savepoint_name;
• SAVEPOINT RollNo;

• To release a SAVEPOINT :

• RELEASE SAVEPOINT savepoint_name;


• To enable the AUTOCOMMIT process:
• SET AUTOCOMMIT = 1 ;

• To disable the AUTOCOMMIT process:


• SET AUTOCOMMIT = 0;
• AUTOCOMMIT :
• It is used to enable/disable the auto-commit
process that commits each transaction after
its execution.
SQL DELETE vs. TRUNCATE

SQL DELELTE SQL TRUNCATE

SQL DELETE supports the WHERE SQL TRUNCATE doesn't support


clause. the WHERE clause.

SQL DELETE can remove single,


SQL TRUNCATE can only remove
multiple, or all rows/records from
all the records from a table.
a table.
S.No DROP TRUNCATE
1. It is used to eliminate the whole It is used to eliminate the tuples from the
database from the table. table.

2. Integrity constraints get removed in the Integrity constraint doesn’t get removed in
DROP command. the Truncate command.

3. The structure of the table does not exist. The structure of the table exists.

4. Here the table is free from memory. Here, the table is not free from memory.

5. It is slow as compared to the It is fast as compared to the DROP


TRUNCATE command. command.
ALTER Command UPDATE Command
It is a DDL language (Data Definition It is a DML language (Data Manipulation
Language) Language)

ALTER command will perform all the actions UPDATE command will perform all the
in the table at a structural level. actions in the table at the data level.

It is used for adding, deleting, and modifying It is used for updating the data (records) in
attributes of the table in the database. the existing table.

By default, all the values in the tuple are


It sets the specified value to the tuple if this
initialized as NULL if the ALTER command is
command is used.
used.

Changes are made to the data inside the


Changes are made to the table structure.
table.

Usage: Structure of a table, name of table,


Usage: change data of row or column.
functions, etc.

Example: ALTER TABLE Movies ADD COLUMN Example: UPDATE Movies SET Movierate = 4
Movierate int(5); WHERE Moviename='Spiderman';

You might also like