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

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

DBMS Lab Term Work 1

The document provides an overview of database commands, categorizing them into DDL, DML, and DQL. It details the syntax and examples for commands such as CREATE, ALTER, INSERT, UPDATE, DELETE, and SELECT. Additionally, it includes a term work section with a task to create an 'Employees' table and perform various data retrieval queries.

Uploaded by

spdxpace
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)
19 views5 pages

DBMS Lab Term Work 1

The document provides an overview of database commands, categorizing them into DDL, DML, and DQL. It details the syntax and examples for commands such as CREATE, ALTER, INSERT, UPDATE, DELETE, and SELECT. Additionally, it includes a term work section with a task to create an 'Employees' table and perform various data retrieval queries.

Uploaded by

spdxpace
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/ 5

Theory and Concept

Lab 1

1. DDL Command

Definition: - DDL commands define and modify the structure of database objects like tables, indexes, and
views. These commands primarily affect the schema and do not deal with data manipulation.s

Command Description
CREATE Creates a new database, table, index, or view.
ALTER Modifies an existing database object (add, modify, or delete columns).
DROP Deletes a database object permanently (table, database, index).
TRUNCATE Removes all records from a table but keeps the structure.
RENAME Renames an existing database object.

• CREATE: - CREATE Command is used to create a new database, table, view, or index.
Syntax-

CREATE TABLE table_name (


column1 datatype constraints,
column2 datatype constraints,
...
);

Example-

CREATE TABLE Students (


ID NUMBER(10) PRIMARY KEY,
Name VARCHAR2(50) NOT NULL,
Age NUMBER(10),
Course VARCHAR2(50)
);

• ALTER: - The ALTER command is used to modify an existing database object, such as adding,
modifying, or dropping a column.
Syntax:

1. ALTER TABLE table_name ADD column_name datatype;

2. ALTER TABLE table_name MODIFY column_name datatype;

3. ALTER TABLE table_name DROP COLUMN column_name;


Example:

1. ALTER TABLE Students ADD Email VARCHAR2(100);

2. ALTER TABLE Students MODIFY Age NUMBER(10) NOT NULL;

3. ALTER TABLE Students DROP COLUMN Course;

• RENAME Command: - The RENAME command is used to rename an existing database object.
Syntax:

ALTER TABLE old_table_name RENAME TO new_table_name;

Example:

ALTER TABLE Students RENAME TO Student_Details;

• TRUNCATE Command: - The TRUNCATE command is used to remove all records from a table
while keeping its structure intact.
Syntax:

TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE Students;

• DROP Command: - The DROP command is used to delete a database object permanently.
Syntax:

DROP TABLE table_name;

Example:

DROP TABLE Students;

2. DML Command
Definition: - DML (Data Manipulation Language) commands are used to interact with and manipulate
data stored in a database. These commands help in inserting, updating, deleting, and retrieving records
from database tables.
Types of DML Commands
1. INSERT – Adds new records to a table.
2. UPDATE – Modifies existing records in a table.
3. DELETE – Removes records from a table.

• INSERT Command: - The INSERT command is used to add new rows (records) to a table.
Syntax:

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Example:
(Inserting Single line record)
INSERT INTO Students (ID, Name, Age, Course) VALUES (1, 'John Doe', 20, 'Computer Science');

(Inserting Multiple line records)


INSERT INTO Students (ID, Name, Age, Course)
VALUES
(2, 'Jane Doe', 22, 'Electronics'),
(3, 'Mike Smith', 21, 'Mechanical');

• UPDATE Command: - The UPDATE command is used to modify existing records in a table.
Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example:
(Updating Single column)

UPDATE Students SET Age = 21


WHERE ID = 1;

(Updating Multiple columns)


UPDATE Students SET Age = 23, Course = 'Data Science'
WHERE Name = 'Jane Doe';

• DELETE Command: - The DELETE command is used to remove records from a table.
Syntax:

DELETE FROM table_name WHERE condition;


Example:
DELETE FROM Students WHERE ID = 2;

Warning: If you do not use the WHERE clause, all records in the table will be deleted!

DELETE FROM Students;

3. DQL Command
Definition: DQL (Data Query Language) consists of commands used to retrieve data from a database.
The primary command in DQL is SELECT, which allows users to fetch records based on different
conditions.

• SELECT Command: - The SELECT command is used to retrieve data from one or more tables in a
database.
Syntax:

SELECT column1, column2, ...


FROM table_name
WHERE condition;

column1, column2, ...: Specifies the columns to be retrieved.


table_name: The table from which data is retrieved.
WHERE condition (optional): Filters records based on a condition.

Example:

1. Retrieve All Columns

SELECT * FROM Students;

2. Retrieve Specific Columns

SELECT Name, Age FROM Students;

3. Use WHERE Clause (Filtering Data)

SELECT * FROM Students WHERE Age > 20;

4. Using ORDER BY (Sorting Data)

SELECT * FROM Students ORDER BY Name ASC; (For Ascending order)


SELECT * FROM Students ORDER BY Age DESC; (For descending order)

5. Using DISTINCT (Avoid Duplicates)


SELECT DISTINCT Course FROM Students;

Term Work-1

Create the following table “Employees”

Column Name Datatype Size

emp_id (PRIMARY KEY) NUMBER 10


emp_name (NOT NULL) VARCHAR2 20
salary NUMBER 10,2
dept_id NUMBER 10
Join_date DATE

Insert the following data in the Employees table

emp_id emp_name salary dept_id join_date


101 Alice Johnson 50000 1 15-JAN-23
102 Bob Smith 60000 2 10-JUN-22
103 Charlie Brown 70000 3 25-SEP-21
104 David Williams 55000 1 05-DEC-22
105 Eve Carter 65000 2 20-MAR-23

Q. Based on above two tables answer the following Questionaries: -

1. Retrieves all records from the employee’s table.


2. Retrieves only employee names and their salaries.
3. Retrieves employees who have a salary greater than 60,000.
4. Retrieves employees in department 1 with a salary greater than 50,000.
5. Retrieves unique department IDs from the employee’s table.
6. Retrieves all employees sorted by salary in ascending order.
7. Retrieves all employees sorted by joining date in descending order.
8. Retrieves employees who either belong to department 1 or have a salary greater than 60,000.
9. Sorts employees first by dept_id in ascending order and then by salary in descending order within each
department.
10. Retrieves employees whose salaries are between 55,000 and 70,000.
11. Retrieves employees who belong to department 1 or 3.

You might also like