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

dbms

The document provides an overview of data management concepts, including definitions of data, information, fields, records, files, tables, databases, and database management systems (DBMS). It explains SQL commands for creating, updating, and querying databases, along with examples of how to use these commands effectively. Additionally, it covers primary and foreign keys, data types, and basic SQL functions such as COUNT, AVG, SUM, MIN, and MAX.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

dbms

The document provides an overview of data management concepts, including definitions of data, information, fields, records, files, tables, databases, and database management systems (DBMS). It explains SQL commands for creating, updating, and querying databases, along with examples of how to use these commands effectively. Additionally, it covers primary and foreign keys, data types, and basic SQL functions such as COUNT, AVG, SUM, MIN, and MAX.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

DataCollection of raw facts and figures

Information Processed data used for effective decision making.

FieldA field is a single piece of information

Record a record is one complete set of fields

File and a file is a collection of records

TableA table is a collection of related data in an organized manner in


the form of rows and columns. It is an organized arrangement of data and
information in tabular form containing rows and columns, making it
easier to understand and compare data.

Figure 1 A

Database database is an organised electronic filing system, collecting data


organized by fields, records, and files.
Dbms A database management system (DBMS) is a software system for
creating and managing databases. It is a medium of interface between users
and database. DBMS enables end users to create, protect, read, update and
delete data in a database.
Eg Types : MS Access, MS SQL SERVER, Oracle etc.
DBMS components data, database(backend), Hardware(Storage devives, systems
etc), Software(API,APP’s, etc), Network and users.
SQLStructured Query Language. It used to create, update, manipulate databases
through commands.

SQL Commands are mainly categorized into five categories:


 DDL – Data Definition Language
 DQL – Data Query Language
 DML – Data Manipulation Language
 DCL – Data Control Language
 TCL – Transaction Control Language

1. The CREATE TABLE statement is used to create a new table in a database.


For example: CREATE TABLE Employees
EmplyoeeID int,
EmployeeName varchar(255),
Address varchar(255),
Country varchar(255);
City varchar(255);
Department varchar(255);
EmpSal int;
Data Types
Each column in a table is associated with a specific data type that defines
the kind of data it can store. Some of the data types are int, varchar, char,
date, etc. Data types ensures that the data entered in the column belongs to
a particular format and structure to the stored information. For example, ID
and age might be integer, and Name and course could be strings.

primary key
It is the unique identifier for each row in a table. There can only be one
primary key in a table, and it can't be null. ID is the primary key for the
above table. Each student is uniquely identified by their ID. Now two rows
have the same ID. The first column is usually the primary key of the table.
(Refer Figure 1A)

Foreign Key
A foreign key is a column or a set of columns in a table that refers to the
primary key of another table. It establishes the relationship between the
table. A foreign key can be null and there can be more than one foreign key
in the table. For example, in the Department table you might have an emp_id
column as a foreign key. This allows you to link departments to specific
employees.
2. UPDATE statement
The UPDATE statement is used to modify the existing data in a table.
It is used to change the values of one or more columns in a table based on specified
conditions.
For example:

UPDATE Employees SET Email = '[email protected]' WHERE EmployeeID =


101;

3. ALTER statement
The ALTER statement is used to modify the structure of database objects, such as
tables, views, or schemas.
It can be used to add, modify, or drop columns, constraints, or indexes in a table.
For example:
ALTER TABLE Employees ADD Email VARCHAR(255);

4. SELECT statement
The select command is used to select data from a database.
Eg:
SELECT EmployeeName, EmployeeID, City FROM Employees;
SELECT * FROM Employees;
5. FROM clause
The FROM command is used to specify which table to select or delete data from.
Eg: SELECT EmployeeName, City FROM Employees;

6. The WHERE clause is used to filter records. It is used to extract only those
records that fulfill a specified condition.

Eg:
SELECT * FROM Employees WHERE City = 'delhi';
SELECT EmployeeName, EmployeeID FROM Employees WHERE Empsal >
= 55000;

The WHERE clause can be combined with AND, OR, and NOT operators.

7. The AND and OR operators are used to filter records based on more than
one condition:

 The AND operator displays a record if all the conditions separated


by AND are TRUE.
 The OR operator displays a record if any of the conditions separated
by OR is TRUE.

SELECT * FROM Employees WHERE Country = 'India' AND City = 'Bangalore';


SELECT * FROM Employees WHERE Country = 'Germany' OR Country = 'Spain';

8. DELETE statement
The DELETE statement is used to remove one or more rows from a table based on
specified conditions.
It deletes specific rows of data while keeping the table structure intact.

For example, DELETE FROM Employees WHERE Department = 'Marketing';

Q1: Consider the following MOVIE table and write the SQL queries based on it.

MovieID MovieName Category ReleaseDate ProductionCost BusinessCost

001 Hindi_Movie Musical 2018-04-23 124500 130000

002 Tamil_Movie Action 2016-05-17 112000 118000

003 English_Movie Horror 2017-08-06 245000 360000

004 Bengali_Movie Adventure 2017-01-04 72000 100000

005 Telugu_Movie Action NULL 100000 NULL

006 Punjabi_Movie Comedy NULL 30500 NULL

(a) Display all the information from the Movie table.

(b) List business done by the movies showing only MovieID, MovieName and Total_Earning.
Total_Earning to be calculated as the sum of ProductionCost and BusinessCost.

(c) List the different categories of movies.

(d) Find the net profit of each movie showing its MovieID, MovieName and NetProfit. Net
Profit is to be calculated as the difference between Business Cost and Production Cost.

(e) List MovieID, MovieName and Cost for all movies with ProductionCost greater than
10,000 and less than 1,00,000.

(f) List details of all movies which fall in the category of comedy or action.

(g) List details of all movies which have not been released yet.

Answers:

a. SELECT * FROM Movie;


b. SELECT MovieID, MovieName, (ProductionCost + BusinessCost) AS Total_Earning
FROM Movie WHERE ReleaseDate IS NOT NULL;

MovieID MovieName Total_Earning


1 Hindi_Movie 254500
2 Tamil_Movie 230000
3 English_Movie 605000
4 Bengali_Movie 172000

c. SELECT DISTINCT Category FROM MOVIE;

d. SELECT MovieID, MovieName, BusinessCost - ProductionCost AS NetProfit


FROM Movie WHERE ReleaseDate IS NOT NULL;

e. SELECT MovieID, MovieName, ProductionCost AS Cost


FROM MOVIE WHERE ProductionCost > 10000 AND ProductionCost < 100000;

f. SELECT * FROM MOVIE WHERE Category = 'Comedy' OR Category = 'Action';

g. SELECT * FROM MOVIE WHERE ReleaseDate IS NULL;


Question 2

Using the sports database containing table (MATCH_DETAILS) and write


the queries for the following:

(a) Display the MatchID of all those matches where both the teams have
scored more than 70.
Answer:
SELECT MatchID FROM MATCH_DETAILS WHERE FirstTeamScore >
70 AND SecondTeamScore > 70;
(b) Display the MatchID of all those matches where FirstTeam has scored
less than 70 but SecondTeam has scored more than 70.
Answer:

SELECT MatchID FROM MATCH_DETAILS WHERE FirstTeamScore < 70


AND SecondTeamScore > 70;

(c) Display the MatchID and date of matches played by Team 1 and won
by it.
Answer:
SELECT MatchID, MatchDate FROM MATCH_DETAILS WHERE FirstTeamID = 1 AND
FirstTeamScore > SecondTeamScore;

(d) Display the MatchID of matches played by Team 2 and not won by it.
Answer:
SELECT MatchID FROM MATCH_DETAILS WHERE SecondTeamID = 2 AND SecondTeamScore <= FirstTeamScore;
Question 3

Consider the following table named "Product", showing details of


products being sold in a grocery shop.

a. Identify the primary key in Product.


b. Add a new column Discount to the table Product.
c. Calculate the value of the discount in the table Product as 10 per
cent of the UPrice for all those products where the UPrice is more
than 100, otherwise the discount will be 0.
d. Increase the price by 12 per cent for all the products manufactured
by Dove.
e. Display the total number of products manufactured by each
manufacturer.
Answers :
b. ALTER TABLE Product ADD COLUMN Discount float;
c. UPDATE Product SET Discount = IF(UPrice > 100, (UPrice * (10/100)) + UPrice,
0);
d. UPDATE Product SET UPrice = (UPrice * (12/100)) + UPrice WHERE
Manufacturer = 'Dove';
e. SELECT Manufacturer, COUNT(*) AS TotalProducts FROM Product
f. SELECT PName, avg(UPrice) FROM Product
g. SELECT PName, MAX(UPrice), MIN(UPrice) FROM Product
h. SELECT COUNT(*) FROM Product WHERE Manufacutrer = ‘Dove’;

Basic Functions: Avg, Count, Max, Min, Sum in SQL


COUNT()
The COUNT() function returns the number of rows that matches a specified
criterion.
Syntax:
SELECT COUNT(column_name) FROM table_name WHERE condition;

AVG()
The AVG() function returns the average value of a numeric column.
Syntax:
SELECT AVG(column_name) FROM table_name WHERE condition;

SUM()
The SUM() function returns the total sum of a numeric column.
Syntax:
SELECT SUM(column_name) FROM table_name WHERE condition;
The SQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
Example

COUNT() Example

The following SQL statement finds the number of products:

SELECT COUNT(ProductID) FROM Products;


AVG() Example
The following SQL statement finds the average price of all products:
SELECT AVG(Price) FROM Products;
SUM() Example
The following SQL statement finds the sum of the "Price" fields in the "products"
table:
SELECT SUM(Price) FROM Products;

MIN() Example

To Find the lowest price in the Price column:

SELECT MIN(Price) FROM Products;


MAX() Example
To Find the highest price in the Price column:
SELECT MAX(Price) FROM Products;

You might also like