DBMS (Practical File)
DBMS (Practical File)
Mahamaya Technical
University
PROGRAM:-1
TITLE:-- DATA DEFINATION LANGUAGE AND DATA MANIPULATION
LANGUAGE
OBJECTIVE:--Write the queries for DDL and DML.
Theory & Concepts:
Introduction about SQLSQL (Structured Questionry Language) is a nonprocedural language, you specify what you want, not how to get it. A
block structured format of English key words is used in this Questionry language. It has the following components.
DDL (Data Definition Language)The SQL DDL provides command for defining relation schemas, deleting relations and modifying relation schema.
DML (DATA Manipulation Language)It includes commands to insert tuples into, delete tuples from and modify tuples in the database.
View definitionThe SQL DDL includes commands for defining views.
Transaction Control- SQL includes for specifying the beginning and ending of transactions.
Embedded SQL and Dynamic SQLEmbedded and Dynamic SQL define how SQL statements can be embedded with in general purpose programming
languages, such as C, C++, JAVA, COBOL, Pascal and Fortran.
IntegrityThe SQL DDL includes commands for specifying integrity constraints that the data stored in the database must specify.
Updates that violate integrity constraints are allowed.
AuthorizationThe SQL DDL includes commands for specifying access rights to relations and views.
Data Definition LanguageThe SQL DDL allows specification of not only a set of relations but also information about each relation, including Schema for each relation
The domain of values associated with each attribute.
The integrity constraints.
The set of indices to be maintained for each relation.
The security and authorization information for each relation.
The physical storage structure of each relation on disk.
Domain types in SQLThe SQL standard supports a variety of built in domain types, including Char (n)- A fixed length character length string with user specified length .
Varchar (n)- A variable character length string with user specified maximum length n.
Int- An integer.
Small integer- A small integer.
Numeric (p, d)-A Fixed point number with user defined precision.
Real, double precision- Floating point and double precision floating point numbers with machine dependent
precision.
Float (n)- A floating point number, with precision of at least n digits.
Date- A calendar date containing a (four digit) year, month and day of the month.
Time- The time of day, in hours, minutes and seconds Eg. Time 09:30:00.
Number- Number is used to store numbers (fixed or floating point).
datatype
varchar2
varchar2
varchar2
varchar2
varchar2
varchar2
number 6
number 10,2
size
6
20
30
30
15
15
Ans() :-
Product_master
Columnname
Product_no
Description
Profit_percent
Unit_measure
Qty_on_hand
Reoder_lvl
Sell_price
Cost_price
datatype
varchar2
varchar2
number
varchar2
number
number
number
number
size
Name
Ivan
Vandana
Pramada
Basu
Ravi
Rukmini
city
Bombay
Madras
Bombay
Bombay
Delhi
Bombay
pincode
state
bal.due
400054
780001
400057
400056
100001
400050
Maharashtra
Tamilnadu
Maharashtra
Maharashtra
Delhi
Maharashtra
15000
0
5000
0
2000
0
Ans 2:
(i) Given data insert into client_master tableInsert into client_master values(0001,ivan,bombay,maharashtra,400054,15000);
Insert into client_master values(0002,vandana,madras,tamilnadu,780001,0);
Insert into client_master values(0003,pramada,bombay,maharashtra,400057,5000);
Insert into client_master values(0004,basu,bombay,maharashtra,400056,0);
Insert into client_master values(0005,ravi,delhi,delhi,100001,2000);
Insert into client_master values(0006,rukmini,bombay,maharashtra,400050,0);
Reorder
price
Sell
price
Cost
P00001 1.44floppies
P03453 Monitors
P06734 Mouse
P07865 1.22 floppies
P07868 Keyboards
P07885 CD Drive
P07965 540 HDD
P07975 1.44 Drive
P08865 1.22 Drive
20
3
5
20
3
3
3
3
3
525
12000
1050
525
3150
5250
8400
1050
1050
500
11200
500
500
3050
5100
8000
1000
1000
5
6
5
5
2
2
4
5
5
piece
piece
piece
piece
piece
piece
piece
piece
piece
100
10
20
100
10
10
10
10
2
iii)
iv)
v)
vi)
vii)
viii)
ix)
x)
SOLUTIONS
Date:--
Addition
Multiplication
Division
- Subtraction
** exponentiation
() Enclosed operation
Renaming columns used with Expression Lists: - The default output column names can be renamed by the user
if required
Syntax:
Select column name
Columnname
From table name;
result_columnname,
result_columnname,
Logical Operators:
The logical operators that can be used in SQL sentenced are
AND
OR
NOT
Percent (%) ; the % character matches any substring we consider the following examples.
Perry % matches any string beginning with perry
% idge % matches any string containing idge as substring.
- - - matches any string exactly three characters.
- - - % matches any string of at least of three characters.
Oracle functions:
Functions are used to manipulate data items and return result. function follow the format of function _name
(argument1, argument2 ..) .An arrangement is user defined variable or constant. The structure of function is
such that it accepts zero or more arguments.
Examples:
Avg
return average value of n
Syntax:
Avg ([distinct/all]n)
Min
return minimum value of expr.
Syntax:
MIN((distict/all )expr)
Count
Returns the no of rows where expr is not null
Syntax:
Count ([distinct/all)expr]
Count (*)
Returns the no rows in the table, including duplicates and those with nulls.
Max
Return max value of expr
Syntax:
Max ([distinct/all]expr)
Sum
Returns sum of values of n
Syntax:
Sum ([distinct/all]n)
Sorting of data in table
Syntax:
Select columnname, columnname
From table
Order by columnname;
Assignment No. # 2
Question.1 Using the table client master and product master answer the following Questionries.
i.
ii.
iii.
iv.
v.
vi.
vii.
viii.
ix.
x.
xi.
xii.
xiii.
SOLUTIONS
Date:--
Column Level Constraints: If the constraints are defined along with the column definition, it is called a
column level constraint.
Table Level Constraints: If the data constraint attached to a specify cell in a table reference the
contents of another cell in the table then the user will have to use table level constraints.
Null Value Concepts:- while creating tables if a row locks a data value for particular column that value
is said to be null . Column of any data types may contain null values unless the column was defined as
not null when the table was created
Syntax:
Create table tablename
(columnname data type (size) not null )
Primary Key: primary key is one or more columns are a table used to uniquickly identity each row in
the table. Primary key values must not be null and must be uniQuestion across the column. A
multicolumn primary key is called composite primary key.
Syntax: primary key as a column constraint
Create table tablename
(columnname datatype (size) primary key,.)
Primary key as a table constraint
Create table tablename
(columnname datatype (size), columnname datatype( size)
Primary key (columnname, columnname));
UniQuestion key concept:-A uniQuestion is similar to a primary key except that the purpose of a
uniQuestion key is to ensure that information in the column for each record is uniQuestion as with
telephone or devices license numbers. A table may have many uniQuestion keys.
Syntax: UniQuestion as a column constraint.
Create table table name
(columnname datatype (size) uniQuestion);
Syntax:
Create table tablename
(columnname datatype (size) default value,.);
Foreign Key Concept : Foreign key represents relationship between tables. A foreign key is column
whose values are derived from the primary key of the same of some other table . the existence of foreign
key implies that the table with foreign key is related to the primary key table from which the foreign key
is derived .A foreign key must have corresponding primary key value in the primary key table to have
meaning.
Foreign key as a column constraint
Syntax :
Create table table name
(columnname datatype (size) references another table name);
Foreign key as a table constraint:
Syntax :
Create table name
(columnname datatype (size).
primary key (columnname);
foreign key (columnname)references table name);
Check Integrity Constraints: Use the check constraints when you need to enforce intergrity rules that
can be evaluated based on a logical expression following are a few examples of appropriate check
constraints.
A check constraints name column of the coient_master so that the name is entered in upper case.
A check constraint on the client_no column of the client _master so that no client_no value starts
with c
Syntax:
Create table tablename
(columnname datatype (size) CONSTRAINT constraintname)
Check (expression));
Datatype
varchar2
Size
6
Sal_name
Address
City
State
Pincode
Sal_amt
Tgt_to_get
Ytd_sales
varchar2
varchar2
varchar2
varchar2
Number
Number
Number
Number
20
20
20
6
8,2
6,2
6,2
Attributes
Primary key/first letter
must start with s
Not null
Not null
Remarks
II.
Sales_order
Columnname
S_order_no
S_order_date
Varchar2
30
Datatype
varchar2
Date
Size
6
6
Client_no
Dely_add
Salesman_no
Varchar2
Varchar2
Varchar2
25
6
6
Dely_type
Billed_yn
Dely_date
Char
Char
Date
1
1
Order_status
Varchar2
10
Attributes
Primary/first letter must be 0
Primary key reference clientno
of client_master table
1. Sales_order_details:Column
Datatype
Size
Attributes
S_order_no
Varchar2
Product_no
Varchar2
Primary key/foreign
key references
s_order_no of
sales_order
Primary key/foreign
key references
product_no of
product_master
Qty_order
Qty_disp
Product_rate
Number
Number
Number
8
8
10,2
Insert the following data into their respective tables using insert statement:
Data for sales_man master table
Salesma
n_no
Salesman
name
Addre
ss
Cit
y
Pin
code
State
Salam
t
Tgt_to_g
et
Ytd
Sales
Remark
500001
Kiran
A/14
worli
Bo
mb
ay
40000
2
Mah
3000
100
50
Good
500002
Manish
65,nari
man
Bo
mb
ay
40000
1
Mah
3000
200
100
Good
500003
Ravi
P-7
Bandr
a
Bo
mb
ay
40003
2
Mah
3000
200
100
Good
500004
Ashish
A/5
Juhu
Bo
mb
ay
40004
4
Mah
3500
200
150
Good
Delay
date
20-jan96
27-jan96
20-feb96
07-apr96
22may-96
26may-96
Orderstat
us
Ip
S_orderdate
Client no
Bill
yn
N
Salesman no
0001
Dely
type
F
019001
12-jan-96
019002
25-jan-96
0002
50002
016865
18-feb-96
0003
500003
019003
03-apr-96
0001
500001
046866
20-may-96
0004
500002
010008
24-may-96
0005
500004
50001
C
F
F
C
Ip
Product no
P00001
P07965
P07885
Qty ordered
4
2
2
Qty disp
4
1
1
Product_rate
525
8400
5250
019002
046865
046865
019003
019003
046866
046866
010008
010008
P00001
P07868
P07885
P00001
P03453
P06734
P07965
P07975
P00001
10
3
10
4
2
1
1
1
10
SOLUTIONS
0
3
10
4
2
1
0
0
5
525
3150
5250
1050
1050
12000
8400
1050
525