Name: Vaishnavi Kailas Raut Seat No.: S190088621 Group B Assignment No: 1
Name: Vaishnavi Kailas Raut Seat No.: S190088621 Group B Assignment No: 1
Group B
Assignment No: 1
Aim: Design any database with at least 3 entities and relationships between them. Draw
suitable ER/EER diagram for the system.
An Entity–relationship model (ER model) describes the structure of a database with the help
of a diagram, which is known as Entity Relationship Diagram (ER Diagram). An ER model is
a design or blueprint of a database that can later be implemented as a database. The main
components of E-R model are: entity set and relationship set.
An ER diagram shows the relationship among entity sets. An entity set is a group of similar
entities and these entities can have attributes. In terms of DBMS, an entity is a table or
attribute of a table in database, so by showing relationship among tables and their attributes,
ER diagram shows the complete logical structure of a database. Lets have a look at a simple
ER diagram to understand this concept.
A simple ER Diagram:
In the following diagram we have two entities ATM and Transactions and their relationship.
The relationship between ATM and Transactions is weak relation. The attributes for ATM
entities are ATM id, Address and Time. For transaction entities are Trans no, Amount and
type.
ER Diagram
Geometric shapes and their meaning:
Components of a ER Diagram
Entity
course
WeakEntity:
An entity that cannot be uniquely identified by its own attributes and relies on the
relationship with other entity is called weak entity. The weak entity is represented by a
double rectangle.
Attribute
1. Key attribute
2. Composite attribute
3. Multivalued attribute
4. Derived attribute
1. Key attribute:
A key attribute can uniquely identify an entity from an entity set. For example, student roll
number can uniquely identify a student from a set of students
Key attribute is represented by oval same as other attributes however the text of key
attribute is underlined.
name
student
address
mobile
2. Composite attribute:
pin
• Address is composite attribute
country
3. Multivalued attribute:
An attribute that can hold multiple values is known as multivalued attribute. It is represented
with double ovals in an ER Diagram. For example – A person can have more than one phone
numbers so the phone number attribute is multivalued.
name
student
address
mobile
4. Derived attribute:
A derived attribute is one whose value is dynamic and derived from another attribute. It is
represented by dashed oval in an ER Diagram. For example – Person age is a derived
attribute as it changes over time and can be derived from another
student
DOB
mobile
age
Relationship
When a single instance of an entity is associated with a single instance of another entity then
it is called one to one relationship. For example, a person has only one passport and a
passport is given to one person.
When a single instance of an entity is associated with more than one instances of another
entity then it is called one to many relationship. For example – a customer can place many
orders but a order cannot be placed by many customers.
When more than one instances of an entity is associated with more than one instances of
another entity then it is called many to many relationship. For example, a can be assigned to
many projects and a project can be assigned to many students.
Participation Constraints
• Total Participation − Each entity is involved in the relationship. Total participation
is represented by double lines.
• Partial participation − Not all entities are involved in the relationship. Partial
participation is represented by single lines.
Instructor
stu_id
name
stu_name
Study
Student Department
In
address
location
mobile
Course
name depart_name
Subject
inst_id
sub_name sub_id
Conclusion: We have studied and created a database with at least 3 entities and relationships
between them with creation of ER/EER diagram for the system
Assignment No: 2
Aim:- Design and implement a database (for assignment no 1) using DDL statements
and apply normalization on them
RDBMS:
A relational database refers to a database that stores data in a structured format,
using rows and columns. This makes it easy to locate and access specific values within the
database. It is "relational" because the values within each table are related to each other.
Tables may also be related to other tables. The relational structure makes it possible to
run queries across multiple tables at once.
RDBMS Concepts:
Before we proceed to explain MySQL database system, let's revise few definitions related to
database.
• Database: A database is a collection of tables, with related data.
• Table: A table is a matrix with data. A table in a database looks like a simple spread
sheet.
• Column: One column (data element) contains data of one and the same kind, for
example the Column post code.
• Row: A row (tuple, entry or record) is a group of related data, for example the data
of one subscription.
• Redundancy: Storing data twice, redundantly to make the system faster.
• Primary Key: A primary key is unique. A key value cannot occur twice in one table.
With a key, you can find at most one row.
• Foreign Key: A foreign key is the linking pin between two tables.
• Compound Key: A compound key(composite key) is a key that consists of multiple
columns, Because one column is not sufficiently unique.
CREATE:
Create statements is used to define the database structure schema:
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES [,....]);
Example:
CREATE TABLE EMPLOYEE
(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);
INSERT:
The INSERT statement is a SQL query. It is used to insert data into the row of a table.
Syntax:
INSERT INTO TABLE_NAME
(column1, column2, column3 ,.... columnN)
VALUES (value1, value2, value3 , .... valueN);
Or
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3 , .... valueN);
Example:
INSERT INTO employee (Name, Salary) VALUES ("Meghana", 20000);
UPDATE:
This command is use d to update or modify the value of a column in the table.
Syntax:
UPDATE table_name SET [column_name1=value1,…, column_nameN=valueN]
[WHERE CONDITION]
Example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
ALTER:
Syntax to add new column:
ALTER TABLE table_name ADD column_name datatype;
Syntax to drop column:
ALTER TABLE table_name DROP COLUMN column_name;
Syntax to change datatype:
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
Example:
ALTER TABLE CUSTOMERS ADD Address Varchar(20);
ALTER TABLE CUSTOMERS DROP Address;
DROP
It is used to delete both the structure and record stored in the table.
Syntax:
DROP TABLE ;
Example:
Normalization:
• Normalization is the process of organizing the data in the database.
• Normalization is used to minimize the redundancy from a relation or set of relations.
It is also used to eliminate the undesirable characteristics like Insertion, Update and
Deletion Anomalies.
• Normalization divides the larger table into the smaller table and links them using
relationship.
• The normal form is used to reduce redundancy from the database table.
Types of Normalization:
First Normal Form(1NF):
A relation is in 1NF if it contains an atomic value.
An attribute (column) of a table cannot hold multiple values. It should hold only
atomic values.
Student Name Dob Age Maths Science Lang Maths Science Lang total Book Date of
no. Unit1 Unit1 Unit1 Unit2 Unit2 Unit2 issued return
1 Amruta 1/05/2002 19 59 88 71 79 79 57 433 2/06/2020 4/08/2020
An attribute that is not part of any candidate key is known as non-prime attribute.
BOOK DETAILS-
Student no. Book Date of return
issued
1 2/06/2020 4/08/2020
2 3/09/2020 9/10/2020
3 31/08/2020 9/09/2020
4 27/10/2020 18/11/2020
5 09/07/2020 20/08/2020
An attribute that is not part of any candidate key is known as non-prime attribute.
Assignment No. 3
Aim: Create Table with primary key and foreign key constraints.
Primary key:
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of
single or multiple columns (fields).
CREATE TABLE Persons (
Person_ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
To create a PRIMARY KEY constraint on the "ID" column when the table is already created,
use the following SQL:
Foreign Key:
• The Foreign key constraint is used to prevent actions that would destroy links
between tables.
• A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table.
• The table with the foreign key is called the child table, and the table with the primary
key is called the referenced or parent table.
• Notice that the "PersonID" column in the "Orders" table points to the "PersonID"
column in the "Persons" table.
• The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
• The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders"
table.
The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key
column, because it has to be one of the values contained in the parent table.
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is
already created, use the following SQL:
Assignment No: 4
Aim: Perform following SQL queries on the database created in assignment 1.
• Implementation of relational operators in SQL
• Boolean operators and pattern matching
• Arithmetic operations and built in functions
• Group functions
• Processing Date and Time functions
• Complex queries and set operators
Operator in SQL:
SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
An operator is a reserved word or a character used primarily in an SQL statement's WHERE
clause to perform operation(s), such as comparisons and arithmetic operations. These
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for
multiple conditions in a statement.
Relational Operator:
Relational operators are used for comparing numbers and strings. If a string is compared to a
number, MySQL will try to convert the string to a number.
Relational operators in SQL are as follows:
Numeric Functions:
String Functions:
Function Input Argument Value Returned
First letter of each word is changed to
INITCAP ( s ) s = character string uppercase and all other letters are in lower
case.
LOWER ( s ) s = character string All letters are changed to lowercase.
UPPER ( s ) s = character string All letters are changed to uppercase.
s1 and s2 are character Concatenation of s1 and s2. Equivalent
CONCAT ( s1, s2 )
strings to s1 || s2
s1 and s2 are character Returns s1 right justified and padded left
LPAD ( s1, n [, s2] ) strings and n is an integer with n characters from s2; s2 defaults to
value space.
s1 and s2 are character Returns s1 left justified and padded right
RPAD ( s1, n [, s2] ) strings and n is an integer with n characters from s2; s2 defaults to
value space.
s is a character string Returns s with characters removed up to
LTRIM ( s [, set ] ) and set is a set of the first character not in set; defaults to
characters space
s is a character string Returns s with final characters removed
RTRIM ( s [, set ] ) and set is a set of after the last character not in set; defaults
characters to space
s = character string,
REPLACE ( s, Returns s with every occurrence of
search_s = target string,
search_s [, replace_s search_s in s replaced by replace_s;
replace_s = replacement
]) default removes search_s
string
s = character string, m = Returns a substring from s, beginning in
SUBSTR ( s, m [, n
beginning position, n = position m and n characters long; default
])
number of characters returns to end of s.
LENGTH ( s ) s = character string Returns the number of characters in s.
s1 and s2 are character
Returns the position of the nth occurrence
INSTR ( s1, s2 [, m strings, m = beginning
of s2 in s1, beginning at position m, both
[, n ] ] ) position, n = occurrence
m and n default to 1.
of s2 in s1
The GROUP BY statement groups rows that have the same values into summary rows, like
"find the number of customers in each country".
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Date and time functions are scalar functions that perform an operation on a date and time
input value and returns either a string, numeric, or date and time value.
Complex queries:
Queries such as find duplicate row, second highest salary, fetch first or last record form table,
display first five rows are complex queries.
Set operators are used to join the results of two (or more) SELECT statements. The SET
operators are UNION, UNION ALL, INTERSECT, and MINUS.
UNION Operation
The UNION operator is used to combine the result-set of two or more SELECT statements.
• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
Syntax:
INTERSECT
The SQL INTERSECT clause/operator is used to combine two SELECT statements, but
returns rows only from the first SELECT statement that are identical to a row in the second
SELECT statement. This means INTERSECT returns only common rows returned by the
two SELECT statements.
Just as with the UNION operator, the same rules apply when using the INTERSECT
operator. MySQL does not support the INTERSECT operator.
Syntax
INTERSECT
MINUS
The Minus Operator in SQL is used with two SELECT statements. The MINUS operator is
used to subtract the result set obtained by first SELECT query from the result set obtained
by second SELECT query. In simple words, we can say that MINUS operator will return
only those rows which are unique in only first SELECT query and not those rows which are
common to both first and second SELECT queries.
Basic Syntax:
Conclusion: In this way, we have studied and implemented different SQL operator on sql
query for our database.
Assignment No: 5
Aim: Execute DDL/DML statements which demonstrate the use of views. Update the base
table using its corresponding view. Also consider restrictions on updatable views and perform
view creation from multiple tables.
VIEW:
A view contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database.
You can add SQL statements and functions to a view and present the data as if the data were
coming from one single table.
Updatable Views
Some views are updatable and references to them can be used to specify tables to be updated
in data change statements. That is, you can use them in statements such
as UPDATE, DELETE, or INSERT to update the contents of the underlying table.
1. The SELECT statement which is used to create the view should not include GROUP
BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex queries.
5. The view should be created from a single table. If the view is created using multiple
tables then we will not be allowed to update the view.
• It is not possible to create an index on a view.
• Indexes can be used for views processed using the merge algorithm. However, a view
that is processed with the temptable algorithm is unable to take advantage of indexes on
its underlying tables (although indexes can be used during generation of the temporary
tables).
To create a View from multiple tables we can simply include multiple tables in the
SELECT statement.
CREATE VIEW v AS
SELECT tbl1.NAME, tbl1.ADDRESS, tbl2.MARKS
FROM tbl1, tbl2
WHERE tbl1.id = tbl2.id;