0% found this document useful (0 votes)
9 views3 pages

Exp 1

Uploaded by

anu1529ravina
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)
9 views3 pages

Exp 1

Uploaded by

anu1529ravina
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/ 3

...\OneDrive\Documents\SQL Server Management Studio\exp1.

sql 1
use exp20
CREATE TABLE Patients (
PatientID INT PRIMARY KEY,
Name VARCHAR(100),
Address VARCHAR(100)
);

CREATE TABLE MedicalDoctors (


DoctorID INT PRIMARY KEY,
Name VARCHAR(100),
VisitingDays VARCHAR(100)
);

CREATE TABLE TestsAndExaminations (


TestID INT PRIMARY KEY,
Date DATE,
Type VARCHAR(100),
PatientID INT,
DoctorID INT,
FOREIGN KEY (PatientID) REFERENCES Patients(PatientID),
FOREIGN KEY (DoctorID) REFERENCES MedicalDoctors(DoctorID)
);
select * from patients
select * from MedicalDoctors
select * from TestsAndExaminations

-- Inserting sample data


INSERT INTO Patients (PatientID, Name, Address) VALUES
(4, 'suzanne', 'Vashi');
(2, 'Jane Smith', 'Mumbai'),
(3, 'Alice Johnson', 'Vashi');

INSERT INTO MedicalDoctors (DoctorID, Name, VisitingDays) VALUES


(1, 'Dr. Smith', 'Monday'),
(2, 'Dr. Patel', 'Tuesday');

INSERT INTO TestsAndExaminations (TestID, Date, Type, PatientID, DoctorID) VALUES


(1, '2010-05-08', 'Blood Test', 1, 1),
(2, '2010-05-08', 'X-ray', 2, 2),
(3, '2010-05-09', 'Ultrasound', 3, 1);

--a) patients in vashi

SELECT Name FROM Patients


WHERE Address = 'Vashi'
AND PatientID IN (
SELECT PatientID
FROM TestsAndExaminations
...\OneDrive\Documents\SQL Server Management Studio\exp1.sql 2
WHERE Date = '2010-05-08'
);
--b) various test and examinations on each

SELECT p.Name AS PatientName, te.Type AS TestType FROM Patients p


JOIN TestsAndExaminations te ON p.PatientID = te.PatientID;

--c) Find the name of the doctors who visit only on Tuesday

SELECT Name
FROM MedicalDoctors
WHERE VisitingDays = 'Tuesday';

--trigger after insert


create trigger trig1
on Patients After insert
AS
BEGIN
PRINT 'a new value inserted in table'
END;
-- create function

CREATE FUNCTION CountPatientsFromVashi()


RETURNS INT
AS
BEGIN
DECLARE @vashi_patient_count INT;
SELECT @vashi_patient_count = COUNT(*)
FROM Patients
WHERE Address = 'Vashi';

RETURN @vashi_patient_count;
END;

SELECT dbo.CountPatientsFromVashi() AS VashiPatientCount;


...\OneDrive\Documents\SQL Server Management Studio\exp1.sql 3

You might also like