Mehak Dbms
Mehak Dbms
PRACTICAL FILE
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.
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.
OUTPUT:-
Practical No. 3
Use of SELECT Statements.
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:-
OUTPUT:-
● Foreign Key:-
● Unique Key:-
OUTPUT:-
● Composite Key:-
OUTPUT:-
Practical No. 5
Use of ALTER, RENAME, DROP, DELETE, and UPDATE commands
Example:-
OUTPUT:-
OUTPUT:-
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:-
OUTPUT:-
● UPDATE command:- The UPDATE command is used to modify one or more rows in a
table.
Example:-
● COUNT():- The COUNT() function is used to count the number of rows in a table.
Example:-
OUTPUT:-
● SUM():- The SUM() function is used to find the sum of a column in a table.
Example:-
OUTPUT:-
● AVG():- The AVG() function is used to find the average of a column in a table.
Example:-
OUTPUT:-
● MAX():- The MAX() function is used to find the maximum value of a column in a table.
Example:-
OUTPUT:-
● MIN():- The MIN() function is used to find the minimum value of a column in a table.
Example:-
OUTPUT:-
Practical No. 7
Use of Operator Functions.
Example:-
OUTPUT:-
Example:-
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.
Example:-
SELECT 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.
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:-
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