Dirs Lab Manual

download Dirs Lab Manual

of 18

Transcript of Dirs Lab Manual

  • 8/10/2019 Dirs Lab Manual

    1/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 1

    DIRS MANUAL

    Introduction

    Database: it is a collection of related data, where data is a fact that can be recorded.

    DBMS (database management system): is a collection of programs that enables the user to create andmanipulate the data.

    Schema: The description of database is called database schema. Which is specified during thedatabase design and diagram to represent schema is called schema diagram.

    Relational model concepts:

    The relational model represents the database as a collection of relation. each relation resembles thetable of value or flat file of records.

    The relation is thought of an a table of values then each row in the table represents a collection ofrelated data values. In relation each row is called tuple and column headers are called as attribute.

  • 8/10/2019 Dirs Lab Manual

    2/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 2

    1. Identify the Entity, Attributes, relationships for given problemdefinition.

    Entity: its a real world object which has an individual existence.Example: Student, course and employee etc.

    Attributes: these are the properties of the entity which describe them.Example: For entity student-> Name, USN, address and phone number are the attributes.

    Relationship: To relate one entity to another we need something called relationship betweentwo entities. As data is related to each other we consider that relation and connect the entities.

    In above diagram EMPLOYEE and DEPARTMENT are Entity and MANAGES arerelationship between those entity.

    Types of Attributes

    1. Composite vs simple attributes

    Composite attributes can be divided into smaller subpart, which represents the more basic attributes with independent meanings.Example is Address of an person, it can be subdivided into street_address, city, State,and Zip etc.

    Simple attributes are non divisible. Example University Seat Number of student.

    2. Single valued vs Multi valuedMost of attributes have single value for entity are called single valued.Example Age of person

    Multi valued attribute may have lower and upper bounds to constrain the number ofvalues allowed for each individual entity.Example College Degree

    Cardinality of Relationship

    While creating relationship between two entities, we may often need to face the cardinality problem. This simply means that how many entities of the first set are related to how manyentities of the second set. Cardinality can be of the following three types.

    MANAGESEMPLOYEE DEPARTMENT

  • 8/10/2019 Dirs Lab Manual

    3/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 3

    One-to-One

    Only one entity of the first set is related to only one entity of the second set. E.g. A teacherteaches a student. Only one teacher is teaching only one student. This can be expressed in the

    following diagram as:

    One-to-Many

    Only one entity of the first set is related to multiple entities of the second set. E.g. A teacherteaches students. Only one teacher is teaching many students. This can be expressed in the

    following diagram as:

    Many-to-One Multiple entities of the first set are related to multiple entities of the second set. E.g. Teachersteach a student. Many teachers are teaching only one student. This can be expressed in thefollowing diagram as:

    Many-to-Many Multiple entities of the first set is related to multiple entities of the second set. E.g. Teachersteach students. In any school or college many teachers are teaching many students. This can

    be considered as a two way one-to-many relationship. This can be expressed in the followingdiagram as:

    The Concept of KeysA key is an attribute of a table which helps to identify a row. There can be many differenttypes of keys which are explained here.

  • 8/10/2019 Dirs Lab Manual

    4/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 4

    Super Key or Candidate Key: It is such an attribute of a table that can uniquely identify arow in a table. Generally they contain unique values and can never contain NULL values.There can be more than one super key or candidate key in a table e.g. within a STUDENTtable Roll and Mobile No. can both serve to uniquely identify a student.

    Primary Key: It is one of the candidate keys that are chosen to be the identifying key for theentire table. E.g. although there are two candidate keys in the STUDENT table, the collegewould obviously use Roll as the primary key of the table.

    Alternate Key: This is the candidate key which is not chosen as the primary key of the table.They are named so because although not the primary key, they can still identify a row.

    Composite Key: Sometimes one key is not enough to uniquely identify a row. E.g. in a singleclass Roll is enough to find a student, but in the entire school, merely searching by the Roll isnot enough, because there could be 10 classes in the school and each one of them may containa certain roll no 5. To uniquely identify the student we have to s ay something like class VII,roll no 5. So, a combination of two or more attributes is combined to create a uniquecombination of values, such as Class + Roll.

    Foreign Key: Sometimes we may have to work with an attribute that does not have a primarykey of its own. To identify its rows, we have to use the primary attribute of a related table.Such a copy of another related tables primary key is called foreign key.

    Strong and Weak EntityBased on the concept of foreign key, there may arise a situation when we have to relate anentity having a primary key of its own and an entity not having a primary key of its own. Insuch a case, the entity having its own primary key is called a strong entity and the entity nothaving its own primary key is called a weak entity. Whenever we need to relate a strong and aweak entity together, the ERD would change just a little.

    Say, for example, we have a statement A Student lives in a Home. STUDENT is obviously astrong entity having a primary key Roll. But HOME may not have a unique primary key, asits only attribute Address may be shared by many homes (what if it is a housing estate?).HOME is a weak entity in this case.The ERD of this statement would be like the following

    As you can see, the weak entity itself and the relationship linking a strong and weak entitymust have double border.

  • 8/10/2019 Dirs Lab Manual

    5/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 5

    Consider the Company database and Draw ER diagram.

  • 8/10/2019 Dirs Lab Manual

    6/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 6

    2. Enhanced Entity Relationship(EER) Diagram

    Here in this section we come across the concept of class and subclass relationships and typeinheritance.EER has concept of subclass called as specialization and superclass called asgeneralization.

    EMPLOYEE (class or superclass)

    SECRETARY TECHNICIAN ENGINEER (subclass)

    Super class and subclass has a relationship called as IS A relationship. i.e. we calledENGINEER is a EMPLOYEE.

    Specialization and Generalization

    - Specialization is a process of defining a set of subclass of an entity type this entity typecalled super-class of a specialization.

    - The set of subclasses that form a specialization defined on the basis of somedistinguishing characteristics of the entities in the super class.

    Specialization Process allows us to do following

    1. Define a set of subclasses of an entity2. Establish additional specific attributes with subclass.3. Establish additional specific relationship types between each subclass and other entity

    type or subclasses.- Now think of reverse process of abstraction in which we suppose the difference among

    several entities.- First identify their common features and generalize into single super class of which the

    original entity type are special subclass.

    Generalization

    The process of generalizing entities, where the generalized entities contain the properties ofall the generalized entities is called Generalization. In generalization, a number of entities are

    brought together into one generalized entity based on their similar characteristics. For anexample, pigeon, house sparrow, crow and dove all can be generalized as Birds.

  • 8/10/2019 Dirs Lab Manual

    7/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 7

    Generalization

    Specialization

    Specialization is a process, which is opposite to generalization, as mentioned above. Inspecialization, a group of entities is divided into sub-groups based on their characteristics.Take a group Person for example. A person has name, date of birth, gender etc. These

    properties are common in all persons, human beings. But in a company, a person can be

    identified as employee, employer, customer or vendor based on what role do they play incompany.

    Similarly, in a school database, a person can be specialized as teacher, student or staff; based on what role do they play in school as entities.

    Difference between generalization and specialization

  • 8/10/2019 Dirs Lab Manual

    8/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 8

    3. Relational Model Concepts and Relational Algebra

    Relational model was first introduced by Ted Codd of IBM Research in 1970 in classic paper.due to its mathematical foundation and simplicity it got more importance. Relational model

    represents the database as a collection of relations.

    Important terminologies in relational model

    Tables: In relation data model, relations are saved in the format of Tables. This format storesthe relation among entities. A table has rows and columns, where rows represent records andcolumns represents the attributes.

    Tuple: A single row of a table, which contains a single record for that relation is called atuple.

    Relation instance: A finite set of tuples in the relational database system represents relationinstance. Relation instances do not have duplicate tuples.

    Relation schema: This describes the relation name (table name), attributes and their names.

    Relation key: Each row has one or more attributes which can identify the row in the relation(table) uniquely, is called the relation key.

    Attribute domain: Every attribute has some pre-defined value scope, known as attributedomain.

    Definition of Keys

    1. Primary Keya. It is a key whose values are used to identify tuples in a relation.

    b. It will be underlined in relational schema.c. Value of primary key cannot be NULL.

    2. Foreign Keya. It is a key which is a primary key in our relation R1(table) and used in another

    relation R2 (table)

    R1

    SSN NAME ..

    R2

    In above example ssn and courseno are primary keys as they identify each row in tableuniquely and SSN in table R2 is foreign key.

    CourseNO CName SSN

  • 8/10/2019 Dirs Lab Manual

    9/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 9

    Constraints

    Every relation has some conditions that must hold for it to be a valid relation. Theseconditions are called Relational Integrity Constraints. There are three main integrityconstraints.

    Key Constraints

    Domain constraints

    Referential integrity constraints

    KEY CONSTRAINTS: A primary key serves as the unique identifier for rows in the table.

    The syntax of the primary key constraint (following the Constraint keyword and the

    constraint name, if they're specified) is

    Primary Key( column-name , ... )

    Each primary key column's definition must include Not Null.

    For a table with a primary key constraint, blocks any attempt to insert or update arow that would cause two rows in the same table to have identical value(s) fortheir primary key column(s).

    A table definition can have no more than one primary key constraint.

    DOMAIN CONSTRAINTS:

    Domain integrity means the definition of a valid set of values for an attribute. You define

    - data type,

    - lenght or size

    - is null value allowed

    - is the value unique or not

    for an attribute.

    You may also define the default value, the range (values in between) and/or specific values

    for the attribute. These definitions ensure that a specific attribute will have a right and proper

    value in the database.

    REFERENTIAL INTEGRITY CONSTRAINTS:

    constraint prevents any actions that would destroy link between tables with the

    corresponding data values. A foreign key in one table points to a primary key in another

    table. Foreign keys prevent actions that would leave rows with foreign key values when there

    are no primary keys with that value. The foreign key constraints are used to enforcereferential integrity.

  • 8/10/2019 Dirs Lab Manual

    10/18

  • 8/10/2019 Dirs Lab Manual

    11/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 11

    Notation: A1, A2, An (r)Where a 1, a2 , an are attribute names of relation r.

    Duplicate rows are automatically eliminated, as relation is a set.

    for example:

    subject , author (Books )

    Selects and projects columns named as subject and author from relation Books.

    Union Operation ( )

    Union operation performs binary union between two given relations and is defined as:

    r s = { t | t r or t s}

    Notion: r U s

    Where r and s are either database relations or relation result set (temporary relation).

    For a union operation to be valid, the following conditions must hold:

    r, s must have same number of attributes.

    Attribute domains must be compatible.

    Duplicate tuples are automatically eliminated.

    author (Books ) author (Articles )

    Output : Projects the name of author who has either written a book or an article or both.

    Set Difference ( )

    The result of set difference operation is tuples which present in one relation but are not in thesecond relation.

    Notation: r s

    Finds all tuples that are present in r but not s.

    author (Books ) author (Articles )

    Output: Results the name of authors who has written books but not articles.

    Cartesian Product ()

    Combines information of two different relations into one.

    Notation: r s

    Where r and s are relations and there output will be defined as:

  • 8/10/2019 Dirs Lab Manual

    12/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 12

    r s = { q t | q r and t s}

    author = 'tutorialspoint' (Books Articles )

    Output : yields a relation as result which shows all books and articles written bytutorialspoint.

    Rename operation ( ) Results of relational algebra are also relations but without any name. The rename operationallows us to rename the output relation. rename operation is denoted with small greek letterrho

    Notation: x (E)

    Where the result of expression E is saved with name of x.

    Additional operations are:

    Set intersection

    Assignment

    Natural join

  • 8/10/2019 Dirs Lab Manual

    13/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 13

    4. Mapping of ER to Relational Model

    ER Model when conceptualized into diagrams gives a good overview of entity-relationship,which is easier to understand. ER diagrams can be mapped to Relational schema that is, it is

    possible to create relational schema using ER diagram. Though we cannot import all the ERconstraints into Relational model but an approximate schema can be generated.

    There are more than one processes and algorithms available to convert ER Diagrams intoRelational Schema. Some of them are automated and some of them are manual process. Wemay focus here on the mapping diagram contents to relational basics.

    ER Diagrams mainly comprised of:

    Entity and its attributes

    Relationship, which is association among entities.

    Mapping Entity

    An entity is a real world object with some attributes.

    Mapping Process (Algorithm):

    [ Image: Mapping Entity ]

    Create table for each entity

    Entity's attributes should become fields of tables with their respective data types.

    Declare primary key

    Mapping relationship

    A relationship is association among entities.

    Mapping process (Algorithm):

  • 8/10/2019 Dirs Lab Manual

    14/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 14

    [ Image: Mapping relationship ]

    Create table for a relationship

    Add the primary keys of all participating Entities as fields of table with their respective data

    types. If relationship has any attribute, add each attribute as field of table.

    Declare a primary key composing all the primary keys of participating entities.

    Declare all foreign key constraints.

    Mapping Weak Entity Sets

    A weak entity sets is one which does not have any primary key associated with it.

    Mapping process (Algorithm):

    [ Image: Mapping Weak Entity Sets ]

    Create table for weak entity set

    Add all its attributes to table as field

    Add the primary key of identifying entity set

    Declare all foreign key constraints

    Mapping hierarchical entities

    ER specialization or generalization comes in the form of hierarchical entity sets.

    Mapping process (Algorithm):

    [ Image: Mapping hierarchical entities ]

    Create tables for all higher level entities

  • 8/10/2019 Dirs Lab Manual

    15/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 15

    Create tables for lower level entities

    Add primary keys of higher level entities in the table of lower level entities

    In lower level tables, add all other attributes of lower entities.

    Declare primary key of higher level table the primary key for lower level table

    Declare foreign key constraints.

    Consider the Company database and Draw Relational Schema.

    EMPLOYEE

    Ssn Fname Lname Salary Address Dob Dnum

    DEPARTMENT

    Dno Dname Dlocation Mgr_Joining Date Mrg_Ssn

    PROJECT

    Pno Pname Plocation Dnum

    WORKS_ON

    WN_No ESsn Pnum Hrs

    DEPENDENT

    Depen_No Name Gender Bod Relation DSsn

  • 8/10/2019 Dirs Lab Manual

    16/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 16

    5. Understand the Working environment Microsoft SQL Server 2008

    What is SQL Server Management Studio?

    SQL Server Management Studio (SSMS) is the main administration console for SQL Server.

    SSMS enables you to create database objects (such as databases, tables, stored procedures, viewsetc), view the data within your database, configure user accounts, perform backups, replication,transfer data between databases, and more.

    SQL Server Management Studio is a graphical user interface; so many tasks are "point and click".It is also the interface that enables you to run SQL scripts, so there are also tasks that require

    programming/scripting. However, many tasks can be performed either via GUI or SQL script, soit's your choice which one you use. For example, you can create a database using the GUI or byrunning a SQL script.

    What does SQL Server Management Studio looks like?

    When you open up SQL Server Management Studio, you will be prompted to connect toSQL Server with a log in screen that looks like the following screenshot. You can either keepthe default authentication settings or change them.:

    Here's what SQL Server Management Studio looks like once you've connected (and opened anew query):

  • 8/10/2019 Dirs Lab Manual

    17/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 17

    Lets use SQL Scripting for Creating Database and steps are as follows:

    Step1: Goto start menu -> all programs -> click on Microsoft Sql Server 2008 -> click on Sqlserver management studio

    Then following screen appears

    Then click on connect, dont change anything on screen. Now you will get into sql server, tocreate run your sql query we need query window so click on option called New query

    Now create the database using new query window

    Syntax : CREATE DATABASE db_name;

    Example CREATE DATABASE Company;

    1. Then click on ! Execute symbol or Press F5. And also save your query by using filemenu-> save as option for saving the query.

    2. Now from dropdown in above window select your database and if your database isnot available then click on Databases side menu and click on refresh option.

  • 8/10/2019 Dirs Lab Manual

    18/18

    DIRS LAB MANUAL 2014

    Computer Engineering Dept Page 18

    Now we will create the tables in our database company.

    1. First select your database from dropdown then type the create table command forEmployee with required data type and constraints.

    2. Now select the query that need to be executed and click on ! Execute symbol. Thenrefresh your database, you can check your table created.

    Similarly you can execute all the commands using query window.