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

Mehak Dbms

1. The document discusses practical assignments for a Database Management Systems laboratory course. 2. Practical 1 introduces SQL and its sublanguages - DDL, DML, DCL, and DQL. Practicals 2-8 demonstrate the use of SQL commands like CREATE, SELECT, INSERT, UPDATE, DELETE, ALTER and functions like COUNT, SUM, AVG to manipulate database tables. 3. The practicals cover skills like creating and populating tables, retrieving and filtering data using SELECT, modifying tables using DDL commands, and using aggregation functions and subqueries.

Uploaded by

bmehak154
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)
35 views

Mehak Dbms

1. The document discusses practical assignments for a Database Management Systems laboratory course. 2. Practical 1 introduces SQL and its sublanguages - DDL, DML, DCL, and DQL. Practicals 2-8 demonstrate the use of SQL commands like CREATE, SELECT, INSERT, UPDATE, DELETE, ALTER and functions like COUNT, SUM, AVG to manipulate database tables. 3. The practicals cover skills like creating and populating tables, retrieving and filtering data using SELECT, modifying tables using DDL commands, and using aggregation functions and subqueries.

Uploaded by

bmehak154
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/ 21

GURU NANAK DEV ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER APPLICATIONS

PRACTICAL FILE

Database Management Systems Laboratory


(UGCA-1925)

Submitted to: Submitted by:


Prof. Birpal Kaur Mehak
Roll No. 2191037
BCA 4th
Practical No. 1

Introduction to SQL? Explain DDL, DML, DCL, and DQL?

SQL, or Structured Query Language, is a programming language used to manage and manipulate data in
relational databases. It is a standard language that is widely used in the IT industry and is supported by
various database management systems, including MySQL, Oracle, Microsoft SQL Server, and
PostgreSQL.

The main purpose of SQL is to enable users to interact with and retrieve data from databases, as well as to
perform various operations such as data insertion, modification, deletion, and querying. It provides a set of
commands and functions to perform these operations, which are collectively known as SQL statements.

SQL is a declarative language, meaning that it allows users to specify what they want to do with the data
without necessarily specifying how to do it. For example, to retrieve data from a database, a user would
write a SELECT statement, which specifies the columns to be retrieved and the conditions for the data to
be selected.

SQL also provides various data manipulation functions, such as arithmetic operations, string manipulation,
date and time functions, and aggregations, which allow users to perform complex data transformations on
the data stored in databases.

Dividing SQL into Sublanguages

SQL, or Structured Query Language, is a complex language that can be divided into several
sublanguages based on the tasks they perform. Some of the commonly recognized sublanguages of SQL
include:
1. Data Definition Language (DDL): DDL deals with creating, altering, and dropping the
structure of the database objects, such as tables, indexes, views, and schemas. The main DDL
statements are CREATE, ALTER, and DROP. For example, the CREATE TABLE statement is
used to create a new table in a database.

2. Data Manipulation Language (DML): DML is used to manipulate the data stored in the
database objects. The main DML statements are INSERT, UPDATE, DELETE, and MERGE.
For example, the INSERT statement is used to add new data to a table, while the UPDATE
statement is used to modify existing data in a table.

3. Data Control Language (DCL): DCL is used to control access to the database objects. It deals
with granting and revoking privileges and permissions to the database users. The main DCL
statements are GRANT and REVOKE. For example, the GRANT statement is used to give a user
permission to perform specific operations on a table.

4. Data Query Language (DQL): DQL is used to retrieve data from the database objects. The
main DQL statement is SELECT. For example, the SELECT statement is used to retrieve
specific columns from a table that meets certain conditions.
Practical No. 2
Create table and insert data into a table.

CREATE TABLE customers ( customer_id


INT PRIMARY KEY, first_name
VARCHAR(50) NOT NULL, last_name
VARCHAR(50) NOT NULL, email
VARCHAR(100) UNIQUE,
phone_number VARCHAR(20)
);

INSERT INTO customers (customer_id, first_name, last_name, email, phone_number) VALUES


(1, 'John', 'Doe', 'johndoe@example.com', '123-456-7890'),
(2, 'Jane', 'Smith', 'janesmith@example.com', '234-567-8901'),
(3, 'Bob', 'Johnson', NULL, '345-678-9012');

SELECT * FROM customers;

OUTPUT:-
Practical No. 3
Use of SELECT Statements.

CREATE TABLE customers ( customer_id


INT PRIMARY KEY, first_name
VARCHAR(50) NOT NULL, last_name
VARCHAR(50) NOT NULL, email
VARCHAR(100) UNIQUE,
phone_number VARCHAR(20)
);

INSERT INTO customers


VALUES
(1, 'John', 'Doe', 'johndoe@example.com', '123-456-7890'),
(2, 'Jane', 'Smith', 'janesmith@example.com', '234-567-8901'),
(3, 'Bob', 'Johnson', NULL, '345-678-9012');

Statement 1:-
SELECT * FROM customers;

OUTPUT:-

Statement 2:-
SELECT first_name, last_name FROM
customers;

OUTPUT:-
Statement 3:-
SELECT * FROM customers
WHERE first_name = 'John';

OUTPUT:-

Statement 4:-
SELECT DISTINCT email FROM customers;

OUTPUT:-

Statement 5:-
SELECT first_name, last_name FROM customers ORDER BY last_name ASC;

OUTPUT:-
Practical No. 4
Use of keys.

● Primary Key:-

CREATE TABLE customers (


customer_id INT PRIMARY KEY,
first_name VARCHAR(50), last_name
VARCHAR(50),
email VARCHAR(100),
phone_number VARCHAR(20)
);

INSERT INTO customers


VALUES
(1, 'John', 'Doe', 'johndoe@example.com', '123-456-7890'),
(2, 'Jane', 'Smith', 'janesmith@example.com', '234-567-8901');

SELECT * FROM customers;

OUTPUT:-

● Foreign Key:-

CREATE TABLE orders (


order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

INSERT INTO orders (order_id, customer_id, order_date) VALUES


(1, 1, '2023-05-01'),
(2, 2, '2023-05-02');

SELECT * FROM orders;


OUTPUT:-

● Unique Key:-

CREATE TABLE students (


student_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50), email
VARCHAR(100),
UNIQUE (email)
);

INSERT INTO students (id, name, email) VALUES (1,


'John Smith', 'john.smith@example.com'),
(2, 'Jane Doe', 'jane.doe@example.com'),
(3, 'Bob Johnson', 'bob.johnson@example.com');

SELECT * FROM students;

OUTPUT:-

● Composite Key:-

CREATE TABLE orders (


order_id INT,
customer_id INT,
order_date DATE,
PRIMARY KEY (order_id, customer_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

INSERT INTO orders (order_id, customer_id, order_date) VALUES (1, 1,


'2023-05-01'),
(2, 2, '2023-05-01'),
(3, 3, '2023-05-01');
SELECT * FROM orders;

OUTPUT:-
Practical No. 5
Use of ALTER, RENAME, DROP, DELETE, and UPDATE commands

● ALTER command:- The ALTER command is used to modify the structure of an


existing table in the database. You can use the ALTER command to add or remove
columns, change the data type of a column, or modify constraints on a table.

Example:-

CREATE TABLE customers (


customer_id INT PRIMARY KEY,
first_name VARCHAR(50), last_name
VARCHAR(50),
email VARCHAR(100),
phone_number VARCHAR(20)
);

SELECT * FROM customers;

OUTPUT:-

ALTER TABLE customers ADD COLUMN address VARCHAR(255);

OUTPUT:-

● RENAME command:- The RENAME command is used to rename an existing table in


the database.

Example:-
RENAME TABLE customers TO customer;
OUTPUT:-

● DROP command:- The DROP command is used to delete an entire table, or to delete a
column from a table.

Example:
DROP TABLE students;

OUTPUT:-

● DELETE command:- The DELETE command is used to delete one or more rows from
a table.

Example:-

DELETE FROM customer WHERE customer_id = 1;

OUTPUT:-

● UPDATE command:- The UPDATE command is used to modify one or more rows in a
table.

Example:-

UPDATE customer SET first_name = 'Jane' WHERE customer_id = 2;


OUTPUT:-
Practical No. 6
Use of Aggregate Functions.

● COUNT():- The COUNT() function is used to count the number of rows in a table.

Example:-

SELECT COUNT(*) FROM customer;

OUTPUT:-

● SUM():- The SUM() function is used to find the sum of a column in a table.

Example:-

SELECT SUM(price) FROM orders;

OUTPUT:-

● AVG():- The AVG() function is used to find the average of a column in a table.

Example:-

SELECT AVG(price) FROM orders;

OUTPUT:-
● MAX():- The MAX() function is used to find the maximum value of a column in a table.

Example:-

SELECT MAX(price) FROM orders;

OUTPUT:-

● MIN():- The MIN() function is used to find the minimum value of a column in a table.

Example:-

SELECT MIN(price) FROM orders;

OUTPUT:-
Practical No. 7
Use of Operator Functions.

● Arithmetic operators:- Arithmetic operators in SQL include + (addition), -


(subtraction), * (multiplication), / (division), % (modulus), and ^ (exponentiation). These
operators are used to perform basic arithmetic calculations on numerical data.

Example:-

SELECT (price * quantity) AS total_price FROM orders;

OUTPUT:-

● Comparison operators:- Comparison operators in SQL include = (equal to), <> or


!= (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or
equal to). These operators are used to compare values in the tables.

Example:-

SELECT * FROM products WHERE price > 50;

OUTPUT:-

● Logical operators:- Logical operators in SQL include AND, OR, and NOT. These
operators are used to combine conditions in the WHERE clause of a SELECT statement.

Example:-

SELECT * FROM customer WHERE country = 'USA' AND (age > 18 OR email IS NOT NULL);
OUTPUT:-
Practical No. 8
Use of Sub-Queries.

● Aggregating data:- Sub-queries can be used to perform aggregate functions on a subset


of data. For example, to find the average order value for each customer.

Example:-

SELECT customer_id,

AVG(order_total) as avg_order_total FROM (SELECT customer_id,

SUM(price * quantity) as order_total FROM orders GROUP BY customer_id) subquery GROUP BY


customer_id;

OUTPUT:-

● Joins:- Subqueries can be used to join two or more tables based on a specific condition.
For example, you can use a subquery to join the "orders" and "customers" tables to retrieve
all orders made by customers from a specific country.

Example:-

SELECT * FROM orders WHERE customer_id IN (SELECT customer_id FROM customers WHERE country =
'USA');

OUTPUT:-
Practical No. 9
Perform the following PL-SQL programs: (a) Write a program to
add two numbers, (b) Write a program to check whether a
number is odd or even, and (c) Write a program to find the
factorial of a number.

(a) Write a program to add two numbers

DECLARE
num1 NUMBER := 10;
num2 NUMBER := 20;
s NUMBER;
BEGIN
s := num1 + num2;
dbms_output.put_line('The sum of ' || num1 || ' and ' || num2 || ' is ' || s); END;

OUTPUT:-

(b) Write a program to check whether a number is odd or even

DECLARE
num NUMBER := 15;
BEGIN
IF MOD(num, 2) = 0 THEN
dbms_output.put_line(num || ' is even');
ELSE
dbms_output.put_line(num || ' is odd');
END IF;
END;

OUTPUT:-
(c) Write a program to find the factorial of a number.

DECLARE
num NUMBER := 5;
factorial NUMBER := 1;
BEGIN
FOR i IN 1..num LOOP
factorial := factorial * i;
END LOOP;
dbms_output.put_line('The factorial of ' || num || ' is ' || factorial); END;

OUTPUT:-
Practical No. 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy