Database Systems -F.Y.CS. -Prof Valerie

Similar documents
Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

We know how to query a database using SQL. A set of tables and their schemas are given Data are properly loaded

Lesson 8: Introduction to Databases E-R Data Modeling

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

not necessarily strictly sequential feedback loops exist, i.e. may need to revisit earlier stages during a later stage

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

COMP 378 Database Systems Notes for Chapter 7 of Database System Concepts Database Design and the Entity-Relationship Model

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

Foundations of Information Management

2. Conceptual Modeling using the Entity-Relationship Model

Entity-Relationship Model

EECS 647: Introduction to Database Systems

Chapter 7 Data Modeling Using the Entity- Relationship (ER) Model

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

The Relational Data Model: Structure

How To Manage Data In A Database System

Chapter 2: Entity-Relationship Model. E-R R Diagrams


There are five fields or columns, with names and types as shown above.

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

Chapter 2: Entity-Relationship Model

THE ENTITY- RELATIONSHIP (ER) MODEL CHAPTER 7 (6/E) CHAPTER 3 (5/E)

Review: Participation Constraints

Lecture 6. SQL, Logical DB Design

ER modelling, Weak Entities, Class Hierarchies, Aggregation

The Entity-Relationship Model

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Chapter 3. Data Modeling Using the Entity-Relationship (ER) Model

Data Modeling. Database Systems: The Complete Book Ch ,

Databases and BigData

Database Design. Database Design I: The Entity-Relationship Model. Entity Type (con t) Chapter 4. Entity: an object that is involved in the enterprise

Conceptual Design Using the Entity-Relationship (ER) Model

æ A collection of interrelated and persistent data èusually referred to as the database èdbèè.

DATABASE MANAGEMENT SYSTEMS. Question Bank:

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

The Relational Data Model and Relational Database Constraints

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Data Analysis 1. SET08104 Database Systems. Napier University

Foundations of Information Management

Databases What the Specification Says

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Unit 2.1. Data Analysis 1 - V Data Analysis 1. Dr Gordon Russell, Napier University

Concepts of Database Management Seventh Edition. Chapter 6 Database Design 2: Design Method

three Entity-Relationship Modeling chapter OVERVIEW CHAPTER

How To Create A Table In Sql (Ahem)

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

BCA. Database Management System

Review Entity-Relationship Diagrams and the Relational Model. Data Models. Review. Why Study the Relational Model? Steps in Database Design

DataBase Management Systems Lecture Notes

The Relational Model. Why Study the Relational Model?

OBJECTS AND DATABASES. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 21

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

Relational Databases

Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

SQL NULL s, Constraints, Triggers

Fundamentals of Database Design

Section of DBMS Selection & Evaluation Questionnaire

Chapter 1: Introduction. Database Management System (DBMS)

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

3. Relational Model and Relational Algebra

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

IT2305 Database Systems I (Compulsory)

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

XV. The Entity-Relationship Model

Introduction This document s purpose is to define Microsoft SQL server database design standards.

IT2304: Database Systems 1 (DBS 1)

Lecture 12: Entity Relationship Modelling

CSE 233. Database System Overview

1 File Processing Systems

Data Modeling Basics

OVERVIEW 1.1 DATABASE MANAGEMENT SYSTEM (DBMS) DEFINITION:-

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

Chapter 2 Database System Concepts and Architecture

Chapter 1: Introduction

CSE 132A. Database Systems Principles

Database System Concepts

Database Design Process. Databases - Entity-Relationship Modelling. Requirements Analysis. Database Design

Basic Concepts of Database Systems

AVOIDANCE OF CYCLICAL REFERENCE OF FOREIGN KEYS IN DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

Exercise 1: Relational Model

CSC 443 Data Base Management Systems. Basic SQL

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

Introduction to database management systems

Database Management Systems

DATABASE INTRODUCTION

SQL Server An Overview

ECS 165A: Introduction to Database Systems

Database Design. Adrienne Watt. Port Moody

CSC 742 Database Management Systems

Information Systems Modelling Information Systems I: Databases

Converting E-R Diagrams to Relational Model. Winter Lecture 17

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #5: En-ty/Rela-onal Models- - - Part 1

Modern Systems Analysis and Design

7.1 The Information system

Introdução às Bases de Dados

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

1. INTRODUCTION TO RDBMS

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

Transcription:

Database Systems -F.Y.CS -Prof Valerie

UNIT 1 INTRODUCTION TO DBMS 2

UNIT 1 INTRODUCTION TO DBMS 3 Example of simple college database which stores student and course information. Student: Stud-id Name Subjects Grade 1 Bob CS A 2 Tom MATH B

UNIT 1 INTRODUCTION TO DBMS 4 Example of database catalog. STUDENT Table Student_id Name Subjects Class 1 Bob CS 1 20 Tom MATH 2 Column_name Data_type Belongs_to_relation Stud_id Character (4) STUDENT Name Character (10) STUDENT Subjects Character (20) STUDENT Class Integer (1) STUDENT

UNIT 1 INTRODUCTION TO DBMS 5

UNIT 1 INTRODUCTION TO DBMS 6

UNIT 1 INTRODUCTION TO DBMS 7

UNIT 1 INTRODUCTION TO DBMS 8 Levels Of Abstraction:- Process of hiding irrelevant details from user is called data abstraction. We have three levels of abstraction: Physical level: This is the lowest level of data abstraction. It describes how data is actually stored in database. You can get the complex data structure details at this level. Logical level: This is the middle level of 3-level data abstraction architecture. It describes what data is stored in database. View level: Highest level of data abstraction. This level describes the user

UNIT 1 INTRODUCTION TO DBMS 9 interaction with database system. Example: Let s say we are storing customer information in a customer table. At physical level these records can be described as blocks of storage (bytes, gigabytes, terabytes etc.) in memory. These details are often hidden from the programmers. At the logical level these records can be described as fields and attributes along with their data types, their relationship among each other can be logically implemented. The programmers generally work at this level because they are aware of such things about database systems. At view level, user just interact with system with the help of GUI and enter the details at the screen, they are not aware of how the data is stored and what data is stored; such details are hidden from them. Data Independence

UNIT 1 INTRODUCTION TO DBMS 10 Logical Data Idependence Logical data independence indicates that the conceptual schema can be changed without affecting the existing external schemas. The change would be absorbed by the mapping between the external and conceptual levels. Physical Data Independence Physical data independence indicates that the physical storage structures or devices could be changed without affecting conceptual schema. The change would be absorbed by the mapping between the conceptual and internal levels. DBMS Architecture There are following 3 levels or layers of DBMS Architecture: 1. External level 2. Conceptual level 3. Internal level

UNIT 1 INTRODUCTION TO DBMS 11 EXTERNAL LEVEL The user s view of the database. Consists of a number of different external views of the DB. Describes part of the DB for particular group of users. Provides a powerful and flexible security mechanism by hiding parts of the DB from certain users. It permits users to access data in a way that is customized to their needs, so that the same data can be seen by different users in different ways, at the same time. CONCEPTUAL LEVEL The logical structure of the entire database as seen by DBA. What data is stored in the database. The relationships among the data. Complete view of the data requirements of the organization, independent of any storage consideration.

UNIT 1 INTRODUCTION TO DBMS 12 INTERNAL LEVEL Physical representation of the DB on the computer. How the data is stored in the database. - Storage space allocation for data and indexes - Record description for storage - Record placement - Data compression, encryption DATA MODELS Client-Server Architecture:- Client-User machine that provides user interface capabilities and local processing. Client handles user interface programs and application programs. Server-System containing both hardware and software. Provides services to the client machines.such as file access, printing, archiving, or database access. Server handles query and transaction functionality related to SQL processing. Servers with specific functionalities:- File server-maintains the files of the client machines. Printer server Connected to various printers; all print requests by the clients are forwarded to this machine

UNIT 1 INTRODUCTION TO DBMS 13 Two-Tier Client/Server Architecture Provides application programming interface (API). Allows client-side programs to call the DBMS.Both client and server machines must have the necessary software installed. Three-Tier and n-tier Architectures for Web Applications provides Application server or Web server which adds intermediate layer between client and the database server. Runs application programs and stores business rules N-tier-Divide the layers between the user and the stored data further into finer components.

UNIT 1 INTRODUCTION TO DBMS 14 Object-based logical models: -Describe data at the conceptual and view levels. -Provide fairly flexible structuring capabilities. -Object-based logical model includes the following. 1.Entity-relationship model. 2.Object-oriented model. 1. The E-R Model The entity-relationship model is based on a perception of the world as consisting of a collection of basic objects (entities) and relationships among these objects as it's name itself suggests. An entity is a distinguishable object that exists. Each entity has associated with it a set of attributes describing it. E.g. number and balance for an account entity. A relationship is an association among several entities. e.g. A cust_acct relationship associates a customer with each account he or she has. The set of all entities or relationships of the same type is called the entity set or relationship set. Another essential element of the E-R diagram is the mapping cardinalities, which express the number of entities to which another entity can be associated via a relationship set. The overall logical structure of a database can be expressed graphically by an E-R diagram: rectangles: represent entity sets. ellipses: represent attributes. diamonds: represent relationships among entity sets. lines: link attributes to entity sets and entity sets to relationships.

UNIT 1 INTRODUCTION TO DBMS 15 Entity-relationship model 2. Object-oriented data model: Object-oriented programming (especially in Java, C++, or C#) has become the popular software development methodology. This led to the development of an object-oriented data model. Record-based logical models: Also describe data at the conceptual and view levels.named so because the database is structured in fixed-format records of several types.each record type defines a fixed number of fields, or attributes.the three most widely-accepted models are the relational, network, and hierarchical. 1.The Relational Model. Data and relationships are represented by a collection of tables. Each table has a number of columns with unique names. Student_id Name Subjects Class 1 Bob CS 1 20 Tom MATH 2

UNIT 1 INTRODUCTION TO DBMS 16 2. The Network Model. Data are represented by collections of records. Relationships among data are represented by links. Organization is that of an arbitrary graph. 3. The Hierarchical Model. Similar to the network model. Organization of the records is as a collection of trees, rather than arbitrary graphs.

UNIT 1 INTRODUCTION TO DBMS 17 Entity Relational Model Entity:- The basic object that the ER model represents is an entity, which is a thing in the real world with an independent existence.an entity may be an object with a physical existence (for example, a particular person, car,house, or employee) Each entity has attributes the particular properties that describe it. For example, an EMPLOYEE entity may be described by the employee s name, age, address, salary, and job. rectangles: represent entity sets. Example:- Where customer,account are entities. Attributes:- Each entity has attributes the particular properties that describe it. For example, an EMPLOYEE entity may be described by the employee s name, age, address, ellipses: represent attributes. In the diagram above name, mob_no,city are the attributes of customer entity and Acc_no,balance are the attributes of account entity. Several types of attributes occur in the ER model: 1.simple versus composite 2.single valued versus multivalued 3. stored versus derived. 1.simple versus composite:- Attributes that are not divisible are called simple or atomic attributes. Composite attributes can form a hierarchy; for example, Street_address can be further subdivided into three simple component attributes: Number, Street, and Apartment_number 2. Single-Valued versus Multivalued Attributes. Most attributes have a single value for a particular entity; such attributes are called single-valued. For example, Age is a single-valued attribute of a person.

UNIT 1 INTRODUCTION TO DBMS 18 Attribute can have a set of values for the same entity for instance, a Colors attribute for a car can have many values such as blue, green. 3. In some cases, two (or more) attribute values are related for example, the Age and Birth_date attributes of a person. For a particular person entity, the value of Age can be determined from the current (today s) date and the value of that person s Birth_date. The Age attribute is hence called a derived attribute and is said to be derivable from the Birth_date attribute, which is called a stored attribute. Entity-relationship diagram Rectangles represent entity sets. Diamonds represent relationship sets. Lines link attributes to entity sets and entity sets to relationship sets. Ellipses represent attributes Double ellipses represent multivalued attributes. Dashed ellipses denote derived attributes. Underline indicates primary key attributes Entity Set: The collection of all entities of a particular entity type in the database at any point in time is called an entity set. Example: set of specific person, company, event, plant Entity Sets customer and loan

UNIT 1 INTRODUCTION TO DBMS 19 Relationship set:- A relationship is an association among several entities A relationship type R among n entity types E1, E2,..., En defines a set of associations or a relationship set among entities from these entity types. fig 7.9

UNIT 1 INTRODUCTION TO DBMS 20 A relationship type of degree two is called binary, and one of degree three is called ternary. Cardinality Ratios(Mapping) for Binary Relationships. The cardinality ratio for a binary relationship specifies the maximum number of relationship instances that an entity can participate in. For a binary relationship set the mapping cardinality must be one of the following types: 1.One to one 2.One to many 3.Many to one 4.Many to many Diagram given below:- (a) One to One (b) One to Many

UNIT 1 INTRODUCTION TO DBMS 21 (a)many to One (b)many to Many PARTICIPATION CONSTRAINTS: The participation constraint specifies whether the existence of an entity depends on its being related to another entity via the relationship type. There are two types of participation constraints total and partial. Diag 7.12

UNIT 1 INTRODUCTION TO DBMS 22 Key Constraint:- An important constraint on the entities of an entity type is the key or uniqueness constraint on attributes. A super key of an entity set is a set of one or more attributes whose values uniquely determine each entity. A candidate key of an entity set is a minimal super key Customer-id is candidate key of customer account-number is candidate key of account Although several candidate keys may exist, one of the candidate keys is selected to be the primary key. The combination of primary keys of the participating entity sets forms a super key of a relationship set. (customer-id, account-number) is the super key of depositor NOTE: this means a pair of entity sets can have at most one relationship in a particular relationship set. E.g. if we wish to track all access-dates to each account by each customer, we cannot assume a relationship for each access. For example, the Name attribute is a key of the COMPANY entity type. Sometimes several attributes are called as composite attribute which together form a key. In ER diagrammatic notation, each key attribute has its name underlined inside the oval, as illustrated in Figure 7.7(a). Fig7.7

UNIT 1 INTRODUCTION TO DBMS 23 weak entity type:- An entity type may also have no key, in which case it is called a weak entity type. Entities belonging to a weak entity type are identified by being related to specific entities from another entity type in combination with one of their attribute values. We call this other entity type the identifying or owner entity type,10 and we call the relationship type that relates a weak entity type to its owner the identifying relationship of the weak entity type. A weak entity type always has a total participation constraint (existence dependency) with respect to its identifying relationship because a weak entity cannot be identified without an owner entity. A weak entity type normally has a partial key, which is the attribute that can uniquely identify weak entities that are related to the same owner entity. The weak entity type and its identifying relationship are distinguished by surrounding their boxes and diamonds with double lines. The partial key attribute is underlined with a dashed or dotted line. An entity set that does not have a primary key is referred to as a weak entity set. Diag:- Generalization and Aggregation: Generalization is a bottom-up approach in which two lower level entities combine to form a higher level entity. In generalization, the higher level entity can also combine with other lower level entity to make further higher level entity.

UNIT 1 INTRODUCTION TO DBMS 24 diag: generalization Aggregration is a process when relation between two entity is treated as a single entity. Here the relation between Center and Course, is acting as an Entity in relation with Visitor. diag:- Aggregation Binary versus ternary:- The degree of a relationship type is defined as the number of participating entity type. A relationship type of degree two is called binary and a relationship type of degree three is called ternary. A ternary relationship is when three entities participate in the relationship. For Example: The University might need to record which teachers taught which subjects in which

UNIT 1 INTRODUCTION TO DBMS 25 courses. A binary relationship is when two entities participate and is the most common relationship degree Relational Data Model:- A domain D is a set of atomic values. By atomic we mean that each value in the domain is indivisible as far as the formal relational model is concerned. A common method of specifying a domain is to specify a data type. examples of domains follow: Social_security_numbers. The set of valid nine-digit Social Security numbers. (This is a unique identifier assigned to each person in the United States for employment, tax, and benefits purposes.) Names: The set of character strings that represent names of persons. Employee_ages. Possible ages of employees in a company; each must be an integer value between 15 and 80.

UNIT 1 INTRODUCTION TO DBMS 26 A data type or format is also specified for each domain.the data type for Employee_ages is an integer number between 15 and 80. A relation schema R, denoted by R(A1, A2,...,An), is made up of a relation name R and a list of attributes, A1, A2,..., An. Each attribute Ai is the name of a role played by some domain D in the relation schema R. D is called the domain of Ai and is denoted by dom(ai). A relation student, which stores information about university students, would contain four attributes describing each student. as follows: STUDENT(Name, Ssn, Home_phone, Address) Using the data type of each attribute, the definition is sometimes written as: STUDENT(Name: string, Ssn: string, Home_phone: string, Address: string) A relation r of the relation schema R(A1, A2,..., An), also denoted by r(r), is a set of n-tuples r = {t1, t2,..., tm}. Each n-tuple t is an ordered list of n values t =<v1, v2,..., vn>, where each value vi, 1 <i n, is an element of dom (Ai) or is a special NULL value. Example:- Student relation with attributes and tuples Student Name Ssn age Tom 305-61-2435 19 Bob 381-62-1245 20 Where Name,Ssn,Age are attributes, Tom,Bob,305-61-2435,381-62-1245,19,20 are tuples Student is the relation name. Relational Model Notation We will use the following notation in our presentation: 1. A relation schema R of degree n is denoted by R(A1, A2,..., An). 2. The uppercase letters Q, R, S denote relation names. 3. The lowercase letters q, r, s denote relation states.

UNIT 1 INTRODUCTION TO DBMS 27 4. The letters t, u, v denote tuples. 5. The name of a relation schema such as STUDENT indicates the current set of tuples in that relation whereas STUDENT(Name, Ssn,...) refers only to the relation schema. 6.An attribute A can be qualified with the relation name R to which it belongs by using the dot notation R.A for example, STUDENT.Name or STUDENT.Age. This is because the same name may be used for two attributes in different relations. However, all attribute names in a particular relation must be distinct. 7. An n-tuple t in a relation r(r) is denoted by t = <v1, v2,..., vn>, where vi is the value corresponding to attribute Ai. The following notation refers to component values of tuples: 8.Both t [Ai] and t.ai (and sometimes t[i]) refer to the value vi in t for attribute Ai. 9. Both t[au, Aw,..., Az] and t.(au, Aw,..., Az), where Au, Aw,..., Az is a list of attributes from R, refer to the subtuple of values <vu, vw,..., vz> from t corresponding to the attributes specified in the list. As an example, consider the tuple t = < Barbara Benson, 533-69-1238, 19> from the STUDENT relation. we have t [Name] = < Barbara Benson >, and t [Ssn, Age] = < 533-69-1238, 19>. Characteristics of Relations:- 1.Ordering of Tuples in a Relation. A relation is defined as a set of tuples. when we display a relation as a table, the rows are displayed in a certain order. Many tuple orders can be specified on the same relation. For example, tuples in the STUDENT relation in could be ordered by values of Name, Ssn, Age, or some other attribute. The definition of a relation does not specify any order: There is no preference for one ordering over another. When a relation is implemented as a file or displayed as a table, a particular ordering may be specified on the records of the file or the rows of the table. 2.Ordering of Values within a Tuple and an Alternative Definition of a Relation The ordering of values in a tuple and hence of attributes in a relation schema is important. However, at a more abstract level, the order of attributes and their values is not that important as long as the correspondence between attributes and values is maintained. An alternative definition of a relation can be given, making the ordering of values in a tuple unnecessary.

UNIT 1 INTRODUCTION TO DBMS 28 When a relation is implemented as a file, the attributes are physically ordered as fields within a record. We will generally use the first definition of relation, where the attributes and the values within tuples are ordered, because it simplifies much of the notation. 3.Values and NULLs in the Tuples. Each value in a tuple is an atomic value; that is, it is not divisible into components within the framework of the basic relational model. Hence, composite and multivalued attributes are not allowed. This model is sometimes called the flat relational model. An important concept is that of NULL values, which are used to represent the values of attributes that may be unknown or may not apply to a tuple. A special value, called NULL, is used in these cases. For example, Another student has a NULL for home phone, presumably because either he does not have a home phone or he has one but we do not know it (value is unknown). In general, we can have several meanings for NULL values, such as value unknown, value exists but is not available, or attribute does not apply to this tuple (also known as value undefined). 4.Interpretation (Meaning) of a Relation. The relation schema can be interpreted as a declaration or a type of assertion. For example, STUDENT relation of that, in general, a student entity has a Name, Ssn, Home_phone, Address, Office_phone, Age, and Gpa. Each tuple in the relation can then be interpreted as a fact or a particular instance of the assertion. For example, the first tuple asserts the fact that there is a STUDENT whose Name is Benjamin Bayer, Ssn is 305-61-2435, Age is 19. An alternative interpretation of a relation schema is as a predicate; in this case, the values in each tuple are interpreted as values that satisfy the predicate. For example,the predicate STUDENT (Name, Ssn,...) is true for the five tuples in relation STUDENT. Relational Constraints:- UNIQUE Constraint The UNIQUE constraint is used to uniquely identify each record in a database. It requires that each value in a column should be unique,i.e. no 2 rows of same column should have duplicate values. It provides you the guarantee for uniqueness in a column or set of columns and allows us to input the NULL values, unless there is not defined NOT NULL constraint for the same column. As in UNIQUE constraint, there is a fact that there could be and number of rows which can include NULL for columns without NOT NULL constraints, reason because nulls are not considered equal to anything. It can be defined at table level or at column level. Example :

UNIT 1 INTRODUCTION TO DBMS 29 CREATE TABLE employee ( EID int NOT NULL, Name varchar(20) NOT NULL, Address varchar(50), Salary int, ); The column EmpID cannot have null values and the values in it are distinct. CHECK constraint : The check constraint is used to check the value which is entered into a record. It is used to define condition which each row must satisfy. If the condition value evaluates to fall, then the record violates the constraint and you cannot enter it into the table. We cannot define check constraint in SQL view. Syntax : CREATE TABLE table_name ( column1 datatype, column2 datatype,... CONSTRAINT constraint_name CHECK (column-name_contition));

UNIT 1 INTRODUCTION TO DBMS 30 The check constraint can be defined by a create table statement or alter table statement. Primary key constraint A primary key constraint is a field in a table which uniquely identifies each rows/records in a database table. Characteristics of Primary Key : Primary key constraint must contain unique values. Primary key constraint column cannot have null values. Table can have only one primary key. If multiple fields are used as a primary key, then they are called as composite key. Example : CREATE TABLE employee ( EmpID int NOT NULL, Name varchar(30) NOT NULL, Address varchar(50), Salary int, PRIMARY KEY (EmpID) ); Primary key applied on EmpID column, therefore now the column cannot be left blank nor duplicate values can exist. A primary key constraint keeps the attributes of UNIQUE Constraint in itself. A table can contain only one primary key constraint, but can have several many constraints.

UNIT 1 INTRODUCTION TO DBMS 31 Referential Integrity:- An integrity constraint (Ie) is a condition specified on a database schema and restricts the data that can be stored in an instance of the database. If a database instance satisfies all the integrity constraints specified on the database schema, it is a legal instance. A DBMS enforces integrity constraints, in that it permits only legal instances to be stored in the database. For referential integrity to hold in a relational database, any field in a table that is declared a foreign key can contain either a null value, or only values from a parent table's primary key or a candidate key. [2] In other words, when a foreign key value is used it must reference a valid, existing primary key in the parent table. Integrity constraints are specified and enforced at different times: When a database application is run, the DBMS checks for violations and disallows changes to the data that violate the specified ICs. Sometimes the information stored in a relation is linked to the information stored in another relation. If one of the relations is modified, the other must be checked, and perhaps modified, to keep the data consistent. An IC involving both relations must be specified if a DBMS is to make such checks. The most common IC involving two relations is a foreign key constraint. Fig3.4 referential integrity NOT NULL Constraint NOT NULL constraint restricts a column from having a NULL value. Once NOT NULL constraint is applied to a column, you cannot pass a null value to that column. It enforces a column to contain a proper value. One important point to note about NOT NULL constraint is that it cannot be defined at table level. Example using NOT NULL constraint

UNIT 1 INTRODUCTION TO DBMS 32 CREATE table Student(s_id int NOT NULL, Name varchar(60), Age int); ER to TABLE An entity set is mapped to a relation in a straightforward way: Each attribute of the entity set becomes an attribute of the table. Note that we know both the domain of each attribute and the (primary) key of an entity set. Consider the Employees entity set with attributes ssn, name, and lot shown in Figure 3.8. Figure 3.8 The Employees Entity Set ssn name lot I 231-31-5368 Smiley 22 131-24-3650 Smethurst 35 Figure 3.9 An Instance of the Employees Entity Set The following SQL statement captures the preceding information, including the domain constraints and key information: CREATE TABLE Employees ( ssn CHAR(11), name CHAR(30), lot INTEGER, PRIMARY KEY (ssn) )

UNIT 1 INTRODUCTION TO DBMS 33 Relationship to tables with constraint key If a relationship set involves n entity sets and some of them are linked via arrows in the ER diagram, the key for anyone of these m entity sets constitutes a key for the relation to which the relationship set is mapped. Hence we have m candidate keys, and one of these should be designated as the primary key. The table corresponding to Manages has the attributes ssn, did, since. However, because each department has at most one manager, no two tuples can have the same did value but differ on the ssn value. A consequence of this observation is that did is itself a key for Manages. The Manages relation can be defined using the following SQL statement: CREATE TABLE Manages (ssn CHAR (11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did~references Departments) A second approach is to include the information about the relationship set in the table corresponding to the entity set with the key, taking advantage of the key constraint. This approach eliminates the need for a separate Manages relation, and queries asking for a department's manager can be answered without combining information from two relations. The only drawback to this approach is that space could be wasted if several departments have no managers. In this case the added fields would have to be filled with null values. The following SQL statement, defining a DepLMgr relation that captures the information in both Departments and Manages, illustrates the second approach

UNIT 1 INTRODUCTION TO DBMS 34 to translating relationship sets with key constraints: CREATE TABLE DepLMgr ( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR (11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) Note that ssn can take on null values. Relationships to tables without key constraints To represent a relationship, we must be able to identify each participating entity and give values to the descriptive attributes of the relationship. Thus, the attributes of the relation include: The primary key attributes of each participating entity set, as foreign key fields. The descriptive attributes of the relationship set. The set of nondescriptive attributes is a superkey for the relation. If there are no key constraints,this set of attributes is a candidate key. Figure 3.11 The Reports_To Relationship Set role indicators supervisor and subordinate are used to create meaningful field names in the CREATE statement for the Reports..To table: CREATE TABLE Reports_To ( supervisor...ssn CHAR (11), subordinate...ssn CHAR (11), PRIMARY KEY (supervisor_ssn,subordinate_ssn), FOREIGN KEY (supervisor...ssn) REFERENCES Employees(ssn),

UNIT 1 INTRODUCTION TO DBMS 35 FOREIGN KEY (subordinate...ssn) REFERENCES Employees(ssn) ) Observe that we need to explicitly name the referenced field of Employees because the field name differs from the name(s) of the referring field(s).

UNIT 1 INTRODUCTION TO DBMS 36