Question

Dr. ANB Associates Doctor Doctor ID PK speciality patient ID FK test/procedure Doctor ID FK Patient ID FK Name Test date PatiCreate the database and tables for the database. Show all SQL statements. Include primary and foreign keys. Insert data into each table. Show select statements and display the output of each table. Note:Student’s name must be inserted into table as part of the data! Perform the SQL below: Query one table and use WHERE to filter the results. The SELECT clause should have a column list, not an asterisk (*). State the purpose of the query; show the query and the output. Get information from at least 3 tables in one statement, and provide the output using the Join operator. Use ANSI Join syntax. State the purpose of the query; show the query and the output. Add a screen shot of SS Management Studio showing the query and results. Get information from 2 tables in one statement, and provide the output using the Left Outer Join operator. State the purpose of the query; show the query and the output. The outer join should be designed to retrieve information from the left table that has no matches in the right table. If that is not possible for your database, explain why. Create a query using the IN keyword with a subquery. State the purpose of the query; show the query and the output. Create a query using an aggregate function (i.e., min, max, avg, sum, count) and the GROUP BY command. State the purpose of the query; show the query and the output. Create a query using an aggregate function (i.e., min, max, avg, sum, count) and the GROUP BY command using the HAVING clause to filter the aggregate results. State the purpose of the query; show the query and the output. Update one row. State the purpose of the query; show the result set for the row(s) before the update; show the query; show the row(s) after the update. Delete one row. State the purpose of the query; show the result set before the delete; show the query; show the result set after the delete.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

TABLE1:

CREATE TABLE doctor (

D_id int NOT NULL,

speciality varchar(20),

pid int,

   dob date,

   ssn int,

   primary key(D_id)

);

INSERT INTO doctor
VALUES (101,'Surgeon; 1091,'1988-05-13',11);

INSERT INTO doctor
VALUES (96,'Cardiologist',2981,'1994-10-15',6);

INSERT INTO doctor
VALUES (5,'General physician',97,'1980-12-30',17);

OUTPUT:

$sqlite3 database.sdb < main.sql
101|Surgeon|1091|1988-05-13|11

96|Cardiologist|2981|1994-10-15|6

5|General physician|97|1980-12-30|17

TABLE2:

CREATE TABLE appointments (

a_id int NOT NULL,

adate date;,

atime timestamp,

   pid int;

   primary key(a_id)

);

INSERT INTO appointments
VALUES (2975,'2019-05-13','12:00:00',1091);

INSERT INTO appointments
VALUES (2976,'2019-05-10','13:19:25',2981);

INSERT INTO appointments VALUES (2971,'2019-05-14','01:45:23',97);

OUTPUT:

2975|2019-05-13|12:00:00|1091

2976|2019-05-10|13:19:25|2981

2971|2019-05-14|01:45:23|97

TABLE 3:

CREATE TABLE test(

D_id int,

pid int,

tname varchar(20),

tdate date,

ttime timestamp,

price int,

result varchar(20),

foreign key (D_id) references doctor(D_id)

);

INSERT INTO test
VALUES (101,1091,'Sugar','2019-05-14','18:20:00',100,'HIGH');

INSERT INTO test
VALUES (96,2981,'Full body','2019-05-18','10:50:16',2000,'NORMAL');

INSERT INTO test
VALUES (5,97,'UTI,'2019-05-14','11:55:00',500,'POSITIVE');

OUTPUT:

101|1091|Sugar|2019-05-14|18:20:00|100|HIGH

96|2981|Full body|2019-05-18|10:50:16|2000|NORMAL

5|97|UTI|2019-05-14|11:55:00|500|POSITIVE

TABLE 4:

CREATE TABLE employee(

E_id int NOT NULL,

a_id int,

pid int,

primary key(E_id),

foreign key (a_id) references appointments(a_id)

);

INSERT INTO employee
VALUES (109,2971,97);

INSERT INTO employee
VALUES (103,2976,2981);

INSERT INTO employee
VALUES (10,2975,1091);

OUTPUT:

109|2971|97

103|2976|2981

10|2975|1091

TABLE 5:

CREATE TABLE patient(

pid int NOT NULL,

pname varchar(20),

pno int,

paddress varchar(20),

dob date,

pvisit date,

D_id int,

primary key(pid),

foreign key (D_id) references doctor(D_id)

);

INSERT INTO patient
VALUES (1091,'James',9870473810,'New york','1997-03-03','2019-05-13',101);

INSERT INTO patient
VALUES (97,'Emily',7892053182,'Chicago','1980-09-17','2019-05-14',5);

INSERT INTO patient
VALUES (2981,'Jasmine',8906541230,'New york','1966-12-10','2019-5-10',96);

OUTPUT:

1091|James|9870473810|New york|1997-03-03|2019-05-13|101

97|Emily|7892053182|Chicago|1980-09-17|2019-05-14|5

2981|Jasmine|8906541230|New york|1966-12-10|2019-5-10|96

//To add foreign key pid to all the tables

Alter table doctor add foreign key(pid) references patient(pid);

alter table appointments add foreign key(pid) references patient(pid);

alter table test add foreign key pid references patient(pid);

alter table employee add foreign key(pid) references patient(pid);

OPERATIONS ON TABLES:

QUERY 1.) select using field names;

select d_id,speciality,pid,dob,ssn from doctor;

OUTPUT:

101|Surgeon|1091|1988-05-13|11

96|Cardiologist|2981|1994-10-15|6

5|General physician|97|1980-12-30|17

QUERY 2.) select * from patient where pid=(select pid from employee where E_id=109);

OUTPUT:

97|Emily|7892053182|Chicago|1980-09-17|2019-05-14|5

QUERY3.) SELECT *
FROM patient
LEFT OUTER JOIN test
ON patient.pid = test.pid;

OUTPUT:

1091|James|9870473810|New york|1997-03-03|2019-05-13|101|101|1091|Sugar|2019-05-14|18:20:00|100|HIGH 97|Emily|7892053182|Chicago|1980-09-17|2019-05-14|5|5|97|UTI|2019-05-14|11:55:00|500|POSITIVE 2981|Jasmine|8906541230|New york|1966-12-10|2019-5-10|96|96|2981|Full body|2019-05-18|10:50:16|2000|NORMAL

QUERY4.) select sum(price) as total_sum_of_prices from test group by pid;

OUTPUT:

total_sum_of_prices

500

100

2000

QUERY 5.) select sum(price) as total_sum_of_prices from test group by pid having result='PSOTIVE';

OUTPUT:

500

Add a comment
Know the answer?
Add Answer to:
Create the database and tables for the database. Show all SQL statements. Include primary and foreign...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SQL queries and procedures TABLE: Employees Business Rules: EmployeeID is defined as the primary key. Address...

    SQL queries and procedures TABLE: Employees Business Rules: EmployeeID is defined as the primary key. Address has been denormalized to include City and State for performance reasons. DeptNbr is a foreign key that references DeptNbr in the Department table Manager is a foreign key that references EmployeeID in the Employees table Data Structure: (EmployeeId, LastName, FirstName, Street, City, State, Zip, DateOfHire, DateOfBirth, JobTitle, Salary, DeptNbr(fk), Manager(fk)) TABLE: Department Business Rules: DeptNbr is defined as the primary key. Data Structure: (DeptNbr,...

  • The database(or the tables) are follow: CREATE TABLE employees ( id SERIAL NOT NULL PRIMARY KEY,...

    The database(or the tables) are follow: CREATE TABLE employees ( id SERIAL NOT NULL PRIMARY KEY, name TEXT NOT NULL, salary REAL NOT NULL DEFAULT 25000.0 ); CREATE TABLE employee_audit_log ( employee_id INTEGER NOT NULL, occurred_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); INSERT INTO employees(name, salary) VALUES ('Arnold Schwarznegger', 35000), ('Yuri Gargarin', 27000), ('Anakin Skywalker', 450000), ('Said Faroghi', 15000), ('Zino Holwerda', 8500); 1. Create a trigger for the employees table so that a new row is inserted in employee audit_log...

  • im currently working on an sql database homework for a fundamentals class but im not too...

    im currently working on an sql database homework for a fundamentals class but im not too sure if my results are alright and would like to compare results, the assignment is as follows: O4pk,fk evaluate de data models, the specifications and table contents: equipment_tb Assigned_Equipment_tb workers_tb pk serial_number int pk,fk serial number int pk worker_id int, Surrogate brand char(10) worker id int Lastname char(100) model char(15) assigned_date date Firstname char(50) processor char(15) department int processor_velocity number (3,2) email char(200) memory...

  • create a handler you can visit to set up your database table. It contains all the...

    create a handler you can visit to set up your database table. It contains all the fields needed to make a simple workout tracker. name - the name of the exercise reps - the number of times the exercise was performed weight - the weight of the weights used date - the date the exercise was performed lbs - a boolean indicating if the measurement is in lbs or kg. 1 indicates lbs, 0 indicates kgs. Requirements You need to...

  • Write the following SQL statements in Microsoft Access by using the Books database from Week 2...

    Write the following SQL statements in Microsoft Access by using the Books database from Week 2 Assignment 2. Once complete, copy and paste the SQL statements in a Microsoft Word document: Write SQL statements: To update the publisher name from READ WITH US to READ FOR US To verify the updated name field for the publisher with ID 6 To make the following updates to the Publisher table (be careful with WHERE): Make Contact="John Travolta" for publisher with ID number...

  • SQL Query Question: I have a database with the tables below, data has also been aded...

    SQL Query Question: I have a database with the tables below, data has also been aded into these tables, I have 2 tasks: 1. Add a column to a relational table POSITIONS to store information about the total number of skills needed by each advertised position. A name of the column is up to you. Assume that no more than 9 skills are needed for each position. Next, use a single UPDATE statement to set the values in the new...

  • Create SQL statements based on the given tables: Code to create the tables: CREATE DATABASE LAB...

    Create SQL statements based on the given tables: Code to create the tables: CREATE DATABASE LAB4DB; USE LAB4DB; CREATE TABLE CUSTOMER ( CUS_CODE int primary key, CUS_LNAME varchar(15), CUS_FNAME varchar(15), CUS_INITIAL varchar(1), CUS_AREACODE varchar(3), CUS_PHONE varchar(8), CUS_BALANCE float(8) ); INSERT INTO CUSTOMER VALUES('10010','Ramas','Alfred','A','615','844-2573','0'); INSERT INTO CUSTOMER VALUES('10011','Dunne','Leona','K','713','894-1238','0'); INSERT INTO CUSTOMER VALUES('10012','Smith','Kathy','W','615','894-2285','345.86'); INSERT INTO CUSTOMER VALUES('10013','Olowski','Paul','F','615','894-2180','536.75'); INSERT INTO CUSTOMER VALUES('10014','Orlando','Myron','','615','222-1672','0'); INSERT INTO CUSTOMER VALUES('10015','O''Brian','Amy','B','713','442-3381','0'); INSERT INTO CUSTOMER VALUES('10016','Brown','James','G','615','297-1228','221.19'); INSERT INTO CUSTOMER VALUES('10017','Williams','George','','615','290-2556','768.93'); INSERT INTO CUSTOMER VALUES('10018','Farriss','Anne','G','713','382-7185','216.55'); INSERT INTO CUSTOMER VALUES('10019','Smith','Olette','K','615','297-3809','0'); /*...

  • Project Steps: 1. Database Design 1.1. Design and create a database in third normal form based...

    Project Steps: 1. Database Design 1.1. Design and create a database in third normal form based on the following requirements: • Each Job is for a specific customer and there can be more than one job per customer. • The name and address must be tracked for each customer. • Each job is described by up to 2000 characters. • Each job has a status of ‘open, ‘in process’, or ‘complete’. • Each job has a start date and end...

  • Write Ten SQL SELECT statements to query the STUDENT schema you created for practice lab. 4....

    Write Ten SQL SELECT statements to query the STUDENT schema you created for practice lab. 4. List all cities that have 10 or more students and instructors combined. Show city, state, number of student residents, number of instructor residents, and total student/instructor residents in that city. Sort by total in descending order. 5. List the instructor id and name of the instructors that teach fewer than 10 sections. 7. Find how many students are enrolled in sections taught by Todd...

  • The Ch07_ConstructCo database stores data for a consultingcompany that tracks all charges to projects. The...

    The Ch07_ConstructCo database stores data for a consulting company that tracks all charges to projects. The charges are based on the hours each employee works on each project. The structure and contents of the Ch07_ConstructCo database are shown in Figure P7.1.Note that the ASSIGNMENT table in Figure P7.1 stores the JOB_CHG_HOUR values as an attribute (ASSIGN_CHG_HR) to maintain historical accuracy of the data. The JOB_CHG_HOUR values are likely to change over time. In fact, a JOB_CHG_ HOUR change will be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT