0% found this document useful (0 votes)
93 views

Practical 1: - Creating Database

The document describes creating and using a university database. It includes creating tables for students, departments, professors, colleges, and universities. Relationships between the tables are defined, such as colleges being linked to a single university and each college having a dean. The entity relationship diagram visually represents the relationships between the tables.

Uploaded by

Abhishek Mamgai
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)
93 views

Practical 1: - Creating Database

The document describes creating and using a university database. It includes creating tables for students, departments, professors, colleges, and universities. Relationships between the tables are defined, such as colleges being linked to a single university and each college having a dean. The entity relationship diagram visually represents the relationships between the tables.

Uploaded by

Abhishek Mamgai
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/ 20

Practical 1

• 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

• Creating Table in a SQL based database:

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

• Describing a table structure:

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

• Inserting values in table:

SQL Command:
SQL> insert into
student(name,roll_no,class,address)values(‘Prabhat',06,'BCA',Hat
limore');

Output:
1 row created.

Or

SQL Command:

SQL> insert into student values('kishore',01,'BCA','Nagri');

1 row created.

Output:
1 row create.

4
Practical 5

• For viewing data in table:

SQL Command:
SQL> select * from student;

Output:
NAME ROLL_NO CLASS ADDRESS

----------------------- ---------- ------------ ----------

Prabhat 06 BCA Hatlimore

Kishore 01 BCA Nagri

Amarjeet 30 BCA airwan

Vinay 08 BCA barnoti

5
Practical 6

• Renaming table:

SQL Command:
SQL> rename student to candidates;

Output:
Table renamed.

• Drop command to delete a database or table from a database:

SQL Command:
SQL> Drop table student;

• Truncate command to truncate table:

SQL Command:
SQL> truncate table employees;

6
Practical 7

• Selecting Distinct values from a table:

SQL command:
SQL> Select DISTINCT * from student;

Output:
NAME ROLL_NO CLASS ADDRESS

------------------ -------- ------------ ----------

Prabhat 06 BCA Hatlimore

• Ordering select query results

SQL command:
SQL> SELECT * FROM STUDENT ORDER BY name;

Output:
NAME ROLL_NO CLASS ADDRESS

----------------------- ---------- ------ ---------------

Amarjeet 30 BCA airwan

Atinder 04 BCA
sawanchakDushyant 34 BCA
jagatpur

Kishore 01 BCA Nagri

7
Practical 8

• Altering table structure in a database

Adding new table


alter table student drop column name;

Dropping a column
alter table student drop column name;

Modify a existing column


alter table student modify(name varchar(22));

• Using “NOT NULL”

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%';

EMP_ID NAME DEPT_ID SALARY

------ ------- ----------- ---------

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);

EMP_ID NAME DEPT_ID SALARY

---------- -------------------- ---------- ----------

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;

EMP_ID NAME DEPT_ID SALARY

--------- -------------------- ---------- ----------

2 sonu 22 55000

3 panku 22 75000

12
Practical 11

Design A University Database Using an Entity-Relationship Diagram

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.

What is E-R Model?

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

UID (primary key) int


Name varchar (20)
Chancellor varchar (20)

Table - College

CID (primary key) int


University (foreign key references UID in University table) int

Dean (foreign key references DeanID from Dean table) int

Name varchar (20)

Table - Dean

DeanID (primary key) int


Name varchar (20)
Age int

Table - Department

DID (primary key) int


College ( foreign key references CID in College table) int
Chair (foreign key references PID in professor table) int
Name varchar (20)

Table - Professor
PID (primary key) int
Department ( foreign key references DID in Department table) int
Name varchar (20)

15
Table - Course

CourseID (primary key) int


Department ( foreign key references DID in Department table) int
Name varchar (20)

Table - Section

SectionID (primary key) int


Course ( foreign key references CourseID in Course table) int

Professor (foreign key references PID in professor table) int


Name varchar(20)

Table - Student

StudentID (primary key) int


Department ( foreign key references DID in Department
int
table)
Name varchar (20)
DateofEnrollment smalldatetime
TelephoneNumber varchar(20)

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

Student (foreign key references StudentID in Student table) int

Section ( foreign key references SectionID in Section table) int

16
Practical 12

Hospital Database Schema and ER Diagram

17
Hospital

Hosp-id Primary Key

HCity int

HAddress varchar

Hos-Name Varchar

Pat-id int (Foreign key references to Pat-id of Patient table)

Doc-id int (Foreign key references to Doc-id of Doctor table)

Patient

Pat-id Primary Key

PName varchar

PAddress varchar

PDiagnosis varchar

Record-id Int (Foreign key references to Record-id of Medical Record table)

18
Pat-id Primary Key

Hosp-id Int (Foreign key references to Hosp-id of Hospital table)

Medical Record

Record-id Primary Key

Problem varchar

Date_of_examination date

Pat-id Int (Foreign key references to Pat-id of Patient table)

Doctor

Doc-id Primary Key

DName varchar

Qualification varchar

19
Doc-id Primary Key

Salary int

Hosp-id Int (Foreign key references to Hosp-id of Hospital table)

20

You might also like