Practical 1: - Creating Database
Practical 1: - Creating Database
• Creating database
SQL command:
SQL> create database school;
Output:
Query OK, 1 row affected.
• Using database
SQL command:
SQL> use school;
Output:
Database changed.
1
Practical 2
SQL command:
SQL> create table student(name varchar(23),roll_no
number(12),class varchar2(12),address varchar(23));
Output:
Table created.
2
Practical 3
SQL Command:
SQL> describe employees;
Output :
Name Null? Type
EMP_ID NUMBER(5)
EMP_NAME VARCHAR2(20)
DEPT_ID NUMBER(10)
DEPT_NAME NAME(12)
SALARY NUMBER(21)
3
Practical 4
SQL Command:
SQL> insert into
student(name,roll_no,class,address)values(‘Prabhat',06,'BCA',Hat
limore');
Output:
1 row created.
Or
SQL Command:
1 row created.
Output:
1 row create.
4
Practical 5
SQL Command:
SQL> select * from student;
Output:
NAME ROLL_NO CLASS ADDRESS
5
Practical 6
• Renaming table:
SQL Command:
SQL> rename student to candidates;
Output:
Table renamed.
SQL Command:
SQL> Drop table student;
SQL Command:
SQL> truncate table employees;
6
Practical 7
SQL command:
SQL> Select DISTINCT * from student;
Output:
NAME ROLL_NO CLASS ADDRESS
SQL command:
SQL> SELECT * FROM STUDENT ORDER BY name;
Output:
NAME ROLL_NO CLASS ADDRESS
Atinder 04 BCA
sawanchakDushyant 34 BCA
jagatpur
7
Practical 8
Dropping a column
alter table student drop column name;
SQL command:
SQL> create table student name varchar2(12), roll_no number(12)
primary key, class varchar2(21) NOT NULL, dob date);
or
SQL> create table student(name varchar2(12), roll_no number(12)
constraint pk_roll primary key ,class varchar2(21) not null,
dob date);
8
Practical 9
• Key constraints
Foreign key
SQL Command:
SQL> create table department(dept_no number(10)
primary key,dept_name varchar2(25),dept_loc
char(5,e_no number(11), foreign key(e_no) references
employee (e_no);
Primary key
SQL Command:
SQL> create table student1(roll_nonumber(12)primary
key,dobdate,name varchar2(20),class
varchar2(2),e_mail varchar2(20));
9
Practical 10
• “Count” command
SQL Command:
SQL> select count(*) from employees;
COUNT(*)
----------
• Sum function
SQL Command:
SQL> select sum (salary) from employees;
SUM(SALARY)
-----------
295000
• MAX function
SQL Command:
SQL> select max(salary) from employees;
MAX(SALARY)
-----------
75000
10
• MIN function
SQL Command:
SQL> select min (salary) from employees;
MIN(SALARY)
-----------
55000
• AVG function
SQL Command:
SQL> select avg(salary) from employees;
AVG(SALARY)
-----------
59000
• LIKE operator
SQL Command:
SQL> select emp_id,name,dept_id,salary from employees where
name like 'a%';
3 anku 4 55000
11
• IN operator
SQL Command:
SQL> select emp_id,name,dept_id,salary from employees where
dept_id in(20,22);
2 sonu 22 55000
3 panku 22 75000
• BETWEEN operator
SQL Command:
SQL> select emp_id,name,dept_id,salary from employees where
dept_id between 22 and 30;
2 sonu 22 55000
3 panku 22 75000
12
Practical 11
Database developers involved in the task of designing a database have to translate real world
data into relational data, i.e. data organized in the form of tables. First they have to understand
the data, then represent it in a design view and then translate into a RDBMS. One of the
techniques that is great to use is the E-R diagram. Most of the developers who are involved in
data base systems might already be familiar with it or atleast heard about it. I am going to try to
briefly explain the concept and give an example to understand it.
This model was first introduced by Dr.Peter Chen in 1976 in a paper titled "The Entity-
Relationship Model- Toward a Unified View of Data". The most useful thing in this model is, it
allows us to represent data in the form of a diagram popularly known as E-R diagram and from
this diagram we can map the data into a relational schema. First I will try to informally define
some terms used in this model.
In the E-R model all the above listed terms are represented in a diagrammatic technique known
as the E-R diagram.
Properties or attributes of an entity are shown in ellipses and are attached to their respective
entity by a single solid line. In this diagram I am showing properties for only student entity for
the sake of clarity of the diagram.
The relationship between entities are shown as diamonds and the entities which are a part of the
relationship are connected to the diamond by a solid line labeled either '1' or 'M' indicating
whether the relationship is one-to-many, one-to-one or many-to-many.
All the regular entities represented by a rectangle can be translated into base tables.
There is a 1-M relationship between University and College and 1-1 relationship between Dean
and College. So primary key in table University will be a foreign key in table College and
primary key in table Dean will be foreign key in table College. The rest of the tables also follow
the same pattern.
13
14
Table - University
Table - College
Table - Dean
Table - Department
Table - Professor
PID (primary key) int
Department ( foreign key references DID in Department table) int
Name varchar (20)
15
Table - Course
Table - Section
Table - Student
There is only one many-to-many relationship in the above diagram and that is between section
and student. That means a student can register for many sections and a section has many
students. To establish this relationship we will create a new table called Student_Registration.
Table - Student_Registration
16
Practical 12
17
Hospital
HCity int
HAddress varchar
Hos-Name Varchar
Patient
PName varchar
PAddress varchar
PDiagnosis varchar
18
Pat-id Primary Key
Medical Record
Problem varchar
Date_of_examination date
Doctor
DName varchar
Qualification varchar
19
Doc-id Primary Key
Salary int
20