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

DBMS

The document outlines a series of SQL experiments involving the creation of tables, insertion of data, and application of constraints, joins, and views. It covers various SQL operations such as creating tables with different data types, using SELECT statements with clauses, and performing set operations like UNION and INTERSECT. Additionally, it includes examples of sub-queries and generating views to manage employee and department data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DBMS

The document outlines a series of SQL experiments involving the creation of tables, insertion of data, and application of constraints, joins, and views. It covers various SQL operations such as creating tables with different data types, using SELECT statements with clauses, and performing set operations like UNION and INTERSECT. Additionally, it includes examples of sub-queries and generating views to manage employee and department data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Experiment 1: Two Assignments on the Creation of Table with Different Types

of Constraints

Assignment 1: Create a Table with Constraints

CREATE TABLE products (

product_id NUMBER PRIMARY KEY,

product_name VARCHAR2(100) NOT NULL,

price NUMBER(10, 2) CHECK (price > 0),

category VARCHAR2(50) UNIQUE

);

INSERT INTO products (product_id, product_name, price, category)

VALUES (1, 'Laptop', 800.00, 'Electronics');

INSERT INTO products (product_id, product_name, price, category)

VALUES (2, 'Smartphone', 600.00, 'Electronics');

Assignment 2: Insert Data and Check Constraints

INSERT INTO products (product_id, product_name, price, category)

VALUES (3, 'Smartwatch', 150.00, 'Electronics');

SELECT * FROM products;


Output:
Table 'products' created successfully.

1 row(s) inserted.

1 row(s) inserted.

ERROR: Unique constraint violated for category.

Product_id Product_name Price Category


1 Laptop 800.00 Electronics
2 Smartphone 600.00 Electronics
3 Smartwatch 150.00 Electronics
Experiment 2: Two Assignments on Insert, Delete, and Modify Records from the
Tables

Assignment 1: Insert, Update, and Delete Records

CREATE TABLE employees ( emp_id

NUMBER PRIMARY KEY, emp_name

VARCHAR2(50), emp_salary NUMBER

);

INSERT INTO employees (emp_id, emp_name, emp_salary)

VALUES (1, 'Alice', 50000);

INSERT INTO employees (emp_id, emp_name, emp_salary)

VALUES (2, 'Bob', 60000);

UPDATE employees SET emp_salary = 55000 WHERE emp_name = 'Alice';

DELETE FROM employees WHERE emp_name = 'Bob';

SELECT * FROM employees;

Assignment 2: Modify Records Using UPDATE and DELETE

CREATE TABLE students ( student_id NUMBER PRIMARY KEY,

first_name VARCHAR2(50), last_name VARCHAR2(50), age

NUMBER );

INSERT INTO students (student_id, first_name, last_name, age)


VALUES (1, 'John', 'Doe', 20);

INSERT INTO students (student_id, first_name, last_name, age)

VALUES (2, 'Jane', 'Smith', 22);

UPDATE students SET age = 21 WHERE student_id = 1;

DELETE FROM students WHERE student_id = 2;

SELECT * FROM students;


Output:

emp_id emp_name emp_salary

1 Alice 55000

STUDENT_ID FIRST_NAME LAST_NAME AGE


1 JOHN DOE 21
Experiment 3: Two Assignments on Exploring SELECT Statement Using Various
Clauses

Assignment 1: Use WHERE, ORDER BY, and GROUP BY Clauses

CREATE TABLE sales (

sale_id NUMBER PRIMARY KEY,

product_name VARCHAR2(50),

category VARCHAR2(30),

quantity NUMBER,

price_per_unit NUMBER );

INSERT INTO sales VALUES (1, 'Laptop', 'Electronics', 2, 60000);

INSERT INTO sales VALUES (2, 'Mouse', 'Electronics', 5, 500);

INSERT INTO salesVALUES (3, 'Shirt', 'Clothing', 3, 800);

INSERT INTO sales VALUES (4, 'Laptop', 'Electronics', 1, 62000);

INSERT INTO sales VALUES (5, 'Shirt', 'Clothing', 4, 750);

SELECT * FROM sales

WHERE category = 'Electronics'

ORDER BY price_per_unit DESC;

Assignment 2: Use HAVING Clause with Aggregate Functions

SELECT category, SUM(quantity) AS total_quantity

FROM sales

GROUP BY category;
Output:

SALE_ID PRODUCT_NAME CATEGORY QUANTITY PRICE_PER_UNIT


4 LAPTOP ELECTRONICS 1 62000
2 LAPTOP ELECTRONICS 2 60000
1 MOUSE ELECTRONICS 5 500

CATEGORY TOTAL_QUANTITY
ELECTRONICS 8
CLOTHING 7
Experiment 4: Two Assignments on the Use of Set Operations

Assignment 1: Use UNION to Combine Results

CREATE TABLE dept1 (

emp_name VARCHAR2(50)

);

CREATE TABLE dept2 ( emp_name VARCHAR2(50)

);

INSERT INTO dept1 VALUES ('Alice');


INSERT INTO dept1 VALUES ('Bob');

INSERT INTO dept1 VALUES ('Charlie');

INSERT INTO dept2 VALUES ('Bob');

INSERT INTO dept2 VALUES ('David');

INSERT INTO dept2 VALUES ('Eve');

SELECT emp_name FROM dept1

UNION

SELECT emp_name FROM dept2;

Assignment 2: Using INTERSECT

SELECT emp_name FROM dept1

INTERSECT

SELECT emp_name FROM dept2;


Output:
Dept1
emp_name
Alice

Bob

Charlie

Dept2
emp_name

Bob

David

Eve

emp_name
Bob
Experiment 5: One assignment on generating sub-queries.

CREATE TABLE departments (

dept_id NUMBER PRIMARY KEY,

dept_name VARCHAR2(50) ); CREATE TABLE

employees (

emp_id NUMBER PRIMARY KEY,

emp_name VARCHAR2(50),

salary NUMBER,

dept_id NUMBER REFERENCES departments(dept_id) );

INSERT INTO departments VALUES (1, 'HR');

INSERT INTO departments VALUES (2, 'IT');

INSERT INTO departments VALUES (3, 'Finance');

INSERT INTO employees VALUES (101, 'Alice', 60000, 1);

INSERT INTO employees VALUES (102, 'Bob', 75000, 2);

INSERT INTO employees VALUES (103, 'Charlie', 82000, 2);

INSERT INTO employees VALUES (104, 'David', 50000, 3);

INSERT INTO employees VALUES (105, 'Eve', 75000, 1);

-- Query 1: Employee(s) with highest salary

SELECT 'Highest Salary' AS query_type, emp_name, salary


FROM employees

WHERE salary = (SELECT MAX(salary) FROM employees);

-- Query 2: Employees with salary above average

SELECT 'Above Avg Salary' AS query_type, emp_name, salary

FROM employees

WHERE salary > (SELECT AVG(salary) FROM employees);

-- Query 3: Departments with more than one employee

SELECT 'Dept with >1 Employee' AS query_type, dept_name

FROM departments d

WHERE 1 < (SELECT COUNT(*) FROM employees e WHERE e.dept_id = d.dept_id);


Experiment 6: Two assignments on creating joins and views on the tables.
Assignment 1: Creating JOINs Between Tables

CREATE TABLE departments (

dept_id NUMBER PRIMARY KEY,

dept_name VARCHAR2(50) );

CREATE TABLE employees (

emp_id NUMBER PRIMARY KEY,

emp_name VARCHAR2(50),

salary NUMBER,

dept_id NUMBER REFERENCES departments(dept_id) );

INSERT INTO departments VALUES (1, 'HR');

INSERT INTO departments VALUES (2, 'IT');

INSERT INTO departments VALUES (3, 'Finance');

INSERT INTO employees VALUES (101, 'Alice', 60000, 1);

INSERT INTO employees VALUES (102, 'Bob', 75000, 2);

INSERT INTO employees VALUES (103, 'Charlie', 82000, 2);

INSERT INTO employees VALUES (104, 'David', 50000, 3);

INSERT INTO employees VALUES (105, 'Eve', 75000, 1);

-- INNER JOIN: List all employees with their department names


SELECT e.emp_name, d.dept_name, e.salary
FROM employees e

JOIN departments d ON e.dept_id = d.dept_id;

-- LEFT OUTER JOIN: Show all departments with employee names (if any)

SELECT d.dept_name, e.emp_name

FROM departments d LEFT

JOIN employees e ON d.dept_id = e.dept_id;

Assignment 2: Creating and Using VIEWS

CREATE VIEW high_earners AS

SELECT emp_id, emp_name, salary

FROM employees

WHERE salary > 70000;

SELECT * FROM high_earners;

CREATE VIEW emp_dept_view AS

SELECT e.emp_name, d.dept_name, e.salary

FROM employees e

JOIN departments d ON e.dept_id = d.dept_id;

SELECT * FROM emp_dept_view

WHERE salary >= 75000;


emp_name dept_name salary

Bob IT 75000

Charlie IT 82000

Eve HR 75000
Experiment 7: Three assignments related to creation of database with tables
having different fields and data types.

Assignment 1: Create a students table with various data types

CREATE TABLE students ( student_id NUMBER PRIMARY KEY,

full_name VARCHAR2(100), date_of_birth DATE, email

VARCHAR2(100), marks FLOAT );

INSERT INTO students VALUES (1, 'Rahul Sharma', TO_DATE('2005-08-12','YYYY-MM-DD'),


'[email protected]', 89.5);

INSERT INTO students VALUES (2, 'Pooja Verma', TO_DATE('2004-03-05','YYYY-MM-DD'),


'[email protected]', 92.3);

Assignment 2: Create an orders table with numeric, character and date fields

CREATE TABLE orders ( order_id NUMBER PRIMARY KEY, customer_name

VARCHAR2(50),

order_date DATE,

total_amount NUMBER(10,2)

);

INSERT INTO orders VALUES (101, 'Amit Patel', TO_DATE('2024-04-01', 'YYYY-MM-DD'),


1599.99);

INSERT INTO orders VALUES (102, 'Sneha Rao', TO_DATE('2024-04-05', 'YYYY-MM-DD'),


450.00);
CREATE TABLE products ( product_id

CHAR(5) PRIMARY KEY,

product_name VARCHAR2(50), price

NUMBER(8,2), stock_qty NUMBER

);

INSERT INTO products VALUES ('P001', 'Laptop', 60000.00, 10);

INSERT INTO products VALUES ('P002', 'Smartphone', 25000.00, 25);


OUTPUT:

STUDENT_ EMAIL MARKS


FULL _NAME DATE _OF_
ID
BIRTH

1 R AHUL SHARM A Rahul@gmail.


20 05 -08 -12 89.5
com

POOJA VERMA Pooja@gmail. 92.3


2 2004-03-05 com

CUSTOMER _ TOTAL_AMOUNT
ORDER_ID ORDER DATE
NAME

AMIT PATEL 1599.99


101 2024 -04 -01

102 SNEHA RAO 2024 -04 - 04 450.00

PRODUCT _ID PRODUCT _NAME PRICE QUANTITY

P00 1 10
LAPTOP 60000

P002 SMARTPHONE 25000 25

You might also like