2.SQL Statements
2.SQL Statements
------------------------
CREATE, ALTER, DROP, TRUNCATE, RENAME
Types of SQL ?
These commands when executed are auto commit in nature and all the changes in the
table are reflected and saved immediately
* Once the tables are created and database is generated using DDL commands,
manipulation inside those tables and databases is done using DML commands.
The advantage of using DML commands is, if in case any wrong changes or values are
made, they can be changes and rolled back easily
* DCL commands as the name suggests manages the matters and issues related to the
data control in any database.
DCL commands mainly provides special privilege access to users and is also used
to specify the roles of users accordingly.
There are two commonly used DCL commands, these are:Grant & Revoke
SELECT command in combination with other SQL clauses is used to retrieve and
fetch data from database/tables on the basis of certain conditions applied by user.
* Transaction Control Language as the name suggests manages the issues and matters
related to the transactions in any database.
* Roll back means “Undo” the changes and Commit means “Applying” the changes.
DDL
Data Definition Language (DDL) statements are used to define the database structure
or
schema. Some examples:
Statement Description Example
CREATE To create objects in the database
CREATE TABLE Persons(
PersonID NUMBER(5),
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255));
ALTER Alters the structure of the database ALTER TABLE PersonsADD
DateOfBirth DATE
DROP Delete objects from the database DROP TABLE Persons
TRUNCATE
Remove all records from a table,
including all spaces allocated for
the records are removed
TRUNCATE TABLE Persons
RENAME Rename an object RENAME Persons TO PER
DML
Data Manipulation Language (DML) statements are used for managing data within
schema
objects. Some examples:
Statement Description Example
SELECT Retrieve data from the a database SELECT * FROM Customers
INSERT Insert data into a table
INSERT INTO Customers
(CustomerName, Country) VALUES
(‘L&T’,’INDIA’);
UPDATE Updates existing data within a table
UPDATE Customers
SET ContactName='Alfred Schmidt',
City='Hamburg';
DELETE
Deletes all records from a table, the
space for the records remain
DELETE FROM Customers WHERE
CustomerName='Alfreds Futterkiste'
AND ContactName='Maria Anders';
Page | 15
DCL
Data Control Language (DCL) statements. Some examples:
Statement Description Example
GRANT Gives user's access privileges to
database
GRANT SELECT, INSERT, UPDATE,
DELETE ON Customer TO rmver14;
REVOKE
Withdraw access privileges given
with the GRANT command
REVOKE DELETE ON Customer FROM
rmver14;
TCL
Transaction Control (TCL) statements are used to manage the changes made by DML
statements. It allows statements to be grouped together into logical transactions.
Statement Description Example
COMMIT Save work done COMMIT;
SAVEPOINT
Identify a point in a transaction to
which you can later roll back
SAVEPOINT SP1;
SAVEPOINT SP2;
SAVEPOINT SP3;
ROLLBACK
Restore database to original since
the last COMMIT ROLLBACK SP2;
SET
TRANSACTION
Change transaction options like
isolation level and what rollback
segment to use
SET TRANSACTION READ ONLY;
SET TRANSACTION READ WRITE