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

Week-1 Dbms Lab

1. The document discusses different database models including hierarchical, network, and relational models. The relational model organizes data into tables and allows for relationships between tables. 2. Key aspects of the relational database management system (RDBMS) are described including how data is organized in rows and columns and how tables can be related through common columns. 3. The document outlines various types of relationships between entities like one-to-one, one-to-many, and many-to-many relationships. Codd's 12 rules for relational databases are also summarized.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Week-1 Dbms Lab

1. The document discusses different database models including hierarchical, network, and relational models. The relational model organizes data into tables and allows for relationships between tables. 2. Key aspects of the relational database management system (RDBMS) are described including how data is organized in rows and columns and how tables can be related through common columns. 3. The document outlines various types of relationships between entities like one-to-one, one-to-many, and many-to-many relationships. Codd's 12 rules for relational databases are also summarized.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

INTRODUCTION

Database Management System


This model is like a hierarchical tree structure, used to construct a hierarchy of
records in the form of nodes and branches. The data elements present in the
structure have Parent-Child relationship. Closely related information in the
parent-child structure is stored together as a logical unit. A parent unit may
have many child units, but a child is restricted to have only one parent.
The drawbacks of this model are:
The hierarchical structure is not flexible to represent all the
relationship proportions, which occur in the real world. It cannot
demonstrate the overall data model for the enterprise because of the
non- availability of actual data at the time of designing the data model.
It cannot represent the Many-to-Many relationship.

Network Model
It supports the One-To-One and One-To-Many types only. The basic objects in
this model are Data Items, Data Aggregates, Records and Sets.
It is an improvement on the Hierarchical Model. Here multiple parent-child
relationships are used. Rapid and easy access to data is possible in this model
due to multiple access paths to the data elements.

Relational Model

Does not maintain physical connection between relations Data is organized in


terms of rows and columns in a table The position of a row and/or column in a
table is of no importance The intersection of a row and column must give a
single value
Features of an RDBMS The ability to create multiple relations and enter data
into them An attractive query language Retrieval of information stored in more
than one table . An RDBMS product has to satisfy at least Seven of the 12 rules
of Codd to be accepted as a full- fledged RDBMS.

Relational Database Management System

RDBMS is acronym for Relation Database Management System. Dr. E. F. Codd


first introduced the Relational Database Model in 1970. The Relational model
allows data to be represented in a simple row- column. Each data field is
considered as a column and each record is considered as a row. Relational
Database is more or less similar to Database Management S ystem. In
relational model there is relation between their data elements. Data is stored in
tables. Tables have columns, rows and names. Tables can be related to each
other if each has a column with a common type of information. The most
famous RDBMS packages are Oracle, Sybase and Informix.

Simple example of Relational model is as follows :

1
Student Details Table
Roll_no Sname S_Address

1 Rahul Satelite
2 Sachin Ambawad
i
3 Saurav Naranpur
a

Student Marksheet Table


Rollno Sub1 Sub2 Sub3

1 78 8 94
9
2 54 6 77
5
3 23 7 46
8
Here, both tables are based on students details. Common field in
both tables is Rollno. So we can say both tables are related with each
other through Rollno column.
Degree of Relationship
One to One (1:1)
One to Many or Many to One (1:M / M: 1) Many to Many (M: M)
The Degree of Relationship indicates the link between two entities for a
specified occurrence of each.
One to One Relationship:
(1:1) 1 1
Student Has Roll No.
One student has only one Rollno. For one occurrence of the first entity,
there can be, at the most one related occurrence of the second entity,
and vice-versa.
One to Many or Many to One Relationship: (1:M/M: 1) 1 M
Course Contains Students
As per the Institutions Norm, One student can enroll in one course at a
time however, in one course, there can be more than one student.
For one occurrence of the first entity there can exist many related
occurrences of the second entity and for every occurrence of the second
entity there exists only one associated occurrence of the first.
Many to Many Relationship: (M:M) M M M
Students Appears Tests
2
The major disadvantage of the relational model is that a clear-cut
interface cannot be determined. Reusability of a structure is not possible.
The Relational Database now accepted model on which major database
system are built.
Oracle has introduced added functionality to this by incorporated object-
oriented capabilities. Now it is known is as Object Relational Database
Management System (ORDBMS). Object- oriented concept is added in
Oracle8.
Some basic rules have to be followed for a DBMS to be relational. They
are known as Codd’s rules, designed in such a way that when the
database is ready for use it encapsulates the relational theory to its full
potential. These twelve rules are as follows.
E. F. Codd Rules
1. The Information Rule
All information must be store in table as data values.
2. The Rule of Guaranteed Access
Every item in a table must be logically addressable with the help
of a table name.
3. The Systematic Treatment of Null Values
The RDBMS must be taken care of null values to represent
missing or inapplicable information.
4. The Database Description Rule
A description of database is maintained using the same
logical
structures with which data was defined by the RDBMS.
5. Comprehensive Data Sub Language
According to the rule the system must support data definition,
view definition, data manipulation, integrity constraints,
authorization and transaction management operations.
6. The View Updating Rule
All views that are theoretically updatable are also updatable by
the system.
7. The Insert and Update Rule
This rule indicates that all the data manipulation
commands must be operational on sets of rows having a
relation rather than on a single row.
8. The Physical Independence Rule
Application programs must remain unimpaired when any
changes are made in storage representation or access methods.
9. The Logical Data Independence Rule
The changes that are made should not affect the user’s ability
to work with the data.The change can be splitting table into
many more tables.
10. The Integrity Independence Rule
The integrity constraints should store in the system catalog or in
3
the database.
11. The Distribution Rule
The system must be access or manipulate the data that is
distributed in other systems.
12. The Non-subversion Rule
If a RDBMS supports a lower level language then it
should not bypass any integrity constraints defined in
the higher level.

Object Relational Database Management System


Oracle8 and later versions are supported object-oriented concepts. A structure
once created can be reused is the fundamental of the OOP’s concept. So we can
say Oracle8 is supported Object Relational model, Object - oriented model
both. Oracle products are based on a concept known as a client-server
technology. This concept involves segregating the processing of an application
between two systems. One performs all activities related to the database
(server) and the other performs activities that help the user to interact with the
application (client). A client or front-end database application also interacts
with the database by requesting and receiving information from database
server. It acts as an interface between the user and the database.
The database server or back end is used to manage the database tables and
also respond to client requests.
Introduction to ORACLE
ORACLE is a powerful RDBMS product that provides efficient and effective
solutions for major database features. This includes:
 Large databases and space management control Many concurrent database
users
 High transaction processing performance High availability
 Controlled availability
 Industry accepted standards Manageable security
 Database enforced integrity client/Server environment
 Distributed database systems Portability
 Compatibility Connectivity

An ORACLE database system can easily take advantage of distributed


processing by using its Client/ Server architecture. In this architecture, the
database system is divided into two parts:

A front-end or a client portion


The client executes the database application that accesses database
information and interacts with the user.
A back-end or a server portion
The server executes the ORACLE software and handles the functions
required for concurrent, shared data access to ORACLE database.

4
What is SQL and SQL*Plus
Oracle was the first company to release a product that used the English-
based Structured Query Language or SQL. This language allows end
users to manipulate information of table(primary database object). To use
SQL you need not to require any programming experience. SQL is a
standard language common to all relational databases. SQL is database
language used for storing and retrieving data from the database. Most
Relational Database Management Systems provide extension to SQL to
make it easier for application developer. A table is a primary object of
database used to store data. It stores data in form of rows and columns.

SQL*Plus is an Oracle tool (specific program ) which accepts SQL


commands and PL/SQL blocks and executes them. SQL *Plus enables
manipulations of SQL commands and PL/SQL blocks. It also performs
additional tasks such as calculations, store and print query results in the
form of reports, list column definitions of any table, access and copy data
between SQL databases and send messages to and accept responses from
the user. SQL *Plus is a character based interactive tool, that runs in a
GUI environment. It is loaded on the client machine.
To communicate with Oracle, SQL supports the following categories of
commands:
1. Data Definition Language
Create, Alter, Drop and Truncate
2. Data Manipulation Language
Insert, Update, Delete and Select
3. Transaction Control Language
Commit, Rollback and Save point
4. Data Control Language
Grant and Revoke

Before we take a look on above-mentioned commands we will see the


data types available in Oracle.

Oracle Internal Data types

When you create a table in Oracle, a few items should be important, not only
do you have to give each table a name(e.g. employee, customer), you must also
list all the columns or fields (e.g. First_name, Mname, Last_name) associated
with the table. You also have to specify what type of information thattable will
hold to the database. For example, the column Empno holds numeric
information. An Oracle database can hold many different types of data.

Data type Description


Char(Size) Stores fixed-length character data to store alphanumeric values, with

5
a maximum size of 2000 bytes. Default and minimum size is 1 byte.
Varchar2(Size) Stores variable-length character data to store alphanumeric
values, with maximum size of 4000 bytes.
char(Size) Stores fixed-length character data of length size characters or
bytes, depending on the choice of national character set. Maximum size if
determined by the number of bytes required storing each character with an
upper limit of 2000 bytes. Default and minimum size is 1 character or 1 byte,
depending on the character set.
Nvarchar2(Size) Stores variable-length character string having maximum
length size characters or bytes, depending on the choice of national character
set. Maximum size is determined by the number of bytes required to store
each character, with an upper limit of 4000 bytes.
Long Stores variable-length character data up to 2GB(Gigabytes). Its lenth
would be restricted based on memory space available in the computer.

Number [p,s] Number having precision p and scale s. The precision p


indicates total number of digit varies from 1 to 38. The scale s indicates
number of digit in fraction part varies from -84 to 127.
Date Stores dates from January 1, 4712 B.C. to December 31, 4712 A.D.
Oracle
predefine format of Date data type is DD-MON-YYYY.

Raw (Size) Stores binary data of length size. Maximum size is 2000 bytes. One
must have to specify size with RAW type data, because by default it does not
specify any size.
Long Raw Store binary data of variable length up to
2GB(Gigabytes).
LOBS - LARGE OBJECTS
LOB is use to store unstructured information such as sound and video
clips, pictures upto 4 GB size.
CLOB A Character Large Object containing fixed-width multi-byte characters.
Varying-width character sets are not supported. Maximum size is 4GB.
NCLOB A National Character Large Object containing fixed-width multi-
byte characters.

Varying-width character sets are not supported. Maximum size is 4GB. Stores
national character set data.
BLOB To store a Binary Large Object such a graphics, video clips and sound
files.Maximum size is 4GB.

BFILE Contains a locator to a large Binary File stored outside the database.
Enables byte stream I/O access to external LOBs residing on the database
server. Maximum size is 4GB.Apart from oracle internal data types, user can
create their own data type, which is used in database and other database object.
We will discuss it in the later part.

6
ROADWAY TRAVELS

“Roadway Travels” is in business since 1977 with several buses connecting


different places in India. Its main office is located in Hyderabad.

The company wants to computerize its operations in the following areas:


 Reservations
 Ticketing
 Cancellations

Reservations :
Reservations are directly handeled by booking office.reservations can be
made 60 daysin advance in either cash or credit. In case the ticket is not
available,a wait listed ticket is issued to the customer. This ticket is
confirmed against the cancellation.

Cancellation and modification:


Cancellations are also directly handed at the booking office. Cancellation
charges will be charged. Wait listed tickets that do not get confirmed are
fully refunded.

The following are tabular representation of the above entities and


relationships

BUS:
COLOUMN NAME DATA TYPE CONSTRAINT
Bus No varchar2(10) Primary Key

Source varchar2(20)

Destination varchar2(20)

Couch Type varchar2(20)

Reservation:
COLOUMN NAME DATA TYPE CONSTRAINT

PNRNo number(9) Primary Key

Journey date Date

No-of-seats integer(8)

Address varchar2(50)

7
Contact No Number(9) Should be equal to 10
numbers and not allow
other than numeric
BusNo varchar2(10) Foreign key
Seat no Number

Ticket:
COLOUMN NAME DATA TYPE CONSTRAINT

Ticket_No number(9) Primary Key


Journey date Date
Age int(4)
Sex Char(10)
Source varchar2(10)
Destination varchar2(10)
Dep-time varchar2(10)
Bus No Number2(10)

Passenger:
COLOUMN NAME DATA TYPE CONSTRAINT

PNR No Number(9) Primary Key

Ticket No Number(9) Foreign key

Name varchar2(15)
Age integer(4)
Sex char(10) (Male/Female)
Contact no Number(9) Should be equal to 10
numbers
and not allow other
than numeric

Cancellation:
COLOUMN NAME DATA TYPE CONSTRAINT

PNR No Number(9) Foriegn-key

Journey-date Date
Seat no Integer(9)
Contact_No Number(9) Should be equal to 10
numbers and not allow
other than numeric

8
Practicing DDL & DML Commands DataDefinition Language
The data definition language is used to create an object, alter the
structure of an object and also drop already created object. The Data
Definition Languages used for table definition can be classified into
following:

 Create table command


 Alter table command
 Truncate table command
 Drop table command

9
Week-1
QueriesforCreating,Dropping,andAlteringTables,Views,andCon
straints

 CREATE TABLE

Syntax:

CREATE TABLE tablename (column_name data_ type constraints, …)

Creating of Tables on ROAD WAY TRAVELS:

Table is a primary object of database, used to store data in form of


rows and columns. It is created using following command:

 Create Table <table_name> (column1 datatype(size), column2


datatype(size), column(n) datatype(size));

Example:
SQL> create table Bus(Bus_No varchar(5), source varchar(20),
destination varchar(20),CouchType varchar2(10),fair number);

 Desc command
Describe command is external command of Oracle. The describe
command is used to view the structure of a table as follows.

Desc <table name>

SQL> desc bus;


Name Null? Typ
e
BUS_NO NOT NULL INTEGER2
SOURCE (5)
DESTINATI VARCHAR2(2
ON COUCH 0)
TYPE VARCHAR2(
20)
VARCHAR2(1
0)
FAIR NUMBER

Reservation Table:

SQL> create table Reservation(PNR_NO Numeric(9), No_of_seats Number(8),


1
Address varchar(50), Contact_No Numeric(9),
0 Status char(3));
SQL> desc Reservation

Name Null? Type

PNR_NO NUMBER(9)
NO_OF_SEATS NUMBER(8)
ADDRESS VARCHAR2(50)
CONTACT_NO NUMBER(9)
STATUS CHAR(3)

Cancellation Table:

SQL> create table Cancellation(PNR_NO Numeric(9), No_of_seats Number(8),


Address varchar(50), Contact_No Numeric(9), Status char(3));

SQL> desc Cancellation

Name Null? Type

PNR_NO NUMBER(9)
NO_OF_SEATS NUMBER(8)
ADDRESS VARCHAR2(50)
CONTACT_NO NUMBER(9)
STATUS CHAR(3)

Ticket Table:

SQL> create table Ticket(Ticket_No Numeric(9) primary key, age


number(4), sex char(4) Not null, source varchar(2), destination
varchar(20), dep_time varchar(4));

SQL> desc Ticket

Name Null? Type


TICKET_NO NOT NULL NUMBER(9)
AGE NUMBER(4)
SEX NOT NULL CHAR(4)
SOURCE VARCHAR2(2)
DESTINATION 1 VARCHAR2(20)
DEP_TIME 1 VARCHAR2(4)
 Alteration of Table Addition of Column(s)
Adding of column in table is done using:
Alter table <table_name> add(column1 datatype, column2 datatype _);

SQL> ALTER TABLE Passenger ADD FOREIGN KEY (PNR_NO)


REFERENCES Reservation(PNR_NO);

SQL> ALTER TABLE Cancellation ADD FOREIGN KEY (PNR_NO)


REFERENCES Reservation(PNR_NO);

SQL> alter table Ticket modify tiketnonumber(10);

 Deletion of Column

Alter table <table_name> drop column <column name>;

SQL>Alter Table Emp_master drop column comm;

Alter table <table_name> set unused column <column name>;

Example,
SQL>Alter Table Emp_master set unused column comm;

Alter table <table_name> drop unused columns;

Alter table <table_name> drop (Column1, Column2, _);

 Modification in Column

Modify option is used with Alter table_ when you want to modify any existing
column.

Alter table <table name> modify (column1 datatype, _);


.
SQL> Alter table emp_master modify salary number(9,2);
Table altered.

Truncate Table
Truncate table <table name> [Reuse Storage];

Example

SQL>Truncate Table Emp_master;


1
2
SQL>Truncate Table Emp_master Reuse Storage;
Applying Constraints

 Domain
Integrity
 constraints
Entity
Integrity
 constraints
Referential
 Integrity
constraint

Oracle allows programmers to define constraints

 Column Level
 Table Level

Example
SQL> create table Ticket ( Ticket_No Numeric(9) , age number(4), sex char(4)
Not null, source varchar(2), destination varchar(20), dep_time varchar(4));
Table created.

Check Constraint
SQL> create table Reservation (PNR_NO Numeric(9), No_of_seats
Number(8), Address varchar(50), Contact_No Numeric(10) constraint ck
check(length(contact_no)=10), Status char(3));
Table created.

Check constraint with alter command


SQL> alter table Ticket add constraint check_age check(age>18);
Table altered.
Entity Integrity Constraints
This type of constraints are further classified into
 Unique Constraint
 Primary Key Constraint
Unique
Constraint

Example:
SQL> create table Ticket(Ticket_No Numeric(9) unique, age number(4), sex
char(4) l, source varchar(2), destination varchar(20), dep_time varchar(4))

Example:
1 Unique (ticket_no);
SQL> Alter table ticket add constraint uni1
3
Primary key constraint at the
column level Example:
SQL> create table Ticket(Ticket_No Numeric(9) constraint pk primary key,
age number(4), sex char(4) l, source varchar(2), destination varchar(20),
dep_time varchar(4));

SQL> insert into ticket values (1001,26, ‘M’, ‘KPHB’,’MTM’,’20:00’);


1 row created.
Example:
SQL> create table vendor_master (ven_code varchar(5), ven_name
varchar(20), venadd1 varchar(15), venadd2 varchar(15),vencity varchar(15),
constraint pr_com primary key (ven_code,ven_name));

Primary key with alter command:


SQL> alter table bus add constraint pr primary key (busno);

Referential Integrity Constraint


References constraint defined at column level
Example:
SQL> create table Passenger(PNR_NO Numeric(9) references reservation ,
Ticket_NO Numeric(9) references ticket, Name varchar(20), Age Number(4),
Sex char(10), PPNO varchar(15));

Foreign Key Constraint with alter command


SQL> alter table reservation add constraint fk_icode foreign key (busno)
references bus(bus_no);

SQL> delete from bus where bus_no = 2011;

1
4

You might also like