Week-1 Dbms Lab
Week-1 Dbms Lab
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
1
Student Details Table
Roll_no Sname S_Address
1 Rahul Satelite
2 Sachin Ambawad
i
3 Saurav Naranpur
a
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.
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.
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.
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.
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
BUS:
COLOUMN NAME DATA TYPE CONSTRAINT
Bus No varchar2(10) Primary Key
Source varchar2(20)
Destination varchar2(20)
Reservation:
COLOUMN NAME DATA TYPE CONSTRAINT
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
Passenger:
COLOUMN NAME DATA TYPE CONSTRAINT
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
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:
9
Week-1
QueriesforCreating,Dropping,andAlteringTables,Views,andCon
straints
CREATE TABLE
Syntax:
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.
Reservation Table:
PNR_NO NUMBER(9)
NO_OF_SEATS NUMBER(8)
ADDRESS VARCHAR2(50)
CONTACT_NO NUMBER(9)
STATUS CHAR(3)
Cancellation Table:
PNR_NO NUMBER(9)
NO_OF_SEATS NUMBER(8)
ADDRESS VARCHAR2(50)
CONTACT_NO NUMBER(9)
STATUS CHAR(3)
Ticket Table:
Deletion of Column
Example,
SQL>Alter Table Emp_master set unused column comm;
Modification in Column
Modify option is used with Alter table_ when you want to modify any existing
column.
Truncate Table
Truncate table <table name> [Reuse Storage];
Example
Domain
Integrity
constraints
Entity
Integrity
constraints
Referential
Integrity
constraint
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.
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));
1
4