PROGRAM -1
AIM:-Create a database and write the program to carry out the
following operations:-
1. Add a record in the database.
2. Delete a record in the database.
3. Modify the record in the database.
4. Generate queries.
5. Data operations.
6. List all the records of database.
DATABASE:-
It is a coherect collection of data with some inherent meaning
designed, built and populated with data for a specific purpose.A database store data
that is useful to us. For processing data in a database we use the Structure query
language (SQL).
CREATING A DATABASE:-
We will create a table as a student database. For creating a table we use the
CREATE TABLE command.
Syntax:-
CREATE TABLE tablename
(columnname datatype(size),columnname datatype(size));
Example:-
SQL> Create table student
2 (name varchar(10),rollno number(10),branch varchar(10),address
varchar(15));
Table created.
Result:-
Name Null? Type
----------------------------------------- -------- --------------
NAME varchar2(10)
ROLLNO NUMBER(10)
BRANCH VARCHAR2(10)
ADDRESS VARCHAR2(15)
ADDING RECORDS IN THE DATABASE
We use the INSERT command for adding the data into the table. We can
insert the data into a table by two ways.
A) By inserting the record one by one into the table.
Syntax:-
INSERT INTO tablename
(columnname,columnname)
VALUES(exporession,expression);
Example:-
SQL> insert into student
2 (name,rollno,branch,address)
3 values('puneet',1406183,'cse','kkr');
1 row created.
B) By inserting the record continuously.
Syntax:-
INSERT INTO tablename
VALUES(‘&coulumnname’,’&columnname’)
Example:-
SQL> insert into student
2 values('&name',&rollno,'&branch','&address');
Enter value for name: sumit
Enter value for rollno: 1406189
Enter value for branch: cse
Enter value for address: ambala
old 2: values('&name',&rollno,'&branch','&address')
new 2: values('sumit',1406189,'cse','ambala')
1 row created.
After this If you want to insert more record then use backslash( / )
SQL> /
Enter value for name: tusar
Enter value for rollno: 1406333
Enter value for branch: mech
Enter value for address: chd
old 2: values('&name','&rollno','&branch','&address')
new 2: values('tusar','1406333','mech','chd')
1 row created.
From this type we can insert more records. At last we will get the table given
below.
Result:-
NAME ROLLNO BRANCH ADDRESS
--------------------------- --------------------- ---------------------- -------------------------
puneet 1406183 cse kkr
tusar 1406333 mech chd
rahul 1406188 cse karnal
sumit 1406189 cse ambala
sunil 1406190 mech ynr
sahil 1406191 cse karnal
sanjay 1406192 ece kkr
DELETING RECORD IN THE DATABASE
The verb DELETE in SQL is used to remove rows from table. we can delete
data by two ways:-
A) Removal of all rows:-
Syntax:-
DELETE FROM tablename;
Example:-
SQL> delete from student;
7 rows deleted.
Result:-
NAME ROLLNO BRANCH ADDRESS
-------------------------- -------------------- --------------------- --------------------------
0 rows selected.
B) Removal of a specified rows:-
Syntax:-
DELETE FROM tablename WHERE search condition;
Example:-
SQL> delete from student where rollno>1406200;
1 rows deleted.
Result:-
NAME ROLLNO BRANCH ADDRESS
--------------------------------------------------------------------------------------
puneet 1406183 cse kkr
rahul 1406188 cse karnal
sumit 1406189 cse ambala
sunil 1406190 mech ynr
sahil 1406191 cse karnal
sanjay 1406192 ece kkr
from this table tusar record has been deleted.
MODIFYING THE STRCTURE OF THE DATABASE
We use the ALTER TABLE command for modifying the structure of a table
in the SQL. This can be done in two ways :-
A) Adding new columns:
Syntax:-
ALTER TABLE tablename
ADD(newcolumnname datatype(size),newcolumnname datatype(size)….);
Example:-
SQL> alter table student
2 add(marks number(5));
Table altered.
Result:-
Name Null? Type
----------------------------------------- -------- -------------
NAME VARCHAR2(10)
ROLLNO NUMBER(10)
BRANCH VARCHAR2(10)
ADDRESS VARCHAR2(15)
MARKS NUMBER(5)
A new column Marks has been add.
B) Modifying existing column:-
Syntax:-
ALTER TABLE tablename
MODIFY (columnname newdatatype(newsize));
Example:-
SQL> alter table student
2 modify(marks number(10));
Table altered.
Result:-
Name Null? Type
----------------------------------------- -------- ------------
NAME VARCHAR2(10)
ROLLNO NUMBER(10)
BRANCH VARCHAR2(10)
ADDRESS VARCHAR2(15)
MARKS NUMBER(10)
We can note that the size of marks column has been modify.
MODIFYING THE RECORD IN THE DATABASE
We also use the UPDATE command for modifying data values in the
table.we can modify the table from two type:-
A) Modifying all the rows from a table.
Syntax:-
UPDATE tablename
SET columnname=expression,columnname=expression;
Example:-
SQL> update student
2 set marks='60';
Result:-
NAME ROLLNO BRANCH ADDRESS MARKS
---------- ---------- ---------- --------------- ----------------- --------------------
puneet 1406183 cse kkr 60
rahul 1406188 cse karnal 60
sumit 1406189 cse ambala 60
sunil 1406190 mech ynr 60
sahil 1406191 cse karnal 60
sanjay 1406192 ece kkr 60
The marks of all the student have been modify into 60.
B) Modifying records conditionally:-
Syntax:-
UPDATE tablename
SET columnname = expression, columnname = expression…
WHERE columnname = expression;
Example:-
SQL> update student
2 set marks=90
3 where branch='cse';
4 rows updated.
Result:-
NAME ROLLNO BRANCH ADDRESS MARKS
---------- ---------- ---------- --------------- -----------------------------
puneet 1406183 cse kkr 90
rahul 1406188 cse karnal 90
sumit 1406189 cse ambala 90
sunil 1406190 mech ynr 60
sahil 1406191 cse karnal 90
sanjay 1406192 ece kkr 60
GENERATE QUERIES
We can queries our data in the table by using SELECT command .we can
view data in the table from various ways:-
A) Viewing all rows and all column.
Syntax:-
SELECT * FROM tablename;
Example:-
SQL> select * from student;
NAME ROLLNO BRANCH ADDRESS MARKS
---------- ---------- ---------- --------------- ------------------------------
puneet 1406183 cse kkr 90
rahul 1406188 cse karnal 90
sumit 1406189 cse ambala 90
sunil 1406190 mech ynr 60
sahil 1406191 cse karnal 90
sanjay 1406192 ece kkr 60
6 rows selected.
B) Viewing selected rows and all columns
Syntax:-
SELECT * FROM tablename
WHERE search condition;
Example:-
SQL> select * from student
2 where rollno<1406190;
NAME ROLLNO BRANCH ADDRESS MARKS
---------- ---------- ---------- --------------- ----------
puneet 1406183 cse kkr 90
rahul 1406188 cse karnal 90
sumit 1406189 cse ambala 90
C) Viewing selected coulumns and selected rows
Syntax:-
SELECT columnname,columnname
FROM tablename
WHERE search condition;
Example:-
SQL> select name,address
2 from student
3 where marks=90;
NAME ADDRESS
---------- ---------------
puneet kkr
rahul karnal
sumit ambala
sahil karnal
D) Viewing elimination of duplicates records
Syntax:-
SELECT DISTINCT columnname,columnname
FROM tablename;
Example:-
SQL> select distinct * from student;
NAME ROLLNO BRANCH ADDRESS MARKS
---------- ---------- -------------------------------- --------------
puneet 1406183 cse kkr 90
rahul 1406188 cse karnal 90
sahil 1406191 cse karnal 90
sanjay 1406192 ece kkr 60
sumit 1406189 cse ambala 90
sunil 1406190 mech ynr 60
6 rows selected.
Because no record is duplicate so all record are display.
DATA OPERATIONS
We can perfrom various operations on the data such as given below:-
A) finding maximum value in table
Syntax:-
SELECT MAX(columnname)”expr” FROM tablename;
Example:-
SQL> select max(marks)"maximum_marks"
2 from student;
Result:- maximum_marks
-------------
90
B) Finding minimum value in table
Syntax:-
SELECT MIN(columnname)"expr" FROM tablename;
Example:-
SQL> select min(marks) "minimum" from student;
Result:- minimum
----------
60
C) Finding average value from a table
Syntax:-
SELECT AVG(columnname)"expr" FROM tablename;
Example:-
SQL> select avg(marks)"average" from student;
Result:- average
----------
80
this will ignore null values
D) Finding sum of values in the table
Syntax:-
SELECT SUM(columnname)"expr" FROM tablename;
Example:-
SQL> select sum(marks)"sum_marks" from student;
sum_marks
----------
480
LIST ALL RECORDS IN ORDER
We can arrange records in the table in ascending and descending order.
A) Records in ascending order
Syntax:-
SELECT * FROM tablename ORDER BY columnname,columnname[sort
order];
Example:-
SQL> select * from student
2 order by rollno;
NAME ROLLNO BRANCH ADDRESS MARKS
---------- ---------- ---------- --------------- ----------
puneet 1406183 cse kkr 90
rahul 1406188 cse karnal 90
sumit 1406189 cse ambala 90
sunil 1406190 mech ynr 60
sahil 1406191 cse karnal 90
sanjay 1406192 ece kkr 60
6 rows selected.
B) records in descending order
Syntax:-
SELECT * FROM tablename ORDER BY columnname DESC;
Example:-
SQL> select * from student order by rollno desc;
NAME ROLLNO BRANCH ADDRESS MARKS
---------- ---------- ---------- --------------- ----------
sanjay 1406192 ece kkr 60
sahil 1406191 cse karnal 90
sunil 1406190 mech ynr 60
sumit 1406189 cse ambala 90
rahul 1406188 cse karnal 90
puneet 1406183 cse kkr 90
6 rows selected.