




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Create Doctor ( Doctor Id, Name, department, experience) and Patient ( Patient Id, Name, Doc Id, Admitted date, discharge date, disease) Use SQL to answer the below queries. 1.Create tables according to the structure specified above. 2.Change the data type of experience field from number to varchar. 3.Include a field age in the table Patient table. 4.Add the following constraints. a. Make doctorid as primary key in doctor table b. Doctorid as foreign key in patient table. c. Patient name in patient table as not null.
Output: -
2.Change the data type of experience field from number to varchar Code: - ALTER TABLE Doctor MODIFY Experience VARCHAR(50); Output: -
3.Include a field age in the table Patient table Code: - ALTER TABLE Patient ADD Age INT; Output: -
4.Add the following constraints. a. Make doctorid as primary key in doctor table b. Doctorid as foreign key in patient table. c. Patient name in patient table as not null. Code: - ALTER TABLE Doctor ADD CONSTRAINT PK_Doctor PRIMARY KEY (DoctorId); ALTER TABLE Patient ADD CONSTRAINT FK_Patient_Doctor FOREIGN KEY (DocId) REFERENCES Doctor(DoctorId); ALTER TABLE Patient MODIFY Name VARCHAR(100) NOT NULL;
Output: -
7.Display the number of days the patient is admitted in the hospital. Code: - SELECT PatientId, Name, AdmittedDate, DischargeDate, (DischargeDate
Output: -
10.Display the patient name and patient_id who were admitted more than a month in the hospital and serviced by a doctor in ophthalmology department. 11.Retrieve the details of doctor who have more than 2 patients. Code: - SELECT P.PatientId, P.Name AS PatientName FROM Patient P JOIN Doctor D ON P.DocId = D.DoctorId WHERE P.DischargeDate > ADD_MONTHS(P.AdmittedDate, 1) AND D.Department = 'Ophthalmology'; Code: - SELECT D.DoctorId, D.Name AS DoctorName, D.Department, COUNT(P.PatientId) AS PatientCount FROM Doctor D LEFT JOIN Patient P ON D.DoctorId = P.DocId GROUP BY D.DoctorId, D.Name, D.Department HAVING COUNT(P.PatientId) > 2;