Upload
manya-sharma
View
248
Download
0
Embed Size (px)
Citation preview
8/8/2019 Computer File 4th Sem
1/35
Computer
Project Report
CONTENTS
8/8/2019 Computer File 4th Sem
2/35
Introduction
Codds Rule
Structure Query Language
Types of SQL commands
Views Indexes
Type of Index
Using Index
SQL Commands
Bibliography
8/8/2019 Computer File 4th Sem
3/35
INTRODUCTION
The foundation of Relational Database technology was laid by Dr. E.F. Codd, who in
his papers A Relational Model of Data for Large Shared Data Banks laid the basic
principle of the RDBMS. He laid down certain principle of database management,
referred to as relational model. The principles were soon applied to as experimental
systems, and start was made on the design of a database language that would
interact with such system.
The relational model was evolved through many stages since its inception in the
early 1970s. Performance of the model was an early concern. However, performance
can be measured in the two ways performance at execution time and performanceduring design and implementation. In the first case we measure the performance
when the database is under construction. In the second case the relational model
was a clear winner from the start outperforming both hierarchical and network
models by considerable margins. Although its critics attacked the slow processing
speeds of the earlier relational implementation, the relational model no longer
suffers from these shortcomings.
The access language SQL, together with its earlier predecessors and competitors,also received a lot of criticism. Early version of SQL was not uniform and
standardized in the treatment of certain concepts. But with the introduction of ANSI
SQL standards, this problem- the lack of standardization also got resolved.
CODDS RULE
8/8/2019 Computer File 4th Sem
4/35
Dr. E.F.Codd was the founder of RDBMS and he laid down certain principle of
database management system referred to as RDBMS. He provides a set of
following 12 rules which qualify a database product as relational:-
1. Information Rule All information in a relational database including table
names, column names is represented by values in a table. This view speeds
up the design and learning process. User productivity is improved since
knowledge of only one language is necessary to access all data such as table
description, attribute definition, integrity constraints, etc.
2. Guaranteed Access Rule Every piece of data in a relational database
can be accessed by using a combination of a table name, a primary key value
that uniquely identifies a record.
3. Systematic Treatment of Null Values The RDBMS handles records
that have unknown or inapplicable values in a pre-defined manner. Also, the
RDBMS distinguishes between zeroes, blanks and nulls in the records and
handles such values in a consistent manner that produces correct answers,
comparisons and calculations
4. Active On-lone Catalog Based on Relational Model : - The description
of database and its contents are database tables and therefore can be queried
online via data manipulation language. The Database Administrator
productivity is improve since changes and additions to catalog can be done
with same commands that are used to access any other table.
5. Comprehensive Data Sub-language Rule :- The RDBMS may support
several languages. But at least one of them should allow the user to do all the
8/8/2019 Computer File 4th Sem
5/35
following: define tables and views, query and update data, set integrity
constraints, set authorizations and define transactions. User productivity is
improved since there is just one approach that can be used for all database
operations.
6. View Updating Rule :- Any view that can be updated theoretically can be
updated using the RDBMS. Data consistency is ensured since the changes
made in the view are transmitted to base table and vice versa.
7. High Level Insert, Update and Delete :- The RDBMS supports insertion,
updating and deletion at a table level. The performance is improved since the
commands act on a set of records rather than one record at a time.
8. Physical Data Independence :- The execution of adhoc request and
application programs is not affected by changes in the physical data access
and storage methods. Database administrator can make changes to the
physical naccess and storage method which improve performance and do not
require changes in the application programs or requests.
9. Logical Data Independence :- Logical changes in tables and views such
as adding/deleting columns or changing field lengths need not necessitate
modifications in programs or in the format of ad hoc requests. The database
can change and grow to reflect changes in reality without requiring user
intervention or changes in the applications .
10. Integrity Independence :- Like table and view definitions, integrity
constraints are stored in the on-line catalog and can therefore be changed
8/8/2019 Computer File 4th Sem
6/35
without necessitating changes in the application programs. Integrity
constraints specific to a particular Relational Database must be definable in
relational data sub-language and storable in catalog.
11. Distribution Independence :- Application programs and ad hoc
requests are not affected by changes in distribution of physical data. This
improves system reliability since application programs will work even if the
programs and data are moved to different sites.
12. Non subversion Rule :- If the RDBMS has a language that access the
information of a record at a time, this language should not be used to bypass
the integrity constraints. This is necessary for data integrity.
STRUCTURE QUERY LANGUAGE
8/8/2019 Computer File 4th Sem
7/35
Structured Query Language (SQL) is the standard command set used to
communicate with the relational database management systems. All the tasks
related to relational data management creating tables, querying the database for
information, modifying the data in the database, deleting them, granting access to
users, and so on can be done using SQL. Different database vendors use differentdialects of SQL.
The basic features of all these different flavors are the same they have the same
base, the ANSI SQL standard. Implementation of the advanced features of SQL
differs from vendor to vendor, but here also the concepts are the same. So the SQL
skills are very much transferable. In other words, except for the vendor specific
enhancements, the SQL is the same.
An outstanding functionality of SQL is its support for automatic navigation to the
target data. This functionality enables the user to specify what information is
required and not how to get it. The SQL user does not need to know how data is
stored. The query language takes care of retrieving, replacing, adding and deleting
data thus making the database readily accessible to end users who have little or no
knowledge of programming.
Relational query processing for non-procedural language assumes simplification. Itis useful for task like data analysis, reporting and planning.
8/8/2019 Computer File 4th Sem
8/35
A procedural programming language needs every specification about data. These
includes storage, access mode and retrieval of data, file control operations like
open and close, and linking files and functions. SQL is simpler in operation than
procedural languages like C/C++ for database management. Procedural language
like C/C++ are more suited for system programs that interact with the hardware or
communicate between computers.
The greatest advantage of using SQL is that while developing applications,
programmers are more productive as they can concentrate on the essential logic
and data requirements of their applications. They do not have to spend time on the
detail of data representation.
Although SQL is the default language used in an RDBMS, MS-SQL Server has added
more features to the base level syntax of SQL and has its own SQL called Transact-
SQL (T-SQL).
8/8/2019 Computer File 4th Sem
9/35
TYPES OF SQL COMMANDS
SQL can be classified based on its functionality. SQL provides a comprehensive set
of commands for a variety of tasks including the following:
Querying Data
Updating, inserting and deleting data
Creating, modifying and deleting database objects
Controlling access to the database
Providing for data integrity and consistency
SQL statements are divided into the following categories:
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Query Language (DQL)
Data Control Language (DCL)
Data Administration Statements (DAS)
Transaction Control Statements (TCS)
8/8/2019 Computer File 4th Sem
10/35
FEATURES OF SQL
SQL is very powerful language that benefits all types of users of the RDBMS. You
use SQL to perform all tasks with the RDBMS. SQL is a very flexible language that
enables you to accomplish your development efforts. SQL enables you to work with
large groups of data rather than restricting you to single rows of data. Additionally,SQL permits the results of one query to be the input to another query statement.
1. Data Definition Language (DDL)
Data Definition Language is used to create, alter and delete database objects.
The commands used are CREATE, ALTER and DROP. The principal logical
data definition statements are CREATE TABLE, CREATE VIEW, CREATE
INDEX, ALTER TABLE, DROP TABLE, DROP VIEW and DROP INDEX.
2. Data Manipulation Language(DML)
Data Manipulation Language commands let users insert, modify and delete
the data in the database. SQL provides three data manipulation statement
INSERT, UPDATE and DELETE.
3. Data Query Language(DQL)
This is one of the most commonly used SQL statements. This SQL statement
enables the users to query one or more tables to get the information they
want. SQL has only one data query statement SELECT.
8/8/2019 Computer File 4th Sem
11/35
4. Data Control Language (DCL)
The data control language consists of commands that control the users
access to the database objects. Thus DCL is mainly related to the security
issues that are determining who has access to the database objects and what
operations they can perform on them. The task of the DCL is to prevent
unauthorized access to data. The Database Administrator (DBA) has thepower to give and take the privilege to a specific user, thus giving or denying
access to the data. The DCL commands are GRANT and REVOKE.
5. Data Administration Statements (DAS)
Data administration commands allow the user to perform audits and analysis
on operations within the database. They are also used to analyze the
performance of the system. Two data administration command are START
AUDIT and STOP AUDIT. One thing to be remembered here is that, data
administration is totally different from database administration. Database
administration is the overall administration of the database and data
administration is only a subset of that.
6. Transaction Control Statement (TCS)
Transaction control statements are statements, which manage all the
changes made by the DML statements. For example transaction statements
commit data. Some of the transaction control statements are COMMIT,
ROLLBACK, SAVEPOINT and SET TRANSACTION.
VIEWS
8/8/2019 Computer File 4th Sem
12/35
A view is a virtual table, which gives access to a subset of columns from one or
more tables. It is a query stored as an object in the database. Therefore, a view is an
object that derives its data from one or more tables. These tables are referred to as
the base or underlying tables.
A view serves as a security mechanism. It ensures that users are able to retrieve
and modify only the data which is seen by them. The remaining data in the
underlying tables can neither be seen nor accessed. The usage of complex queries
can also be simplified using views. Complex queries can be stored in the form of a
view and data from the view can be extracted using simple queries.
After the view is defined it can be referenced like any other table in the database.
Although a view is similar to a table, it is not stored in the database. It derives its set
of values from the underlying tables.
Syntax of Creating View :-
CREATE VIEW view-name [[column [,column].]]
AS sub query
[WITH CHECK OPTION];
The following figure illustrates the example of the view:-
8/8/2019 Computer File 4th Sem
13/35
Title
Title_Id Title Pub_Id Price
BU2075 You cannotcombat Computer stress
0736 19.99
MC3021 The GourmetMicrowave
0877 2.99
BU7832 Straight Talkabout Computer
1389 19.99
PS2091 Is Anger theEnemy
0736 10.95
TC7777 Sushi, Anyone? 0877 14.99
vwPublisherTitles
Pub_Name Title Price
New Moon Books You cannot combatComputer stress 19.99
Binnet & Hardly The GourmetMicrowave
2.99
Publisher
Pub_Id Pub_Name City State Country
0736 New MoonBooks
Boston MA USA
0877 Binnet &Hardly
Washington DC USA
1389 Algodatainfosystem
Berkeley CA USA
8/8/2019 Computer File 4th Sem
14/35
Algodata Infosystems Straight Talks aboutComputer
19.99
New Moon Books Is Anger the Enemy 10.95
Binnet & Hardly Sushi, Anyone? 14.99
View Derived from the Publishers and Titles Tables.
To get the output, like in the above table, you have to enter the following command:
CREATE VIEW vwPublisherTitles
AS
SELECT Pub_Name, Title, Price FROM Publishers JOIN Titles ON Publishers.Pub_Id= Titles.Pub_Id
The output can be seen using views by entering the following simple SELECT
statement: SELECT * FROM vwPublisherTitles
Views ensure security of data by restricting access to:
Specific rows of table
Specific columns of a table
Specific rows and columns of a table
The rows fetched using joins
The statistical summary of data in a given table
8/8/2019 Computer File 4th Sem
15/35
The subsets of another view or a subset of views and tables
Advantages of Views
Views provide several advantages:
Providing relevant data for users
Hiding data complexity
Organizing data from heterogeneous sources
Reducing the object size.
INDEXES
An index is a structure that provides faster access to the rows of a table based on
the values of one or more columns. The index stores data values and pointers to the
rows where those data values occur. In the index the data values are stored and
stored in the ascending or descending order. So, the RDBMS can quickly search the
index to find a particular data values and hence the row associated with it.
8/8/2019 Computer File 4th Sem
16/35
GUIDELINES FOR INDEX CREATION ARE:-
A column that is often accessed in a sorted order should be indexed so that
the system can take advantage of indexed order.
Columns that are regularly used in joins should be indexed, because the
RDBMS can perform the join faster.
If the number of updates operations (INSERT, UPDATE and DELETE) is more,
compared to the querying operations, then indexing the table will affect
performance, because every time a table is updated, the index also has to be
sorted.
Indexing on columns that have one or two values like gender will not have
any real performance improvement.
Indexing small tables with only a few rows does not improve the
performance.
TYPE OF INDEXES
COMPOSITE INDEX
When an index is made up of more that one column it is called composite index.
Composite index columns do not have to be specified in the same order as in the
8/8/2019 Computer File 4th Sem
17/35
CREATE TABLE statement. You can use any order you want for better performance,
it is a good idea to start with the column that you use most often in searches.
Create index inn_name on Emp(LNAME, FNAME)
UNIQUE INDEX : -
A unique index is one in which no two rows are permitted or in which no duplicate
values are allowed for the same index value. These indexes are usually created on
the primary key of a table. You specify the index as a unique index by using the
keyword UNIQUE. For unique indexes, the RDBMS checks for duplicate values
when the index is created and each time new data is added.
CLUSTERED INDEX : -
Many RDBMS offer you the choice of making your index clustered or non-clustered.
When you create a clustered index, it means that the system will sort the rows of a
table when there is a change made to the index. In non-clustered index, the physical
order of the rows is not the same as their indexed order. There can be as many non-
clustered indexes per table. Clustered indexes are much faster than non-clustered
ones.
USING INDEXES
A unique index on the primary key prevents duplicate and gurantees that every
value in primary key column will infact uniquely identifies the row.
8/8/2019 Computer File 4th Sem
18/35
A column that is often accessed in a sorted order should be indexed so that the
system can take advantage of indexed order.
Command to Dropping an Index
Drop index indexname;
SQL COMMANDSCREATING TABLE : -
Example 1:- The detail about a newspaper that publishes advertisement for
8/8/2019 Computer File 4th Sem
19/35
Tebisco, Inc to be stored in a Recruitment database
Attributes Data
Newspaper code 0001
Newspaper Name Hindustan Times
Region Delhi
Type of Newspaper General
Contact Person Mr. xyz
HO Address Xyz New Delhi-01
City New Delhi
State Delhi
Zip 110001
Country Code 001
Phone (011)2343242
Solution:-
CREATE Statement : -
CREATE TABLE Newspaper
(
cNewspaperCode char(4) NOT NULL,
8/8/2019 Computer File 4th Sem
20/35
cNewspaperName char(20) NOT NULL,
vRegion varchar(20),
vTypeofNewspaper varchar(20),
vContactPerson varchar(35),
vHOAddress varchar(35),cCity char(20),
cState char(20),
cZip char(10),
cCountryCode char(3),
cPhone char(15)
)
2.INSERT Statement :-
Insert statement is used to insert the values in a table. To insert values in above
table, command should be:
INSERT into Newspaper
VALUES (0001 , Hindustan Times , Delhi , General,Mr. xyz, xyz NewDelhi , New Delhi, Delhi, 110001, 001, (011)2343242 )
3. DROP Statement: -
DROP statement is used to remove the table from the database.
DROP TABLE Newspaper
The above command will delete the Newspaper table from the database.
Example 2 :-
8/8/2019 Computer File 4th Sem
21/35
Let us take table named Record
Table: Record
vFirstName vLastName cCity cPhone
Rahul Sharma Delhi 2222222
Sanjay Gupta Punjab 4444444
4. SELECT Statement
SELECT statement can be used to access and retrieve data from the database.
SELECT * from Record
Selecting columns: - SELECT statement can be used to retrieve specific columns
from a table by specifying the column name from the table.
Syntax:
SELECT column_name1[, column_name2,]
FROM table_name
QUERY
8/8/2019 Computer File 4th Sem
22/35
SELECT VFirstName, cCity
FROM Record
The above statement will retrieve VFirstName and cCity from Record
OUTPUT
vFirstName cCity
Rahul Delhi
Sanjay Punjab
5. WHERE StatementWHERE Statement is used with SELECT statement to display the Selected files
from a table.
Syntax:
SELECT column_list FROM table_name
WHERE search_condition
QUERY
SELECT vFirstName, vLastName
FROM Record
WHERE cCity = Delhi
The above query will show the vFirstName and vLastName of record where cCity is
8/8/2019 Computer File 4th Sem
23/35
Delhi
OUTPUT
vFirstName vLastName
Rahul Sharma
EXAMPLE 3:-
ID Title Author Publisher Year Price
8/8/2019 Computer File 4th Sem
24/35
A01 A paintedHouse
Grishan Random House 2001 195.55
A02 Abduction Cook Pan Books 2000 360
A03 Airport Hailey Corgi Books 1968 175.45
B01 Biplane Bach Dell Books 1966 283.35
B02 Bloodline Sheldon Warner Books 1977 100.15
B03 Blue Gold Cussler Simon & Schuster 2000 285
C01 Catch 22 Heller Random House 1994 250.1
D01 Doctors Segal Bantam Books 1988 150
D02 Dragon Cussler Harper Collins 1990 123.55
F01 Flood Tide Cussler Simon & Schuster 1997 414.5
H01 Hawaii Michener Mandarin Books 1959 124.5
H02 Hotel Hailey Corgi Books 1965 175.75
I01 Icon Forsyth Corgi Books 1996 182.35
I02 Illusion Bach Dell Books 1997 330
I03 Inca Gold Cussler Harper Collins 1994 124
I04 Invasion Cook Pan Books 1997 177.9
O01 One Bach Dell Books 1988 289
8/8/2019 Computer File 4th Sem
25/35
P01 Prizes Segal Bantam Books 1995 256.1
S01 Serpent Cussler Simon & Schuster 1999 532.8
S02 Sheba Higgins Signet Books 1995 125
T01 The Class Segal Bantam Books 1985 145.8
T02 TheRunner
Reich Headline Books 2000 199.95
T03 TheSimpleTruth
Baldacci Simon & Schuster 1997 200
T04 Thunder Point
Higgins Signet Books 1993 95.65
T05 Timeline Crichton Centuary Books 1999 623.3
V01 Vector Cook Macmillian 1999 424.8
6.Distinct :
Eliminating Duplicates Select using DISTINCT
SELECT (DISTINCT) Publisher
FROM BOOK;
The result will be the name of the publishers, with no duplicate as shown below:
8/8/2019 Computer File 4th Sem
26/35
Publisher
Bantam Books
Century Books
Corgi Books
Dell Books
Harper Collins
Headline Books
Macmillan
Mandarin Books
Pan Books
Random House
Signet Books
Simon & Schuster
Warner Books
7.Select Using IN:-
8/8/2019 Computer File 4th Sem
27/35
SELECT Title, Author, Publisher, Year
FROM Book
WHERE Year IN(1999,2000)
OUTPUT
Title Author Publisher Year
Abduction Cook Pan Books 2000
Blue Gold Cussler Simon & Schuster 2000
Serpent Cussler Simon & Schuster 1999
The Runner Reich Headline Books 1999
Timeline Crichton Century Books 1999
Vector Cook Macmillan 1999
8. Select Using BETWEEN
BETWEEN can be used to get those items that fall within a range
SELECT Title, Author, Publisher, Price
FROM Book
WHERE Price BETWEEN 400 AND 600;
OUTPUT
8/8/2019 Computer File 4th Sem
28/35
Title Author Publisher Price
Flood Tide Cussler Simon & Schuster 414.5
Serpent Cussler Simon & Schuster 532.8
Vector Cook Macmillan 424.8
9. SELECT using LIKE
Like is very popular clause and also very useful. E.g. if you want to get all the
details of the books whose publishers name starts with S, use LIKE as follows:
SELECT Title, Author, Publisher
FROM Book
WHERE Publisher LIKE S%;
OUTPUT
Title Author Publisher
Blue Gold Cussler Simon & Schuster
Flood Tide Cussler Simon & Schuster
Serpent Cussler Simon & Schuster
Sheba Higgins Signet Books
The Simple Truth Baldacci Simon & Schuster
8/8/2019 Computer File 4th Sem
29/35
10.Ordering While Selecting
For getting the result in a particular order we use thr ORDER BY clause. Consider
the query Get all the distributor details in descending order of discount. The SQL
statement will be as follows:
QUERY
SELECT Title, Author, Publisher, Price FROM Book
WHERE Price>300
ORDER BY Price DESC;
OUTPUT
Title Author Publisher Price
Timeline Crichton Century Books 623.3
Serpent Cussler Simon & Schuster 532.8
Vector Cook Macmillan 424.8
Flood Tide Cussler Simon & Schuster 414.5
Abduction Cook Pan Books 360
Illusion Bach Dell Books 330
8/8/2019 Computer File 4th Sem
30/35
11. AND, OR & NOT
QUERY
SELECT * FROM Book
WHERE ( Year = 1997 OR Price > 200)
AND Publisher IN (Simon & Schuster, Dell Books)
OR Author LIKE C% AND NOT ID = A02;
OUTPUT
ID Title Author Publisher Year Price
B01 Biplane Bach Dell Books 1966 283.35
B03 Blue Gold Cussler Simon & Schuster 2000 285
F01 Flood Tide Cussler Simon & Schuster 1997 414.5
I02 Illusion Bach Dell Books 1997 330
I04 Invasion Cook Pan Books 1997 177.9
O01 One Bach Dell Books 1988 289
S01 Serpent Cussler Simon & Schuster 1999 532.8
T03 The SimpleTruth
Baldacci Simon & Schuster 1997 211
T05 Timeline Crichton Century Books 1999 623.3
8/8/2019 Computer File 4th Sem
31/35
AGGREGATE FUNCTIONS
Consider a table Employee
Empno Name Deptid Basic Hra Deduction Tax
100 Joseph D1 8000 1600 2000 2400
101 Erich D2 6000 1200 0 1500
102 Ken D1 5500 1100 450 1375
103 Tom D1 4800 960 200 1200
104 Jeffrey D2 6400 1280 1400 1920
105 Clive D3 5600 1120 560 1120
106 Robin D4 4700 940 580 940
107 Michael D3 5300 1060 1600 1325
108 Stephen D4 4200 840 0 840
109 Sidney D4 6800 1360 1000 2040
110 Robert D2 7000 1400 1250 2100
111 Jack D1 7500 1500 500 2250
8/8/2019 Computer File 4th Sem
32/35
1. COUNT()
COUNT() is used to count the number of values in a column.
QUERY 1 :- Get the number of employees in the department D1
SELECT COUNT(Name)
FROM Employee
WHERE Deptid = D1
OUTPUT :- 4
QUERY 2 :- Get the number of employees in the department D1 and basic pay lessthan 6000
SELECT COUNT(Name)
FROM Employee
WHERE Deptid = D1
AND Basic < 6000
OUTPUT:- 2
2. SUM()
SUM() is used to find the sum of the values in a column.
QUERY 1:-Find the total basic pay for all the employees in the organization.
SELECT SUM(Basic)
FROM Employee;
OUTPUT:- 71800
8/8/2019 Computer File 4th Sem
33/35
QUERY 2 :- Find the total Basic Pay for all the employees in the department D1
SELECT SUM(Basic)
FROM Employee
WHERE Deptid=D1
OUTPUT:- 25800
3.AVG()
This function is used to find the average of the values in a column.
QUERY 1 :- Find the average HRA of an employee.
SELECT AVG(HRA)
FROM Employee
OUTPUT:- 1196.67
QUERY 2 :- Find the average pay of an employee in the department D1 whose HRA
is greater than 1000.
SELECT AVG(Basic + Hra Deductions Tax)
FROM Employee
WHERE Deptid = D1
AND HRA > 1000
OUTPUT:- 5408.33
8/8/2019 Computer File 4th Sem
34/35
4. MAX() and MIN()
MAX() is used to find the maximum value in a column.
MIN() is used to find the minimum value in a column
QUERY 1:-Find the name of the Employee who gets the maximum basic pay.
SELECT Name
FROM Employee
WHERE Basic = (SELECT MAX(Basic) FROM Employee);
OUTPUT:-
Name
Joseph
8/8/2019 Computer File 4th Sem
35/35
BIBLIOGRAPHY
1. Database Management System
by Alexion Leon and Mathews Leon
2. Database Management System
By Rajesh Narang
3. NIIT Study Material
4. Aptech Study Material
5. Faculty: Mrs. Deepa Gangwani