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

Chapter 3 SQL FUNDAMENTALS

1. SQL is a language used for storing and managing data in relational database management systems (RDBMS). 2. SQL commands are divided into four categories: DDL, DML, DQL, and DCL. DDL includes commands like CREATE, ALTER, and DROP for defining and modifying database schema. DML includes INSERT, UPDATE, DELETE for manipulating data. DQL is SELECT statement for querying data. DCL includes GRANT and REVOKE for managing permissions. 3. The INSERT statement is used to add new rows to existing tables. It can specify values for all columns or only some columns. NULL values can also be inserted.

Uploaded by

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

Chapter 3 SQL FUNDAMENTALS

1. SQL is a language used for storing and managing data in relational database management systems (RDBMS). 2. SQL commands are divided into four categories: DDL, DML, DQL, and DCL. DDL includes commands like CREATE, ALTER, and DROP for defining and modifying database schema. DML includes INSERT, UPDATE, DELETE for manipulating data. DQL is SELECT statement for querying data. DCL includes GRANT and REVOKE for managing permissions. 3. The INSERT statement is used to add new rows to existing tables. It can specify values for all columns or only some columns. NULL values can also be inserted.

Uploaded by

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

CHAPTER 3

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

(Data Manipulation Language) has three commands


DML Insert , Update , Delete (They are called as Transactions)

(Data Query/Retrieval Language) has one command


DQL/DRL Select

(Transaction Control Language) it has three commands


TCL Commit, Rollback, Save point

(Data Control Language) has two commands


DCL Grant, Revoke
Logging on to Oracle SQL
• SQL*PLUS: It is a client environment where the developer will write the queries and submit the
queries is execution.
•  Start  Programs  Oracle Database 11G Express Edition  Run SQL Command Line
• At the SQL Prompt Type the following
• SQL> CONNECT Username/Password (Username is “HR” and Password is “manager”)
• SQL> CONNECT HR/MANAGER (OR) SQL> CONN HR/MANAGER
• (HR is the default sample user in which the sample tables are already created, most of the further
examples will be based on these sample tables)
• If it responds “The account is locked” then:
• SQL> CONNECT SYSTEM/<password>
• (SYSTEM is a name of the DBA/Admin user for which the password is set at the time of installing
Oracle. In our Lab, MANAGER is the password for the user SYSTEM.)
• ALTER USER HR IDENTIFIED BY MANAGER ACCOUNT UNLOCK;
Schema Objects

• A schema is a collection of database objects.


• A schema is owned by a database user and has the same name as that user.
• Schema objects are logical structures created by users.
• Objects such as tables or indexes hold data, or can consist of a definition only, such as a view
or synonym.
Note:
• There is no relationship between a tablespace and a schema. Objects in the same schema can
use storage in different tablespaces, and a tablespace can contain data from different
schemas.
Data definition language
DDL: This language is used to manage database objects. It is collection of five commands.
• CREATE, ALTER, DROP, TRUNCATE, RENAME ,Flashback, Purge
1. Create table
Create is a command: used to create a new database object.
object:
-> it is a collection of dissimilar datatypes and stored in one memory location.
ex: Table, Views, Indexes, squences, synonyms, function etc.,
Table:
-> it is a collection of info. stored in rows and columns format.
-> max. 1000 columns are allowed and un-limited rows per a table.
-> it is common db object for any rdbms packages.
Create table
Syntax:
• Create Table <table_name>(Column1 datatype(size), Column2
datatype(size),...)
• ex:
• Create table Emp7(Empno Number(2),Ename Varchar2(10),Sal Number(6,2));
• To see the table list:
• Select * from Tab;
• How to see the structure of the table:
• syntax Desc <table_Name>:
• -> it shows structure of the table.
ex: Desc Emp7;
Creating a table from another table:-

• 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

ALTER command -> is used to modify structure of the table.


-> this command works on columns only.
-> it has 4 options.
a. add b.modify c.rename d.drop  
A.ADD COLUMN

• Adding a Column:- used to add a new column/columns into existing table.


• to add column to exist table
• Syntax :-
• ALTER TABLE <tabname> ADD (colname DATATYPE(SIZE) [ , colname -------])
• ex: Alter table Emp7 Add(Deptno Number(2), Job Varchar2(10));
b.modify

• -> used to incre/decre size of the column or to chanage datatype of a column.


• to increase size of empno column:
• Alter table Emp7 Modify Empno Number(4);
to change datatype of a column:
• Alter table Emp007 Modify Empno Varchar2(4);
• note: Empno column must be empty.
Changing Datatype:-
• Ex
• ALTER TABLE emp MODIFY (ename CHAR(20)) ;
• NOTE :-
• To change datatype of a column the column should be empty.
• Changing Column from NULL to NOT NULL
• ALTER TABLE emp MODIFY (ename NOT NULL) ;
• Changing column from NOT NULL to NULL:-
• ALTER TABLE emp MODIFY(ename NULL) ;
C. Drop(Column):

• 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):

Renaming a Column :- -> used to rename a column name.


• Syntax:-
• ALTER TABLE <tabname> RENAME COLUMN <oldname> to <newname> ;
ALTER TABLE emp RENAME COLUMN sal TO salary ;
ex: Alter Table Emp7 Rename Column Sal to Basic;
4. Rename(table Name):

• RENAME: This command is used to change/ to rename the table


name .
• syntax:
• Rename <old table_name> to <new table_name>;
• ex:
• Rename Emp7 to Emp707;
3. DROP command :-
• DROP command is used to drops a table from database.
• This command is used to remove a table temp.
• When a table is dropped, all its data will be lost.
• Syntax :-
• DROP TABLE <TABNAME> ;
• Example :-
• DROP TABLE customer;
4. Rename(table Name):

• RENAME: This command is used to change the table name .


-> used to rename a table name.
syntax:
• Rename <old table_name> to <new table_name>;
ex:
• Rename Emp7 to Emp707;
5.TRUNCATE command :-

-> used delete all records from table permanently.


-> structure is present.
This command is used to delete ALL the rows of the table permanently.
• We cannot get back the deleted rows. TRUNCATE deletes all the data from a table.
• Syntax :-
• TRUNCATE TABLE <TABLENAME>
• Example :-
• TRUNCATE TABLE EMP ;
Flashback Table :-

• 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

Note :- TRUNCATE is faster than DELETE


DQL/DRL (SELECT)

• This command is used to retrieve the data from existing table.


• Using this command we can retrieve all the records & also specific records
from existing table (by using „where‟ clause)
• Using this command we can retrieve the data from the table in 3 ways
• 1. Projection
• 2. Selection
• 3. Joins
• Syntax:
• SELECT * FROM <TABLE NAME>
• Ex: SELECT * FROM EMP;
• * represents all columns
Projection:
• Retrieving the data from specific columns is known as Projection
• Syntax: SELECT COL1,COL2……..COLN FROM <TABLE NAME>
• Ex: SELECT EID,ENAME FROM EMP;
• Selection:
• Retrieving the data from based on some condition is called selection
• In SQL, whenever we need to check a condition, we need to use a special
• clause called “where‟
• Syntax: SELECT * FROM <TABLENAME> WHERE (CONDITION);
• Ex: SELECT * FROM EMP WHERE Empno=101;
TCL (Transaction Control Language).

• 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

• ROLLBACK command is used to undo • The COMMIT command is used to


the changes made by the DML save the modifications done to the
commands. database values by the DML
• It rollbacks all the changes of the commands.
current transaction. • It will make all the changes
permanent that cannot be rolled
back.
Syntax:
COMMIT;
DCL

Dcl Stands For (Data Control Language).


Dcl Is Used To Control User Access In A Database.
This Command Is Related To The Security Issues.
Using Dcl Command, It Allows Or Restricts The User From Accessing Data In Database
Schema.
It Is Used To Grant Or Revoke Access Permissions From Any Database User.
Dcl Has Two Commands Are As Follows,
1. Grant
2. Revoke
1. GRANT COMMAND
• GRANT command gives user's access privileges to the database.
This command allows specified users to perform specific tasks.
Syntax:
GRANT <privilege list>
ON <relation name or view name>
TO <user/role list>;
Example : GRANT Command
> GRANT ALL ON employeeTO ABC;
[WITH GRANT OPTION]
>GRANT select , insert, update , delete system . employee to user;
• In the above example, user 'ABC' has been given permission to view and
modify the records in the 'employee' table.
2. REVOKE COMMAND

• REVOKE command is used to cancel previously granted or denied permissions from


the user.
• Revoke: Take back permissions from the user.
Syntax:
REVOKE <privilege list>
ON <relation name or view name>
FROM <user name>;

Example : REVOKE Command


• REVOKE UPDATE ON employee fROM ABC;
Difference between GRANT and REVOKE command.

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.

You might also like