DBMD Record Ashok

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Database Management Lab

Lab Record

M. Ashok Chakravarthi
BU21CSEN0500144

Department of Computer Science and Engineering


GITAM School of Technology, GITAM (Deemed to be University)
Bengaluru
Exercise -2

HOSPITAL MANAGEMENT E-R DIAGRAM

Aim:
To develop an entity relationship diagram for
hospital management system.

Entities:
1)Patient
2)Medical records
3)Hospital
4)Doctor
Attributes:
 PName
 Pat_id
 PDiagnosis
 Paddress
 Hosp_id
 Hosp_Name
 HAddress
 HCity
 Record_id
 Date of examination
 Problem
 Salary
 Qualification
 DName
 Doc_id
Relation:
1. Has a(one to many)
2. Admitted in (many to one)

M. ASHOK
BU21CSEN0500144
DDL COMMANDS
Ashok
BU21CSEN0500144

DDL COMMANDS IN SQL


DDL stands for Data definition language
• Data Definition Language (DDL) is a subset of SQL and part of DBMS
(Database Management System). DDL changes the structure of the table
like creating a table, deleting a table, altering a table, etc.
• All the command of DDL are auto-committed that means it permanently
save all the changes in the database.
Here are some commands that come under DDL:
1.CREATE
2.ALTER
3.DROP
4.TRUNCATE

Create Command:
It is used to create a new table in the database. The user has to give
information like table name, column names, and their datatypes.
Syntax:
Create table student(id number(10) primary key,name char(20),sec char(15));
Alter Command:
This command is used to remove an existing table along with its
structure from the Database.
Syntax:
DROP TABLE table_name;

Truncate Command:
This command is used to remove all rows from the table, but the
structure of the table still exists.
Syntax:
TRUNCATE TABLE table_name;

Drop Table:
This command is used to add, delete or change columns in the existingtable. The
user needs to know the existing table name and can do add, deleteor modify
tasks
easily.
Syntax:
To add a new column to the existing table
ALTER TABLE table_name ADD column_name
DML COMMANDS
Ashok Chakaravarthi
BU21CSEN0500144
DML Commands
The DML commands in Structured Query Language change the data
present in the SQL database. We can easily access, store, modify, update
and delete the existing records from the database using DML commands.

Following are the four main DML commands in SQL:

1. SELECT Command
2. INSERT Command
3. UPDATE Command
4. DELETE Command

Insert Command:

Insert command is used to insert data into a table.

Syntax:

Insert into <table name> (column list) values (column value);


Select Command:

Select command is used to retrieve data from the database.

Syntax:

Select * from table name;

Delete Command:

Delete command is used to delete records from a database table.

Syntax:

Delete from<table name>;


Update Command:

Update command is used to update existing data within a table.

Syntax:

Update <table name> set column number = value number where


condition;
Experiment-7
Enforcing Constraints in SQL
M Ashok
BU21CDEN0500144

1. Not null:
This constraint tells that we cannot store a null value in a
column. That is, if a column is specified as NOT NULL then we will not be
able to store null in this particular column any more.

2. Unique:
This constraint when specified with a column, tells that all the
values in the column must be unique. That is, the values in any row of a
column must not be repeated.

3. Default:
This constraint specifies a default value for the column when no
value is specified by the user.

4. Check:
This constraint helps to validate the values of a column to
meet a particular condition. That is, it helps to ensure that the value stored
in a column meets a specific condition.

5. Primary key:
A primary key is a field which can uniquely identify each row
in a table. And this constraint is used to specify a field in a table as primary
key.
6. FOREIGN KEY:
A Foreign key is a field which can uniquely identify each
row in a another table. And this constraint is used to specify a field as
Foreign key.
Experiment:8
Use of different of operators for nested sub-queries

Ashok

BU21CSEN0500144

AIM: To familiarize the operators for nested sub-queries in SQL.

DESCRIPTION:

In SQL, an aggregate function is a function that operates on a set of values to perform a


calculation and return a single result. Aggregate functions are often used in combination
with the GROUP BY clause to perform calculations on groups of rows within a table.

Here are the some common commands for aggregated queries:

1 . COUNT

2.SUM

3.AVG

4.MAX

5.MIN

First create a table for employees and insert values in the created table
COUNT:

AVERAGE:
MAX:

MIN:
EXPERIMENT: 9
Implementation of Nested Sub-Queries
ASHOK
BU21CSEN0500144
AIM: implementing of nested sub queries.

Independent sub query:

A subquery is a query within another query. The outer query is called as main
query and inner query is called as subquery.

SUBQUERY is a query inside a query. An INDEPENDENT SUBQUERY is a subquery


that can be run on its own, without the main subquery.

Corelated sub query:

A correlated subquery is evaluated once for each row processed by the parent
statement. The parent statement can be a SELECT, UPDATE, or DELETE
statement. Correlated subqueries are used for row-by-row processing. Each
subquery is executed once for every row of the outer query.

Create table and insert data


USING IN:

USING NOT IN:

ALL:
ANY:
EXPERIMENT:10
USE OF JOINS
M. Ashok
BU21CSEN0500144
OUTER JOINS:
MAKING NULL INTO ZERO’S
VIEW IS CREATED
IN SUBQUERY:
GOING TO JOIN THE TABLE:

Disallowing Null Values:


INNER JOINS:

IN SQL :
INNER JOIN OPERATION:

LEFT JOIN:
Right join:
EXPERIMENT-11
Ashok.M
BU21CSEN0500144
AIM: To familiarise with Group by and Having Functions.
The GROUP BY Statement in SQL is used to arrange identical data into
groups with the help of some functions. if a particular column has the
same values in different rows, it will arrange these rows in a group.
Employee DB:

Department DB:
Group by with single column:

Group by with multiple column:


Single column group by with having clause:

Multiple column group by with having clause:


Single column group by with where clause:

Multiple column group by with where clause:


Experiment-12
Views
M. Ashok
BU21CSEN0500144

A view is nothing more than an SQL statement stored in the database


with an associated name. A view is a composition of a table as a
predefined SQL query. Views in SQL are a kind of virtual table. A view
also has rows and columns as they are in a real table in the database.
We can create a view by selecting fields from one or more tables
present in the database. A View can either have all the rows of a table
or specific rows based on certain conditions.

Table creation:

Inserting values into table:


Creating a View:

Displaying a view:
Deleting a Row from a View:

Updating a view:

inserting a row in a view:


Drop a View:
Experiment no:13

Declaring triggers and use of cursors

ASHOK

BU21CSEN0500144

Aim: Declaring triggers and use of cursors using SQL .

• A trigger is a database object that is associated with a table. It will be


activated when a
defined action is executed for the table.

• The trigger can be executed when you run one of the following MySQL
statements on the table: INSERT, UPDATE and DELETE and it can be
invoked before or after the event.
Types of triggers:
Triggers are of two types –

Row-Level Trigger: It is a trigger, which is activated for each row by a


triggering statement such as insert, update, or delete. For example, if a table
has inserted, updated, or deleted multiple rows, the row trigger is fired
automatically for each row affected by the insert, update, or delete statement.

Statement-Level Trigger: It is a trigger, which is fired once for each event that
occurs on a table regardless of how many rows are inserted, updated, or deleted.

syntax for creating a trigger is --

CREATE [OR REPLACE ] TRIGGER trigger_name {BEFORE | AFTER |


INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS nj
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements EXCEPTION
Exception-handling-statements
END;

Create the Student_info table:

Create a table to log trigger actions:


Before Insert Trigger: It is activated before the insertion of data into the table.

Before Update Trigger: It is activated before the update of data in the table.
Before Delete Trigger: It is activated before the data is removed from the table.