Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views5 pages

Second Lab Practicle DBMS

Practical

Uploaded by

Pragati Shirole
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)
4 views5 pages

Second Lab Practicle DBMS

Practical

Uploaded by

Pragati Shirole
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/ 5

Second lab Practical DBMS

To study and implement SQL DDL (Data Definition Language) and DML (Data
Manipulation Language) commands on a database.

Theory
1. DDL (Data Definition Language):

Commands used to define the structure of a database (create, modify, delete


objects).

Examples:

o CREATE → create tables, views, indexes, etc.


o ALTER → modify table structure.
o DROP → delete objects.
o TRUNCATE → remove all data but keep structure.
2. DML (Data Manipulation Language):

Commands used to manipulate (insert, update, delete, query) the data.

Examples:

o INSERT → add new records.


o SELECT → retrieve data.
o UPDATE → modify data.
o DELETE → remove records.
-- 1. Create table

CREATE TABLE Student (

RollNo INT PRIMARY KEY,

Name VARCHAR(50) NOT NULL,

Class VARCHAR(20),

Dept VARCHAR(20),

Marks INT CHECK (Marks BETWEEN 0 AND 100)

);

-- 2. Create a View

CREATE VIEW CS_Students AS

SELECT Name, Class, Marks FROM Student WHERE Dept = 'Computer';

-- 3. Create an Index

CREATE INDEX idx_name ON Student(Name);

-- 4. Create a Sequence (in MySQL we use AUTO_INCREMENT, in Oracle use


SEQUENCE)

-- Oracle Example:

CREATE SEQUENCE stu_seq START WITH 1 INCREMENT BY 1;

-- 5. Create a Synonym (only in Oracle)


CREATE SYNONYM stu FOR Student;

-- 6. Add Constraint

ALTER TABLE Student ADD UNIQUE (Name);

SQL DML Queries


CREATE TABLE Students (

StudentID INT PRIMARY KEY,

FirstName VARCHAR(50) NOT NULL,

LastName VARCHAR(50),

DOB DATE,

Marks INT CHECK (Marks >= 0 AND Marks <= 100) );

1. Insert Data

INSERT INTO Students (StudentID, FirstName, LastName, DOB, Marks)

VALUES (1, 'Vikas', 'Shinde', '1995-04-28', 85);

INSERT INTO Students (StudentID, FirstName, LastName, DOB, Marks)

VALUES (2, 'Ashvinii', 'kadlak', '1997-08-15', 90);

2. Select Data

-- Select all columns

SELECT * FROM Students;


-- Select specific columns

SELECT FirstName, Marks FROM Students;

-- Using WHERE with operators

SELECT * FROM Students WHERE Marks >= 80;

3. Update Data

UPDATE Students

SET Marks = 95

WHERE StudentID = 2;

4. Delete Data

DELETE FROM Students

WHERE StudentID = 1;

5. Aggregate Functions

-- Average Marks

SELECT AVG(Marks) AS AvgMarks FROM Students;

-- Count of students

SELECT COUNT(*) AS TotalStudents FROM Students;

6. String Function

-- Concatenate FirstName and LastName

SELECT FirstName || ' ' || LastName AS FullName FROM Students;

7. Numeric Function
-- Round Marks

SELECT StudentID, ROUND(Marks/10)*10 AS RoundedMarks FROM Students;

8. Using Set Operators

-- Union example (assuming another table PassedStudents exists)

SELECT StudentID, FirstName FROM Students

UNION

SELECT StudentID, FirstName FROM PassedStudents;

9. Using LIKE and Wildcards

SELECT * FROM Students

WHERE FirstName LIKE 'A%'; -- Names starting with A

10. Using IN / BETWEEN

SELECT * FROM Students

WHERE Marks BETWEEN 80 AND 100;

SELECT * FROM Students

WHERE StudentID IN (1, 2, 3);

You might also like