You are on page 1of 44

EXPERIMENT NO :- 1

AIM :- To design ER model for specified system.

THEORY :-

1. ENTITY:- 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.

2. Relationship :- In the context of databases, is a situation that exists between two


relational database tables when one table has a foreign key that references the
primary key of the other table.
3. Attribute :- Entities are represented by means of their properties, called
attributes. All attributes have values.

4. Derived Attribute :- Derived attributes are the attributes that do not exist in the
physical database, but their values are derived from other attributes present in
the database.

5. COMPOSITE ATTRIBUTE :- Composite attribute is an attribute where the values


of that attribute can be further subdivided into meaningful sub-parts.

6. Multivalued attribute :- An attribute that can hold multiple values is known as


multivalued attribute. It is represented with double ovals in an ER Diagram.

7. Single Attribute :- An attribute that has only single value for an entity is known as
single valued attribute.

Problem Statement:-
Construct an ER diagram for a Resume Builder application.which has number of
users where the user can login and create their Resume.The Resume contains user
details which includes qualifications, experience, skills , etc
ER diagram :-

CONCLUSION :- THUS , WE HAVE STUDY ABOUT ALL TYPE OF ATTRIBUTE & HOW
TO DRAW ER diagram

Experiment NO 2

Aim :- Map ER Model to Relational Schema Model

Theory :-

Case 1:  Binary Relationship with 1:1 cardinality with


total participation of an entity

A person has 0 or 1 passport number and Passport is always owned by 1 person.


So it is 1:1 cardinality with full participation constraint from Passport.
First Convert each entity and relationship to tables.  Person table corresponds to
Person Entity with key as Per-Id. Similarly Passport table corresponds to Passport
Entity with key as Pass-No. Has Table represents relationship between Person and
Passport (Which person has which passport). So it will take attribute Per-Id from
Person and Pass-No from Passport.
Case 2: Binary Relationship with 1:1 cardinality and partial participation of
both entities

A male marries 0 or 1 female and vice versa as well. So it is 1:1 cardinality with
partial participation constraint from both. First Convert each entity and relationship
to tables.  Male table corresponds to Male Entity with key as M-Id. Similarly Female
table corresponds to Female Entity with key as F-Id. Marry Table represents
relationship between Male and Female (Which Male marries which female). So it
will take attribute M-Id from Male and F-Id from Female.
Case 3: Binary Relationship with n: 1 cardinality

In this scenario, every student can enroll only in one elective course but for an
elective course there can be more than one student. First Convert each entity and
relationship to tables.  Student table corresponds to Student Entity with key as S-Id.
Similarly Elective_Course table corresponds to Elective_Course Entity with key as
E-Id. Enrolls Table represents relationship between Student and Elective_Course
(Which student enrolls in which course). So it will take attribute S-Id from and
Student E-Id from Elective_Course.
Case 4: Binary Relationship with m: n cardinality

In this scenario, every student can enroll in more than 1 compulsory course and for
a compulsory course there can be more than 1 student. First Convert each entity
and relationship to tables.  Student table corresponds to Student Entity with key as
S-Id. Similarly Compulsory_Courses table corresponds to Compulsory Courses
Entity with key as C-Id. Enrolls Table represents relationship between Student and
Compulsory_Courses (Which student enrolls in which course). So it will take
attribute S-Id from Person and C-Id from Compulsory_Courses.
Case 5: Binary Relationship with weak entity

In this scenario, an employee can have many dependants and one dependant can
depend on one employee. A dependant does not have any existence without an
employee (e.g; you as a child can be dependant of your father in his company). So

it will be a weak entity and its participation will always be total. Weak Entity does
not have key of its own. So its key will be combination of key of its identifying entity
(E-Id of Employee in this case) and its partial key (D-Name).
First Convert each entity and relationship to tables.  Employee table corresponds to
Employee Entity with key as E-Id. Similarly Dependants table corresponds to
Dependant Entity with key as  D-Name and E-Id. Has Table represents relationship
between Employee and Dependants (Which employee has which dependants). So
it will take attribute E-Id from Employee and D-Name from Dependants.
TABLES :-

User Table
Username Image Mobile No
user_Id

Username is primary

Login Table
ID Username Password

username is Foreign Key of user Table

Resume Table
User R_Type R_name

Detail Table
ID_No Name Mobile No Certificate

ID_No is primary Key

Experience Table
No of year Job Type Company Name Last Company
Character Certificate

Last company character Certificate is primary Key

Skill Table
Specialization Skill Name
Skill_type

Primary key

Foreign Key
CONCLUSION :- THUS , WE STUDY TO CREATE Map ER Model to Relational
Schema Model IN THIS EXPERIMENT.

Experiment No :-3
Aim:- Performing DDL command.
Solution :-
1) Create Command:-
1) Theory :- This command is to create the table in the database.so as
to store the value in receptive table.
2) Syntax :- CREATE TABLE Registration
(ID int,
Name varchar(255),
Username varchar(255),
Password varchar(255)
);
3) Example:-

2) Rename Command:-
1) Theory:- This command is use to rename any name
that has to change.
2) Syntax:- ALTER TABLE Registration
CHANGE COLUMN Password TO PWD INT;
3) Example:-
a) Before execution
b) After execution

3) Truncate Command :-
1) Theory:- The TRUNCATE TABLE command deletes the data inside a table,
but not the table itself.

2) Syntax:- TRUNCATE TABLE Registration;


3)Example:-
a) Before execution:-
INSERT INTO Registration (ID,NAME,Username,PWD)
VALUES (1, 'Ramesh', 'qwerty@gmail.com', 123456 )

b) After execution:-
TRUNCATE TABLE Registration;

4) Alter Command:-
1) Theory:- ALTER TABLE statement is used to add, delete, or modify
columns in an existing table.The ALTER TABLE statement is also used to add and
drop various constraints on an existing table.
2) Syntax:-
1) Adding Column
ALTER TABLE Registration ADD phone_no varchar(255);

2) Drop column :-
1) ALTER TABLE Registration DROP COLUMN phone_no;

3) MODIFY COLUMN:-
1) ALTER TABLE Registration ALTER COLUMN PWD bigint(255);

5) Drop Table:-
1) Theory :- The DROP TABLE statement is used to drop an
existing table in a database.

2) Syntax:- DROP TABLE Registration;


3) Example:-
Conclusion:- Thus, we have study the basic of DDL Command.
EXPERIMENT NO:-4
AIM:- To perform DML Commands (INSERT, DELETE, UPDATE).
THEORY:-
1. INSERT:- The INSERT into statement is part of the Data Manipulation Language
commands. It is used to insert a new row in a table. There are two ways of using
INSERT statement for inserting rows:
SYNTAX:-
INSERT INTO table_name VALUES (value1, value2, value3..., value n);
INSERT INTO table_name (column1, column2..., column n) VALUES (value1,
value2, value3..., value n);
2. UPDATE: -The UPDATE <table> statement is part of the Data Manipulation
Language commands. It is used to update the data of an existing table in the
database. We can update single columns as well as multiple columns using
UPDATE statement as per user's requirement.
SYNTAX:-
UPDATE table_name SET column1 = value1, column2 = value2,... WHERE
condition;
3. DELETE:- The DELETE Statement in SQL is used to delete existing records from a
table. We can delete a single record or multiple records depending on the
condition we specify in the WHERE clause.
SYNTAX:-
DELETE FROM table_name WHERE some_condition;
4) DELETE ALL:-
Theory : The Delete All SQL query is use to delete the whole table.
Syntax : DELETE FROM login;
OUTPUT:-
1) INSERT:-
INSERT INTO LOGIN (ID ,Name, USERNAME, PASSWORD) VALUES
(1,'chaitali sheth', 'chaitali@gmail.com', 'sunil@mail.com', ‘chaitali123’),
(2,'sumit shah', 'sumit@mail.com', ‘sumit123’),
(3,'Sonal shah', 'sonal@mail.com', ‘sonal123’),
(4,'dhruv sheth', 'dhruv@mail.com',’dhruv123’),
(5,'divyesh jain', 'divyesh@mail.com', ‘divyesh123’),
(6,'raj jain', 'raj@gmail.com', ‘raj123’),
(7,'chintan patel', 'chintan@mail.com', ‘chintan123’),
(8,'harsh patel', 'harsh@mail.com', ‘harsh123’),
(9,'yash kakadiya', 'yash@mail.com', ‘yash123’),
(10,‘darshil shah', 'darshil@mail.com',’darshil123’);
BEFORE AFTER

2) UPDATE:-
UPDATE login SET name = 'dhruv sheth' Where name = 'Dhruv Sheth'

BEFORE AFTER
3) DELETE:-
DELETE FROM login WHERE (id = '4' or id = '7')
BEFORE AFTER

4) Delete All :

CONCLUSION:- Thus we successfully learnt and implemented DML Commands


using MYSQL.
Experiment No:-5
Aim:- To perform DQL Commands.
Theory:-
1. SELECT :- SELECT Query statement is part of Data Query Language
commands. It is used to fetch the data from a database table which returns
this data in the form of a result table. It is used to display data from
single/multiple tables. It can also be used to filter the rows based on the
condition .The user can also view the data in a particular order. SELECT
Query statement is used to display data from single/multiple tables. It can also
be used to filter the rows based on the condition provided by the user. The
user can also view the data in a particular order

SYNTAX :-
• Select * FROM <tableName>;
• SELECT * <column1, column2,....,column n> FROM <table_name>;
• SELECT * FROM <tableName> WHERE <condition>;
• SELECT * FROM <tableName> WHERE <condition1> [AND|OR]
<condition2>;

OUTPUT :-
1. SELECT dname , age FROM login WHERE ID = 1 or city = 4;

Before After
2. SELECT * FROM ‘login’ WHERE name like ‘d%’;

Before After
3. SELECT * FROM ‘login’ WHERE name LIKE ‘H%’ AND username like
'_C%’;

Before After

4. SELECT * FROM `login` WHERE name RLIKE '^[dhr]';

Before After
5. Aggregate Function : In Database management system an
aggregate function is a function where the values of multiple rows are
grouped together as input on certain criteria to form a single value of
more significant meaning.

a) Count () : Return total number of records.


Syntax : SELECT count (*) from login;

Before After

b) Sum () : The specified value from the record.


Syntax : SELECT sum(did) from login;

Before After
c) Avg () : Average is use to calculate average value of the specified
column from the records of the table.
Syntax : SELECT avg(ID) from login;

Before After

d) Min () : Minimum is use to calculate minimum value of the specified


column from the records of the table.
Syntax : SELECT min(ID) from login;

Before After

e) Max () : Maximum is use to calculate maximum value of the specified


column from the records of the table.
Syntax : SELECT max(ID) from login;
Before After

Conclusion:-Thus,we have study the basic of DQL Command.


Experiment No:-6
Aim:- To perform simple Queries.
Theory:-
1. Having Clause :-Having Clause in SQL specifies that an SQL select
statement should only return rows where aggregate values meet the
specified conditions.
Syntax :-
SELECT ID , name from login GROUP by ID , ID HAVING
SUM(ID) > 2

Before After

2. Order By Clause :- Order By Clause shows the record in ascending or


descending order of the specified condition.
Syntax :-
SELECT name , ID from login group by name , ID HAVING SUM(ID) > 2
order by ID desc
Before After

3. Group By Clause :-
The Group By Clause is often use to arrange the identical duplicate
data into groups with the select statement.
Syntax : -
SELECT name , SUM(Id) FROM login GROUP BY name

Before After

4. Distinct Clause : -
The Distinct Clause is used to filter unique records out of the duplicate
records that satisfy the query criteria.
Syntax :-
SELECT DISTINCT(name) , ID from login

Before After

Conclusion:-Thus , we have study how to execute simple Queries.


Experiment no:- 7
Aim:- Perform Nested queries and complex queries in mysql.
Theory:
A nested query or Sub query or Inner query is a query within another sql query
and imbedded within the WHERE clause.
A sub query is used to return data that will used in the main query as a condition
to further restrict the data to be retrieved.
It can be used with the SELECT, INSERT, UPDATE, AND DELETE statements along
with the operators like =, < , >, >=, <=, IN, BETWEEN, etc.
CODE
CREATE TABLE reg (fname varchar(10), mname varchar (10), lname varchar (10),
mno bigint(10),username varchar (9) , age varchar (10) , gender varchar (10) ,pwd
varchar (10));

INSERT INTO reg VALUES


(1,dhruv,alpesh,sheth,9876543210,dhruv.sheth@universal.edu.in,23,male,qwerty
21@),(1,chaitali,alpesh,sheth,9876543210,chaitali.sheth@universal.edu.in,34,fem
ale,123456789 ) );
OUTPUT:
After Execution
1. < : The operator <, selects the value which are less than to the given value.
CODE:
SELECT `ID`, `fname`, `mname`, `lname`,`mno`,`username`,`age`,`gender`,`pwd`
FROM reg WHERE age<40
OUTPUT:
After Execution

2. > : The operator > selects the rows which values are greater than the given
value.

CODE:
SELECT `ID`, `fname`, `mname`, `lname`,`mno`,`username`,`age`,`gender`,`pwd`
FROM reg WHERE age>20
OUTPUT:
After Execution

3. = : This operator selects the rows whose values are equals to the given
value.
CODE:
SELECT `ID`, `fname`, `mname`, `lname`,`mno`,`username`,`age`,`gender`,`pwd`
FROM reg WHERE age=20
OUTPUT:
After Execution

4. >= : the operator >= is used to select the data which are equal or greater
than given value.
CODE:
SELECT `ID`, `fname`, `mname`, `lname`,`mno`,`username`,`age`,`gender`,`pwd`
FROM reg WHERE age>=20
OUTPUT:
After Execution

5. <= : the operator <= selects the rows which have the values equals or less
than equal to the given value.
CODE:
SELECT `ID`, `fname`, `mname`, `lname`,`mno`,`username`,`age`,`gender`,`pwd`
FROM reg WHERE age<=20
OUTPUT:
After Execution
6. IN Operator : IN operator associated with the condition and it selects the
rows which satisfies the condition.

CODE:
SELECT * Form ‘reg’ WHERE ID IN(SELECT ID FROM reg WHERE age<25);
OUTPUT:
After Execution

7. Between Operator :
SELECT `ID`, `fname`,`username`, `age`, `pwd` FROM reg WHERE age BETWEEN 18
AND 25;
OUTPUT:
After Execution
CONCLUSION: Thus we learnt and successfully performed nested and complex
query in sql.
Experiment No : 8
Aim: To perform various join operation like inner join, right join, left join, full
outer join.

Theory:
A JOIN clause is used to combine rows from two or more tables, basaed on a
related column between them.
Different types of SQL JOINs
• Inner join
• Left join
• Right join
• Full Outer join
Here are the different types of the JOINs in SQL:
Registration table:-

Login table:-
1. Inner JOIN: Returns records that have matching values in both tables
Syntax:
Select table1.column,table2.column...From table1 INNER JOIN table2 ON
condition.

CODE:
SELECT reg.ID,login.id,login.fname FROM login INNER JOIN reg ON login.id = reg.ID

Output:-

LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table

Syntax:-
Select table1.column,table2.column... From table1 LEFT JOIN table2 ON condition.
CODE:
SELECT login.id,reg.ID,reg.fname FROM login LEFT JOIN reg ON reg.ID = login.id
Output:-
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the lefttable
Syntax:-
Select table1.column,table2.column... From table1 RIGHT JOIN table2 ON
condition.
CODE:
SELECT reg.ID,login.id,reg.fname FROM reg RIGHT JOIN login ON reg.ID = login.id
Output:-

FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table FULL OUTER JOIN
Syntax:-
Syntax for mysql:-
Select table1.column,table2.column... From table1 LEFT JOIN table2 ON condition.
UNION Select table1.column,table2.column... From table1 RIGHT JOIN table2 ON
condition.
Syntax for sql:-
Select table1.column,table2.column... From table1 FULL JOIN table2 ON
condition.
CODE:
SELECT login.id,reg.ID,login.fname FROM login FULL JOIN reg ON reg.ID = login.id
Output:-
id ID fname
1 1 dhruv
2 null Dhruv
3 3 chaitali
4 4 xyz

Conclusion: Thus, we studied and successfully implemented the join operation


Inner join, left outer join, right outer join, full join.
Experiment No : 9

Aim: To perform various VIEW OPERATION on a Database.


Theory:
A VIEW is nothing more than a SQL statement that is stored in the database with an
associated name. A view is actually a composition of a table in the form of a predefined SQL
query.
A VIEW can contain all rows of table or select rows from a table. A VIEW can be created
from one or many tables which depends on the written SQL query to create a view.
VIEW, which are a type of virtual tables allow users to do the following:

• Structure data in a way that users or classes of users find natural or intuitive.
• Restrict access to the data in such a way that a user can see and (sometimes) modify
exactly what they need and no more.
• Summarize data from various tables which can be used to generate reports.
For example, we will be using the student_details table to demonstrate and perform various
VIEW operations on it.
Sample Table

1. Creating Views : A view can be created using the CREATE VIEW statement.
Syntax:

CREATE VIEW view_name AS


SELECT column1, column2.....
FROM table_name
WHERE condition;

Before Execution:
After Execution:

2. CREATE OR REPLACE VIEW: We can use this to create or replace view statement to
add or remove fields from a view.
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;

3. DROPPING VIEW: TO drop a view we use DROP VIEW statement.

Syntax: DROP VIEW view_name;

Before Execution:
After Execution:

CONCLUSION: In this experiment we have studied and implemented VIEWS using SQL.
Experiment No : 10

AIM: To create Database Triggers


THEORY:
• Triggers are stored programs, which are automatically executed or
fired when some events occur.
• Triggers are, in fact, written to be executed in response to any of
the following events

Components of Triggers

• Event(E) – SQL statement that causes the trigger to fire(or


activate). This event may be insert, update, or delete operation
database table.
• Condition (C) – A condition that must be satisfied for execution
of trigger.
• Action (A) – This code or statement that execute when triggering
condition is satisfied and trigger is activated on database table.

BEFORE and AFTER of Trigger:

BEFORE triggers run the trigger action before the triggering statement
is run. AFTER triggers run the trigger action after the triggering
statement is run.

Syntax:
create trigger [trigger_name]

[before | after]

{insert | update |

delete} on

[table_name]

[for each row]

[trigger_body]
Created a database called “triggers”.
Creating table for customers:

CREATE TABLE

customer_details (

CID INT NOT NULL

AUTO_INCREMENT,

CNAME VARCHAR(20),

AGE INT(11),

CITY VARCHAR(30),

GENDER VARCHAR(20),

PRIMARY KEY(CID)

);

Creating a trigger for incrementing age by 10


Query:

CREATE TRIGGER calculate before INSERT ON customer_details


FOR EACH ROW SET new.age = new.age+10;
Performing Insert Operation in customer_details table:

Query:
INSERT INTO `customer_details` (`CID`, `CNAME`, `AGE`, `CITY`,
`GENDER `) VALUES (1, 'RAM', 40, 'Mumbai', 'Male');

After operation
To use this variant we need one more table i.e, Percentage where the
trigger will store the results. Use the below code to create the
Percentage Table.
create table Final_age(per int );
Query:
CREATE TRIGGER

total_calculate after insert ON

customer_details FOR EACH

ROW insert into Final_age

values(new.age);

Query:
INSERT INTO `customer_details` (`CID`, `CNAME`, `AGE`, `CITY`,
`GENDER`) VALUES (4, 'Mohan', 35, 'Pune', 'Male');
Explanation: Here when we insert data to the table, total_age trigger will store
the result in the Final_age table.

CONCLUSION: Thus, we have performed Trigger operation.

You might also like