0% found this document useful (0 votes)
2 views13 pages

DD File

The document outlines a series of practical exercises related to database management, including inserting, updating, and deleting records, performing join operations, applying constraints, and using subqueries. It also covers creating and managing multiple databases, including user permissions and views. Each practical includes SQL code examples and aims to demonstrate specific database functionalities.

Uploaded by

vinaykumarprof01
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 views13 pages

DD File

The document outlines a series of practical exercises related to database management, including inserting, updating, and deleting records, performing join operations, applying constraints, and using subqueries. It also covers creating and managing multiple databases, including user permissions and views. Each practical includes SQL code examples and aims to demonstrate specific database functionalities.

Uploaded by

vinaykumarprof01
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/ 13

INDEX

S. No. Practical Page No. Date Signature


1 To insert data in a table using insert and display the
records in a table.
2 Update or Delete records of a table and modifying
structure of a table using Alter and Drop command.
3 To perform join operation between various tables.
4 Applying constraint using two tables.
5 To retrieve data from different tables using sub queries
and correlated queries.
6 Create two databases either on single DBMS and Design
Database to fragment and share the fragments from
both database and write single query for creating view.
7 Viewing a Global Database Name

8 Changing the Domain in a Global Database Name


9 Changing a Global Database Name
10 To see which privileges you currently have available in a
distributed database.
11 Creating private, public and global Database Links.
12 Creating Fixed User Database Links

13 Creating Connected User and Current User Database


Links
Practical-1
Aim:
To insert data in a table using insert and display the records in a table.

Code:
-- Create table
CREATE TABLE students (
student_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
course VARCHAR2(100),
admission_year NUMBER
);

-- Insert data
INSERT INTO students VALUES (1, 'Aarav', 'Sharma', 'Computer Science', 2022);
INSERT INTO students VALUES (2, 'Riya', 'Verma', 'Information Technology', 2021);
INSERT INTO students VALUES (3, 'Karan', 'Patel', 'Electronics', 2022);
INSERT INTO students VALUES (4, 'Sneha', 'Nair', 'Computer Science', 2023);
INSERT INTO students VALUES (5, 'Yash', 'Mehta', 'Mechanical', 2020);

-- Display records
SELECT * FROM students;

Output:

1
Practical-2
Aim:
Update or Delete records of a table and modifying structure of a table using Alter and Drop command.

Code:
-- Update
UPDATE students SET course = 'Data Science' WHERE student_id = 2;

-- Delete
DELETE FROM students WHERE admission_year = 2020;

-- Alter: Add column


ALTER TABLE students ADD email VARCHAR2(100);

-- Update email
UPDATE students SET email = 'aarav.sharma@example.com' WHERE student_id = 1;

-- Drop column
ALTER TABLE students DROP COLUMN email;

Output:
-- Update

-- Delete

-- Alter: Add column

2
-- Update email

-- Drop column

3
Practical-3
Aim:
To perform join operation between various tables.

Code:
-- Create the 'courses' table
CREATE TABLE courses (
course_id NUMBER PRIMARY KEY,
course_name VARCHAR2(100)
);
-- Create the 'student_details' table
CREATE TABLE student_details (
student_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
course_id NUMBER,
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

-- Insert data into 'courses'


INSERT INTO courses VALUES (101, 'Computer Science');
INSERT INTO courses VALUES (102, 'Information Technology');
INSERT INTO courses VALUES (103, 'Mechanical Engineering');
INSERT INTO courses VALUES (104, 'Electronics and Communication');
-- Insert data into 'student_details'
INSERT INTO student_details VALUES (1, 'Aarav', 'Sharma', 101);
INSERT INTO student_details VALUES (2, 'Riya', 'Verma', 102);
INSERT INTO student_details VALUES (3, 'Karan', 'Patel', 103);
INSERT INTO student_details VALUES (4, 'Sneha', 'Nair', NULL);

-- Perform INNER JOIN


SELECT s.student_id, s.first_name, s.last_name, c.course_name
FROM student_details s
INNER JOIN courses c ON s.course_id = c.course_id;

-- Perform LEFT JOIN


SELECT s.student_id, s.first_name, s.last_name, c.course_name
FROM student_details s
LEFT JOIN courses c ON s.course_id = c.course_id;

-- Perform RIGHT JOIN


SELECT s.student_id, s.first_name, s.last_name, c.course_name
FROM student_details s
RIGHT JOIN courses c ON s.course_id = c.course_id;

-- Perform FULL OUTER JOIN


SELECT s.student_id, s.first_name, s.last_name, c.course_name
FROM student_details s
FULL OUTER JOIN courses c ON s.course_id = c.course_id;

4
Output:

-- Perform INNER JOIN

-- Perform LEFT JOIN

-- Perform RIGHT JOIN

-- Perform FULL OUTER JOIN

5
Practical-4
Aim:
Applying constraint using two tables.

Code:
-- Create departments table with constraints
CREATE TABLE departments (
dept_id NUMBER PRIMARY KEY,
dept_name VARCHAR2(100) UNIQUE NOT NULL
);

-- Create students table with constraints


CREATE TABLE students (
student_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50) NOT NULL,
last_name VARCHAR2(50),
dept_id NUMBER,
admission_year NUMBER CHECK (admission_year >= 2000),
CONSTRAINT fk_dept FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);

-- Insert valid data


INSERT INTO departments VALUES (1, 'Computer Science');
INSERT INTO departments VALUES (2, 'Mechanical Engineering');
INSERT INTO departments VALUES (3, 'Electrical Engineering');

INSERT INTO students VALUES (101, 'Aarav', 'Sharma', 1, 2022);


INSERT INTO students VALUES (102, 'Riya', 'Verma', 2, 2021);
INSERT INTO students VALUES (103, 'Karan', 'Patel', 3, 2020);

-- Will fail due to UNIQUE constraint


INSERT INTO departments VALUES (4, 'Computer Science');
-- Will fail due to FOREIGN KEY constraint
INSERT INTO students VALUES (104, 'Sneha', 'Nair', 5, 2023);
-- Will fail due to CHECK constraint
INSERT INTO students VALUES (105, 'Yash', 'Mehta', 1, 1995);

Output:

6
7
Practical-5
Aim:
To retrieve data from different tables using sub queries and correlated queries.

Code:
-- Simple Subquery: Students in "Computer Science"
SELECT first_name, last_name
FROM students
WHERE dept_id = (
SELECT dept_id
FROM departments
WHERE dept_name = 'Computer Science'
);

-- Subquery with IN: Students in "Computer Science" or "Mechanical Engineering"


SELECT first_name, last_name
FROM students
WHERE dept_id IN (
SELECT dept_id
FROM departments
WHERE dept_name IN ('Computer Science', 'Mechanical Engineering')
);

-- Nested Subquery: Department name of student with ID 101


SELECT dept_name
FROM departments
WHERE dept_id = (
SELECT dept_id
FROM students
WHERE student_id = 101
);

-- Correlated Subquery: Students with admission year > avg in their department
SELECT first_name, last_name, dept_id, admission_year
FROM students s
WHERE admission_year > (
SELECT AVG(admission_year)
FROM students
WHERE dept_id = s.dept_id
);

-- Correlated Subquery with EXISTS: Departments that have at least one student
SELECT dept_name
FROM departments d
WHERE EXISTS (
SELECT 1
FROM students s
WHERE s.dept_id = d.dept_id
);

8
-- NOT EXISTS: Departments with no students
SELECT dept_name
FROM departments d
WHERE NOT EXISTS (
SELECT 1
FROM students s
WHERE s.dept_id = d.dept_id
);

Output:
-- Simple Subquery: Students in "Computer Science"

-- Subquery with IN: Students in "Computer Science" or "Mechanical Engineering"

-- Nested Subquery: Department name of student with ID 101

-- Correlated Subquery: Students with admission year > avg in their department

-- Correlated Subquery with EXISTS: Departments that have at least one student

-- NOT EXISTS: Departments with no students

9
Practical-6
Aim:
Create two databases either on single DBMS and Design Database to fragment and share the fragments
from both database and write single query for creating view.

Code:
-- As DBA
CREATE USER db1_user IDENTIFIED BY db1pass;
GRANT CONNECT, RESOURCE TO db1_user;

CREATE USER db2_user IDENTIFIED BY db2pass;


GRANT CONNECT, RESOURCE TO db2_user;

-- Connect as db1_user
-- Horizontal Fragment: Students from department 1 and 2
CREATE TABLE student_fragment1 (
student_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
dept_id NUMBER
);
INSERT INTO student_fragment1 VALUES (101, 'Aarav', 1);
INSERT INTO student_fragment1 VALUES (102, 'Riya', 2);

-- Connect as db2_user
-- Horizontal Fragment: Students from department 3
CREATE TABLE student_fragment2 (
student_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
dept_id NUMBER
);
INSERT INTO student_fragment2 VALUES (103, 'Karan', 3);
INSERT INTO student_fragment2 VALUES (104, 'Sneha', 3);

-- Grant access to the main user


-- In db1_user
GRANT SELECT ON student_fragment1 TO dd_practicals;
-- In db2_user
GRANT SELECT ON student_fragment2 TO dd_practicals;

-- Create view from dd_practicals


CREATE OR REPLACE VIEW global_student_view AS
SELECT * FROM db1_user.student_fragment1
UNION ALL
SELECT * FROM db2_user.student_fragment2;

-- Display the results


SELECT * FROM global_student_view;

10
Output:
-- Create two DB users

-- Horizontal Fragment1: Students from department 1 and 2

-- Horizontal Fragment2: Students from department 3

-- Grant access to the main user

-- Create view from dd_practicals

-- Display the results

11
Practical-7
Aim:
Viewing a Global Database Name

Code:
SELECT name, db_unique_name FROM v$database;
SELECT * FROM global_name;
SELECT name, value
FROM v$parameter
WHERE name IN ('db_name', 'db_domain');

Output:

12

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