0% found this document useful (0 votes)
18 views12 pages

Database II Practical 2021-2022

The document outlines practical exercises related to database design, focusing on DDL and DML commands to create and manipulate tables in SQL. It covers various SQL JOIN types, functions, and how to create and manage database objects. Additionally, it provides specific SQL queries for data extraction and manipulation tasks involving employee and department records.

Uploaded by

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

Database II Practical 2021-2022

The document outlines practical exercises related to database design, focusing on DDL and DML commands to create and manipulate tables in SQL. It covers various SQL JOIN types, functions, and how to create and manage database objects. Additionally, it provides specific SQL queries for data extraction and manipulation tasks involving employee and department records.

Uploaded by

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

DATABASE DESIGN II(COM 322) PRACTICAL

PRACTICAL ONE
TOPIC: DDL and DML
AIM: To recall the DDL and DML used in Database designI to
create the tables (Dept and Emp)
Here are the SQL codes for the DDL for creating the tables:

CREATE TABLE Dept


(
Deptno INTEGER NOT NULL ,
Dname VARCHAR (15) ,
School VARCHAR (15)
) ;
ALTER TABLE Dept ADD CONSTRAINT Dept_PK PRIMARY KEY ( Deptno )
;

CREATE TABLE Emp


(
Empno INTEGER NOT NULL ,
Ename VARCHAR (15) ,
DOB DATE ,
Designation VARCHAR (15) ,
Sal INTEGER ,
AcademicAll INTEGER ,
Dept_Deptno INTEGER NOT NULL
) ;
ALTER TABLE Emp ADD CONSTRAINT Emp_PK PRIMARY KEY ( Empno ) ;

ALTER TABLE Emp ADD CONSTRAINT Emp_Dept_FK FOREIGN KEY


( Dept_Deptno ) REFERENCES Dept ( Deptno ) ;

After the table created, use the DML syntax:


INSERT
INSERT INTO tablename VALUES (value1, value2, ..., valuen.
To insert all the records in the tables(Dept and Emp)
PRACTICAL TWO
TOPIC: SQL Join
AIM: To understand SQL Join and the types
A JOIN clause is used to combine rows from two or more tables,
based on a related column between them.
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
SQL INNER JOIN Keyword
The INNER JOIN keyword selects records that have matching
values in both tables.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Note: The INNER JOIN keyword selects all rows from both tables
as long as there is a match between the columns. If there are
records in the "Orders" table that do not have matches in
"Customers", these orders will not be shown!

SQL LEFT JOIN Keyword


The LEFT JOIN keyword returns all records from the left table
(table1), and the matching records from the right table
(table2). The result is 0 records from the right side, if
there is no match.
LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Note: In some databases LEFT JOIN is called LEFT OUTER JOIN.
SQL RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all records from the right
table (table2), and the matching records from the left table
(table1). The result is 0 records from the left side, if there
is no match.
RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Note: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

SQL FULL OUTER JOIN Keyword


The FULL OUTER JOIN keyword returns all records when there is
a match in left (table1) or right (table2) table records.
Tip: FULL OUTER JOIN and FULL JOIN are the same.
FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Note: FULL OUTER JOIN can potentially return very large
result-sets!
PRACTICAL THREE
TOPIC: SQL Join
Aim: To know how to create join in MYSQL to extract data from
more than one table at a time

1. Find the name and salary of employees in Business

SELECT ENAME, SAL, SCHOOL

FROM dept, emp

WHERE DEPT.DEPTNO= EMP.DEPT_DEPTNO AND SCHOOL = "ENGINEERING";

2. Join the dept table to the emp table and show in department
number order

SELECT DEPTNO, DNAME, DESIGNATION, ENAME

FROM dept, emp

where dept.deptno = emp.Dept_Deptno

order by deptno;
PRACTICAL FOUR
TOPIC: SQL Join
Aim: To know how to create join in MYSQL to extract data from
more than one table at a time

1. List all departments that have employees, plus those


departments that do not have employees

SELECT DEPTNO, DNAME, DESIGNATION, ENAME


FROM dept
left join emp
ON dept.deptno = emp.Dept_Deptno
order by deptno;

2. List all departments that do not have employees

SELECT DEPTNO, DNAME, DESIGNATION, ENAME


FROM dept
left join emp
ON dept.deptno = emp.Dept_Deptno
where (designation AND ename) is NULL
order by deptno;
PRACTICAL FIVE

TOPIC: FUNCTION

Aim: To Know Mysql Function (String and Arithmetic)

Function Description
ASCII Returns the ASCII value for the specific
character
CHAR_LENGTH Returns the length of a string (in
characters)
CHARACTER_LENGTH Returns the length of a string (in
characters)
CONCAT Adds two or more expressions together
INSERT Inserts a string within a string at the
specified position and for a certain
number of characters
LCASE Converts a string to lower-case
LEFT Extracts a number of characters from a
string (starting from left)
LENGTH Returns the length of a string (in bytes)
LOCATE Returns the position of the first
occurrence of a substring in a string
LOWER Converts a string to lower-case
RIGHT Extracts a number of characters from a
string (starting from right)
UCASE Converts a string to upper-case
UPPER Converts a string to upper-case
AVG Computes the average value (disregards any
NULLs)

COUNT Counts the number of values (disregards


any NULLs)
SUM Gives a sum total (of a column or column
expression)

MIN Presents the minimum value

MAX Presents the maximum value

PRACTICAL SIX

TOPIC: FUNCTION

Aim: To Know how to create functions in MySQL

1. Find out how many LECTURERS there are in the designation


column without listing them.

SELECT COUNT(designation) AS Lecturer FROM emp WHERE


designation = "LECTURER"

2. Find the number of different designation in the employee


table

SELECT COUNT(DISTINCT(designation)) AS Designation_Types

FROM emp;
PRACTICAL SEVEN

TOPIC: FUNCTION

Aim: To Know how to create functions in MySQL

1. Compute the average annual salary plus commission for all


lecturer

SELECT AVG(sal + AcademicAll) * 12 AS Compensation

FROM emp

WHERE designation = "LECTURER";

2. Find the highest and lowest paid employee and the difference
between them.

SELECT MAX(sal) AS Rich, MIN(sal) AS Poor, MAX(sal) - MIN(sal) AS


Difference

FROM emp;

3. Find the number of characters in the longest department name.

SELECT MAX(LENGTH(dname)) AS Longest

FROM dept;
PRACTICAL EIGHT

TOPIC: CREATING OBJECTS AND MANIPULATING DATA

Aim: To Know how to Create Object and Manipulating Data

1. Create a new table called loans with columns as follows

ln_no - numeric of width 3 with zero decimal


places

empno - numeric of width 4 with zero decimal


places

type - variable length character limited to one


character

amount - numeric of width 8 with two decimal


places

CREATE TABLE loans (


ln_no NUMERIC(3),

empno NUMERIC(4),

type VARCHAR(1),

amount NUMERIC(8,2))
2. Insert the following data

ln_no empno type amount

----- ----- --------- --------

23 7499 M 20000.00

42 7499 C 2000.00

65 7844 M 3564.20

INSERT INTO loans VALUES(23,7499,”M”,20000.00);


INSERT INTO loans VALUES(42,7499,”C”,2000.00);
INSERT INTO loans VALUES(65,7844,”M”,3564.20);

3. Check that you have created three new records in loans

SELECT * FROM loans;

4. Add a new column called balance to the loans table. The

balance column should be of the same datatype as the amount

column.

ALTER TABLE loans ADD balance NUMERIC(8,2);

5. Set the balance for each loan to be equal to the amount of

the loan.

UPDATE loans SET balance = amount;

6. Add 10% interest to all ‘M’ type loans

UPDATE loans
SET balance = balance*1.1
WHERE type = ‘M’;

7. Remove all loans less than $3000


DELETE FROM loans WHERE amount < 3000;

8. Change the name of the loans table to accounts.

ALTER TABLE loans RENAME TO accounts;

You might also like