22331A05I7 DBMSlab
22331A05I7 DBMSlab
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:
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;
}
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),…..);
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.
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.
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.
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>;
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>
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,....);
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:
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
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.
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.
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:
Output:
observations:
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.