SQL
SUB-LANGUAGES/STATEMENTS/COMMANDS
-------------------------------------
===================================================================================
==================================================================
DDL(DATA DEFINITION LANGUAGE):
------------------------------
1)CREATE: create statement is used to create a table in the database.
---------
SYNTAX: create table table_name
(col_name1 datatype(size) constraint(s),
col_name2 datatype(size) constraint(s),
.....................................
.....................................
col_nameN datatype(size) constraint(s));
To copy the data from one table to another:
------------------------------------------
SYNTAX: create table table_name
as
select * from table_name;
2)ALTER: Alter statement is used to modify the table w.r.t columns.
--------
ADDING A COLUMN:
alter table table_name
add col_name datatype constraint(s);
RENAME A COLUMN:
alter table table_name
rename column old col_name to new col_name;
DELETE A COLUMN:
alter table table_name
drop column col_name;
3)RENAME: Rename statement is used to rename a table.
---------
SYNTAX: rename old table_name to new table_name;
4.TRUNCATE: Truncate is used to remove all records of a table at one shot. Truncate
makes a table empty. Truncate will not remove
-----------
the table from database instead it removes records from table.
syntax: truncate table table_name;
5)DROP: Drop statement is used to remove a table from database.
------
SYNTAX: drop table table_name;
IF DROP is applied on table, the table will be removed from database and gets
stored in BIN. To restore the table from BIN to DATABASE
we follow FLASHBACK.
flashback table table_name to before drop;
To remove the table PERMANENTLY from DATABASE without storing in BIN we use PURGE.
BEFORE DROP: drop table table_name purge;
AFTER DROP: purge table table_name;
===================================================================================
================================================================
DML (DATA MANIPULATION LANGUAGE)
--------------------------------
1)INSERT: INSERT statement is used to add records into the table.
---------
SYNTAX: insert into table_Name values(v1,v2,v3…);
or
insert into table_Name (col_name1,col_name2,…...) values(v1,v2,….);
2.UPDATE: UPDATE is used to update existing data in a table with newer data.
--------
SYNTAX: update table_name
set col_name=value
where condition(s);
update table_name
set col_name=VALUE,col_name=value..
where condition(s);
3)DELETE: DELETE is used to remove particular records from a table.
---------
SYNTAX:delete from table_name
where condition(s);
NOTE:ALL the actions performed using DDL statements is PERMANENT.
-----ALL the actions performed using DML statements is NOT PERMANENT.
===================================================================================
=============================================================
TCL(TRANSACTION CONTROL LANGAUGE): Since all the DML actions are not permanent, it
can be saved permanently using TCL
---------------------------------
1)ROLLBACK: Rollback statement is used to undo all the DML actions.
-----------
SYNTAX: rollback;
2)COMMIT:Commit statement is used to save all the DML actions as PERMANENT.
--------
SYNTAX: commit;
3)SAVEPOINT: Savepoint is a named point created for each and every DML actions.
------------
SYNTAX: savepoint savepoint_name;
===================================================================================
=============================================================
DCL (DATA CONTROL LANGUAGE):
---------------------------
DCL is mainly used to provide a permission or to take back a permission for
accessing data from one database user to another.
1)GRANT: Grant is mainly used to provide a permission for accessing of a data from
one database user to another.
--------
SYNTAX: grant permission_type on table_name to user_name;
2)REVOKE: Revoke is mainly used to take back a permission for accessing of a data
from one database user to another.
---------
SYNTAX: revoke permission_type on table_name from user_name;