Chapter 3 SQL FUNDAMENTALS
Chapter 3 SQL FUNDAMENTALS
FUNDAMENTALS SQL
AMOUD UNIVERSITY
FACULTY OF COMPUTING AND ICT
JUNIOR CLASSES –BCS IT
Fundamentals SQL
Structure Query Language (SQL) is a language used for storing and managing data in RDBMS.
SQL was the first commercial language introduced for E.F.T Codd's Relational model.
Today almost all RDBMS (MySQL, Oracle, Informix, Sybase, and MS Access) uses SQL as the standard
database language. SQL is used to perform all type of data operations in RDBMS.
Types of commands in SQL
(Data Definition Language) has 7 or more commands
DDL Create, Alter, Drop, Truncate, Rename, Flashback, Purge
• Syntax :-
• CREATE TABLE <TABNAME>
• AS SELECT STATEMENT [WHERE <cond>];
• Example :-
• Create table emp11 from table emp ?
• CREATE TABLE emp11 AS SELECT * FROM emp;
• After executing above command a new table is created called emp11 from the
result of SELECT Statement
Copying only structure :-
• Create new table emp12 from emp and into the new table copy only structure
but do not copy data?
CREATE TABLE emp12 AS SELECT * FROM emp WHERE 1=2;
• Because no record in emp table satisfies condition 1=2 , so no record is copied
to EMP12 only the structure is copied.
• Create table emp2 AS select * from emp where deptno = 30;
•
2. ALTER
• Droping a Column:- -> used to drop un-used columns from the table permanently.
• Syntax :-
• ALTER TABLE <Tablename> DROP COLUMN COLNAME ;
• Example :-
• ALTER TABLE Emp DROP COLUMN Dob;
• ALTER TABLE Emp DROP (Ename,sal);
• NOTE :- All Columns In A Table Cannot Be Dropped , Because The Table Should Contain At Least One
Column..
• Ex: Alter Table Emp7 Drop Column Job;
D. Rename(Column Name):
• Oracle flashback feature allows recovering a table after drop.When a table is dropped oracle moves it to the recyclebin rather than
actually droping it.
• A Recycle Bin is logical collection of dropped objects. The contents of Recycle Bin is viewed by using SHOW RECYCLEBIN
command.
• Flashback command is introduced in ORACLE 10g , which is used to restore a table after drop.
• Example :-
• SQL>DROP TABLE EMP ; (table is moved to recyclebin)
• To restore the table EMP issue the following command
• SQL>FLASHBACK TABLE EMP TO BEFORE DROP;
• A table can be restored with a new name.
• SQL>FLASHBACK TABLE EMP TO BEFORE DROP RENAME TO EMPL;
PURGE command :-
• PURGE command releases space occupied by the object in recyclebin .
• SQL>PURGE TABLE EMP ;
• After purging the table FLASHBACKING is not possible To empty the RECYCLEBIN issue the following command
• SQL>PURGE RECYCLEBIN ;
• The table can be dropped without being sent to the RECYCLEBIN. To do so issue the following command.
• SQL>DROP TABLE EMP PURGE ;
ENABLING & DISABLING RECYCLEBIN :-
• Recyclebin can be enabled & disabled.
• By default recyclebin is enabled.
• When it is enabled the dropped objects are placed in recyclebin.
• When it is disabled the dropped objects are not placed in recyclebin.
• To disable the recyclebin issue the following command
• SQL>ALTER SESSION SET RECYCLEBIN = OFF
• To enable the Recyclebin issue the following command
• SQL> ALTER SESSION SET RECYCLEBIN =ON
OCA question :-
•Evaluate the following SQL statements in the given order:
•DROP TABLE dept;
•CREATE TABLE dept
•(deptno NUMBER(3) PRIMARY KEY,
•deptname VARCHAR2(10));
•DROP TABLE dept;
•FLASHBACK TABLE dept TO BEFORE DROP;
Which statement is true regarding the above FLASHBACK operation?
•A. It recovers only the first DEPT table.
•B. It recovers only the second DEPT table.
•C. It does not recover any of the tables because FLASHBACK is not possible in this case.
•D. It recovers both the tables but the names would be changed to the ones assigned in the RECYCLEBIN.
DATA MANIPULATION LANGUAGE
• Data manipulation language : used to manipulate Existing the data.
• means performing operations on the data in the tables of the database.
• We perform mainly 3 types of data manipulation operations. They are
A. Insertion
B. Update
C. Deletion
INSERT COMMAND
INSERT command: used to insert new rows /records into the existing table
• -> it has two syntaxes.
• a. inserting values into all columns
• insert into <table_name>values (value1,value2,.......);
• 1.Insert into student values(101,’Arun’,60);
• b. inserting values into required columns
• Syntax:b
• insert into <table_name>(column1,column2,.....)
• values(value1, value2,.......);Example :-
• 2. insert into Emp007(Empno,Ename) values(&Empno,'&ename');
• Note: for varchar to value we need enclose with single (‘ ‘) quotes .
Inserting NULL values :-
• Use the Insert Statement to Add records to existing Tables.
• NULL values are inserted when value is
• Unknown
• Not Applicable
• NULL is not equal to 0 and not equal to space
• Note: Null is a keyword which is used to represent unavailable or undefined
symbol.
• SQL Buffer:
-> it is a temporary buffer, used to hold last excuted query.
• -> to execute Buffer's query press '/' operator on Sql Prompt
UPDATE COMMAND:
• UPDATE : -> used to modify existing column values.
• This command is used to modify the data in the existing table By using this command we
can modify all the records in the table & also specific records in the table (Using „where‟
clause)
• Update statement is used to update rows in existing tables which is in your own schema or
if you have update privilege on them.
• Syntax
• UPDATE <TABLENAME> SET <COL>=VALUE/EXPRESSION [WHERE <CONDITION>];
• EG: UPDATE FRIENDS SET NAME = UPPER(NAME);
• UPDATE FRIENDS SET PHONE=9876543210 WHERE SNO=1;
• Syntax change for more than one data simultaneously
• Syntax:
UPDATE <TABLE NAME> SET COL1=VALUE,
COL2=VALUE………COLN=VALUE;
• Ex: UPDATE EMP SET EID=007,SAL=10000;
iii. DELETE command :-
• DELETE command: -> used to delete all rows or specified rows from table Temporarily.
• -> Structure is present.
• Syntax:-
• DELETE FROM <TABNAME> [WHERE <cond> ----] ;
example
Delete all employee records ?
DELETE FROM emp ;
• Delete employee records whose empno=7844 ?
• DELETE FROM emp WHERE empno=7844 ;
Delete employee records having more than 30 yrs expr ?
DELETE FROM emp WHERE (SYSDATE-hiredate)/365 >= 3 ;
Difference between DELETE and TRUNCATE :-
DELETE TRUNCATE
• DML command • DDL command
• Deletes all or particular records • deletes only all records
• Data can be restored • Data cannot be restored
• Deletes row by row • doesn’t read record before deleting
• Used by developer • used by DBA
• Triggers can be created • triggers cannot be created
• What is a Transaction?
• A transaction is a set of SQL statements which Oracle treats as a Single Unit. i.e. all the statements should
execute successfully or none of the statements should execute.
This command is used to manage the changes made by DML statements.
TCL allows the statements to be grouped together into logical transactions.
TCL It is collection of three commands.
1. COMMIT
2. ROLLBACK
3. SAVEPOINT
1.COMMIT COMMAND
This command is used to make changes permanent to the data base.
COMMIT command saves all the work done.
PUT It ends the current transaction and makes permanent changes during the
transaction.
Syntax:
commit;
• Example
• insert into emp (empno,ename,sal) values (101,’Abid’,2300);
• commit;
2. ROLLBACK COMMAND
*ROLLBACK: The rollback will undo the changes which are not permanent.
• ROLLBACK command is used to restores database to original since the last
COMMIT.
It is used to restores the database to last committed state.
• To rollback the changes done in a transaction give rollback statement. Rollback
restore the state of the database to the last commit point.
• Syntax:
• ROLLBACK;
• Example :
• delete from emp;
• rollback; /* undo the changes */
3. SAVEPOINT COMMAND
You can also set a savepoint at any point with in a transaction. These allow you to roll back
changes that savepoint .
Savepoints can be useful to break up very long transactions, because, if you make a mistake
after you’ve set a savepoint, you don’t have to roll back the transaction all the way to the start.
Syntax:
SAVEPOINT <savepoint_name>
Example:
SAVEPOINT S1;
It is used to temporarily save a transaction, so that you can rollback to that point whenever necessary
• For example
• insert into students values(107,'dahir','[email protected]',50);
• SAVEPOINT A
• insert into students values(106,'mille','[email protected]',60);
• SAVEPOINT B
• insert into students values(106,'mille','[email protected]',70);
• When we SELECT data from the table
• select * from students;
Difference between ROLLBACK and COMMIT commands.
COMMIT
ROLLBACK
REVOKE
GRANT
• Used to provide any user access privileges or • REVOKE command disallows a user
other priviliges for the database. to perform certain activities.
• GRANT command allows a user to perform
certain activities on the database. • Used to take back permissions from
• It grants access privileges for database objects
any user.
to other users. • It revokes access privileges for
database objects previously granted
to other users.