dbms
dbms
Engineering
Course Name: Database Management
Systems Lab
Course Code: 13020441
Submitted to:
Submitted by:
Dr. Jyoti Godara
Name: Divyajeet
Assistant Professor
Reg No. 221302105
o DDL changes the structure of the table like creating a table, deleting a
table, altering a table, etc.
o All the command of DDL are auto-committed that means it permanently
save all the changes in the database.
o CREATE
o ALTER
o DROP
o TRUNCATE
o INSERT
o UPDATE
o DELETE
o Grant
o Revoke
TCL commands can only use with DML commands like INSERT, DELETE
and UPDATE only.
These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.
o COMMIT
o ROLLBACK
o SAVEPOINT
It deals with small quantity of data. It deals with large amount of data.
Data redundancy is common in this Keys and indexes do not allow Data
model. redundancy.
Data fetching is slower for the large Data fetching is fast because of
amount of data. relational approach.
Query:
create table client_master(
Client_No varchar(6),
Name varchar(20),
City varchar(15),
Pincode number(8),
State varchar(15),
Balance number(10,2));
Output:
Query:
Insert into client_master values(‘C00001’, ‘Ivan Bayross’, ‘Mumbai’, 400054,
‘Maharashtra’, 15000);
Insert into client_master values(‘C00002’, ‘Vandana’, ‘Madras’, 780001, ‘Tamil
Nadu’, 0);
Insert into client_master values(‘C00003’, ‘Pramada’, ‘Mumbai’, 400057,
‘Maharashtra’, 5000);
Insert into client_master values(‘C00004, ‘Sachin’, ‘Mumbai’, 400056,
‘Maharashtra’, 0);
Insert into client_master values(‘C00005’, ‘Ravi’, ‘Delhi’, 100001, null, 2000);
Insert into client_master values(‘C00006’, ‘Rukmini’, ‘Madras’, 400050, ‘Tamil
Nadu’, 0);
Output:
Query:
Desc client_master;
Output:
4. Show all records of client_master.
Query:
Output:
Query:
Output:
6. Delete record of client C00006.
Query:
Output:
Query:
Output:
Query:
Select * from client_master where city = ‘Mumbai’ or city = ‘Madras’;
Output:
Query:
Output:
Query:
Output:
11.Display client_no, name, balance of clients staying in Mumbai.
Query:
Output:
Query:
Output:
Query:
Output:
14.Find out the names of all the clients.
Query:
Output:
Ans. Oracle Database offers a rich set of data types to store different types of
data efficiently. Here's an explanation of various data types available in
Oracle:
- **DATE**: Stores date and time values with a precision of one second.
- **BLOB**: Binary Large Object for storing large binary data like
images or documents.
- **CLOB**: Character Large Object for storing large character data like
text documents.
Description Varchar2 15
Profit Number 4,2
Quantity Number 8
Sell_price Number 8,2
Cost_price Number 8,2
Query:
Create table Product(
Product_no varchar(6),
Description varchar(15),
Profit number(4,2),
Quantity number(8),
Sell_price number(8,2),
Cost_price number(8,2));
Output:
2. Insert data in Product as given in table below.
Query:
Alter table Product
Add Category varchar(20);
Output:
4. Insert category value as ‘Input’ or ‘Storage’ or ‘Output’
Query:
Update Product set category = ‘Input’ where Description = ‘Mouse’ or
Description = ‘Keyboards’;
Update Product set category = ‘Storage’ where Description = ‘Hard Disk’ or
Description = ‘Pen Drive’;
Update Product set category = ‘Output’ where Description = ‘Monitors’;
Output:
Query:
Alter table Product
Modify Description varchar(20);
Output:
Query:
Select * from Product;
Output:
7. Show all products whose selling price is less than 1000.
Query:
Select * from Product where sell_price < 1000;
Output:
8. List all records from product table for products whose description
second character is either ‘a’ and ‘e’.
Query:
Select * from Product where Description like(‘_a%’) or Description like(‘_e%);
Output:
9. List all products having selling price between 2000 and 4000 and cost
price is greater than 1000.
Query:
Select * from Product where sell_price between 2000 and 4000 and cost_price >
1000;
Output:
Practical 4: Working with constraints.
1. Define constraints. Discuss all constraints in SQL.
Ans. SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the table. If there is any violation between the
constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a
column, and table level constraints apply to the whole table.
The following constraints are commonly used in SQL:
NOT NULL => Ensures that a column cannot have a NULL value.
UNIQUE => Ensures that all values in a column are different.
PRIMARY KEY => A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table.
FOREIGN KEY => Prevents actions that would links between tables.
CHECK => Ensures that eh values in a column satisfies a specific condition
DEFAULT => Sets a default value for a column if no value is specified
2. Explain difference between following:
Unique and Primary Key Constraint
Table Level and Column Level Constraints
Not Null and Primary Key Constraint
Ans.
Differences:
Unique and Primary Key Constraint:
Unique Constraint: Ensures that all values in a column are unique. Multiple columns
can have unique constraints, and NULL values are allowed.
Primary Key Constraint: A special case of the unique constraint. It uniquely identifies
each record in a table and does not allow NULL values.
Table Level and Column Level Constraints:
Table Level Constraints: Apply to the entire table and can reference multiple columns
in the table.
Column Level Constraints: Apply only to the column in which they are defined.
Not Null and Primary Key Constraint:
NOT NULL Constraint: Ensures that a column cannot have NULL values.
Primary Key Constraint: Ensures the uniqueness of a column or a set of columns and
also implies that the column(s) cannot have NULL values.
Query:
Create table New_Prod_master(
Product_no varchar(6) Check(Product_no like ‘P%’) Primary Key,
Description varchar(15) Not Null,
Profit number(4,2) Not Null,
Quantity number(8) Not Null,
Sell_price number(8,2) Not Null Check(Sell_price <>0),
Cost_price number(8,2) Not Null Check(Cost_price <>0));
Output:
Query:
Insert into New_Prod_master (Product_no, Description, Profit, Quantity, Sell_price,
Cost_price)
Values
(‘P00001’, ‘Hard Disk’, 5, 100, 4500, 4000),
(‘P03453’, ‘Monitors’, 6, 10, 12000, 11280),
(‘P06734’, ‘Mouse’, 5, 20, 1050, 1000),
(‘P07865’,’Pen Drive’, 5, 100,345,325),
(‘P07868’, ‘Keyboards’, 2,10,3150,3050);
Output:
Query:
Output:
6. Change the cost price of pen drive to 370 and quantity to 200.
Query:
Update New_Prod_master set Cost_price = 370, Quantity = 200 where Description = ‘Pen
Drive’;
Output:
Query:
Create table Persons(
PersonId int(2) Primary Key,
LastName Varchar(15) Not Null,
FirstName Varchar(15) Not Null,
Age int(2) Not Null);
Output:
Query:
Insert into Persons (PersonId, LastName, FirstName, Age)
Values
(1, ‘Hansen’, ‘Ola’, 30),
(2, ‘Syendson’, ‘Tove’, 23),
(3, ‘Pettersen’, ‘Kari’, 20);
Output:
Query:
Create table Orders(
OrderId int(2) Primary Key,
OrderNumber int(6) Not Null,
PersonId int(2),
Item Varchar(20) Check(Item In (‘cotton’, ‘scissor’, ‘paper’, ‘tape’)),
Foreign Key (PersonId) References Persons(PersonId));
Output:
Orders Table
Query:
Insert into Orders (OrderId, OrderNumber, Item, PersonId)
Values
(1, 77895, ‘cotton’, 3),
(2, 44678, ‘cotton’, 3),
(3, 22456, ‘paper’, 2),
(4, 24562, ‘tape’, 1),
(5, 50621, ‘scissor’, Null));
Output:
7. Insert new record in Orders table with values - OrderID - 6, OrderNumber -45673, Item
-tape , PersonID -2.
Query:
Insert into Orders (OrderId, OrderNumber, Item, PersonId)
Values(6,45673, ‘tape’, 2);
Output:
8. Give one example of Alter Table command for implementation of Primary Key, Not null
and Foreign Key.
Implementation of Primary Key using alter table command
2. Discuss use and syntax all aggregate functions, five string and date functions.
Aggregate Functions:
1) Count(): Counts the number of rows in a specified column or all rows in a table.
Syntax: ‘Count(expression)’ or ‘Count(*)’ for all rows.
2) Sum(): Calculates the sum of values in a specified column.
Syntax: ‘Sum(expression)’
3) Avg(): Calculates the average of values in a specified column.
Syntax: ‘Avg(expression)’
4) Min(): Finds the minimum value in a specified column.
Syntax: ‘Min(expression)’
5) Max(): Finds the maximum value in a specified column.
Syntax: ‘Max(expression)’
String Functions:
1) Concat(): Concatenates two or more strings into one.
Syntax: ‘Concat(string1, string2, ….)’
2) Upper(): Converts a string to uppercase.
Syntax: ‘Upper(string)’
3) Lower(): Converts a string to lowercase.
Syntax: ‘Lower(string)’
4) Length(): Returns the length of a string.
Syntax: ‘Length(string)’
5) SubString(): Extracts a substring from a string.
Syntax: ‘SubString(string, start, length)’
Date Functions:
1) Current_date(): Returns the current date.
Syntax: ‘Current_date()’
2) Current_time(): Returns the current time.
Syntax: ‘Current_time()’
3) Current_timestamp(): Returns the current timestamp.
Syntax: ‘Current_timestamp()’
4) Date_Add(): Adds a specified interval to a date.
Syntax: ‘Date_add(date, Interval value unit)’
5) Datediff(): Calculates the difference between two dates.
Syntax: ‘Datediff(end_date, start_date)’
Query:
create table Orders(
OrderId int,
ProductName varchar(30),
OrderDate date);
Query:
Create table Product_Spend(
Category varchar(20),
Product varchar(30),
User_id int,
Spend decimal(10,2),
Transaction_Date datetime
);