Database Essentials For Data Science Lab
Database Essentials For Data Science Lab
1. Model any given scenario into ER Model using any tool ERD Plus, ER Win,
Oracle SQL developer
Student
Course
Professor
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.
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.
DDL changes the structure of the table like creating tables deleting tables
altering a tables etc.
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 );
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;
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:
Syntax1
Example2: Delete all rows from the emp table whose Ename is SCOTT
Example
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
Example
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
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
Output:
sum
17541.18
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