Sie sind auf Seite 1von 11

COMP08/COMP12- MIDTERM 1/11

PART I. Entity Relationship Diagram (ERD)

ERD complements Data Flow Diagram (DFD). While DFD focuses on processes and data flow between them, ERD focuses on
data and the relationships between them. It helps to organise data used by a system in a disciplined way. It helps to ensure
completeness, adaptability and stability of data. It is an effective tool to communicate with senior management (what is the data
needed to run the business), data administrators (how to manage and control data), database designers (how to organise data
efficiently and remove redundancies). It consists of three components.

Entity: It represents a collection of objects or things in the real world whose individual members or instances have the following
characteristics:

• Each can be identified uniquely in some fashion.


• Each plays a necessary role in the system we are building.
• Each can be described by one or more data elements (attributes).

Entities generally correspond to persons, objects, locations, events, etc. Examples are employee, vendor, supplier, materials,
warehouse, delivery, etc.

There are five types of entities.

• Fundamental entity: It does not depend on any other entity for its existence. For e.g. materials
• Subordinate entity: It depends on another entity for its existance. For example, in an inventory management system,
purchase order can be an entity and it will depend on materials being procured. Similarly invoices will depend on
purchase orders.
• Associative entity: It depends on two or more entities for its existence. For example, student grades will depend on the
student and the course.
• Generalisation entity: It encapsulates common characteristics of many subordinate entities. For example, a four wheeler
is a type of vehicle. A truck is a type of four wheeler .
• Aggregation entity: It consists of or an aggregation of other entities. For example, a car consists of engine, chasis, gear
box, etc. A vehicle can also be regarded as an aggregation entity, because a vehicle can be regarded as an aggregation of
many parts.

Attributes: They express the properties of the entities.

Every entity will have many attributes, but only a subset, which are relevant for the system under study, will be chosen. For
example, an employee entity will have professional attributes like name, designation, salary, etc. and also physical attributes like
height, weight, etc. But only one set will be chosen depending on the context.

Attributes are classified as entity keys and entity descriptors.

• Entity keys are used to uniquely identify instances of entities. Attributes having unique values are called candidate keys
and one of them is designated as primary key. The domains of the attributes should be pre-defined. If 'name' is an attribute
of an entity, then its domain is the set of strings of alphabets of predefined length.

Attribute Types:

(a) CUSTOMER entity type


• Atomic / Simple Attribute with simple attributes

• Composite Attributes
• Single-Valued
• Multi Valued
• Stored Attribut
• Derived Attribute (b) CUSTOMER relation
COMP08/COMP12- MIDTERM 2/11

• Simple (or atomic) attribute


can't be broken down into smaller
components. (a) CUSTOMER
• Composite attributes, can be entity type with
broken down into component parts. composite
attribute
• Single-valued attribute
(represented by a single line ellipse)
is an attribute which has only
one value in a table.
• Multi-valued attribute has (b) CUSTOMERrelationwithaddress detail
multiple values per instance, such
as: all courses taken by a student,
shown above. It is represented by
double line ellipse
• Stored attribute - a regular
attribute
• Derived attribute which is calculated from a stored attribute, and thus should not be shown on the ERD.

Relationships: They describe the


association between entities.
Example
They are characterised by optionality and
cardinality.
Attribute
Attribute Multi_valued
Optionality is of two types, namely, attribute
mandatory and optional.
Age
Age D
Degree
egree
1. Mandatory relationship means
associated with every instance of
the first entity there will be atleast A
A:: 24
24 A
A:: B
B.Sc
.Sc,, M
M.Sc.
.Sc.
one instance of the second entity. B
B:: 27
27 B
B:: B
B.Sc,
.Sc, M.Sc.,
.Sc., D
Dr.
r. Eng.
Eng.
2. Optional relationship means that
there may be instances of the first
entity, which are not associated Single_Valued
with any instance of the second attribute
Attribute values
entity. For example, employee-
spouse relationship has to be
optional because there could be Example
unmarried employees. It is not
correct to make the relationship
mandatory. Attribu
Attribute
te

Cardinality is of three types: one-to-one,


Ag
Age
e B
Birth
irthD
Day
ay S
Sto
tore
red
dAttrib
Attribu
ute
te
one-to-many, many-to-many.

1. One-to-one relationship means an A


A::2
24 Y
Years
ears A : 13 July 1980
A : 13 July 1980
instance of the first entity is B
B::4400YYear
earss B
B::2233D De c1
ec 19 96644
associated with only one instance of
the second entity. Similarly, each
instance of the second entity is
D
De
eriv
rive
ed
dA ttribu
Attribute
te
related to one instance of the first Attrib
Attribu
ute
te V
Valu
alue
entity.
2. One-to-many relationship means
that one instance of the first entity is
related to many instances of the second entity, while an instance of the second entity is associated with only one instance
of the first entity.
COMP08/COMP12- MIDTERM 3/11

3. In many-to-many relationship an instance of the first entity is related to many instances of the second entity and the same
is true in the reverse direction also.

Other types of relationships are multiple relationships between entities, relationships leading to associative entities, relationship of
entity with itself, EXCLUSIVE-OR and AND relationships

ERD notation: There are two type of notation used:

1. Peter Chen notation


2. Bachman notation.

Not surprisingly, Peter Chen and Bachman are the name inventors of the notation. The following table gives the notation.

COMPONENT REPRESENTATION

ENTITY OR OBJECT TYPE PURCHASE ORDER

RELATIONSHIP

CARDINALITY

OPTIONALITY

PETER CHEN BACHMAN

Example for Bachman notation


COMP08/COMP12- MIDTERM 4/11

Example for Peter Chen notation


COMP08/COMP12- MIDTERM 5/11

Given below are a few examples of ER diagrams using Bachman notation. First the textual statement is given followed by the
diagram

1. In a company, each division is managed by only one manager and each manager manages only one division

2. Among the automobile manufacturing companies, a company manufactures many cars, but a given car is manufactured in only
one company

3. In a college, every student takes many courses and every course is taken by many students

4. In a library, a member may borrow many books and there may be books which are not borrowed by any member

5. A teacher teaches many students and a student is taught by many teachers. A teacher conducts examination for many students
and a student is examined by many teachers.

6. An extension of example-3 above is that student-grades depend upon both student and the course. Hence it is an associative
entity
COMP08/COMP12- MIDTERM 6/11

7. An employee can play the role of a manager. In that sense, an employee reports to another employee.

8. A tender is floated either for materials or services but not both.

9. A car consists of an engine and a chasis

PART II. RELATIONAL LANGUAGE


COMP08/COMP12- MIDTERM 7/11

Query language
A query language is a language in which a user requests information from the database. These can be divided as procedural or non-
procedural. In procedural language, the user instructs the system to perform a sequence of operations on the database to compute
the desired result. In non-procedural language, the user describes the desired information without giving a specific procedure for
obtaining that information.

What is SQL?
• SQL stands for Structured Query Language.
• SQL is a standard computer language for accessing and manipulating databases
• SQL is the set of commands that is recognized by nearly all RDBMS.
• SQL commands can be divided into following categories.
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Transaction Control Language (TCL)

DDL (Data Definition Language) Commands: These commands are used for creation; alteration and removal of database objects
e.g.: CREATE TABLE, ALTER TABLE, and DROP TABLE.

DML (Data Manipulation Language) Commands: These commands are used to add, modify or remove the data contained in the
database tables. e.g.: INSERT, DELETE, UPDATE, SELECT etc. are DML commands.

TCL (Transaction Control Language) Commands: These commands are used to start or end transactions. e.g.: COMMIT,
ROLLBACK, ROLL BACK TO etc. are TCL commands.

DML commands:

Note: All commands given below are based on the table Student:

Table: Student
Name Roll Class Age Marks
Ragha 101 C12 15 91
v
Ajay 102 M12 14 92
Gagan 103 C12 15 80
Shika 104 E12 14 85

1. INSERT INTO: A row or tuple can be inserted in an existing table with the help of an INSERT command.
Syntax:
Insert into tablename [column list] values (val1, val2,…)
Note: Values should be inserted in the same order as table creation.

Example: Write a command to insert a row in the table student.


Ans: Insert into student values (‘ahmed’, 105, C12, 16, 98);

2. UPDATE STATEMENT: Update command is used to make changes to the existing values.
Example 1: Write a command to change the marks from 40 to 86 for rollno 2
Ans: Update Student Set marks =86 where rollno = 2;

Example 2: Write a query to change the marks as 95 and grade a A+ for rollno 31.
Ans: Update Student set marks=95 and grade=’A+’ where rollno = 31;

Example 3: Write a command to update all the marks by adding 5 marks for all students.
Ans: Update Student set marks = marks +5;

Example 4: Write a command to change the grade as A for all students whose marks is greater than 90.
Ans: Update Student set grade=’A’ where marks > 90;
COMP08/COMP12- MIDTERM 8/11

3. DELETE COMMAND: This command is used to delete one row or more than one row.
Example: Write a command to delete the details of students who have scored less than 33 marks.
Ans: Delete from Student where marks < 33;

4. SELECT: The SELECT statement is used to select data from a table. The tabular result is stored in a result
table (called the result-set).
Syntax:
SELECT column_name(s)
FROM table_name

Note: SQL statements are not case sensitive. SELECT is the same as select.

Example 1: Select * from student;


Example 2: Select all from student;
• The above two commands display all the details from the table Student.

Example 3: Select name, marks from student;


• This command will display only the name & marks columns from the table student.

Example 4: Select Distinct class from student;

• DISTINCT can be used to select column names and values deleting duplicates.

Output:
Class
C12
M12
E12
5. WHERE clause: The WHERE clause is used to specify a selection criterion.

• To conditionally select data from a table, a WHERE clause can be added to the SELECT statement.

Syntax
SELECT column FROM table
WHERE column operator value

With the WHERE clause, the following operators can be used:

Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you want to return for at least one of the columns

Example 1: Select name, marks from student where age > 14;
Output:
Name Marks
Ragha 90
v
Gagan 90
COMP08/COMP12- MIDTERM 9/11

Example 2: Write a query to display all details about the students whose age is 15.
Ans: Select * from Student where age=15;

Example 3: Write a query to display name, roll and class of all students whose marks is above 90.
Ans: Select name, roll, class from Student where marks > 90;
Example 4: Write a query to display the name and age of students who belong to class C12.
Ans: Select name, age from student where class = ‘C12’;

Using Quotes
• Note that we have used single quotes around the conditional values in the examples.
• SQL uses single quotes around text values (most database systems will also accept double quotes). Numeric values should not be
enclosed in quotes.

For text values:


This is correct:
SELECT * FROM Persons WHERE FirstName = 'Tove'
This is wrong:
SELECT * FROM Persons WHERE FirstName = Tove

For numeric values:


This is correct:
SELECT * FROM Persons WHERE Year > 1965
This is wrong:
SELECT * FROM Persons WHERE Year > '1965'

IN operator: IN operator in the where clause can be used to check for discrete values.

Example 1: Write a query to get those names of all students whose marks are 90 or 92.
Ans: SELECT name FROM student WHERE marks IN (90, 92);

Example 2: Write a query to get details of all students whose belong to class C12 or E12.
Ans: SELECT * FROM student WHERE class IN (‘C12’, ‘E12’);
BETWEEN-AND Operator: This operator is used to define a range of values. The lower and higher value is also included in the
range of values.

Example: Write a query to display the roll & name of all students whose marks is between 90 & 95.
Ans: SELECT roll, name FROM student WHERE marks BETWEEN 90 AND 95;

LIKE operator: This operator is used to check character type fields. It is used to check substrings. There are two types of wild
cards used with LIKE operator.
1. Underscore (_): It stands for a single character. e.g.: h_t can stand for hat, hit, hot etc.
2. Percentage (%): It stands for occurrence of 0 or more characters.
Example 1: Select name, roll from student where name like ‘Sh%’;
Output:
Name Roll
Shika 104

Example 2: Write a query to display the details of all students whose names start with ‘R’;
Ans: SELECT * FROM student WHERE name LIKE ‘R%’;

LOGICAL or BOOLEAN operators: The logical operators AND, OR, NOT are used to have two or more criteria in a single
command.
Example 1: Write a query to display the name and roll of all students who are in class C12 and their age is 15.
Ans: SELECT name, roll FROM student WHERE class = ’C12’ AND age = 5;

Example 2: Write a query to display the details of all students who are in class E12 or whose age is above 13.
Ans: SELECT * FROM student WHERE class = ‘E12’ OR age >13;
COMP08/COMP12- MIDTERM 10/11

EXERCISES:

1. Write the correct SQL commands for the following on the basis of table Supplier.

No. Name Price Supplier Stock


1 Motherboard 7000 Intel 20
2 Keyboard 1000 TVSE 70
3 Mouse 500 Logitech 60
4 Soundcard 600 Samsung 50
5 Speaker 600 Samsung 25
6 Monitor 3000 Philips 22
7 CD-ROM 2800 Creative 32
8 Printer 7900 HP 10

a. List all Name and Price with price between 3000 and 7000. Ans:Select name,price from supplier where price between
3000 and 7000)
b. To set the price to 1200 for ‘Keyboard’. Ans: update supplier set price=1200 where name=”keyboard”
c. To delete rows with stock between 20 and 40.Ans: Delete from supplier where stock between 20 and 40
d. To add a column quantity with data type number of size 4.Ans: not covered
e. To display Name, Price, stock of all suppliers whose name starts with 'S'. Ans: select name,price,stock from supplier
where name like “S*”;
f. To display supplier without duplication. Ans: Select distinct supplier from supplier
g. To insert a row with appropriate values. Ans: insert into supplier values (‘9’,’ribbon’,20,’HP’,15)
h. To increase the stock of all Samsung suppliers. Ans: update supplier set stock=stock+5 where supplier=”Samsung”
i. To display all supplier details whose supplier is Philips and stock is above 40. Ans: select * from supplier where
supplier=”Philips” and stock > 40;
j. To display name, supplier, stock for name as keyboard or mouse. Ans: Select name,supplier,stock from supplier where
name=”keyboard” or name=”mouse”;

2. Write the correct SQL commands for the following on the basis of table Flight.

TicketNo FlightNo Legno Fare Capacity FlightDate


100012 BD776 5 2000 100 15-Sep-2002
0
100015 BD676 2 1000 80 15-Sep-2002
0
100017 BD652 3 2000 70 20-Sep-2002
0
100020 BD776 1 2000 35 18-Sep-2002
0
100119 BD652 4 2200 75 22-Sep-2002
0
100032 BD776 6 1800 78 15-Sep-2002
0

a. To display details of flights after flight date 16-Sep-2002. Ans: Select * from flight where flightdate>”16-
Sep-2002”;
b. To display TicketNo, FlightNo and capacity for FlightNo BD652. Ans: Select ticketno,flightno,capacity from
flight where flightno=”BD652”
c. To display list of all flights whose fare is >=11000 and <=20000. Ans: Select flightno from flight where fare
between 11000 and 20000
d. Display FlightNo of all flights without duplications.Ans: Select distinct flightno from flight
COMP08/COMP12- MIDTERM 11/11

e. To delete details of FlightNo 676.Ans: delete from flight where flightno=”BD676”


To insert a new row with the following data: Ans: insert into flight values (100050,”BD676”, 2, 21000, 80, ’15-
Oct-2002’)
f. To change the flight capacity to 50 for the FlightNo bd776. Ans: Update flight set capacity=50 where
flightno=”BD776”
g. To increase the price of all the flights by 1000. Ans: Update flight set price=price+1000;

Das könnte Ihnen auch gefallen