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

22331A05I7 DBMSlab

Uploaded by

ankitarath900
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)
21 views13 pages

22331A05I7 DBMSlab

Uploaded by

ankitarath900
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

WEEK-1

Aim :
1.To demonstrate the creation of a C program that reads STUDENT records from the file and
shows the fields as selected by the user as output. Analyze the problems of maintaining such
data in a file.
Description:
Data Redundancy and Inconsistency: Duplicate data can lead to inconsistencies, and there’s no
inherent mechanism to maintain data integrity across files.
Data Retrieval: Accessing data in files can be challenging due to complex file structures,
limited metadata, and potential fragmentation, which can slow down retrieval and increase
management complexity.
Security issues: File systems often have basic security mechanisms that may be insufficient for
protecting sensitive data from unauthorized access and breaches.
Language, Libraries, Methods & Variables Used:

• void insert(): Adds a new student


record to student.txt.
• void display(): Reads and displays all
student records from student.txt.
• void delete(): Deletes a specific Variables:
student record from student.txt based FILE *fp, FILE *fpt, char name[30], int id,
on the ID. int marks, int del_id, int found, int choice.
• int main(): The program's entry point
provides a menu for the user to choose
an action.

Program:
#include<stdio.h>
#include<stdlib.h>
void insert()
{
FILE *fp;
char name[30];
int id, marks;
fp = fopen("student.txt", "a");
if (fp == NULL)
{
printf("Error opening file.\n");
return;
}
printf("Enter student ID: ");
scanf("%d", &id);
printf("Enter student name: ");
scanf("%s", name);
printf("Enter marks: ");
scanf("%d", &marks);
fprintf(fp, "%d\t%s\t\t%d\n", id, name, marks);
fclose(fp);
}

void display()
{
FILE *fp;
char name[30];
int id, marks;
fp = fopen("student.txt", "r");
if (fp == NULL)
{
printf("Error opening file.\n");
return;
}
printf("Student Records:\n");
while (fscanf(fp, "%d %s %d", &id, name, &marks) != EOF)
{
printf("Student ID: %d\n", id);
printf("Student name: %s\n", name);
printf("Marks: %d\n", marks);
printf("\n");
}
fclose(fp);
}
void delete()
{
FILE *fp, *fpt;
char name[30];
int id, marks, del_id;
fp = fopen("student.txt", "r");
fpt = fopen("Duplicate.txt", "w");
if (fp == NULL || fpt == NULL)
{
printf("Error opening file.\n");
return;
}

printf("Enter the ID of the student to be deleted: ");


scanf("%d", &del_id);
int found = 0;
while (fscanf(fp, "%d %s %d", &id, name, &marks) != EOF)
{
if (id == del_id)
{
found = 1;
continue;
}
fprintf(fpt, "%d\t%s\t\t%d\n", id, name, marks);
}
fclose(fp);
fclose(fpt);
if (found)
{
remove("student.txt");
rename("Duplicate.txt", "student.txt");
printf("Record deleted successfully.\n");
} else
{
remove("Duplicate.txt");
printf("Record not found.\n");
}
}
int main()
{
int choice;
while (1)
{
printf("\n1. Insert student record\n");
printf("2. Display student records\n");
printf("3. Delete student record\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
delete();
break;
case 4:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
Output:

----

Observation(s):

• The program uses file handling to store, retrieve, and manage student records. fopen is
used to open files in different modes:
• ‘a’:(append mode) in insert() to add new records to the end of the file without altering
the existing content.
• ‘r’ (read mode) in display() to read and display all records.
• ‘r’,’w’ : (write mode) in delete() to read records from student.txt and write to
duplicate.txt, respectively.
• The program checks if files are opened successfully using “if(fp==NULL)” and prints
an error message if the file operation fails. This is crucial to ensure that the program
does not proceed with file operations if the file cannot be opened. The code asks the
user to enter details for five employees, including ID, name, department, and salary. It
then writes these details to a file named "employee details.txt" using the fprintf
function.
• The program ensures that all opened files are properly closed using “fclose(fp)” to
prevent memory leaks and file corruption.
Signature of the Lab Faculty:
Date:

WEEK-2
Aim :
2. Demonstrate usage of DDL and DML commands.

Command:1 create
In SQL, the `CREATE` statement is used to
define and establish new database objects
Description : such as tables, views, indexes, or databases
themselves, specifying their structure and
attributes.
create table <tablename> (column1
Syntax :
datatype(size),column2 datatype(size),…..);

Create a table student with attributes:


sno,sname,marks,age

Experiment: create table Student(sno number(20),sname


varchar(20),marks number(20),age
number(20));
Desc Student;
Output :

Observations :
• This query creates a new table named Student with four
columns:sno,sname,marks,age .
• The subsequent command “Desc Student” will display the structure of the Student
table, including column names, data types, and their sizes.

Command:2 Alter add


add new columns or constraints to an existing
Description : table, modifying its structure without
affecting existing data.
Alter table<table name>add(column1
Syntax : datatype(size),column2 datatype(size),...)

Add email to the existing table


Experiment:
alter table Student add(email varchar2(20));
desc Student
Output :

Observations :
• This query used To make changes to the existing table, we use alter.
• The new email column has a data type of varchar2 with a maximum length of 20
characters, suitable for storing email addresses has been added using alter+add.

Command:3 Alter + modify


Used to change data type/data type size for
Description :
the existing table.
alter table<table name>modify(column1
Syntax :
datatype(size),....)

Change the size of the datatype and change


the data type for an existing table
Experiment:
alter table Student modify(age
varchar2(30),marks float(20));
desc Student;
Output :

Observations :
• Here we changed the datatype of marks from number to float using the modify
command
• Here we also changed the size of the datatype of age from 20 to 30.

Command: Alter + drop


Description : Deletes rows/columns in the existing table
Alter table<table name>drop(col1
Syntax :
datatype(size),......)

Add column name city and delete it from


the table.

Experiment-1: Alter table Student add(city varchar2(20));


Desc Student;
Alter table Student drop column city;
Desc Student;
Output :

Observations:
• By using alter drop we can delete a column that the user wants.
• Here we added the column city and we deleted it later by using the alter drop
command, output shows the add and drop of the city column.
Command: Alter rename
Description : Used to change the name of the column
alter table <tablename> rename column <old
Syntax :
name> to <new name>;

Change the column name email to mail

Experiment: alter table Student rename column email to


mail;
Desc Student;
Output :

Observations:
• Here this command is used to rename the existing column name.
• We are displaying it using desc command
• As we can see from the output email has been renamed as mail.

Command: Rename
Description : Used to change the name of the table
Rename <old tablename> to <new
Syntax :
tablename>

Change the table name


Experiment:
rename Student to Students;
desc Students;
Output :

Observations:
• This command is used to change the name of the entire table.
• As we changed the table name from Student to Students.

Command: insert
Description : Used to insert the data into the table
insert into <tablename>
Syntax :
values(value1,value2,....);

insert into student


values(01,'sai',94,20,'[email protected]');
insert into student
values(02,'ankita',97,20,'[email protected]');
insert into student
Experiment: values(03,'shyam',96,21,'[email protected]')
;
insert into student
values(04,'pravs',95,19,'[email protected]');
Select * from Student;

Output :

Observations:
• By using this function we can insert the data into the table.
• Here we used the select command is used to retrieve data from the database, here it
retrieved data from the Student database.

Command: update
Description : Used to update the values in the table
update <tablename> set col1=newvalue where
Syntax :
(condition);
update student set sname='sri' where
Experiment: sname='sai';
Select * from Student;
Output :
Before the use of the update command:

After the use of the update command:

Observations:
• By using the update command we can update the table values in the table by specifying
column name .

Command: delete
To delete the specified row where the
Description :
condition satisfies
delete from <tablename> where (condition);
Syntax :
delete from <tablename>;#deletes entire table

delete from Student where (sname='sri');


Experiment:
select * from student
Output :
Before

Observations:
• By using the delete function we can clear the cells in the table. but still table exists.
• We can specify the condition also. It works similarly to the truncate command.

Signature of the Lab Faculty:


Date:

WEEK-3
Aim :
3.DEMONSTRATE SELECT & PREDEFINED FUNCTIONS

Command: select
Description : Used to retrieve the data from the table
select * from <tablename>;
Syntax :
select <columns> from <tablename>;
select * from employee;
Experiment1:
select ename,age from employee;
Output :

Observations:
• By using the select command we can select only the specified columns from the table.
• By using select can perform certain operations on that data
• If the specified column field is not present in the table raises error.

Command: SELECT WITH CONDITIONS


Select command can allow you to specify the
Description : condition to filter the data. And used to
retrieve the data that is specified.
select <col1,col2,.....,coln> from
Syntax :
<table_name>
Experiment-2:
select * from employee;
select ename,age,dept from employee where dept = ‘cse’;
Output :

Observations:

● By using the select command we can display the inserted data into the table.
● By using select can perform certain operations on that data and by specifying condition
through where clause we can retrieve the required data.
● here using the where condition we have retrieved data of employees who belong to cse dept
and their name and age.
Experiment-3:
NUMBER AND CHARACTER
Command:
FUNCTIONS
allow us to get information about strings and
Description : modify the contents of those strings in
multiple ways
select abs(number) from<teble name>;
select round(number, decimals) from<table
name>;
select trunc(number, decimals) from<table
name>;
select greatest(number1, number2, ...)
from<table name>;
select least(number1, number2, ...)
Syntax :
from<table name>;
select mod(number, divisor) from<table
name>;
select ceil(number) from<table name>
select floor(number) from<table name>

Experiment-1:
1.select abs(-50) from dual
2.select 50+60 from dual
3.select mod(50,8) from dual
4.select round(167.09673443) from dual
5.select trunc(1.78232,2) from dual
6.select greatest(101,670,345,234,789) from dual
7.select least(101,670,345,234,789) from dual
8.select ceil(1233.90) from dual
9.select floor(1233.90) from dual
Output :
observations:
Returns the absolute value of a given number.
● Returns the remainder of the division of the given number by the divisor. ● Rounds
the given number to the specified number of decimal places.
● Truncates the given number to the specified number of decimal places without
rounding.
● Returns the greatest value from the list of given numbers.
● Returns the smallest value from the list of given numbers.
● Returns the smallest integer greater than or equal to the given number.
● Returns the largest integer less than or equal to the given number
Experiment-4:

select upper('pravallika')from dual;


select lower('SAI SRI')from dual;
select initcap('pravallika')from dual;
select len('pravs')from dual;
select substr('pravallika',2,6)from dual;
select instr('pravallika','p')from dual;
select lpad('pravallika',7,'*')from dual;
select rpad('saisri',7,'*')from dual;
select ltrim('nnnrunning','n')from dual;
select rtrim('runningggg','g')from dual;
select trim('g' from 'running')from dual;
select replace('saritha','a','h')from dual;

Output:
observations:

● Converts the string ‘pravallika’ to uppercase.


● Converts the string 'SAISRI' to lowercase.
● Capitalizes the first letter of the string 'pravallika' and makes all other letters lowercase.
● Returns the length of the string 'PRAVS'.
● Extracts a substring starting from the 2nd character for a length of 6 characters from
'Pravallika'.
● Returns the position of the first occurrence of the substring 'S' in 'Pravallika'.
● LPads the string 'pravallika' on the left with '*’ to make the total length 7.
● RPads the string 'saisri' on the right with '*' to make the total length 6.
● Removes the characters 'n' from the beginning of the string 'nnnrunning'.
● Removes the characters 'g' from the end of the string 'runningggg'.
● Removes the character 'g' from both the beginning and end of the string 'running'.
. ● Replaces all occurrences of 'a' with 'h' in the string 'saritha'.

Experiment-5:
Command: DATE FUNCTIONS
Date functions are functions that help to
format dates and carry out date-related
Description : calculations on your data. They are usually
only effective on data types that are in date
format on SQL
select sysdate from dual;
select add_months(date, 3) from dual;
select last_day(date) from table;
select next_day(date, 'sunday') from table;
Syntax : select months_between(date1, date2) from
table;

Experiment:
select sysdate from dual;
select add_months(sysdate,5)from dual;
select last_day(sysdate)from dual;
select next_day(sysdate,'Friday')from dual;
select months_between(to_date('2024-07-25', 'YYYY-MM-DD'),to_date('2025-08-25',
'YYYY-MM-DD')) from dual;
Output :

observations:
. Retrieves the current date and time from the database server.
2. Adds 5 months to the current date and time, returning the resulting date.
3. Returns the last day of the current month.
4. Finds the date of the next Wednesday after the current date.
5. Calculates the number of months between July 25, 2024, and august 21, 2025.

You might also like