ISM Project
ISM Project
Batch 2020-2023
1
Fax: +91 11 2622 0998
E-mail: [email protected]
Website: www.ipu.jimskalkaji.com
ACKNOWLEDGEMENT
Dhananjay Sharma
(01114188820)
2
Content
S NO. TOPICS Pg.no.
1 What is DBMS? Explain its features 5
3
14 Write a query to display the dept_no in descending 30-31
order
15 Write a query to count the number of employees 32
belonging to dept_id 12 the table EMP using a single
query
16 Delete the record with E_id=6,7,8 using a single query. 33
23 Show the entry for Null Entry for Customer account 37-38
number in table bank_detail
24 Define the functions with examples 38-53
4
DBMS
Database Management System (DBMS) is software for storing and retrieving users’ data while
considering appropriate security measures. It consists of a group of programs that manipulate
the database. The DBMS accepts the request for data from an application and instructs the
operating system to provide the specific data. In large systems, a DBMS helps users and other
third-party software store and retrieve data.
DBMS allows users to create their own databases as per their requirements. The term “DBMS”
includes the user of the database and other application programs. It provides an interface
between the data and the software application.
Let us see a simple example of a university database. This database is maintaining information
concerning students, courses, and grades in a university environment. The database is
organized as five files:
To define DBMS:
We need to specify the structure of the records of each file by defining the different types of
data elements to be stored in each record.
We can also use a coding scheme to represent the values of a data item.
5
Basically, your Database will have 5 tables with a foreign key defined amongst the various
tables.
MySQL
Microsoft Access
Oracle
PostgreSQL
dBase
FoxPro
SQLite
IBM DB2
LibreOffice Base
MariaDB
Microsoft SQL Server
Characteristics of DBMS
6
Keys
It is used to uniquely identify any record or row of data from the table. It is also used to
establish and identify relationships between tables.
For example, ID is used as a key in the student table because it is unique for each student. In
the PERSON table, passport number, license number, SSN are keys since they are unique for
each person.
Types of keys:
1. Primary key
It is the first key used to identify one and only one instance of an entity uniquely. An entity can
contain multiple keys, as we saw in the PERSON table. The key which is most suitable from
those lists becomes a primary key.
In the EMPLOYEE table, ID can be the primary key since it is unique for each employee. In the
EMPLOYEE table, we can even select License Number and Passport Number as primary keys
since they are also unique.
7
For each entity, the primary key selection is based on requirements and developers.
2. Candidate key
A candidate key is an attribute or set of attributes that can uniquely identify a tuple.
Except for the primary key, the remaining attributes are considered a candidate key. The
candidate keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of the
attributes, like SSN, passport Number, license Number, etc., are considered a candidate key.
3. Super Key
8
Super key is an attribute set that can uniquely identify a tuple. A super key is a superset of a
candidate key.
4. Foreign key
Foreign keys are the column of the table used to point to the primary key of another
table.
We add the primary key of the DEPARTMENT table, Department_Id, as a new attribute
in the EMPLOYEE table.
9
In the EMPLOYEE table, Department_Id is the foreign key, and both the tables are
related.
5. Alternate key
There may be one or more attributes or a combination of attributes that uniquely identify
each tuple in a relation. These attributes or combinations of the attributes are called the
candidate keys. One key is chosen as the primary key from these candidate keys, and
the remaining candidate key, if it exists, is termed the alternate key. In other words, the
total number of the alternate keys is the total number of candidate keys minus the
primary key. The alternate key may or may not exist. If there is only one candidate key
in a relation, it does not have an alternate key.
For example, employee relation has two attributes, Employee_Id and PAN_No, that act
as candidate keys. In this relation, Employee_Id is chosen as the primary key, so the
other candidate key, PAN_No, acts as the Alternate key.
10
6. Composite key
Whenever a primary key consists of more than one attribute, it is known as a composite
key. This key is also known as Concatenated Key.
11
7. Artificial key
The key created using arbitrarily assigned data are known as artificial keys. These keys are
created when a primary key is large and complex and has no relationship with many other
relations. The data values of the artificial keys are usually numbered in a serial order.
For example, the primary key, which is composed of Emp_ID, Emp_role, and Proj_ID, is large
in employee relations. So, it would be better to add a new virtual attribute to identify each tuple
in the relation uniquely.
12
SQL
SQL is the standard language for dealing with Relational Databases. SQL can be used to
insert, search, update, and delete database records. SQL can do lots of other operations,
including optimizing and maintenance of databases.
13
SQL works with an efficient speed. This high speed can boost the amount of data retrieval. It
can give users a rapid and effective means to obtain, alter, or store data.
SQL can quickly and efficiently retrieve a large volume of data records from a database.
Compared to an unstructured database such as MongoDB, it is a relational database that can
characterize the data in a structured way. Operations like insertion, deletion, querying,
manipulation, and calculations on data via analytical queries in a relational database can be
accomplished in a matter of seconds.
There is no need for large and complex code lines for data extraction. One reason for using
SQL is that it does not require extensive coding and program writing knowledge. It is easy to
maintain database systems without having to write a good amount of code. There is no need
for large complex lines of code for data extraction.
SQL is mostly made up of English statements, making it simple to learn and write SQL queries.
All primary keywords, like SELECT, INSERT INTO, DELETE, UPDATE, etc., can be used to
carry out operations. Also, the syntactical rules are simple and easy to understand, making it a
user-friendly language.
3. Portable
Portability is an extremely desirable feature in any program that stores data in a database. A
software developer would like to use a small in-memory database, such as Apache Derby,
while creating an application.
SQL is highly portable because it is employed in programs on PCs, servers, tablets, and
independent laptops running operating systems such as Windows, Linux, Mac, and even some
mobile phones. It can also be embedded with other programs based on the requirements.
It can be used on local systems (internet and the intranet). SQL databases can be easily
transferred from one device to another.
4. Standardized Language
14
SQL is a relational database query language that has been accepted by ISO and ANSI. It
gives all users a consistent platform worldwide due to proper documentation and years of
establishment.
In 19861986 and 19871987, ANSI (American National Standards Institute) and ISO
(International Standards Organization) standardized the language. The most recent standard
part was produced in 20112011.
5. Interactive Language
SQL is a domain language that is simple to learn and easy to understand. It can also be used
to interface with databases and receive responses to complicated queries in seconds.
It becomes an interactive language for its users because it offers easy commands for all
purposes. SQL commands are also understandable to non-programmers.
SQL is used to build and administer large databases, including data sharing, updating, and
retrieval from numerous tables. This is the primary reason organizations employ SQL in
various applications such as web development, data analysis, IT support, etc.
The SQL language gives each user a unique view of the data. A view is a SQL statement that
includes specified SQL queries that are stored in the database. It can be built from one or
more tables based on the queries written or the aim that the view satisfies.
SQL can provide distinct views of the database's structure and content to different users.
7. Internet Usage
Three-tier Internet architectures are compatible with SQL architecture. Client, application
server, and database components make up the architecture.
15
The SQL programming language is nearly 50 years old and is incredibly mature and still
extensively used. It boasts a vibrant network of specialists eager to provide advice and well-
established best practices.
There are several opportunities to improve one's skills and collaborate. Consultants and SQL
suppliers can provide extra assistance if needed. Your developers will be able to find the
answers they require using SQL.
MySQL
MySQL is an open-source relational database management system. As with other relational
databases, MySQL stores data in tables made up of rows and columns. Users can define,
manipulate, control, and query data using Structured Query Language, more commonly known
as SQL. MySQL’s name is a combination of “My,” the name of MySQL creator Michael
Widenius’s daughter, and “SQL”.
A flexible and powerful program, MySQL is the most popular open-source database system in
the world. As part of the widely-used LAMP technology stack (which consists of a Linux-based
operating system, the Apache web server, a MySQL database, and PHP for processing), it’s
used to store and retrieve data in a wide variety of popular applications, websites, and
services.
SQL Commands
SQL commands are instructions. It is used to communicate with the database. It is also used
to perform specific tasks, functions, and queries of data.
SQL can perform various tasks like create a table, add data to tables, drop the table, modify
the table, set permission for users.
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
16
1. Data Definition Language (DDL)
DDL changes the structure of the table like creating a table, deleting a table, altering a table,
etc.
All the command of DDL is auto-committed that means it permanently save all the changes in
the database.
CREATE
ALTER
DROP
TRUNCATE
17
CREATE
DROP
It is used to delete both the structure and record stored in the table
Syntax;
Example;
ALTER
It is used to alter the structure of the database. This change could be either to modify the
characteristics of an existing attribute or probably to add a new attribute.
18
ALTER TABLE table_name ADD column_name COLUMN-definition;
EXAMPLE
TRUNCATE
It is used to delete all the rows from the table and free the space containing the table.
Syntax:
Example:
DML commands are used to modify the database. It is responsible for all form of changes in
the database.
The command of DML is not auto-committed that means it can't permanently save all the
changes in the database. They can be rollback.
INSERT
UPDATE
DELETE
INSERT
The INSERT statement is a SQL query. It is used to insert data into the row of a table.
19
Syntax:
Or
For example:
UPDATE
This command is used to update or modify the value of a column in the table.
Syntax:
For example:
UPDATE students
DELETE
Syntax:
WHERE Author="Sonoo";
DCL commands are used to grant and take back authority from any database user.
Grant
Revoke
Grant
Example
Revoke
Example
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only.
21
These operations are automatically committed in the database that's why they cannot be used
while creating tables or dropping them.
COMMIT
ROLLBACK
SAVEPOINT
Commit
Syntax:
COMMIT;
Example:
COMMIT;
Rollback
Rollback command is used to undo transactions that have not already been saved to the
database.
Syntax:
ROLLBACK;
Example:
22
ROLLBACK;
SAVEPOINT
It is used to roll the transaction back to a certain point without rolling back the entire
transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
SELECT
This is the same as the projection operation of relational algebra. It is used to select the
attribute based on the condition described by WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
For example:
SELECT emp_name
FROM employee
23
MYSQL sever through XAMPP
The XAMPP Stack of Software is an Open-source Localhost-based server encompassing
software packages that offer several functionalities. You can use the XAMPP Control Panel to
start or deactivate all Software that comes with it.
With XAMPP in place, you can test projects/modifications before releasing them to the
Internet. The MySQL Database forms a vital component of the XAMPP Stack, being one of the
most significant features offered by it. With XAMPP, you can use phpMyAdmin to create & use
your XAMPP MySQL Database in minutes.
Go to your system’s XAMPP folder or simply click the XAMPP Icon to open it. The Control
Panel is now visible, and you may use it to start or stop any module.
Select the “Start” option for the Apache and MySQL modules, respectively. The user will see
the following screen once it has started working:
24
Step 3: Listing commands in Command prompt for establishing connection:
25
Create table EMP in SQL with 5 columns
26
E_name in descending order
28
Write a query to find the maximum salary
29
Writing a query to find the name of the
employee having maximum salary
30
31
Write a query to count the number of
employees belonging to dept_id 12 the
table EMP using a single query
32
Deleting the record with Emp_id = 6, 7, 8
using a single query
34
Delete the table EMP
35
Create a table for Bank detail Bank
Customers with Primary Key as their
account number
36
Insert the database for 10 customers in
bank_detail
37
Define the functions with examples
Structured Query Language aka SQL is used to handle data in databases. It provides various
in-built functions and commands to access and manage databases according to our
requirements.
Aggregate Functions
Scalar Functions
The Aggregate Functions in SQL perform calculations on a group of values and then return a
single value. Following is few of the most commonly used Aggregate Functions:
38
Function Description
Let us look into each one of the above functions in depth. For your better understanding, I will
be considering the following table to explain to you all the examples.
SUM()
Syntax:
1. SELECT SUM(ColumnName)
39
2. FROM TableName;
Example:
Write a query to retrieve the sum of marks of all students from the students’ table.
1. SELECT SUM(Marks)
2. FROM Students;
Output:
`359
COUNT()
Returns the number of rows present in the table either based on some condition or without any
condition.
Syntax:
40
1. SELECT COUNT(ColumnName)
2. FROM TableName
3. WHERE Condition;
Example:
Write a query to count the number of students scoring marks > 75 from the students’ table.
1. SELECT COUNT(StudentID)
2. FROM Students
3. WHERE Marks >75;
Output:
2
AVG()
Syntax:
1. SELECT AVG(ColumnName)
2. FROM TableName;
Example:
Write a query to calculate the average marks of all students from the Students table.
41
1. SELECT AVG(Marks)
2. FROM Students;
Output:
71.8
MIN()
Syntax:
1. SELECT MIN(ColumnName)
2. FROM TableName;
Example:
Write a query to retrieve the minimum marks out of all students from the Students table.
1. SELECT MIN(Marks)
2. FROM Students;
Output:
45
42
MAX()
Syntax:
1. SELECT MAX(ColumnName)
2. FROM TableName;
Example:
Write a query to retrieve the maximum marks out of all students from the students’ table.
1. SELECT MAX(Marks)
2. FROM Students;
Output:
92
43
FIRST()
This function returns the first value of the column which you choose.
Syntax:
1. SELECT FIRST(ColumnName)
2. FROM TableName;
Example:
1. SELECT FIRST(Marks)
2. FROM Students;
Output: 64
LAST()
Used to return the last value of the column which you choose.
Syntax:
1. SELECT LAST(ColumnName)
2. FROM TableName;
Example:
1. SELECT LAST(Marks)
2. FROM Students;
Output: 92
44
Scalar SQL Functions
The Scalar Functions in SQL are used to return a single value from the given input value.
Following is few of the most commonly used Aggregate Functions:
Function Description
LCASE() Used to convert string column values to
lowercase
UCASE() This function is used to convert a string column
values to Uppercase.
LEN() Returns the length of the text values in the
column.
MID() Extracts substrings in SQL from column values
having String data type.
ROUND() Rounds off a numeric value to the nearest
integer.
NOW() This function is used to return the current system
date and time.
FORMAT() Used to format how a field must be displayed.
LCASE()
Syntax:
1. SELECT LCASE(ColumnName)
2. FROM TableName;
45
Example:
1. SELECT LCASE(StudentName)
2. FROM Students;
Output:
Sanjay
Varun
Akash
Rohit
Anjali
UCASE()
Syntax:
1. SELECT UCASE(ColumnName)
2. FROM TableName;
46
Example:
1. SELECT UCASE(StudentName)
2. FROM Students;
Output:
SANJAY
VARUN
AKASH
ROHIT
ANJALI
LEN()
Syntax:
Example:
47
Write a query to extract the length of the student name “Sanjay”.
Output:
6
MID()
This function is used to extract substrings from columns having string data type.
Syntax:
48
Example:
1. SELECT MID(StudentName, 2, 3)
2. FROM Students;
Output:
anj
aru
kas
ohi
nja
ROUND()
This function is used to round off a numeric value to the nearest integer.
49
Syntax:
Example:
For this example, let us consider the following Marks table in the Students table.
1. SELECT ROUND(Marks)
2. FROM Students;
Output:
91
80
54
73
68
50
NOW()
Used to return the current date and time. The date and time are returned in the “YYYY-MM-DD
HH-MM-SS” format.
Syntax:
SELECT NOW();
Example:
SELECT NOW();
Output:
NOW()
51
2022-12-25 11:20:38
FORMAT()
Syntax:
FORMAT(InputValue, Format)
Example:
Output:
52
123-456-789
53