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

SQLDAY 21

The document provides an overview of Data Manipulation Language (DML) including its operations: INSERT, UPDATE, and DELETE, along with their syntax and examples. It also covers Transaction Control Language (TCL) with statements like COMMIT, ROLLBACK, and SAVEPOINT, and introduces Data Control Language (DCL) with GRANT and REVOKE commands. Additionally, it explains the concept of Normalization in databases, detailing its forms and the process of reducing redundancy in tables.

Uploaded by

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

SQLDAY 21

The document provides an overview of Data Manipulation Language (DML) including its operations: INSERT, UPDATE, and DELETE, along with their syntax and examples. It also covers Transaction Control Language (TCL) with statements like COMMIT, ROLLBACK, and SAVEPOINT, and introduces Data Control Language (DCL) with GRANT and REVOKE commands. Additionally, it explains the concept of Normalization in databases, detailing its forms and the process of reducing redundancy in tables.

Uploaded by

pradeepsoratur07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

DAY 21

Thursday, August 13, 2020 9:41 AM

DATA MANIPULATION LANGUAGE ( DML )

It is used to Manipulate the Object by performing insertion , updating and deletion .


1. INSERT
2. UPDATE
3. DELETE

1. INSERT : It is used to insert / create records in the table .

Syntax: INSERT INTO Table_Name VALUES( v1 , v2 , v3 …… ) ;

CUSTOMER
CID CNAME CNO ADDDRESS
NUMBER(2) VARCHAR(10) NUMBER(10) VARCHAR(20)

INSERT INTO CUSTOMER VALUES( 1 , 'DINGA' , 9876543210 ,'BANGALORE' ) ;


CID CNAME CNO ADDDRESS
NUMBER(2) VARCHAR(10) NUMBER(10) VARCHAR(20)
1 DINGA 9876543210 BANGALORE
INSERT INTO CUSTOMER VALUES( 2 , 'DINGI' , 9876543211 ,'MANGALORE' ) ;
CID CNAME CNO ADDDRESS
NUMBER(2) VARCHAR(10) NUMBER(10) VARCHAR(20)
1 DINGA 9876543210 BANGALORE
2 DINGI 9876543211 MANGALORE

PRODUCT
PID PNAME PRICE CID
NUMBER(2) VARCHAR(10) NUMBER(6,2) NUMBER(3)

INSERT INTO PRODUCT VALUES( 11 , 'iPhone' , 10000 , 2 );


PID PNAME PRICE CID
NUMBER(2) VARCHAR(10) NUMBER(6,2) NUMBER(3)
11 iPhone 10000 2
INSERT INTO PRODUCT VALUES( 22 , 'Mac Book' , 20000 , 1 );
PID PNAME PRICE CID
NUMBER(2) VARCHAR(10) NUMBER(6,2) NUMBER(3)

New Section 1 Page 1


11 iPhone 10000 2
22 Mac Book 20000 1

2. UPDATE :It is used to modify an existing value .

Syntax: UPDATE Table_Name


SET Col_Name = Value , Col_Name = Value ,,,,,
[WHERE stmt ] ;

CID CNAME CNO ADDDRESS


NUMBER(2) VARCHAR(10) NUMBER(10) VARCHAR(20)
1 ABHI 1234567890 BANGALORE
2 ABDUL 9876543210 MANGALORE

 WAQT update the phone number of Abdul to 7778889994

UPDATE CUSTOMER
SET CNO =
7778889994
WHERE CNAME ='ABDUL' ;

CID CNAME CNO ADDDRESS


NUMBER(2) VARCHAR(10) NUMBER(10) VARCHAR(20)
1 ABHI 1234567890 BANGALORE
2 ABDUL 7778889994 MANGALORE

 WAQT change the address of the customer to Mysore whose cid is 1 .

UPDATE CUSTOMER
SET ADDRESS = 'MYSORE'
WHERE CID = 1 ;

CID CNAME CNO ADDDRESS


NUMBER(2) VARCHAR(10) NUMBER(10) VARCHAR(20)
1 ABHI 1234567890 MYSORE
2 ABDUL 7778889994 MANGALORE

3. DELETE : It is used to remove a particular record from the table .

Syntax: DELETE FROM Table_Name


[ WHERE stmt ];

CID CNAME CNO ADDDRESS


NUMBER(2) VARCHAR(10) NUMBER(10) VARCHAR(20)

New Section 1 Page 2


1 ABHI 1234567890 BANGALORE
2 ABDUL 1234567891 MANGALORE
 WAQT remove abdul from the list of customers .

DELETE FROM CUSTOMER


WHERE CNAME ='ABDUL' ;

CID CNAME CNO ADDDRESS


NUMBER(2) VARCHAR(10) NUMBER(10) VARCHAR(20)
1 ABHI 1234567890 BANGALORE

ASSIGNMENT ON DML STATEMENTS :

1. WAQT update the salary of employee to double their salary if


He is working as a manager .

2. WAQT change the name of SMITH to SMIITH .

3. WAQT modify the job of KING to 'PRESIDENT' .

4. WAQT to change name of ALLEN to ALLEN MORGAN .

5. WAQT hike the salary of the employee to 10% . If employees earn less than
2000 as a salesman .

6. WAQ TO delete the employees who don’t earn commission .

7. WAQ to remove all the employees hired before 1987 in dept 20

8. Differentiate between TRUNCATE and DELETE statements .

TRUNCATE DELETE
Belongs to DDL Belongs to DML

New Section 1 Page 3


Removes all the records from the Removes a particular record from the
Table permanently . Table .
Auto COMMIT Not auto COMMIT .

3. TRANSACTION CONTROL LANGUAGE ( TCL )

"It is used to control the transactions done on the database ".


The DML Operations performed on the Database are known as Transactions
such as Insertion , Updating and Deletion .

We have 3 Statements :
1. COMMIT
2. ROLLBACK
3. SAVEPOINT

1. COMMIT : "This statement is used to SAVE the transactions into the DB ".

Syntax: COMMIT ;

Example :
Query WORKPLACE DATABASE

 CREATE T1 NAME SAL NAME SAL

 INSERT INTO T1
VALUES( 'A' , 100 ) ; NAMESAL NAMESAL
A100A100
COMMIT ;

 INSERT INTO NAME SAL NAME SAL


T1 ) ;
A 100 A 100
VALUES( 'B' , 200
 INSERT INTO T1 ) ; B 200 B 200
VALUES( 'C' , 300 C 300
New Section 1 Page 4
COMMIT ;

New Section 1 Page 5


VALUES( 'C' , 300 ) ; B 200 B 200
COMMIT ; C 300 C 300

NAME SAL
 UPDATE T1 A 1000
SET SAL = 1000 B 200
WHERE NAME = 'A' ; C 300

 COMMIT ; NAME SAL


A 1000
B 200
C 300

2. ROLLBACK :

This statement is used to Obtain only the saved data from the DB .
It will bring you to the point where you have committed for the last time .

SYNTAX: ROLLBACK ;

3. SAVEPOINT :

This statement is used to mark the positions or restoration points . (nothing


related to DB ) .

SYNTAX: SAVEPOINT Savepoint_Name ;

Example :

Query WORKPLACE SAVEPOINT

 INSERT INTO T1 NAME SAL Savepoints


VALUES( 'A',100) ; A 100 S1
 SAVEPOINT S1 ; B 200
 INSERT INTO T1
VALUES( 'B' , 200 ) ; C 300 S2
 INSERT INTO T1 D 400

New Section 1 Page 6


VALUES( 'B' , 200 ) ; CDE 300 S2
 INSERT INTO T1 F 400
VALUES( 'C' , 300 ) ; 500
 SAVEPOINT S2 ; S3
600
 INSERT INTO T1
VALUES( 'D' , 400 ) ;
 INSERT INTO T1
ROLLBACK TO S3
VALUES( 'E' , 500 ) ;
 SAVEPOINT S3 ; ROLLBACK TO S2
 INSERT INTO T1 ROLLBACK TO S1
VALUES( 'F' , 600 ) ;

SYNTAX: ROLLBACK TO Savepoint_Name ;

4. DATA CONTROL LANGUAGE :

"This statement is used to control the flow of data between the users ".

We have 2 statements :
1. GRANT
2. REVOKE

1. GRANT : THIS STATEMENT IS USED TO GIVE PERMISSION TO A


USER .

SYNTAX: GRANT SQL_STATEMENT


ON TABLE_NAME
TO USER_NAME ;

2. REVOKE : THIS STATEMENT IS USED TO TAKE BACK THE


PERMISSION FROM THE USER .

SYNTAX: REVOKE SQL_STATEMENT


ON TABLE_NAME
FROM USER_NAME ;

Example :

User 1 : SCOTT User 2 : HR

New Section 1 Page 7


User 1 : SCOTT User 2 : HR

EMP SELECT *
ENAME SAL FROM SCOTT.EMP ;
A 100
B 200

GRANT SELECT ON EMP


TO HR ;

EMP SELECT *
ENAME SAL FROM SCOTT.EMP ;
A 100
B 200

REVOKE SELECT ON EMP


FROM HR ;

SELECT *
ENAME SAL FROM SCOTT.EMP ;
A 100
B 200

TRY !!!!

New Section 1 Page 8


SQL> SHOW USER ; USER is "SCOTT" SQL> CONNECT
Enter user-name: HR Enter password: ***** Connected.
SQL> SHOW USER ; USER is "HR"

New Section 1 Page 9


Connected.
SQL> SHOW USER
; USER is "HR"
SQL> SELECT *
2 FROM
SCOTT.EMP; FROM
SCOTT.EMP
*
ERROR at line 2:
ORA-00942: table or view does not exist

SQL> CONNECT
Enter user-name: SCOTT
Enter password: *****
Connected.
SQL> GRANT SELECT ON EMP TO HR;

Grant succeeded.

SQL> CONNECT
Enter user-name: HR
Enter password:
***** Connected.
SQL> SELECT *
2 FROM SCOTT.EMP;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800 20


7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10

New Section 1 Page


DAY 22
Friday, August 14, 2020 9:39 AM

What is Normalization ?

" It is the process of reducing a large table into smaller tables in order to remove
redundancies and anomalies by identifying their functional dependencies is
known as Normalization . "

Or

"The process of decomposing a large table into smaller table is known as


Normalization ."

Or

"Reducing a table to its Normal Form is known as Normalization . "

T1 T2
T3

What is Normal Form ?

A table without redundancies and anomalies are said to be in Normal Form .

Levels of Normal From .


1. First Normal Form ( 1NF )
2. Second Normal Form ( 2NF )
3. Third Normal Form ( 3NF )
4. Boyce - Codd Normal Form ( BCNF )

Note : If any Table / entity is reduced to 3NF , then the table is said to be normalized.

New Section 1 Page 1


1. First Normal Form ( 1NF ) :
- No duplicates records .
- Multivalued data should not be present .

QSPIDERS
QID NAME COURSE
QID NAME C1 C2 C3
1 A JAVA
1 A JAVA MT
2 B JAVA , SQL
2 B JAVA SQL
3 C MT , SQL
3 C SQL MT
1 A MT

2. Second Normal Form ( 2NF )


- Table should be in 1NF
- Table should not have Partial Functional Dependency .

EMPLOYEE - ( EID , ENAME , SAL , DEPTNO , DNAME , LOC )


Eid ename sal Deptno dname Loc
1 A 100 10 D1 L1
2 B 120 20 D2 L2
3 C 320 10 D1 L1
4 D 251 10 D1 L1

Eid - ename ,sal


Deptno - dname , loc

:- ( Eid , deptno ) -> ( Ename , Sal , Dname , Loc ) composite key attribute
results in PFD

R1 - ( EID , ENAME , SAL )


R2 - ( DEPTNO , DNAME , LOC ) Eid ename sal
Deptno dname Loc
1 A 100
10 D1 L1
2 B 120
20 D2 L2
3 C 320
4 D 251
3. Third Normal Form ( 3NF )
- Table should be in 2NF .
- Table should not have Transitive Functional Dependency .

New Section 1 Page 2


Employee - ( EID , Ename , Sal , comm , Pin code , state , country )

EID -> ENAME :- Transitive Functional Dep


SAL
COMM
PINCODE -> STATE
COUNTRY
R1- ( eid , ename , comm )
R2- ( pincode , state , country )

New Section 1 Page 3


New Section 1 Page 4

You might also like