You are on page 1of 59

Table of Contents

1. Entity Relationship Model................................................................................................................................................................................... 6


2. Designing Set of Relations .................................................................................................................................................................................... 7
Translation of Entity Relationship Diagram to Relational Schema (Ramez Elmasri, 2011) ...................................................................... 7
Final set of Relations .............................................................................................................................................................................................. 9
Data Dictionary ........................................................................................................................................................................................................ 9
3. Implementation of Database Design.................................................................................................................................................................. 11
Create Table .......................................................................................................................................................................................................... 11
Database Diagram ................................................................................................................................................................................................ 17
Insert Records........................................................................................................................................................................................................ 18
Insert query ............................................................................................................................................................................................................ 18
4. Querying Database............................................................................................................................................................................................... 23
Select Query .......................................................................................................................................................................................................... 23
Renaming Columns .............................................................................................................................................................................................. 24
Table Aliases.......................................................................................................................................................................................................... 24
Order by .................................................................................................................................................................................................................. 25
Where Clause ........................................................................................................................................................................................................ 26
AVG MAX ............................................................................................................................................................................................................... 27
Join .......................................................................................................................................................................................................................... 27
Union ....................................................................................................................................................................................................................... 28
Sub Query .............................................................................................................................................................................................................. 28
Select Into............................................................................................................................................................................................................... 29
5. Optimizing Database ............................................................................................................................................................................................ 29
Data Optimization Techniques ............................................................................................................................................................................ 29
Creating Index .......................................................................................................................................................................................................... 30
Defragment indexes .............................................................................................................................................................................................. 31
Index Suppression techniques ............................................................................................................................................................................ 34
6. Security................................................................................................................................................................................................................... 34
Security, Privileges and Roles............................................................................................................................................................................. 34
Create Users .......................................................................................................................................................................................................... 35
Users Access Levels ............................................................................................................................................................................................ 39
Creating Views ....................................................................................................................................................................................................... 43
Granting and Revoking Appropriate Privileges................................................................................................................................................. 44
7. Web Interface ........................................................................................................................................................................................................ 46
Insert Records........................................................................................................................................................................................................ 53
Update & Delete Records .................................................................................................................................................................................... 55
Query Existing Records........................................................................................................................................................................................ 56
Bibliography ................................................................................................................................................................................................................. 59
Table OF Figures

Figure 1 Create Table Customer .................................................................................................................................................................................. 11


Figure 2 Create Table Employees ................................................................................................................................................................................ 12
Figure 3 Create Table Books ........................................................................................................................................................................................ 12
Figure 4 Create Table Magazine .................................................................................................................................................................................. 13
Figure 5 create Table Courses ...................................................................................................................................................................................... 13
Figure 6 Create Table Courses ..................................................................................................................................................................................... 14
Figure 7 Create Table Workshop ................................................................................................................................................................................. 14
Figure 8 Create Table Tours ......................................................................................................................................................................................... 15
Figure 9 Create Table Places ........................................................................................................................................................................................ 15
Figure 10 Create Table Services ................................................................................................................................................................................... 16
Figure 11 List of All Tables ........................................................................................................................................................................................... 16
Figure 12 Database Diagram ........................................................................................................................................................................................ 17
Figure 13 Insert Records-Customer Table.................................................................................................................................................................... 18
Figure 14 Insert Records -Employees Table ................................................................................................................................................................. 18
Figure 15 Insert Records -Books Table......................................................................................................................................................................... 19
Figure 16 Insert Records-Magazine ............................................................................................................................................................................. 19
Figure 17 Insert Records-Courses ................................................................................................................................................................................ 20
Figure 18 Insert Records-Places ................................................................................................................................................................................... 20
Figure 19 Insert Records-Tours .................................................................................................................................................................................... 21
Figure 20 Insert Records-Workshop ............................................................................................................................................................................ 21
Figure 21 Insert Records-Lecture ................................................................................................................................................................................. 22
Figure 22 Insert Records-Services ................................................................................................................................................................................ 22
Figure 23 Select Query ................................................................................................................................................................................................. 23
Figure 24 Renaming Columns ...................................................................................................................................................................................... 24
Figure 25 Table Alias .................................................................................................................................................................................................... 24
Figure 26 Where Clause ............................................................................................................................................................................................... 26
Figure 27 AVG MAX ...................................................................................................................................................................................................... 27
Figure 28 Join ............................................................................................................................................................................................................... 27
Figure 29 Union ............................................................................................................................................................................................................ 28
Figure 30 Sub Query..................................................................................................................................................................................................... 28
Figure 31 Select Into .................................................................................................................................................................................................... 29
Figure 32 Clustered Index ............................................................................................................................................................................................ 30
Figure 33 Create Index ................................................................................................................................................................................................. 30
Figure 34 Fragmentation.............................................................................................................................................................................................. 31
Figure 35 rebuild a Defragmented Index ..................................................................................................................................................................... 32
Figure 36 Reorganize Index .......................................................................................................................................................................................... 33
Figure 37 Drop Index .................................................................................................................................................................................................... 34
Figure 38 Create New User .......................................................................................................................................................................................... 35
Figure 39 User Credentials and roles ........................................................................................................................................................................... 36
Figure 40 User2 Mapping ............................................................................................................................................................................................. 37
Figure 41 User3 Mapping ............................................................................................................................................................................................. 38
Figure 42 List of Users .................................................................................................................................................................................................. 38
Figure 43 User2 Login .................................................................................................................................................................................................. 39
Figure 44 user2 Queries ............................................................................................................................................................................................... 40
Figure 45 user3 Login ................................................................................................................................................................................................... 41
Figure 46 user3 Queries ............................................................................................................................................................................................... 41
Figure 47 Sa Queries .................................................................................................................................................................................................... 42
Figure 48 Create View .................................................................................................................................................................................................. 43
Figure 49 Grant View Permission ................................................................................................................................................................................. 43
Figure 50 User Properties ............................................................................................................................................................................................ 44
Figure 51 Grant & revoke Privileges ............................................................................................................................................................................ 45
Figure 52 User Interface............................................................................................................................................................................................... 46
Figure 53 SQL Data Source ........................................................................................................................................................................................... 47
Figure 54 Specify ID for Data Source ........................................................................................................................................................................... 48
Figure 55 Defining Connection..................................................................................................................................................................................... 49
Figure 56 Add Connection............................................................................................................................................................................................ 50
Figure 57 Select Statement .......................................................................................................................................................................................... 51
Figure 58 Advance Select Statement ........................................................................................................................................................................... 52
Figure 59 Test The Results ........................................................................................................................................................................................... 53
Figure 60 Insert Code ................................................................................................................................................................................................... 54
Figure 61 Insert Record Form ...................................................................................................................................................................................... 54
Figure 62 Inserted Record ............................................................................................................................................................................................ 55
Figure 63 Update Record ............................................................................................................................................................................................. 55
Figure 64 Update & Delete Code ................................................................................................................................................................................. 56
Figure 65 Search Option............................................................................................................................................................................................... 57
Figure 66 Search Results .............................................................................................................................................................................................. 57
Figure 67 Search Code ................................................................................................................................................................................................. 58
1. Entity Relationship Model

Address Date C_startdate


DOB C_desc
Desc
Contac# DOJ
Fee
CLName W_No C_No Contac# Has
Salary
Multivalued
CFName Attribute Workshops Courses DOB
Designation
ELname
CustomerSSNo Customer Sub Entity
D
Super Entity Disjoint EFname Address

Services Employees Super Entity


Has
Visits
Disjoint D Sub Entity
Provides
Guide Lecturer

Author Price SIH


G_SSno L_SSno

Desc Publisher

Organizes Assists
Book_ID Conducts
Books
Has Tours
Store_ID
P_ID
Billed
Has Bookstore Tours of
for
Lecture
P_Name
Super Entity Weak Entity
Place
Magazine Frequency P_type Desc
Lno
IS A
Price Location
L_SSno
Duration
Publisher
Mid Desc Historical Building Battlefield
Author
2. Designing Set of Relations

Translation of Entity Relationship Diagram to Relational Schema (Ramez


Elmasri, 2011)
Steps to translate ER diagram to Relational Schema conforming to BCNF
i. Translation of Real World Entity

Customer
CustomerSSno CName CFname CLname DOB Address Contact#

Employee
EmpSSno Designation EName EFname ELname Contact# DOB Address DOJ Salary

Guide Lecturer
Lecture

GSSno LSSno Lno L_SSno Desc Duration

Books
Book_ID Description Author Publisher Price

Magazine
Mid Description Author Publisher Price Frequency

Services
Recordno S_Type Service_ID Description CustomerSSno EmpSSno

Workshop
W_No Description Date

Courses
C_No C_Description C_Startdate Fee

Tours
T_ID T_Name T_Description P_ID Rates Seasons

P_ID P_Name P_Type Location

Place

ii. Translation of the Weak Entity


Weak Entity depends upon the Super entity .Primary key thus formed is
Composite key containing 1 attribute of weak entity and primary key of the
Super entity
Tours-(T_ID, T_Name, T_Description, P_ID, Rates, Seasons) Primary Key: T_ID, P_ID
Place-(P_ID, P_Name, P_Type, Location) Primary key: P_ID

iii) Translation of 1:1 Relationship


Lecturer conducts lecture.
Lecturer-L_SSno
Lecture-Lno, L_SSno, Description, Duration

iv) Translation of 1:M Relationship


Bookstore has Books
BookStore-Store_ID
Books-Book_ID, Description, Author, Publisher, Price

v) Translation of N:M Relationship


Guide-G_SSno
Tours-Tour_ID, T_Name, T_Description, P_ID, Rates, Season

vi) Translation of Multivalued Attributes


EmployeeContactNo can be Landline, Mobile

vii) Translation of Ternary Relationship

Bookstore has Books and Magazine

viii) Translation of Super/Sub type Entities


Services-Workshop, Courses
Places-Historical, Building, Battlefield
Final set of Relations

Data Dictionary

Relation Attributes Data size Primary-Key Foreign-key


Type
Customer CustomerSSno int CustomerSSno
C_Name varchar 200
DOB datetime
Address varchar 200
Contact# int

Relation Attributes Datatype size Primary-Key Foreign-key

Employees EmpSSno varchar 100 EmpSSno


Designation varchar 200
Ename varchar 200
Contact# int 10
DOB datetime
Address varchar 200
DOJ datetime
Salary int

Relation Attributes DataType size Primary-Key Foreign-key

Services Recordno int Recordno


S_Type varchar 100
Service_ID varchar 100 Service_ID
Description varchar 200
CustomerSSno int CustomerSSno
EmpSSno varchar EmpSSno

Relation Attributes DataType size Primary-Key Foreign-key

Workshop W_No varchar 100 W_No


Description varchar 200
Date datetime

Relation Attributes Datatype size Primary-Key Foreign-key

Courses C_No varchar 100 C_No


C_Description varchar 200
C_Startdate datetime
Fee int 10
Relation Attributes Datatype size Primary-Key Foreign-key

Lecture Lno int 10 Lno


L_SSno varchar 100 L_SSno
Description varchar 200
Duration varchar 100

Relation Attributes Datatype size Primary-Key Foreign-key

Books Book_ID varchar 100 Book_ID


Description varchar 200
Author varchar 200
Publisher varchar 200
Price decimal 6,2

Relation Attributes Datatype size Primary-Key Foreign-key

Magazine Mid varchar 100 Mid


Description varchar 200
Author varchar 200
Publisher varchar 200
Price decimal 6,2
Frequency int 10

Relation Attributes Datatype size Primary-Key Foreign-key

Tours T_ID varchar 100 T_ID


T_Name varchar 200
T_Description varchar 200
P_ID int
Rates decimal 6,2
Seasons varchar 200

Relation Attributes Datatype size Primary-Key Foreign-key

Places P_ID int P_ID


P_Name varchar 200
P_Type varchar 200
Location varchar 200
3. Implementation of Database Design
I have used Sql Server 2012 for creating database and for performing the tasks given.

First of All we will create a database named: SIH

After creation select the database and open the New query Window. (LeBlanc, n.d.)

Create Table

Figure 1 Create Table Customer


Figure 2 Create Table Employees

Figure 3 Create Table Books


Figure 4 Create Table Magazine

Figure 5 create Table Courses


Figure 6 Create Table Courses

Figure 7 Create Table Workshop


Figure 8 Create Table Tours

Figure 9 Create Table Places


Figure 10 Create Table Services

Figure 11 List of All Tables


Database Diagram
Employees
EmpSSno
Designation Lecture
Ename Lno
Contact# L_SSno
Courses
DOB Description C_No
Address Duration C_Desc
DOJ C_Startdate
Salary Fee

Places
P_ID
P_Name
P_Type
Location

Services
Customer Recordno
Tours
CustomerSSno T_ID
S_Type
C_Name T_Name
Service_ID
DOB T_Desc
Description
Address P_ID
CustomerSSno
Contact# Rates
EmpSSno
Seasons

Workshop
W_No
Description
Date

Magazine Books
Mid Book_ID
Description Description
Author Author
Publisher Publisher
Price Price
Frequency

Figure 12 Database Diagram


Insert Records

Insert query
Tables Along with data screenshot

Figure 13 Insert Records-Customer Table

Figure 14 Insert Records -Employees Table


Figure 15 Insert Records -Books Table

Figure 16 Insert Records-Magazine


Figure 17 Insert Records-Courses

Figure 18 Insert Records-Places


Figure 19 Insert Records-Tours

Figure 20 Insert Records-Workshop


Figure 21 Insert Records-Lecture

Figure 22 Insert Records-Services


4. Querying Database

Select Query

Figure 23 Select Query


Renaming Columns

Figure 24 Renaming Columns

Table Aliases

Figure 25 Table Alias


Order by
Where Clause

Use of Where clause along with Like (Wildcard)

Figure 26 Where Clause


AVG MAX

Figure 27 AVG MAX

Join
Inner Join between two tables on the basis of common column between the two tables.

Figure 28 Join
Union
Fetching unique values of addresses from the Employees and Customer table.

Figure 29 Union

Sub Query

Figure 30 Sub Query


Select Into

Figure 31 Select Into

5. Optimizing Database

Data Optimization Techniques

Indexing: An index is created on a table to search data quickly and efficiently. Though
users are not able to see the indexes, but it is important in speeding up the search
queries being made on the table. (Anon., n.d.)

In Microsoft SQL Server there are two types of Indexes

Clustered Index-it is an index that stores the actual data


Non-Clustered Index- is a pointer to the data

A table in general can have 1 clustered index and up to 999 non-Clustered Index. By
default when we create a table with Primary key that becomes the clustered index.

Create Index statement is used to create indexes in the table.


Creating Index

Select the Table expand the attributes of the table and expand the Indexes option.
There we can see there is already 1 Clustered Index.

Figure 32 Clustered Index

Creating another Index

Figure 33 Create Index

We can also use Unique Index that will prevent duplicate values.
Defragment indexes

Defragmentation is the process of re-organizing or rebuilding the index.


Checking the fragmentation of an index
Go to Indexes select the index right click select properties.

Figure 34 Fragmentation
Rebuild

Right click rebuild

Figure 35 rebuild a Defragmented Index


Reorganize Index

Figure 36 Reorganize Index


Index Suppression techniques

We can drop the index on the tables using Drop statement.


We cannot remove the index on the primary key.
While dropping proper care should be taken otherwise it may slow down the
performance. (Anon., n.d.)

Figure 37 Drop Index

6. Security

In SQL server different users can be created and assigned different roles. The different
types of roles are mentioned below:

Security, Privileges and Roles

Db_owner: can access, modify, and delete the records.DB_owner can also modify the
table structure.

DB_DataWriter: can only make changes to the records

DB_Datareader: can only read the records. (Harkins, 2004)


Create Users
Three Users

sa- default admin of the database. It can carry any operations on the database.
(db_owner)

User2-can insert, update the records but cannot change the table structure.
(db_datawriter)

User3-can only access(read) the records. (db_datareader)

Creating Users Screen

Figure 38 Create New User


Enter Credentials and select the respective roles

Figure 39 User Credentials and roles

Assigning User2 db_datawriter role for database SIH


Figure 40 User2 Mapping
Figure 41 User3 Mapping

Figure 42 List of Users


Users Access Levels

User2 Login

Figure 43 User2 Login


User2

For the user2 logged in we are unable to perform the select and the Alter command as
the user has been assigned the role of db_writer. Thus the user can only insert the
records.

Figure 44 user2 Queries


User3

Login

Figure 45 user3 Login

For user 3 we are only able to read the records but insert and alter commands cannot
be executed as it has been assigned role of db_datareader

Figure 46 user3 Queries


All the above mentioned command are successfully processed once we login as User-
sa.sa has the db_owner role.

Figure 47 Sa Queries
Creating Views

For creating different views expand the Views folder right click and create new View

Figure 48 Create View

Figure 49 Grant View Permission


Granting and Revoking Appropriate Privileges

We can change the permissions and give extra privileges by selecting the user and
right click properties

Figure 50 User Properties

Then click on the user mappings and we can change as per the roles desired.
Figure 51 Grant & revoke Privileges

7. Web Interface

Visual Studio 2013 has been used for developing the front User interface. (Microsoft,
n.d.)

User Interface

Figure 52 User Interface

SqlDataSource is being used to perform the update and Delete options .Searching and
insertion of records is done by SQL DataAdapter and SQL Command.

Adding New Data Source to the Grid view.


Figure 53 SQL Data Source
Steps for adding the Data Source

Figure 54 Specify ID for Data Source


Figure 55 Defining Connection
Figure 56 Add Connection
Figure 57 Select Statement
Figure 58 Advance Select Statement
Figure 59 Test The Results

Insert Records

For Inserting the records we need form to take inputs from the users .Clicking on the
new button will open the form. Enter all the details and click on the Submit button.

Code Required

Establishing the SQL connection

SqlConnection con = new SqlConnection("Data Source=MTR1-WIN8-PC4\\JAS;Initial


Catalog=SIH;User ID=sa;Password=sara");
Figure 60 Insert Code

Figure 61 Insert Record Form

After Submit Button Click

Figure 62 Inserted Record

Update & Delete Records

For updating record click on the Edit Button

Figure 63 Update Record


Code for Update and Delete

Figure 64 Update & Delete Code

Query Existing Records

We can search the records both by SSno and by Name.

Select the Search option and enter any value and click on the submit button.
Figure 65 Search Option

Figure 66 Search Results


Code to Search Records

Figure 67 Search Code


Bibliography
Anon., n.d. aa964133(v=sql.90). [Online]
Available at: https://technet.microsoft.com/en-us/library/aa964133(v=sql.90).aspx
[Accessed 20 Oct 2016].

Anon., n.d. sql_drop. [Online]


Available at: http://www.w3schools.com/sql/sql_drop.asp
[Accessed 25 October 2016].

Harkins, S., 2004. understanding-roles-in-sql-server-security. [Online]


Available at: http://www.techrepublic.com/article/understanding-roles-in-sql-server-security/
[Accessed 26 OCT 2016].

LeBlanc, P., n.d. Microsoft Sql Severe 2012. s.l.:Microsoft.

Microsoft, n.d. tw738475. [Online]


Available at: https://msdn.microsoft.com/en-us/library/tw738475.aspx
[Accessed 30 OCT 2016].

Ramez Elmasri, S. B., 2011. Fundamentals of database System. sixth ed. s.l.:Addison-Wesley.

You might also like