0% found this document useful (0 votes)
28 views42 pages

DBMD Record Ashok

The document describes experiments conducted with SQL triggers and cursors. It defines triggers as database objects associated with tables that are activated by defined actions like INSERT, UPDATE, DELETE. Triggers can be row-level or statement-level. Cursors are used to process SQL result sets row by row. The experiments create tables, define before insert and update triggers, and use cursors to iterate through rows.

Uploaded by

Kavya Reddy
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)
28 views42 pages

DBMD Record Ashok

The document describes experiments conducted with SQL triggers and cursors. It defines triggers as database objects associated with tables that are activated by defined actions like INSERT, UPDATE, DELETE. Triggers can be row-level or statement-level. Cursors are used to process SQL result sets row by row. The experiments create tables, define before insert and update triggers, and use cursors to iterate through rows.

Uploaded by

Kavya Reddy
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

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.

Common questions

Powered by AI

Aggregate functions in SQL are used to perform calculations on a set of values and return a single result. These functions are often combined with the GROUP BY clause to organize data into groups on which to perform computations. For instance, the COUNT() function can count the number of rows in each group, SUM() adds up values, AVG() computes the average, MAX() finds the maximum value, and MIN() identifies the minimum value within each group . GROUP BY allows these functions to operate on subsets of data, facilitating analyses such as finding average salaries per department or total sales per region .

Key SQL constraints include NOT NULL, UNIQUE, DEFAULT, CHECK, PRIMARY KEY, and FOREIGN KEY constraints. NOT NULL ensures that a column cannot store null values, enforcing the presence of data . UNIQUE requires that all values in a column are distinct, avoiding duplicates . DEFAULT assigns a default value if no specific value is provided . CHECK allows validation against a particular condition for the data in a column, ensuring data meets specified criteria . PRIMARY KEY uniquely identifies each row, and FOREIGN KEY establishes a link between tables, preserving referential integrity by ensuring valid references between related tables . Together, these constraints maintain the accuracy and reliability of the data stored within a database.

Enforcing the NOT NULL constraint ensures fields contain valid and expected data, forbidding null entries that might represent missing information in critical columns . The UNIQUE constraint prevents duplicate values in a column, ensuring data uniqueness and reducing redundancy . The CHECK constraint enhances data reliability by enforcing specific conditions, validating input against logical checks such as range limits, formats, or domain constraints, which uphold business rules and data validity . Together, these constraints maintain data integrity by ensuring accurate and valid data is consistently entered into the database.

SQL triggers are database objects that automatically execute predefined actions in response to specific events on a table, like INSERT, UPDATE, or DELETE operations. They help automate tasks and enforce business rules. Triggers come in two main types: row-level and statement-level. Row-level triggers activate for each affected row, useful for operations that need to validate or modify data on a per-row basis. Statement-level triggers execute once per SQL statement, suitable for operations that don't require row-specific logic . For example, a row-level trigger can enforce a constraint across insertions, while a statement-level trigger can maintain audit logs of operations .

DDL (Data Definition Language) commands are used to define, alter, and manage the structure of database objects, such as tables. They include commands like CREATE, ALTER, DROP, and TRUNCATE, which are auto-committed, meaning changes are permanent . In contrast, DML (Data Manipulation Language) commands facilitate the manipulation of data within these structures, allowing users to select, insert, update, or delete data records. DML commands include SELECT, INSERT, UPDATE, and DELETE, enabling interaction with the database content rather than its structure .

Independent subqueries in SQL can run on their own outside of the main query, providing results that are then used by the outer query without any dependencies on it. They're useful when the subquery doesn't reference any column of the primary query and needs to provide a complete set of results independently . Correlated subqueries, however, reference one or more columns in the outer query. Each execution of the correlated subquery depends on the outer query's current row, making them suitable for row-by-row processing and operations that involve dependencies between tables . These differences affect performance and applicability, with correlated subqueries often being more complex and resource-intensive.

SQL views are virtual tables made from the result of a stored query, allowing users to simplify complex queries by presenting data through easy-to-understand structures. Views can include selected fields from one or more tables, with the potential to restrict access to certain data layers and simplify query execution. They provide an abstraction layer that can optimize data management by representing specific datasets without altering the underlying tables . Views also enhance security, since users can be granted access to views rather than exposing sensitive data within tables directly .

Entity-relationship diagrams (ERDs) are used to visualize and design the structure of a database, which is essential for understanding and organizing the relationships between different entities within a system, such as a hospital management system. In this context, ERDs help outline key entities such as patients, doctors, medical records, and hospitals, defining each entity's attributes and establishing relationships between them, like 'admitted in' and 'has a'. This provides a clear framework for building a robust and efficient database to manage hospital operations .

SQL JOIN operations enable data retrieval from multiple tables by combining rows based on a related column, effectively merging information that spans various tables . The primary types of JOINs include INNER JOIN, which returns only the rows with matching keys in both tables, LEFT JOIN, which returns all rows from the left table and the matched rows from the right table, filling in NULLs where no match is found, and RIGHT JOIN, which does the opposite by taking all rows from the right table and matched ones from the left . These operations are crucial for complex queries that require integration of data from disparate sources within a database.

SQL PRIMARY KEY constraints enhance database design by ensuring each row in a table is uniquely identifiable, which is crucial for maintaining distinct records . A FOREIGN KEY constraint, on the other hand, enforces referential integrity by linking tables through a mutual column. This ensures that relationships between tables remain consistent, as the FOREIGN KEY column in one table must correspond to a valid PRIMARY KEY in another table . These constraints prevent orphaned or duplicate records, enhance data integrity by maintaining consistent inter-table relationships, and support complex queries and database normalization .

You might also like