Beruflich Dokumente
Kultur Dokumente
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:
Entities generally correspond to persons, objects, locations, events, etc. Examples are employee, vendor, supplier, materials,
warehouse, delivery, etc.
• 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.
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.
• 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:
• Composite Attributes
• Single-Valued
• Multi Valued
• Stored Attribut
• Derived Attribute (b) CUSTOMER relation
COMP08/COMP12- MIDTERM 2/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
Not surprisingly, Peter Chen and Bachman are the name inventors of the notation. The following table gives the notation.
COMPONENT REPRESENTATION
RELATIONSHIP
CARDINALITY
OPTIONALITY
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.
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.
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.
• 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
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.
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.
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.
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