0% found this document useful (0 votes)
27 views

Tulsi Ism File Final

The document discusses SQL and data types in SQL. It begins with an introduction to SQL, explaining that SQL is used to manage data in relational database management systems. It then discusses the different types of SQL statements and the history and design of SQL. Finally, it outlines the main SQL data types including numeric, string, date/time, and other types. It provides a high-level overview of SQL and data types.

Uploaded by

Chhavi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Tulsi Ism File Final

The document discusses SQL and data types in SQL. It begins with an introduction to SQL, explaining that SQL is used to manage data in relational database management systems. It then discusses the different types of SQL statements and the history and design of SQL. Finally, it outlines the main SQL data types including numeric, string, date/time, and other types. It provides a high-level overview of SQL and data types.

Uploaded by

Chhavi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

M FILE

SUBMITTED IN PARTIAL PELETION TO THE DEGREE OF

BACHOLAR’S OF BUSINESS
ADMINISTRATION
TO

DELHI SCHOOL OF PROFESSIONAL


STUDIES AND RESEARCH

SUBMITTED TO SUBMITTED BY
MR. KALIK GOYAL TULSI GUPTA
ASSISTANT PROFESSOR BBA 5TH B
(DSPSR) 3621250172
index
S.NO CONTENTS PAGE NO. T. SIGN
1. INTRODUCTION TO SQL. EXPLAIN WHAT ARE
DIFFERENT TYPES OF DATA IN SQL?
1-4
2. EXPLAIN SQL DDL COMMANDS WITH SYNTAX AND
EXAMPLES.
5-6
3. EXPLAIN SQL DML COMMAND WITH SYNTAX AND 7
EXAMPLES.
4. CREATE A TABLE NAMED CUSTOMER (C_ID, 8
C_NAME, ADDRESS, CITY, PINCODE, and
COUNTRY), INSERT AT LEAST 10 VALUES. DISPLAY
THE TABLES.
5. CREATE TABLE NAMED EMPLOYEE CONTAINING 9-10
COLUMNS (EMP_ID, EMP_NAME, EMP_DESIGN,
DOB, EMP_SALARY AND EMP_DEPARTMENT).
INSERRT ATLEAST 10 VALUES.
CONSTRAINTS APPLIES ON EMPLOYEE TABLE:
1. EMP_ID SHOULD BE PRIMARY KEY.
2. EMP_SALARY SHOULD NOT CONTAIN ANY
NULL VALUE.
3. EMP_DESIG SHOULD BE UNIQUE.
6. CREATE EMPLOYEE TABLE: -
EID, ENAME, DESG, BRANCH, SALARY, ADDRESS.
11-15
INSERT 10 RECORDS.
*SALARY COLUMN SHOULD NOT CONYAIN ANY
NULL VALUE
*SALARY SHOULD BE BETWEEN 5000 TO 550000
*ENAME SHOULD BE UNIQUE
PERFORM VARIOUS SQL COMMANDS
1. ADD COLUMN IN ABOVE TABLE DATA OF
JOINING, EXPERIENCE.
2. DISPLAY THE TABLE.
3. COMPLETE THE TABLE DEFINITION
4. DELETE THE COLUMN DESG
5. FIND OUT DEYAILS OF EMPLOYEE WHOSE
SALARY IS ABOVE 25000
6. FIND OUT DETAILS OF EMPLOUEES ORDR
BY SALARY.
7. CALCULATE TOTAL NUMBER OF RECORDS
IN EMPLOYEE TABLE.
8. LIST EMPLOYEES WHOSE SALARY IS
BETWEEN 10000 AND 30000.
7. CREATE THE FOLLOWING TABLE AND PERFORM
SQL COMMANDS. STUDENT (ROLL_NUMBER,
16-19
NAME, AGE, COURSE, MARKS).
1. LIST ALL THOSE STUDENTS WHO ARE
GREATER THAN 18 YEARS OF AGE AND
HAVE OPTED FOR MBA COURSE.
2. LIST NAME OF STUDENT WHOSE NAME
END WITH ‘I’
3. FIND OUT TOTAL NUMBERF OF RECORDS
IN TABLE.
4. FIND OUT THE NAME, COURSE, MARKS
AND SORT IN THE ORDER OF MARKS.
5. DISPLAY NAME AND COURSE OF
STUDENTS.
6. FIND THE STUDENTS WITH MAX MARKS.
7. LIST THE NAME OF STUDENTS ORDER BY
ROLL_NO IN DESCENDING ORDER.
8. FIND OUT THE AVERAGE OF ALL MARKS.
DISPLAY IT AS AVERAGE _MARKS.
8. CREATE THE FOLLOWING TABLES, INSERRT AT
LEAST 10 RECORDS IN EACH.
20-26
TABLE 1: SUPPLIER (S_NO, SNAME, STATUS, CITY)
TABLE 2: PARTS (P_NO, PNAME, COLOR, WEIGHT,
CITY)
TABLE 3: SP (S_NO, P_NO, QUANTITY)
ANSER THE FOLLOWING QUERIES IN SQL: -
A. FIND THE SUPPLIER FOR CITY = ‘DELHI’
B. FIND SUPPLIERS WHOSE NAME START
WITH ‘AB’
C. FIND ALL SUPPLIERS WHOSE STATUS IS 10,
20 OR 30
D. FIND TOTAL NUMBER OF CITY OF THE
SUPPLIERS
E. FIND THE NAME OF SUPPLIERS WHO
SUPPLIES QUANTIY OF THE ITEM P1 MORE
THAN 50
9. WHAT DO YOU UNDERSTAND BY ER MODELLING/ 27-31
EXPLAIN ALL THE SYMBOLS OF ER MODEL WITH
EXAMPLES.
10 DRAW ENTITY RELATIONSHIP DIAGRAM OF AN
ORGANIZATION.
32
11 DRAW ENTITY RELATIONSHIP DIAGRAM OF A
BANKING SYSTEM.
33
12 DRAW ENTITY RELATIONSHIP DIAGRAM OF A
HOSPITAL MANAGEMENT SYSTEM.
34
13 Draw Entity Relationship diagram of an University. 35
Q1.) Introduction to SQL. Explain what are different types of data
types in SQL.
ANS.) SQL ( Structured Query Language) is a domain-specific language used in programming and
designed for managing data held in a relational database management system (RDBMS), or for
stream processing in a relational data stream management system (RDSMS). It is particularly useful
in handling structured data, i.e. data incorporating relations among entities and variables.

SQL (Structured Query Language) is a database sub-language for querying and modifying relational
databases. It was developed by IBM Research in the mid 70’s and standardized by ANSI (American
National Standards Institutes) in 1986.
The Structured Query Language (SQL) is a language of databases. All modern relational databases,
including Access, File-Maker pro, Microsoft SQL Server and Oracle use SQL as their basic building
block. All of the graphical user interfaces that provide data entry and manipulation functionality are
nothing more than SQL translators. They take the actions you perform graphically and convert them
to SQL commands understood by the database. Using SQL, you can insert records, update records,
and delete records. You can also create new database objects such as databases and tables. And you
can drop (delete) them.

SQL offers two main advantages over older read–write APIs such as ISAM or VSAM. Firstly, it
introduced the concept of accessing many records with one single command. Secondly, it eliminates
the need to specify how to reach a record, e.g. with or without an index.

Originally based upon relational algebra and tuple relational calculus, SQL consists of many types of
statements, which may be informally classed as sub languages, commonly: a data query language
(DQL), a data definition language (DDL), a data control language (DCL), and a data manipulation
language (DML). The scope of SQL includes data query, data manipulation (insert, update and
delete), data definition (schema creation and modification), and data access control. Although SQL is
essentially a declarative language (4GL), it includes also procedural elements.

SQL was one of the first commercial languages to utilize Edgar F.Codd’s relational model. The
model was described in his influential 1970 paper, "A Relational Model of Data for Large Shared
Data Banks". Despite not entirely adhering to the relational model as described by Codd, it became
the most widely used database language

History
SQL was initially developed at IBM by Donald D. Chamberlin and Raymond F. Boyce after learning
about the relational model from Ted Codd in the early 1970s. This version, initially called SEQUEL
(Structured English Query Language), was designed to manipulate and retrieve data stored in IBM's
original quasi-relational database management system, System R, which a group at IBM San Jose
Research Laboratory had developed during the 1970s.

1
Chamberlin and Boyce's first attempt of a relational database language was Square, but it was
difficult to use due to subscript notation. After moving to the San Jose Research Laboratory in 1973,
they began work on SEQUEL. The acronym SEQUEL was later changed to SQL because
"SEQUEL" was a trademark of the UK-based Hawker Siddeley Dynamics Engineering Limited
company. After testing SQL at customer test sites to determine the usefulness and practicality of the
system, IBM began developing commercial products based on their System R prototype including
System/38, SQL/DS, and DB2, which were commercially available in 1979, 1981, and 1983,
respectively.

In the late 1970s, Relational Software, Inc. (now Oracle Corporation) saw the potential of the
concepts described by Codd, Chamberlin, and Boyce, and developed their own SQL-based RDMS
with aspirations of selling it to the U.S. Navy, Central Intelligence Agency, and other U.S.
government agencies. In June 1979, Relational Software, Inc. introduced the first commercially
available implementation of SQL, Oracle V2 (Version2) for VAX computers.

Design
SQL deviates in several ways from its theoretical foundation, the relational model and its tuple
calculus. In that model, a table is a set of tuples, while in SQL, tables and query results are lists of
rows: the same row may occur multiple times, and the order of rows can be employed in queries (e.g.
in the LIMIT clause).Critics argue that SQL should be replaced with a language that returns strictly
to the original foundation: for example, see The Third Manifesto. However, no known proof exists
that such uniqueness cannot be added to SQL itself, or at least a variation of SQL. In other words, it's
quite possible that SQL can
be "fixed" or at least improved in this regard such that the industry may not have to switch to a
completely different query language to obtain uniqueness. Debate on this remains open.

What Can SQL do?


SQL can execute queries against a database. SQL can retrieve data from a database.
SQL can insert records in a database. SQL can update records in a database. SQL can delete records
from a database. SQL can create new databases.
SQL can create new tables in a database.
SQL can create stored procedures in a database. SQL can create views in a database.
SQL can set permissions on tables, procedures, and views.

Different data type in SQL


SQL Data Types define the type of value that can be stored in a table column. For example, if we
want a column to store only integer values, then we can define its data type as int.

2
SQL Data Types
SQL data types can be broadly divided into following categories.

Numeric data types such as int, tinyint, bigint, float, real etc.
Date and Time data types such as Date, Time, Datetime etc.
Character and String data types such as char, varchar, text etc.
Unicode character string data types, for example nchar, nvarchar, ntext etc.
Binary data types such as binary, varbinary etc.
Miscellaneous data types – clob, blob, xml, cursor, table etc.

SQL Data Types important points


Not all data types are supported by every relational database vendor. For example, Oracle database
doesn’t support DATETIME and MySQL doesn’t support CLOB data type. So while designing
database schema and writing sql queries, make sure to check if the data types are supported or not.
Data types listed here doesn’t include all the data types, these are the most popularly used data types.
Some relational database vendors have their own data types that might be not listed here. For
3
example, Microsoft SQL Server has data types but since it’s not supported by other popular database
vendors, it’s not listed here.
Every relational database vendor has its own maximum size limit for different data types, you don’t
need to remember the limit. Idea is to have the knowledge of what data type to be used in a specific
scenario.

The following table lists the general data types in SQL: -


CHARACTER(n)- Character string. Fixed-length n
VARCHAR(n) or CHARACTER VARYING(n)- Character string. Maximum length n
BINARY(n)- Binary string. Fixed-length n
BOOLEAN- Stores TRUE or FALSE values
VARBINARY(n) or BINARY VARYING(n)- Binary string. Variable length. Maximum length n
INTEGER(p)- Integer numerical (no decimal). Precision p SMALLINT- Integer numerical (no
decimal). Precision 5 BIGINT- Integer numerical (no decimal). Precision 19

4
Q2.) Explain SQL DDL Commands. With Syntax and Examples.
ANS.) The Data Definition Language (DDL) is used to create and destroy databases and databases
objects. These SQL statements define the structure of a database, including rows, columns, tables,
indexes, and databases specifics such as file locations.
CREATE – To create objects in the database.
ALTER – Alters the structure of the database.
DROP – Delete objects from the databases.
TRUNCATE–is used to remove all records from a table, including all spaces allocated for the
records are removed.
COMMENT –is used to add comments to the data dictionary.
RENAME –is used to rename an object existing in the database.
The create table command: -
The create table command defines each column of the table uniquely. Each column has minimum of
three attributes.
Name
Data type
Size (column width).
Each table column definition is a single clause in the create table syntax. Each table column
definition is separated from the other by a comma. Finally, the SQL statement is terminated with a
semicolon. The Structure of Create Table Command
Table name is Student

Column name Data type Size


Reg_no varchar2 10
Name Char 30
DOB Date
Address varchar2 50

Example:
CREATE TABLE Student(Reg_no varchar2(10), Name char(30), DOB date, Address varchar2(50));

The DROP Command: -


Syntax: DROP TABLE <table_name>

Example:
DROP TABLE Student;
It will destroy the table and all data which will be recorded in it.

The TRUNCATE Command: -


Syntax:
TRUNCATE TABLE <Table_name>
5
Example:
TRUNCATE TABLE Student;

The RENAME Command: -


Syntax:
RENAME <OldTableName> TO <NewTableName>

Example:
RENAME <Student> TO <Stu>
The old name table was Student now new name is the Stu.

The ALTER Table Command: -


By The use of ALTER TABLE Command, we can modify our exiting table.

Adding New Columns


Syntax:
ALTER TABLE <table_name>
ADD (<NewColumnName> <Data_Type>(<size>),. n)
Example:
ALTER TABLE Student ADD (Age number(2), Marks number(3));

The Student table is already existing and then we added two more columns Age and Marks
respectively, by the use of above command.

Dropping a Column from the Table


Syntax:
ALTER TABLE <table_name> DROP COLUMN <column_name>
Example:
ALTER TABLE Student DROP COLUMN Age
This command will drop particular column

Modifying Existing Table


Syntax:
ALTER TABLE <table_name> MODIFY (<column_name> <NewDataType>(<NewSize>))
Example:
ALTER TABLE Student MODIFY (Name Varchar2(40));
The Name column already exist in Student table, it was char and size 30, now it is modified by
Varchar2 and size 40.
Restriction on the ALTER TABLE
Using the ALTER TABLE clause the following tasks cannot be performed.
Change the name of the table
Change the name of the column
Decrease the size of a column if table data exists

6
Q3.) Explain SQL DML commands with Syntax and Examples.
ANS.) The SQL commands that deals with the manipulation of data present in database belong
to DML or Data Manipulation Language and this includes most of the SQL statements.
Examples of DML:
SELECT – is used to retrieve data from a database.
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.

The Insert Command: SQL INSERT statement allows to insert single or multiple records into
the tale.
Syntax
INSERT INTO table
(column-1,column-2, …column-n) (value- 1, value-2, … value-n);

The UPDATE Command: -

UPDATE QUERY is used to update existing records in the table. Syntax


UPDATE table SET column1=value1,column2=value2… WHERE condition; Example
The following example update the Mike’s salary to 35000 in the Employee table. UPDATE
Employee SET Salary= 35000 WHERE Name = ‘Mike’;

The DELETE Command

DELETE QUERY is used to delete selected rows, or all rows from the table. Syntax
DELETE from table WHERE condition; Example
The following example delete the record where EID equals to 10 from the Employee table,
DELETE from Employee WHERE EID=10;

7
Q4.) Create a table named Customer (C_id, C_name, Address, city,
pin code, Country). Insert at least 10 values. Display the table.
ANS.)
create database db1;
use db1;
Create table customer (customer_id int, customer_name varchar(20), address varchar(50), city
varchar(20), pin code int, country varchar (20));
insert into customer value(01, 'pradip', '2/22 shastri park', 'New delhi', 110212, 'India');
insert into customer value(02, 'ishan', '2/23 rohini', 'New delhi', 110213, 'India');
insert into customer value(03, 'gaurav', '2/24 rohini sec 10', 'New delhi', 110214, 'India');
insert into customer value(04, 'nikhil', '2/25 welcome', 'New delhi', 110215, 'India');
insert into customer value(05, 'sagar', '2/26 tila mor', 'ghaziabad', 110216, 'India');
insert into customer value(06, 'pranav', '2/27 dilshad garden', 'New delhi', 110216, 'India');
insert into customer value(07, 'naman', '2/28 welcome', 'New delhi', 110217, 'India');
insert into customer value(08, 'himanshu', '2/29 dwarka', 'New delhi', 110218, 'India');
insert into customer value(09, 'vishal', '2/30 rohini sec 24', 'New delhi', 110219, 'India');
insert into customer value(10, 'faiz', '2/31 kalkaji', 'New delhi', 110220, 'India');
select * from customer;

8
Q5.) Create table named Employee containing columns (emp_id,
emp_name, emp_desig, DOB, emp_sal, emp_dept). Insert atleast 10
values.
ANS.) Constraints applied on Employee table:

Emp_id should be primary key


Emp_sal should not contain any Null Value
Emp_desig should be Unique

create database emp;


use emp;
create table employee(emp_id int primary key, emp_name varchar(20), emp_desig varchar(30)
unique, DOB date, emp_sal int not null, emp_dept varchar(50));
insert into employee values(01, 'Ayush', 'Finance Manager', '1990/01/20', 50000, 'Finance');
insert into employee values(02, 'Rohit', 'Marketing Manager', '1998/02/08', 60000, ' Marketing');
insert into employee values(03, 'Navneet', 'Sales Manager', '1995/03/01', 70000, 'Sales');
insert into employee values(04, 'Sharon', 'Promotion Manager', '1993/04/16', 80000, 'Promotion');
insert into employee values(05, 'Mayank', 'Purchase Manager', '1999/05/24', 90000, 'Purchase');
insert into employee values(06, 'Ansh', 'Production Manager', '1991/06/02', 100000, 'Production');
insert into employee values(07, 'Shubham', 'HR Manager', '1992/07/06', 110000, 'HR');
insert into employee values(08, 'Abhishek', Investment Manager', '1994/08/07', 120000,
'Investment');
insert into employee values(09, 'Rohan', 'Research Manager', '1996/09/25', 130000, 'Research');
insert into employee values(10, 'Naman', 'Operations manager', '1997/10/12', 140000, 'Operations');
Select * from employee;

9
10
Q6: Create Employee table: -
EID, ENAME, DESG, BRANCH, SALARY, ADDRESS.
Insert 10 Records.
*salary Column should not contain any Null value
*Salary should be between 5000 to 350000
*ENAME should be Unique Perform various SQL commands
Add column in above table date of joining, Experience.
Display the Table
Complete the Table Definition
Delete the Column DESG
Find out details of employee whose salary is above 25000.
Find out details of employee order by salary.
Calculate total no. of records in employee table.
List Employees whose salary is between 10000 and 30000.
ANS
mysql> use emp;
mysql> create table employee1(eid int, ename varchar(20), desg varchar(30), branch varchar(50),
salary int not null check(salary <350000 and salary >5000), address varchar(50));
mysql> insert into employee1 values(01, 'pradeep', 'finance manager', 'NSP', 40000, '4/A Sonipat,
Haryana');
mysql> insert into employee1 values(02, 'navneet', 'HR manager', 'Gurugram', 60000, '122 A-Block
VikasPuri, Delhi');
mysql> insert into employee1 values(03, 'sharon', 'marketing manager', 'Rohini', 20000, '1/333
Govind Puram, Delhi');
mysql> insert into employee1 values(04, 'ayush', 'operations manager', 'Dwarka', 70000, 'A400
Shastri Nagar, Delhi');
mysql> insert into employee1 values(05, 'mayank', 'sales manager', 'Indrapuram', 50000, '92 Moti
Nagar, Delhi');
mysql> insert into employee1 values(06, 'ansh', 'purchase manager', 'Noida', 80000, 'Plot No.10
Rohini Sec. 24, Delhi');
mysql> insert into employee1 values(07, 'naman', 'promotion manager', 'Mohan Nagar', 90000,
'113/2 Inderlok, Delhi');
mysql> insert into employee1 values(08, 'harshit', 'prroduction manager', 'NSP', 100000, 'B-16
Shastri Nagar, Delhi');
mysql> insert into employee1 values(09, 'lakshay', 'investment manager', 'Gurugram', 110000, '49
Adarsh Nagar, Delhi');
mysql> insert into employee1 values(10, 'abhishek', 'operations manager', 'Noida', 150000, '12/3
Shakti Nagar, Delhi');
mysql> select * from employee1;
11
1. Add column in above table date of joining, Experience. alter table employee add column
experience date;
Display the Table

12
Complete the Table Definition
update employee set experience= '1990/12/12';

Delete the Column DESG


alter table employee drop desg;

13
Find out details of employee whose salary is above 25000.
select * from employee where salary>25000;

Find out details of employee order by salary.


select * from employee order by salary

14
Calculate total no. of records in employee table.
select count(eid) from employee;

List Employees whose salary is between 10000 and 30000.


select * from employee where salary between 10000 and 30000;

15
Q7: Create the following table and perform SQL commands. Student
(Roll_no, name, age, course, marks).
List all those students who are greater than 18 years of age and have
opted for MBA course.
List name of student whose name end with ‘i’.
Find out total number of records in table.
Find out the name, course, marks and sort in the order of marks.
Display name and course of student.
Find the Student with Max marks.
List the name of students ORDER BY Roll_no in Descending order.
Find out the Average of all the Marks. Display it as Average_Marks.
ANS. create database st1;
use st1;
create table student(roll_no int, name varchar(20), age int, course varchar(10), marks int);
insert into student values(1 'pradip', 20, 'mba', 90);
insert into student values(2, 'ishan', 19, 'bba', 80);
insert into student values(3, 'gaurav', 20, 'mba', 85);
insert into student values(4, 'nikhil', 21, 'bcom', 75);
insert into student values(5, 'aditi', 18, 'bba', 60);
select * from student;

16
List all those students who are greater than 18 years of age and have opted for MBA course.
select * from student where age>18 and course= 'mba';

List name of student whose name end with ‘i’.


select * from student where name like 'i%';

Find out total number of records in table.


select count(roll_no) from student;

17
Find out the name, course, marks and sort in the order of marks.
select name, course, marks from student order by marks;

Display name and course of student.


select name, course from student;

Find the Student with Max marks.


select max(marks), name from student;

18
List the name of students ORDER BY Roll_no in Descending order.
select name from student order by roll_no desc;

Find out the Average of all the Marks. Display it as Average_Marks


select avg(marks) from student;

19
Q8.) Create the Following Tables. Insert at least 10 records in each.
Table 1: Supplier (S_No, Sname, Status, City) Table 2: Parts (P_No,
Pname, Color, Weight, City) Table 3: SP (S_No, P_No, Quantity)
Answer the following queries in SQL:
Find the supplier for city = ‘Delhi’
Find suppliers whose name start with ‘AB’
Find all suppliers whose status is 10, 20 or 30
Find total number of city of all suppliers
Find the name of suppliers who supplies quantity of the item P1
more than 50
Ans
Create database dat;
use dat;
Create table supplier(s_no int primary key, s_name varchar(20), status int, city varchar(30));
insert into supplier values(10, 'Ayush', 20, 'Delhi');
insert into supplier values(20, 'Navneet', 40, 'Alipur');
insert into supplier values(30, 'Sharon', 30, 'Ahmedabad');
insert into supplier values(40, 'Rohit', 50, 'Mumbai');
insert into supplier values(50, 'Shubham', 80, 'Ghaziabad');
insert into supplier values(60, 'Naman', 60, 'Gazipur');
insert into supplier values(70, 'Abhishek', 90, 'Delhi');
insert into supplier values(80, 'Krishna', 50, 'Noida');
insert into supplier values(90, 'Amit', 20, 'Noida');
insert into supplier values(100, 'Mayank', 40, 'Mumbai');
select * from supplier;

20
21
Table 2

create table parts(p_no varchar(20) primary key, p_name varchar(20), color varchar(10), weight
int, city varchar(30));
insert into parts values('p1', 'mouse', 'white', 100, 'Delhi');
insert into parts values('p2', 'keyboard', 'white', 500, 'Delhi');
insert into parts values('p3', 'monitor', 'black', 1000, 'Ambala');
insert into parts values('p4', 'speaker', 'black', 400, 'Mumbai');
insert into parts values('p5', 'speaker', 'white', 500, 'mouse');
insert into parts values('p6', 'webcam', 'brown', 200, 'Mathura');
insert into parts values('p7', 'printer', 'white', 400, 'Delhi');
insert into parts values('p8', 'cup', 'black', 900, 'Delhi');
insert into parts values('p9', 'ups', 'blue', 700, 'Mumbai');
insert into parts values('p10', 'cup', 'black', 400, 'Mumbai');
select * from parts;

22
Table 3

create table spa(s_no int primary key, p_no varchar(10), quantity int);

insert into sp values(10, 'p1', 30);


insert into sp values(20, 'p3', 70);
insert into sp values(30, 'p1', 80);
insert into sp values(40, 'p8', 10);
insert into sp values(50, 'p6', 90);
insert into sp values(60, 'p1', 70);
insert into sp values(70, 'p5', 20);
insert into sp values(80, 'p1', 80);
insert into sp values(90, 'p7', 10);

23
insert into sp values(100, 'p1', 90);

select * from sp;

24
Find the supplier for city = ‘Delhi’
Select s_name from supplier where city='delhi';

Find suppliers whose name start with ‘AB’


Select * from supplier where s_name like 'ab%';

Find total number of city of all suppliers


Select count(city) from supplier;

25
Find all suppliers whose status is 10, 20 or 30
Select s_name from supplier where status in (10,20,30);

Find the name of suppliers who supplies quantity of the item P1 more than 50
select s_name from supplier, sp where supplier.s_no=sp.s_no and sp.p_no='p1' and
sp.quantity>50;

26
Q9.) What do you understand by ER Modelling? Explain all the
symbols of ER Model with examples.
ANS.) An entity relationship diagram (ERD), also known as an entity relationship model, is a
graphical representation of an information system that depicts the relationships among people,
objects, places, concepts or events within that system. An ERD is a data modelling technique that
can help define business processes and be used as the foundation for a relational database.
There are three basic components of an entity relationship diagram:
Entities, which are objects or concepts that can have data stored about them.
Attributes, which are properties or characteristics of entities. An ERD attribute can be denoted as a
primary key, which identifies a unique attribute, or a foreign key, which can be assigned to multiple
attributes.
The relationships between and among those entities.
An entity relationship diagram showing relationships between sales reps, customers and product
orders.
For example, an ERD representing the information system for a company's sales department might
start with graphical representations of entities such as the sales representative, the customer, the
customer's address, the customer's order, the product and the warehouse. Then lines or other symbols
can be used to represent the relationship between entities, and text can be used to label the
relationships.
A cardinality notation can then define the attributes of the relationship between the entities.
Cardinalities can denote that an entity is optional (for example, a sales rep could have no customers
or could have many) or mandatory (for example, there must be at least one product listed in an
order.) The three main cardinalities are:
A one-to-one relationship (1:1). For example, if each customer in a database is associated with one
mailing address.
A one-to-many relationship (1: M). For example, a single customer might place an order for multiple
products. The customer is associated with multiple entities, but all those entities have a single
connection back to the same customer.
A many-to-many relationship (M: N). For example, at a company where all call center agents work
with multiple customers, each agent is associated with multiple customers, and multiple customers
might also be associated with multiple agents.
ER Diagrams Symbols, and notations

27
ER Diagram Symbols and Notations
Components of an E-R diagram
An E-R diagram constitutes of following Components
Entity: - Any real-world object can be represented as an entity about which data can be stored in a
database. All the real world objects like a book, an organization, a product, a car, a person are the
examples of an entity. Any living or non-living objects can be represented by an entity. An entity is
symbolically represented by a rectangle enclosing its name.

Entities can be characterized into two types:


Strong entity: A strong entity has a primary key attribute which uniquely identifies each entity.
Symbol of strong entity is same as an entity.

Weak entity: A weak entity does not have a primary key attribute and depends on other entity
via a foreign key attribute.

Attribute: - Each entity has a set of properties. These properties of each entity are termed as
attributes. For example, a car entity would be described by attributes such as price, registration
number, model number, color etc. Attributes are indicated by ovals in an e-r diagram.

28
A primary key attribute is depicted by an underline in the e-r diagram. An attribute can be
characterized into following types:
Simple attribute: - An attribute is classified as a simple attribute if it cannot be partitioned into
smaller components. For example, age and sex of a person. A simple attribute is represented by an
oval.
Composite attribute: - A composite attribute can be subdivided into smaller components which
further form attributes. For example, ‘name’ attribute of an entity “person” can be broken down into
first name and last name which further form attributes. Grouping of these related attributes forms a
composite attribute. ‘name is the composite attribute in this example.

Composite Attribute
Single valued attribute: - If an attribute of a particular entity represents single value for each
instance, then it is called a single-valued attribute. For example, Ramesh, Kamal and Suraj are the
instances of entity ‘student’ and each of them is issued a separate roll number. A single oval is used
to represent this attribute.
Multi valued attribute: – An attribute which can hold more than one value, it is then termed as multi-
valued attribute. For example, phone number of a person. Symbol of multi-valued attribute is shown
below,

Multi Valued Attribute


Derived attribute: A derived attribute calculates its value from another attribute. For example, ‘age’
is a derived attribute if it calculates its value from ‘current date’ & ‘birth date’ attributes. A derived
attribute is represented by a dashed oval.
Relationships: - A relationship is defined as bond or attachment between 2 or more entities.
Normally, a verb in a sentence signifies a relationship.
For example,
An employee assigned a project.
Teacher teaches a student.
Author writes a book.
A diamond is used to symbolically represent a relationship in the e-r diagram.
29
Relationship
Various terms related to relationships
Degree of relationship: - It signifies the number of entities involved in a relationship. Degree of a
relationship can be classified into following types:
Unary relationship: - If only single entity is involved in a relationship then it is a unary relationship.
For example, An employee(manager) supervises another employee.

Unary relationship
Binary relationships: - when two entities are associated to form a relation, then it is known as a
binary relationship. For example, A person works in a company. Most of the times we use only
binary relationship in an e-r diagram. The teacher-student example shown above signifies a binary
relationship.
Other types of relationships are ternary and quaternary. As the name signifies, a ternary relationship
is associated with three entities and a quaternary relationship is associated with four entities.
b.) Connectivity of a relationship: - Connectivity of a relationship describes, how many instances of
one entity type are linked to how many instances of another entity type. Various categories of
connectivity of a relationship are;
One to One (1:1) – “Student allotted a project” signifies a one-to-one relationship because only one
instance of an entity is related with exactly one instance of another entity type.

One to One
One to Many (1:M) – “A department recruits faculty” is a one-to-many relationship because a
department can recruit more than one faculty, but a faculty member is related to only one
department.

30
One to Many
Many to One (M:1) – “Many houses are owned by a person” is a many-to-one relationship because a
person can own many houses but a particular house is owned only a person.

Many to One
Many to Many (M: N) – “Author writes books” is a many-to-many relationship because an author
can write many books and a book can be written by many authors.

Many to Many
Many to Many (M: N) – “Author writes books” is a many-to-many relationship because an author
can write many books and a book can be written by many authors.

31
Q10.) Draw Entity Relationship diagram of an Organization.
ANS.) Entity: A definable thins such as person, abject, concept or event that can have data stored
about it.
Think of entities as noun.
In this ERD of organization we have three Entities: - Department, Project, Employee.
Attributes: A property or characteristic of an entity. Often shown as an oval.
In this ERD of organization: -
Department has: - Name, D_no, Location
Project has: - P_Name, P_Duration, P_No.
Employee: -Name, DOB, Gender, Phone_No. Email.
Relationship: - Relationships are typically shown as diamonds on the connecting lines. In this ERD
of organization we have four relationships: - works for, supervise, control.
PRIMARY KEY: - The field that is unique for all the record occurrences.it denotes as Underline. In
this ERD we have D_no in as primary key in Department, P_no in Project and Email in Employee.

32
Q11.) Draw Entity Relationship diagram of a Banking System.
ANS.) Entity: A definable thins such as person, abject, concept or event that can have data stored
about it think of entities as noun.
In this ERD of organization we have four Entities: - Bank, Card facility, Account, Customer.
TRANSACTION, Accounts.
Attributes: A property or characteristic of an entity. Often shown as an oval.
In this ERD of organization: -
BANK: - B_id, Name, Address
Account: - Acc_no, Acc_type, balance.
Customer: - Name, C_Id, Phone.no., Address.
Card facilities: - Card_no., Credit_card, Debit_card, Rupee-pay
Relationship: - Relationships are typically shown as diamonds on the connecting lines. In this ERD
of organization we have four relationships:-Offers, Has, Has.
PRIMARY KEY: - The field that is unique for all the record occurrences.it denotes as Underline. In
this ERD we have bank_id in as primary key in Bank, Acc_No in Account, C_id in Customer,
Card_No. in Card facilities.

33
Q12.) Draw Entity Relationship diagram of a Hospital Management
System.
ANS.) Entity: A definable thins such as person, Object, concept or event that can have data stored
about it think of entities as noun.
In this ERD we have five Entities: - Patient, Medical records, Hospital, Doctor, Attributes:
A property or characteristic of an entity. Often shown as an oval.
In this ERD: -
Patient: - P. Name, P.Id, P. Address, P.Diagnosis
Hospital:- Hos.Id, Hos.Name, Hos.No.
Medical records: - R.No, Room. Charge, Doc.
Charge Doctor:- Doc_id, Doc_Name, Salary, Qualification
Relationship: - Relationships are typically shown as diamonds on the connecting lines. In this ERD
of organization we have four relationships: - Has, admitted in, has
PRIMARY KEY: - The field that is unique for all the record occurrences.it denotes as Underline. In
this ERD we have P-id in patient, Hos-id in Hospital, R.No in Medical record, Doc-id in Doctor.

34
Q13.) Draw Entity Relationship diagram of a University.
ANS.) Entity: A definable thins such as person, Object, concept or event that can have data stored
about it think of entities as noun.
In this ERD we have five Entities: - Student, University, College, Course and Faculty Attributes:
A property or characteristic of an entity. Often shown as an oval.
In this ERD:-
Student: - Stu_Id, Stu_Name, DOB, Phone_no
University: - Uni_name, Uni_id, Location, Fees
College: - Name, Location, Clg_id
Course: - Course_Id, Name, Duration
Faculty: - Salary, Fac_name, Fac_id, Qualification
Relationship: - Relationships are typically shown as diamonds on the connecting lines. In this ERD
of organization we have four relationship: -Has, Study in, Enrols in, Teaches by
PRIMARY KEY: - The field that is unique for all the record occurrences.it denotes as Underline. In
this ERD we have Stu_id In Student, Uni_id in University, Clg_id in College, Course_id in Course,
Fac_name in Faculty.

35
Q14. Write a command Aggregate Functions?
SQL aggregation function is used to perform the calculations on multiplerows of a single column of a table. It
returns a single value.

o It is also used to summarize the data.


Q. Write a command Aggregate Functions?
SQL aggregation function is used to perform the calculations on multiplerows of a single column of a table. It
returns a single value.

 It is also used to summarize the data.

 Types of SQL Aggregation Function

1. COUNT FUNCTION

o COUNT function is used to Count the number of rows in a databasetable. It can work on both numeric and
non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all therows in a specified table. COUNT(*)
considers duplicate and Null.

 Syntax

COUNT(*) or COUNT( [ALL|DISTINCT] expression )

2. SUM Function

Sum function is used to calculate the sum of all selected columns. Itworks on numeric fields only.

 Syntax

SUM() or SUM( [ALL|DISTINCT] expression )

36
3. AVG function

The AVG function is used to calculate the average value of the numerictype. AVG function returns the average
of all non-Null values.

Syntax

AVG() or AVG( [ALL|DISTINCT] expression )

4. MAX Function

MAX function is used to find the maximum value of a certain column. Thisfunction determines the largest value of all
selected values of a column.

 Syntax

MAX() or MAX( [ALL|DISTINCT] expression )

5. MIN Function

MIN function is used to find the minimum value of a certain column. Thisfunction determines the smallest value of all
selected values of a column.
 SyntaxIN() or MIN( [ALL|DISTINCT] expression )

37
38

You might also like