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

Database Essentials For Data Science Lab

The document discusses database modeling using entity-relationship diagrams and describes the process of converting an ER diagram to tables in a relational database using SQL. It includes the following steps: 1. Identifying entities, attributes, and relationships from a scenario and representing them in an ER diagram. 2. Converting the ER diagram to relational tables by representing entities as tables, attributes as columns, and relationships as primary and foreign keys. 3. Using SQL commands like CREATE TABLE, ALTER TABLE, DROP TABLE, INSERT, SELECT, UPDATE, DELETE to create tables from the ER model and practice data manipulation. 4. Writing SQL queries using aggregation functions like COUNT, SUM, AVG, MAX,

Uploaded by

patricknamdev
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Database Essentials For Data Science Lab

The document discusses database modeling using entity-relationship diagrams and describes the process of converting an ER diagram to tables in a relational database using SQL. It includes the following steps: 1. Identifying entities, attributes, and relationships from a scenario and representing them in an ER diagram. 2. Converting the ER diagram to relational tables by representing entities as tables, attributes as columns, and relationships as primary and foreign keys. 3. Using SQL commands like CREATE TABLE, ALTER TABLE, DROP TABLE, INSERT, SELECT, UPDATE, DELETE to create tables from the ER model and practice data manipulation. 4. Writing SQL queries using aggregation functions like COUNT, SUM, AVG, MAX,

Uploaded by

patricknamdev
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Database Essentials for Data Science Lab

1. Model any given scenario into ER Model using any tool ERD Plus, ER Win,
Oracle SQL developer

In a university, a Student enrolls in Courses. A student must be


assigned to at least one or more Courses. Each course is taught
by a single Professor. To maintain instruction quality, a
Professor can deliver only one course

Step 1) Entity Identification


We have three entities

 Student
 Course
 Professor

Step 2) Relationship Identification


We have the following two relationships

 The student is assigned a course


 Professor delivers a course
Step 3) Cardinality Identification
For them problem statement we know that,

 A student can be assigned multiple courses


 A Professor can deliver only one course

Step 4) Identify Attributes


You need to study the files, forms, reports, data currently
maintained by the organization to identify attributes. You can also
conduct interviews with various stakeholders to identify entities.
Initially, it’s important to identify the attributes without mapping
them to a particular entity.

Once, you have a list of Attributes, you need to map them to the
identified entities. Ensure an attribute is to be paired with exactly
one entity. If you think an attribute should belong to more than
one entity, use a modifier to make it unique.

Once the mapping is done, identify the primary Keys. If a unique


key is not readily available, create one.

Entity Primary Key Attribute


Student Student_ID StudentName
Professor Employee_ID ProfessorName
Entity Primary Key Attribute
Course Course_ID CourseName
2. Converting ER Model to Relational Model (Represent entities and
relationships in Tabular form, represent attributes as columns, identifying
keys) Note: Student is required to submit a document showing the database
tables created from ER Model.
3. Creation of Tables using SQL- Overview of using SQL tool, Data types in
SQL, Creating Tables (along with Primary and Foreign keys), Altering
Tables and Dropping Tables, and Practicing DML commands- Insert, Select,
Update, Delete

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 creating tables adding data to tables
dropping the tables modifying the tables and setting permission for users.

Types of SQL Commands


There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1. Data Definition Language (DDL)

 DDL changes the structure of the table like creating tables deleting tables
altering a tables etc.

 All the commands of DDL are auto-committed which means it permanently


saves all the changes in the database.

Here are some commands that come under DDL:

 CREATE
 ALTER
 DROP
 TRUNCATE
a. CREATE It is used to create a new table in the database.
Syntax:
CREATE TABLE TABLE_NAME
( COLUMN_NAME1 DATATYPES(size),
COLUMN_NAME2 DATATYPES(size),
--------------
COLUMN_NAMEN DATATYPES(size)
);
Example:
CREATE TABLE EMP
( EMPNo VARCHAR2(20), EName VARCHAR2(20), Job VARCHAR2(20),
DOB DATE );

The ‘DROP TABLE’ Statement


This statement is used to drop an existing table. When you use this statement, the
complete information present in the table will be lost.
Syntax
DROP TABLE TableName;
Example
DROP Table Emp;

ALTER Statement
This command is used to delete, modify or add constraints or columns in an
existing table.
The ‘ALTER TABLE’ Statement
This statement is used to add, delete, and modify columns in an existing table.
The ‘ALTER TABLE’ Statement with ADD/DROP COLUMN You can use the ALTER
TABLE statement with ADD/DROP Column command according to your need. If
you wish to add a column, then you will use the ADD command, and if you wish to
delete a column, then you will use the DROP COLUMN command.

Syntax
 ALTER TABLE TableName ADD ColumnName Datatype;
 ALTER TABLE TableName DROP COLUMN ColumnName;
Example
--ADD Column MobNo:
ALTER TABLE Emp ADD MobNo Number(10);
--DROP Column MobNo:
ALTER TABLE Emp DROP COLUMN MobNo ;
The ‘ALTER TABLE’ Statement with MODIFY This statement is used to change the
datatype of an existing column in a table.
Syntax
ALTER TABLE TableName modify ColumnName Datatype;
Example
-- Change the data type to job.
ALTER TABLE Emp modify JOB varchar(10);

TRUNCATE
This command is used to delete the information present in the table but does not
delete the table. So, once you use this command, your information will be lost,
but not the table.
Syntax: TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE EMPLOYEE;

2. Data Manipulation Language


o DML commands are used to modify the database. It is responsible for all forms
of changes in the database.
o The command of DML is not auto-committed which means it can't permanently
save all the changes in the database. They can be rollback.
Here are some commands that come under DML:
o INSERT
o UPDATE
o DELETE

INSERT:
The INSERT statement is an SQL query. It is used to insert data into the row of a
table.
Syntax: INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1,
value2, value3, .... valueN);
Or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
For example: INSERT INTO EMP(EName,Job) VALUES ("SCOTT", "MANAGER");

UPDATE:
This command is used to update or modify the value of a column in the table.
Syntax: UPDATE table_name SET column1= values column2= values columnN =
value WHERE CONDITION;

For example: UPDATE Emp SET Ename = 'SMITH' WHERE EmpNo = '1003';

DELETE:
It is used to remove one or more row from a table.

Syntax1:

DELETE FROM table_name;

Syntax1

DELETE FROM table_name WHERE condition;

Example1: Delete all rows from the emp table

DELETE FROM Emp;

Example2: Delete all rows from the emp table whose Ename is SCOTT

DELETE FROM EName WHERE EName="SCOTT";

3)Data Control Language


DCL commands are used to grant and take back authority from any database user.
Here are some commands that come under DCL:
o Grant
o Revoke

Grant: It is used to give user access privileges to a database.

Example
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;

Revoke: It is used to take back permissions from the user.

Example
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

4) Transaction Control Language:


TCL commands can only be used with DML commands like INSERT, DELETE, and UPDATE.

Here are some commands that come under TCL:


o COMMIT
o ROLLBACK
o SAVEPOINT

a)Commit: The commit command is used to save all the transactions to the database.
Syntax: COMMIT;
Example:
DELETE FROM CUSTOMERS WHERE AGE = 25;
COMMIT;

b) Rollback: The rollback command is used to undo transactions that have not already been saved to the
database.
Syntax: ROLLBACK;
Example:
DELETE FROM CUSTOMERS WHERE AGE = 25;
ROLLBACK;

c. SAVEPOINT:
It is used to roll the transaction back to a certain point without rolling back the entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
5.Data Query Language:
DQL is used to fetch the data from the database. SELECT This statement is used to select data
from a database and the data returned is stored in a result table, called the result set.
Syntax
SELECT Column1, Column2, ...ColumN FROM TableName;
--(*) is used to select all from the table
SELECT * FROM table_name;
Example:
Select * from employee;

4)Creating applications and Practice Queries using COUNT, SUM, AVG, MAX, MIN

table: orders
ord_no purch_amt ord_date customer_id salesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001

1)write a SQL query to calculate total purchase amount of all


orders. Return total purchase amount.
SELECT SUM (purch_amt) FROM orders;

Output:
sum
17541.18

2. Calculate average purchase amount of all orders. Return average


purchase amount.
SELECT AVG(purch_amt) AS average_purchase_amount
FROM orders

3. Count the number of unique salespeople. Return number of


salespeople.
SELECT COUNT(DISTINCT(salesman_id)) AS number_of_salespeople
FROM orders

4. Find the maximum purchase amount.


SELECT MAX(purch_amt)
FROM orders

5. Find the minimum purchase amount.


SELECT MIN(purch_amt)
FROM orders

6. Find the highest purchase amount ordered by each customer.


Return customer ID, maximum purchase amount.
SELECT customer_id, MAX(purch_amt) AS max_purch_amt
FROM orders
GROUP BY customer_id
ORDER BY customer_id

7. Find the highest purchase amount on ‘2012–08–17’ by each


salesperson. Return salesperson ID, purchase amount.
SELECT salesman_id, MAX(purch_amt)
FROM orders
WHERE ord_date = ‘2012–08–17’
GROUP BY salesman_id

8. Find the highest order (purchase) amount by each customer in a


particular order date. Filter the result by highest order (purchase)
amount above 2000.00. Return customer id, order date and maximum
purchase amount.
SELECT customer_id, ord_date, MAX(purch_amt)
FROM orders
GROUP BY customer_id, ord_date
HAVING MAX(purch_amt) > 2000
ORDER BY ord_date, customer_id

9. Find the maximum order (purchase) amount in the range 2000,


6000 (Begin and end values are included.) by combination of each
customer and order date. Return customer id, order date and
maximum purchase amount.
SELECT customer_id, ord_date, MAX(purch_amt)
FROM orders
GROUP BY customer_id, ord_date
HAVING MAX(purch_amt) BETWEEN 2000 AND 6000
ORDER BY ord_date, customer_id;

10. Find the maximum order (purchase) amount by the combination


of each customer and order date. Filter the rows for maximum order
(purchase) amount as either 2000, 3000, 5760, or 6000. Return
customer id, order date and maximum purchase amount.
SELECT customer_id, ord_date, MAX(purch_amt)
FROM orders
GROUP BY customer_id, ord_date
HAVING MAX(purch_amt) IN (2000, 3000, 5760, 6000)
ORDER BY ord_date, customer_id

11. Find the maximum order (purchase) amount by each customer.


The customer ID should be in the range 3002 and 3007(Begin and end
values are included.). Return customer id and maximum purchase
amount.
SELECT customer_id, MAX(purch_amt)
FROM orders
WHERE customer_id BETWEEN 3002 AND 3007
GROUP BY customer_id
ORDER BY customer_id

12. Find the maximum order (purchase) amount for each customer.
The customer ID should be in the range of 3002 and 3007(Begin and
end values are included.). Filter the rows for the maximum order
(purchase) amount is higher than 1000. Return customer id and
maximum purchase amount.
SELECT customer_id, MAX(purch_amt)
FROM orders
WHERE customer_id BETWEEN 3002 AND 3007
GROUP BY customer_id
HAVING MAX(purch_amt) > 1000
ORDER BY customer_id

13. Find the maximum order (purchase) amount generated by each


salesperson. Filter the rows for the salesperson ID is in the range 5003
and 5008 (Begin and end values are included.). Return the salesperson
id and maximum purchase amount.
SELECT salesman_id, MAX(purch_amt)
FROM orders
WHERE salesman_id BETWEEN 5003 AND 5008
GROUP BY salesman_id
ORDER BY salesman_id
14. Count all the orders generated on ‘2012–08–17’. Return the
number of orders.
SELECT COUNT(*)
FROM orders
WHERE ord_date = ‘2012–08–17’

You might also like