Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alagappa university
muruganjit1
1,262 views
53 slides
Jul 08, 2024
Slide 1 of 53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
About This Presentation
Title: Relational Database Management System Concepts(RDBMS)
Description:
Welcome to the comprehensive guide on Relational Database Management System (RDBMS) concepts, tailored for final year B.Sc. Computer Science students affiliated with Alagappa University. This document covers fundamental princ...
Title: Relational Database Management System Concepts(RDBMS)
Description:
Welcome to the comprehensive guide on Relational Database Management System (RDBMS) concepts, tailored for final year B.Sc. Computer Science students affiliated with Alagappa University. This document covers fundamental principles and advanced topics in RDBMS, offering a structured approach to understanding databases in the context of modern computing. PDF content is prepared from the text book Learn Oracle 8I by JOSE A RAMALHO.
Key Topics Covered:
Main Topic : DATA INTEGRITY, CREATING AND MAINTAINING A TABLE AND INDEX
Sub-Topic :
Data Integrity,Types of Integrity, Integrity Constraints, Primary Key, Foreign key, unique key, self referential integrity,
creating and maintain a table, Modifying a table, alter a table, Deleting a table
Create an Index, Alter Index, Drop Index, Function based index, obtaining information about index, Difference between ROWID and ROWNUM
Target Audience:
Final year B.Sc. Computer Science students at Alagappa University seeking a solid foundation in RDBMS principles for academic and practical applications.
About the Author:
Dr. S. Murugan is Associate Professor at Alagappa Government Arts College, Karaikudi. With 23 years of teaching experience in the field of Computer Science, Dr. S. Murugan has a passion for simplifying complex concepts in database management.
Disclaimer:
This document is intended for educational purposes only. The content presented here reflects the author’s understanding in the field of RDBMS as of 2024.
Feedback and Contact Information:
Your feedback is valuable! For any queries or suggestions, please contact [email protected]
Size: 729.09 KB
Language: en
Added: Jul 08, 2024
Slides: 53 pages
Slide Content
RDBMS -Unit IV
Chapter 8
Data Integrity
Prepared By
Dr. S.Murugan, Associate Professor
Department of Computer Science,
AlagappaGovernment Arts College, Karaikudi.
(Affiliated by AlagappaUniversity)
Mailid: [email protected]
Reference Book:
LEARN ORACLE 8i
JOSE A RAMALHO
Data Integrity -Definition
➢Dataintegritycanbedefinedasrulescreatedbythe
developer,orDBA,inordertoassuretheconsistency
ofinformationmaintainedinthetables.
➢Forexample,lookattheDEPTandEMPtablebelow
Data Integrity -Example
Data Integrity -Example
When these tables were created, the following integrity
rules were stipulated:
➢TheDeptnocolumnoftheDEPTtablecannot
containduplicatevalues.Thevaluemustbeunique.
Forexample,therecan’tbetwodepartmentswiththe
code20.
➢TheEnamecolumnoftheEMPtablecannotbeleft
blank(Thiscolumnmustcontainvalue).Other
columnsmayhavenullvalues,allowingthemtobe
leftempty.
Data Integrity -Example
➢TheDeptnocolumnoftheEMPtablecancontain
onlythosedepartmentnumbersthatcorrespondto
theDeptnocolumnoftheDEPTtable.
➢TheSalcolumncontainsonlyvaluesbelow10,000.
➢TheEmpnocolumncannotbeempty,andmustbe
unique.
Understanding Constraints
FOREIGN KEY Constraints
➢Foreachrowofthechildtable,theremustbea
correspondingrowintheparenttable.
➢Thefigure8.2showsthattheDeptnocolumnofthe
EMPtablewasdefinedwiththeFOREIGNKEY
constraint,andthatitisassociatedtotheDEPTtable
throughtheDeptnofield.
➢The names of the FOREIGN KEY and PARENT
KEY columns do not need to be the same; however,
having the same name helps to understand the
association.
The CREATE TABLE Command
The CREATE TABLE command creates a new table
through the following operations:
1. Definition of the columns
2. Definition of the table organization
3. Integrity constraints
4. Tablespaceof the table
5. Storage characteristics
6. An optional cluster
7. Query data
8. Rate of parallelism used in the creation of the table, and
default rate of parallelism for the queries of the table
9. Definition of partitions
10. Arrangement by index or heap (the storage area that
allows random access)
Arguments
schema
An optional parameter that indicates the schema to which the table
belongs.
table
The name of the table to be created.
datatype
Specifies the column’s data type. Up to 254 columns are allowed.
DEFAULT
This is optional, and indicates a default value for the column.
CONSTRAINT
This is optional and indicates some integrity control that applies to
the column; for example, it must contain a value.
TABLE CONSTRAINT
This is optional and indicates some integrity control that applies to
the table; for example, the primary key.
Arguments
PCTFREE
This is optional and indicates the percentage of each data block that
will be reserved for future updating in the table. The default value is
10.
PCTUSED
This is optional and indicates the minimum percentage of used
space that Oracle must maintain before a data block is eligible for a
row insertion. The default value is 40.
INITRANS
The default value is 1. It can vary from 1 to 255 and indicates the
number of transactions assigned inside the data block of the table.
MAXTRANS
Indicates the maximum number of transactions that can update a
data block.
Arguments
TABLESPACE
This is optional; it indicates a tablespacedifferent from that used as
default.
CLUSTER
This is optional; it indicates that the table is part of a cluster.
ENABLE
This is optional; it links the integrity restrictions.
DISABLE
This is optional; it disables the integrity restrictions.
Example for primary Key, default and not
null constraints
Table Creation
create table newagain
(first number(4) constraint pk_newprimary key,
second date default sysdate,
third varchar(20) not null);
Method1forinsertion:
insertintonewagainvalues(1111,'18-JUN-19','karaikudi')
Method2forinsertion:
insertintonewagain(first,third)values(3333,'cccc');
select*fromnewagain
Example for Data Integrity
Parent Table Creation
CREATE TABLE dept1
(deptnoNUMBER (2) PRIMARY KEY,
dnameVARCHAR2(10),
locVARCHAR2(9) );
Example for Data Integrity
insert into dept1 values (10,'sales','Trichy')
1 row(s) inserted.
insert into empagac2 values
(1001,'RAJESH','Manager',1001,'10-apr-
19',15000,150,10)
1 row(s) inserted.
Test for Primary Key: (1001 already exists)
insert into empagac2 values
(1001,'RAJESH','Manager',1001,'10-apr-
19',15000,150,10)
ORA-00001: unique constraint
(SYSTEM.PK_EMP) violated
Example for Data Integrity
Test for Upper Case Letters: (Name typed in
lowercase letters)
insert into empagac2 values
(1002,'mani','Manager',1001,'10-apr-19',15000,150,10)
ORA-02290: check constraint
(SYSTEM.UPPER_ENAME) violated
Example for Data Integrity
Test for Self Referential Integrity constraints 1004 is
not an Employee number
insert into empagac2 values
(1002,'MANI','Manager',1004,'10-apr-
19',15000,150,10)
ORA-02291: integrity constraint
(SYSTEM.FK_MGR) violated -parent key not
found
Example for Data Integrity
Test for Salary field (The salary must >500)
insert into empagac2 values
(1002,'MANI','Clerk',1001,'10-apr-19',500,150,10)
ORA-02290: check constraint (SYSTEM.CK_SAL)
violated
Test for null (Accept null values for commission)
insert into empagac2 values
(1002,'MANI','Clerk',1001,'10-apr-19',1500,'',10)
Example for Data Integrity
Test for Foreign Key (DNO 20 is not present in the
DNO of DEPT Table)
insert into empagac2 values
(1004,'SELVAM','Clerk',1001,'10-apr-19',1500,150,20)
ORA-02291: integrity constraint
(SYSTEM.FK_DEPTNO) violated -parent key not
found
Creating a Table with the Contents of
Another Table
➢UsingtheASoption,wecancreateatablewiththe
SELECTcommandtorecoverthestructureanddata
ofanothertable.
➢Thefollowingexamplecreatesatablecalled
NEWEMPwithallthecolumnsoftheEMPtable
andonlytherowsofdepartment20:
➢createtablenewempasselect*fromempwhere
deptno=20;
Constraint Disabling
Constraints can be disabled by the keyword DISABLE.
SQL> CREATE TABLE dept1
(deptnoNUMBER (2) PRIMARY KEY DISABLE,
dnameVARCHAR2(10),
locVARCHAR2(9) );
This option will allow the creation of duplicate keys,
For Ex:
SQL> insert into dept1 values(10,’aaaa’,’aaaa’);
1 row created.
SQL> insert into dept1 values(10,’bbbb’,’bbbbb’);
1 row created.
When to Create Indexes
➢Anindexcanbecreatedduringthedesign
processofthetablestructurebyusingthe
PRIMARYKEYconstraint.
➢Columnsoftypelobs,long,andlongraw
cannotbeindexed.
Creating Indexes with Constraints
➢Theindexiscreatedduringthetable
creationbyusingthekeywordthrough
usingindex.
Creating Indexes with the CREATE INDEX
Command
Creating Indexes with the CREATE INDEX
Command
➢TheCREATEINDEXcommandis
responsibleforthecreationofsimple
indexesusingauniquecolumn,or
compositeindexesusingmorethanone
column.
➢Tocreatecompositeindexusethe
followingquery:
➢Createindexempind3onempind(name,
idno)
Creating a Function-Based Index
➢Function-basedindexesimprovethequeriesthatuse
avaluereturnedbyafunctionorexpression.
➢Oneadvantageofusingthistypeofindexisthe
abilitytoincludesortsbasedonkeyscontaining
wordswithaccents(Eitherlowercaseorupper
case).
➢ForExample,thecreationofanindexwithallthe
namesconvertedtouppercase,usingtheUPPER
function:
CREATE INDEX x1 ON newemp2 UPPER(ename));
Creating a Function-Based Index
➢Even when there are names in lowercase or combining
lowercase and uppercase, the following SELECT
command returns data from all the entries that include
“Smith”:
➢SELECT * FROM newemp2 WHERE
UPPER(ename) = ‘SMITH’; (If the column ename
contains the value smith, it will be displayed)
➢The following command would not display the
lowercase letters.
SELECT * FROM newemp2 WHERE ename=‘SMITH’
Creating an Index Using Key Compression
➢Oracleallowsthecreationofanindexusingkey
compression,atechniquethathelpstoeliminatethe
duplicatedvaluesofakeycolumn.
➢Tocreateacompressedindex,justaddthe
COMPRESSclause,asshowninthefollowing
example:
CREATEINDEXemp_enameonemp_test(ename)
COMPRESS1
➢KeycompressioncanbedisabledwiththeALTER
INDEXcommandandtheNOCOMPRESSclause:
ALTER INDEX emp_ename REBUILD
NOCOMPRESS;
Changing an Index
➢Changinganindexisrestrictedtomodifyingthe
transactionandstorageparameters.Itisnotpossible
tochangethecolumnsintheindex.Inthissituation,
theindexmustbedeletedandre-createdwithanew
definition.
➢Thefollowingcommandchangestheindex:
SQL>alterindexpk_emp3
INITRANS5
MAXTRANS10
STORAGE(PCTINCREASE50);
Re-creating an Index
➢Anindexcanbere-createdbyusingtheALTER
INDEXcommand.Thisoperationistheequivalent
ofdeletingandreconstructingtheindex.
➢Tore-createanexistingindex,usethefollowing
format:
ALTERINDEXindex_nameREBUILD;
Obtaining Information about Indexes
➢The USER_INDEXES command is used to obtain
information about the index of each user.
➢For example,
SQL> select index_name, table_name, uniqueness,
ini_trans, max_transfrom user_indexes;
Eliminating an Index
The SQL command responsible for the elimination of
an index is DROP INDEX.
Syntax:
DROP INDEX name
Example:
DROP INDEX depemp
Difference between ROWID and ROWNUM
RowIdis Permanent. It locates the physical address.
Rownumis Temperary. It locates the record number.