You are on page 1of 20

H

SUSHILA DEVI BANSAL COLLEGE OF


TECHNOLOGY

BRANCH : - INFORMATION
TECHNOLOGY
SUBJECT : - DBMS
SEMESTER : - 4th

SUBMITTED TO SUBMITTED BY
ANURADHA
Ma’am

1
H

INDEX

S.No Experiment Name Pg. No Remarks

1. To perform various SQL commands of DDL , DML and


DCL

2.
Write SQL command Such as insertion , deletion,
updation for any schema…

3. To Execute Nested Queries, Join Queries , Order by ,


Having clause , and string

4. To perform Set operator like Union , Intersect, Minus


on a set of tables .

5. To Execute various Commands for GROUP function


(avg, count, max, min , sum)

6. Write a PL/SQL block for transaction application


using triggers …

7. Write a DBMS program to prepare report for an


application using function ..

8. Designing of various Input screens / forms .

9. Create reports using database connectivity of Front


end with back end.

10. create database design with normalization and


implementing in any applications ..

2
H

Q1 } To perform various SQL commands of DDL , DML and DCL


1 . List of DDL commands: create, drop, truncate, alter, comment , rename
Create
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

DROP
Syntax
DROP TABLE table_name;

Drop Table Persons ;


This command will delete the persons Table .
Truncate

Syntax
TRUNCATE TABLE table_name;
Truncate Table Persons ;

ALTER
Syntax

ALTER TABLE table_name


ADD column_name datatype;

ALTER TABLE Student


ADD Semester varchar(255);

3
H

Comments

Single line comments

--Select all:
SELECT * FROM Customers;

Multi line comments

/*Select all the columns


of all the records
in the Customers table:*/
SELECT * FROM Customers;

RENAME

ALTER TABLE Student RENAME COLUMN NAME TO FIRST_NAME;

2 . List of DML commands: Select , insert , delete , update .


SELECT

SELECT * FROM Student;


OUTPUT

INSERT
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
4
H

INSERT INTO Student(StudID , NAme , Semester , Dept)


VALUES ('100', 'Hemant ' , '4th' , 'IT');

DELETE
DELETE FROM table_name WHERE condition;

DELETE FROM Student WHERE StudID = '101';

UPDATE
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

UPDATE Student
SET Name = 'Pankaj'
WHERE StudID = '101';

3 . List of DCL commands:


GRANT
Syntax

Grant privilageName
on objectName
To{userName/Public/roleName}
[with Grant Option]
REVOKE

REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}

5
H

Q 2 }Write SQL command Such as insertion , deletion, updation for any schema…

INSERTION

INSERT INTO table_name (column1, column2, column3,...)


VALUES (value1, value2, value3, ...);

INSERT INTO Student(StudID , NAme , Semester , Dept)


VALUES ('100', 'Hemant ' , '4th' , 'IT');

DELETION
DELETE FROM table_name WHERE condition;

DELETE FROM Student WHERE StudID = '101';

UPDATION
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

UPDATE Student
SET Name = 'Pankaj'
WHERE StudID = '101';

6
H

Q 3 } To Execute Nested Queries, Join Queries , Order by , Having clause , and string
Operation
Nested Queries
ANY Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);

SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);

SELECT ALL ProductName


FROM Products
WHERE TRUE;

Join Queries
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

SELECT Orders.OrderID,
Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;

7
H

LEFT JOIN Syntax


SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

SELECT Customers.CustomerName,
Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID =
Orders.CustomerID
ORDER BY Customers.CustomerName;

RIGHT JOIN Syntax


SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
SELECT Orders.OrderID, Employees.LastName,
Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID =
Employees.EmployeeID
ORDER BY Orders.OrderID;
FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Self Join Syntax
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
SELECT A.CustomerName AS CustomerName1,
B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
8
H

ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
SELECT * FROM Customers
ORDER BY Country;
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
String Function

9
H

Q4 } To perform Set operator like Union , Intersect, Minus on a set of tables .


UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

SELECT City FROM Customers


UNION
SELECT City FROM Suppliers
ORDER BY City;

Intersect Syntax
SELECT column1, column2,…, columnN
FROM table1, table2,…, tableN
INTERSECT
SELECT column1, column2,…, columnN
FROM table1, table2,…, tableN
SQL> SELECT NAME, AGE, HOBBY FROM STUDENTS
INTERSECT
SELECT NAME, AGE, HOBBY FROM ASSOCIATES

Minus syntax
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
MINUS
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

SELECT NAME, AGE , GRADE


FROM Table1
MINUS
SELECT NAME, AGE, GRADE
FROM Table2

10
H

Q5 } To Execute various Commands for GROUP function (avg, count, max, min , sum)
GROUP BY Syntax
SELECT column1, column2, ...
FROM table
GROUP BY columnA, columnB, ...;

Count function ()
SELECT country, COUNT(*) AS number
FROM Customers
GROUP BY country;

Sum function()
SELECT SUM(column_name)
FROM table;
SELECT SUM(amount) AS total_sales
FROM Orders;

Avg function()
SELECT AVG(column_name)
FROM table;
SELECT AVG(age) AS average_age
FROM Customers;

Max function()
SELECT MAX(columnn)
FROM table;
SELECT MAX(age)
FROM Customers;

Min function()
SELECT MIN(columnn)
FROM table;
SELECT MIN(age)
FROM Customers;

11
H

Q6 }Write a PL/SQL block for transaction application using triggers …

PL/SQL stands for Procedural Language extensions to the Structured Query Language
(SQL).

CREATE OR REPLACE TRIGGER display_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
1].
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (7, 'Hemant', 22, 'MHOW', 10000.00 );
OUTPUT :-
Old salary:
New salary: 10000
Salary difference:

2].

UPDATE customers
SET salary = salary + 500
WHERE id = 2;
OUTPUT:-
Old salary: 1500
New salary: 2000
Salary difference: 500

12
H

Q7 } Write a DBMS program to prepare report for an application using function ..


CREATE TABLE Customer
(
ID int,
Name varchar(255),
Age int,
Address varchar(255),
Salary varchar(255)
);
insert into Customer(Id, name,Age, Address, Salary) values(1,'Ramesh',32, 'ahemdabad',2000);
insert into Customer(Id, name,Age, Address, Salary) values(2,'Khilan',25, 'Delhi',1500);
insert into Customer(Id, name,Age, Address, Salary) values(3,'Kaushik',23, 'Kotam',2000);
insert into Customer(Id, name,Age, Address, Salary) values(4, 'Chaitali',25, 'Mumbai',6500);
insert into Customer(Id, name,Age, Address, Salary) values(5,'Hardik',27,'Bhopal',8500);
insert into Customer(Id, name,Age, Address, Salary) values(6,'Komal',22,'Mp',4500);

CREATE OR REPLACE FUNCTION totalCustomers


RETURN number IS
total number(2) := 0;
BEGIN
SELECT count(*) into total
FROM customers;

RETURN total;
END;
/

Function created.

DECLARE
c number(2);
BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
/

OUTPUT :-

Total no. of Customers: 6

PL/SQL procedure successfully completed.


13
H

Q 8 } Designing of various Input screens / forms .

In an information system, input is the raw data that is processed to produce output. During the input
design, the developers must consider the input devices such as PC, MICR, OMR, etc.
Therefore, the quality of system input determines the quality of system output. Welldesigned input
forms and screens have following properties −
• It should serve specific purpose effectively such as storing, recording, and retrieving the
information.
• It ensures proper completion with accuracy.
• It should be easy to fill and straightforward.
• It should focus on user’s attention, consistency, and simplicity.
Objectives for Input Design
The objectives of input design are −
• To design data entry and input procedures
• To reduce input volume
• To design source documents for data capture or devise other data capture methods
• To design input data records, data entry screens, user interface screens, etc.
• To use validation checks and develop effective input controls.

Data Input Methods


It is important to design appropriate data input methods to prevent errors while entering data. These
methods depend on whether the data is entered by customers in forms manually and later entered
by data entry operators, or data is directly entered by users on the PCs.
A system should prevent user from making mistakes by −

• Clear form design by leaving enough space for writing legibly.


• Clear instructions to fill form.
• Clear form design.
• Reducing key strokes.
• Immediate error feedback.
Some of the popular data input methods are −

• Batch input method (Offline data input method)


• Online data input method
• Computer readable forms
• Interactive data input

Forms Design
Both forms and reports are the product of input and output design and are business document
consisting of specified data. The main difference is that forms provide fields for data input but reports
are purely used for reading. For example, order forms, employment and credit application, etc.
• During form designing, the designers should know −
14
H

o who will use them


o where would they be delivered
o the purpose of the form or report
Types of Forms
Flat Forms
• It is a single copy form prepared manually or by a machine and printed on a paper. For
additional copies of the original, carbon papers are inserted between copies.
• It is a simplest and inexpensive form to design, print, and reproduce, which uses less
volume.
Unit Set/Snap out Forms
• These are papers with one-time carbons interleaved into unit sets for either
handwritten or machine use.
• Carbons may be either blue or black, standard grade medium intensity. Generally, blue
carbons are best for handwritten forms while black carbons are best for machine use.
Continuous strip/Fanfold Forms
• These are multiple unit forms joined in a continuous strip with perforations between
each pair of forms.
• It is a less expensive method for large volume use.
No Carbon Required (NCR) Paper
• They use carbonless papers which have two chemical coatings (capsules), one on the
face and the other on the back of a sheet of paper.
• When pressure is applied, the two capsules interact and create an image.

15
H

Q 9}: Create reports using database connectivity of Front end with back end.
A client or front-end database application also interacts with the database by requesting and
receiving information from database server. It acts as an interface between the user and the
database.
The database server or back end is used to manage the database tables and also respond to client
requests.
A front-end or a client portion The client executes the database application that accesses database
information and interacts with the user.
A back-end or a server portion The server executes the MySql and handles the functions required
for concurrent, shared data access to MySql database.
Database Connectivity using C/C++

// C++ program for connecting to database (and error handling)


#include<stdio.h>
#include<SQLAPI.h> // main SQLAPI++ header
int main(int argc, char* argv[])
{
// create connection object to connect to database
SAConnection con;
try
{
// connect to database
// in this example, it is Oracle,
// but can also be Sybase, Informix, DB2
// SQLServer, InterBase, SQLBase and ODBC
con.Connect ("test", // database name
"tester", // user name
"tester", // password
SA_Oracle_Client); //Oracle Client
printf("We are connected!\n");
// Disconnect is optional
// autodisconnect will occur in destructor if needed
con.Disconnect();
printf("We are disconnected!\n");
}
catch(SAException & x)
{
// SAConnection::Rollback()
// can also throw an exception
// (if a network error for example),
// we will be ready
try
{
// on error rollback changes
con.Rollback ();
}
catch(SAException &)
{
}
// print error message
printf("%s\n", (const char*)x.ErrText());
} 16
H

return 0;
Create reports using database
In order to use MySQL you will need to have the MySQL Connector/ODBC driver installed on
the system where the OAS Engine is running.
If it is not currently installed you can download from
here: https://downloads.mysql.com/archives/c-odbc/
In order to use the ODBC Administrator you may also need to install the Visual C++
Redistributable for Visual Studio x86.
In MySQL create a separate user from root. You will uses this user in the string below.

No spaces between elements.

Database = DatatBaseName
Table = a table within the database.
Dataset should now show table elements

17
H

Q10} create database design with normalization and implementing in any applications ..

CREATE TABLE Projects(


[ID] INT PRIMARY KEY IDENTITY,
[Name] VARCHAR(100),
[Value] DECIMAL(5,2),
StartDate DATE,
EndDate DATE
)
GO
CREATE TABLE Employees(
[ID] INT PRIMARY KEY IDENTITY,
[FirstName] VARCHAR(50),
[LastName] VARCHAR(50),
[HourlyWage] DECIMAL(5,2),
[HireDate] DATE
)
GO
CREATE TABLE ProjectEmployees(
[ID] INT PRIMARY KEY IDENTITY,
[ProjectID] INT,
[EmployeeID] INT,
[Hours] DECIMAL(5,2),
CONSTRAINT FK_ProjectEmployees_Projects FOREIGN KEY ([ProjectID]) REFERENCES [Projects]
([ID]),
CONSTRAINT FK_ProjectEmployees_Employees FOREIGN KEY ([EmployeeID]) REFERENCES
[Employees] ([ID])
)
GO
CREATE TABLE JobOrders(
[ID] INT PRIMARY KEY IDENTITY,
[EmployeeID] INT,
[ProjectID] INT,
[Description] TEXT,
[OrderDateTime] DATETIME,
[Quantity] INT,
[Price] DECIMAL(5,2),
CONSTRAINT FK_JobOrders_Projects FOREIGN KEY ([ProjectID]) REFERENCES [Projects] ([ID]),
CONSTRAINT FK_JobOrders_Employees FOREIGN KEY ([EmployeeID]) REFERENCES [Employees]
([ID])
)
GO

CREATE TABLE Customers (


[Name] VARCHAR(100),
[Industry] VARCHAR(100),
[Project1_ID] INT,
[Project1_Feedback] TEXT,
[Project2_ID] INT,
[Project2_Feedback] TEXT,
[ContactPersonID] INT,
[ContactPersonAndRole] VARCHAR(255), 18
H

[PhoneNumber] VARCHAR(12),
[Address] VARCHAR(255),
[City] VARCHAR(255),
[Zip] VARCHAR(5)
)

First normal form


ALTER TABLE [Customers]
ADD [ID] INT IDENTITY PRIMARY KEY

sp_rename 'Customers.[ContactPersonAndRole]', 'ContactPerson', 'COLUMN'


GO
ALTER TABLE [Customers]
ADD [ContactPersonRole] VARCHAR(20)
GO

ALTER TABLE [Customers]


DROP COLUMN Project1_ID
ALTER TABLE [Customers]
DROP COLUMN Project1_Feedback
ALTER TABLE [Customers]
DROP COLUMN Project2_ID
ALTER TABLE [Customers]
DROP COLUMN Project2_Feedback
GO

CREATE TABLE ProjectFeedback(


[ID] INT PRIMARY KEY IDENTITY,
[ProjectID] INT,
[CustomerID] INT,
[Feedback] TEXT,
CONSTRAINT FK_ProjectFeedbacks_Projects FOREIGN KEY ([ProjectID]) REFERENCES [Projects] ([ID]),
CONSTRAINT FK_ProjectFeedbacks_Customers FOREIGN KEY ([CustomerID]) REFERENCES [Customers] ([ID])
)

Second Normal Form


ALTER TABLE [Customers]
DROP COLUMN ContactPerson
ALTER TABLE [Customers]
DROP COLUMN ContactPersonRole
ALTER TABLE [Customers]
DROP COLUMN PhoneNumber
19
H

CREATE TABLE ContactPersons(


[ID] INT PRIMARY KEY IDENTITY,
[ContactPerson] VARCHAR(100),
[ContactPersonRole] VARCHAR(20),
[PhoneNumber] VARCHAR(12)
)
GO
ALTER TABLE [Customers]
ADD CONSTRAINT FK_Customers_ContactPersons FOREIGN KEY ([ContactPersonID])
REFERENCES ContactPersons([ID])

Third Normal Form

ALTER TABLE [Customers]


DROP COLUMN City
GO
CREATE TABLE ZipCodes(
[ZipID] VARCHAR(5) PRIMARY KEY,
[City] VARCHAR(255)
)
GO
ALTER TABLE [Customers]
ADD CONSTRAINT FK_Customers_ZipCodes FOREIGN KEY ([Zip])
REFERENCES ZipCodes([ZipID])

20

You might also like