You are on page 1of 5

CREATE TABLE AMBULATORIOS (

NROA INT NOT NULL PRIMARY KEY,


ANDAR INT NOT NULL,
CAPACIDADE INT NOT NULL,
)
CREATE TABLE MEDICOS (
CODM INT NOT NULL PRIMARY KEY,
CPF INT NOT NULL,
NOME VARCHAR(50) NOT NULL,
IDADE INT NOT NULL,
CIDADE VARCHAR(30) NOT NULL,
ESPECIALIDADE VARCHAR(30) NOT NULL,
NROA INT NOT NULL,
)
CREATE TABLE PACIENTES (
CODP INT NOT NULL PRIMARY KEY,
CPF INT NOT NULL,
NOME VARCHAR(50) NOT NULL,
IDADE INT NOT NULL,
CIDADE VARCHAR(30) NOT NULL,
DOENCA VARCHAR(30) NOT NULL,
CODF INT NOT NULL,
)
CREATE TABLE CONSULTAS (
ID INT IDENTITY NOT NULL PRIMARY KEY,
CODM INT NOT NULL,
CODP INT NOT NULL,
DATA VARCHAR(10) NOT NULL,
HORA VARCHAR(08) NOT NULL,
)
CREATE TABLE FUNCIONARIOS (
CODF INT NOT NULL PRIMARY KEY,
CPF INT NOT NULL,
NOME VARCHAR(50) NOT NULL,
IDADE INT NOT NULL,
CIDADE VARCHAR(30) NOT NULL,
SALARIO NUMERIC NOT NULL,
CODP INT NOT NULL,
)
ALTER TABLE CONSULTAS ADD
CONSTRAINT FK_CODM FOREIGN KEY(CODM)REFERENCES MEDICOS(CODM)
ALTER TABLE CONSULTAS ADD
CONSTRAINT FK_CODP FOREIGN KEY(CODP)REFERENCES PACIENTES(CODP)
ALTER TABLE MEDICOS ADD
CONSTRAINT FK_NROA FOREIGN KEY(NROA)REFERENCES AMBULATORIOS(NROA)
ALTER TABLE FUNCIONARIOS ADD
CONSTRAINT FK_CODF FOREIGN KEY(CODF)REFERENCES PACIENTES(CODP)
INSERT INTO AMBULATORIOS VALUES (101,1,15)
INSERT INTO AMBULATORIOS VALUES (102,2,30)
INSERT INTO AMBULATORIOS VALUES (103,2,20)
INSERT INTO AMBULATORIOS VALUES (104,5,40)
INSERT INTO AMBULATORIOS VALUES (105,5,10)
INSERT INTO AMBULATORIOS VALUES (106,6,50)
INSERT INTO MEDICOS VALUES (3101,53647652,'PAULO AUGUSTO',31,'ANANINDEUA','NEURO
LOGISTA',101)
INSERT INTO MEDICOS VALUES (0102,85760652,'IVO PAULO',65,'BELÉM','CARDIOLOGISTA',1
01)
INSERT INTO MEDICOS VALUES (8103,93647633,'ASSIS JORGE',35,'BELÉM','ORTOPEDISTA',1
04)
INSERT INTO MEDICOS VALUES (7103,11647053,'BEATRIZ NOVAES',32,'BELÉM','ORTOPEDISTA
',104)
INSERT INTO MEDICOS VALUES (2104,33547552,'ANDRÉ SOUZA',33,'SÃO PAULO','NEUROLOGISTA
',101)
INSERT INTO MEDICOS VALUES (2105,13644592,'CINTHYA NOBRE',29,'SÃO PAULO','ORTOPEDI
STA',104)
INSERT INTO MEDICOS VALUES (5106,22212300,'PAULO LIZ',40,'CURITIBA','OTORRINOLAR
INCOLOGISTA',103)
INSERT INTO MEDICOS VALUES (8107,35212306,'ROBERTO MAIA',41,'FLORIANÓPOLIS','PSIQU
IATRA',104)
INSERT INTO MEDICOS VALUES (9108,22212307,'CATIA MUNIZ',44,'PALHOÇA','PEDIATRA',10
5)
INSERT INTO MEDICOS VALUES (6109,29872308,'AMANDA SMITH',34,'CURITIBA','PEDIATRA
',105)
INSERT INTO MEDICOS VALUES (5110,22668309,'JACKSON TAKASHI',58,'RIO DE JANEIRO',
'PEDIATRA',105)
INSERT INTO MEDICOS VALUES (2236,20212301,'JÚLIO BORGES',54,'SÃO LUIZ','GASTROENTERO
LOGISTA',106)
INSERT INTO MEDICOS VALUES (3366,22212303,'SAMPAIO QUEIROZ',25,'TEREZINA','OTORR
INOLARINCOLOGISTA',106)
INSERT INTO PACIENTES VALUES (101,33747611,'JORGE MATHEUS',18,'ANANINDEUA','EPIL
EPSIA',0)
INSERT INTO PACIENTES VALUES (102,53647223,'LUIZA SILVA',44,'ANANINDEUA','SINUSI
TE',0)
INSERT INTO PACIENTES VALUES (103,34647332,'MARIA EDUARDA',67,'BELÉM','PNEUMONIA',
0)
INSERT INTO PACIENTES VALUES (104,88797652,'LOPEZ DINIZ',54,'BELÉM','ARITMIA',0)
INSERT INTO PACIENTES VALUES (105,53633351,'VICTOR MENDES',12,'ANANINDEUA','PNEU
MONIA',0)
INSERT INTO PACIENTES VALUES (106,44437652,'ANA MARIA',43,'BELÉM','CÂNCER',0)
INSERT INTO PACIENTES VALUES (107,64337611,'JORGE LUCAS',18,'ANANINDEUA','EPILEP
SIA',0)
INSERT INTO PACIENTES VALUES (108,22647223,'BOSCO SILVA',44,'BELÉM','SINUSITE',0)
INSERT INTO PACIENTES VALUES (109,33497332,'JÚLIA EDUARDA',67,'BELÉM','PNEUMONIA',0)
INSERT INTO PACIENTES VALUES (110,11797652,'TAISU OKADA',54,'BELÉM','ARITMIA',0)
INSERT INTO PACIENTES VALUES (111,09873351,'FÁBIO MARTINS',12,'BELÉM','PNEUMONIA',0)
INSERT INTO PACIENTES VALUES (112,14423652,'ANA QUEIROZ',43,'BELÉM','CÂNCER',0)
INSERT INTO PACIENTES VALUES (113,28747611,'JULIANA BASTOS',18,'CASTANHAL','EPIL
EPSIA',0)
INSERT INTO PACIENTES VALUES (114,53634252,'LUIZ SOUZA',44,'CASTANHAL','SINUSITE
',0)
INSERT INTO PACIENTES VALUES (115,34621132,'LUIZ ALFREDO',67,'FLORIANÓPOLIS','DEPR
ESSÃO',101)
INSERT INTO PACIENTES VALUES (116,81597652,'LUIZ OTÁVIO',54,'FLORIANÓPOLIS','ESQUIZO
FRENIA',102)
INSERT INTO PACIENTES VALUES (117,12333351,'EDINELSON PENA',12,'PALHOÇA','PNEUMONI
A',107)
INSERT INTO PACIENTES VALUES (118,23437652,'ALAN SOARES',43,'FLORIANÓPOLIS','CÂNCER'
,0)
INSERT INTO PACIENTES VALUES (119,33747611,'ALINE MARQUES',18,'FLORIANÓPOLIS','EPI
LEPSIA',103)
INSERT INTO PACIENTES VALUES (120,53009323,'BIANCA NOBRE',44,'PALHOÇA','EPILEPSIA'
,105)
INSERT INTO PACIENTES VALUES (121,14647330,'MARIA JOSÉ',67,'PALHOÇA','DEPRESSÃO',104)
INSERT INTO PACIENTES VALUES (122,83497650,'LAURO VISEU',54,'PALHOÇA','DEMÊNCIA',106
)
INSERT INTO PACIENTES VALUES (123,53833351,'ROBERTO TOSHIMITSU',12,'BENEVIDES','
PNEUMONIA',0)
INSERT INTO PACIENTES VALUES (124,09950652,'LUCAS BARROSO',07,'BELÉM','FEBRE',0)
INSERT INTO PACIENTES VALUES (125,49950652,'GABRIEL DUARTE',09,'ANANINDEUA','BRO
NQUITE',0)
INSERT INTO PACIENTES VALUES (126,29950652,'EDNA SILVA',12,'MARITUBA','DENGUE',0
)
INSERT INTO PACIENTES VALUES (127,00720652,'CAMILA ROCHA',10,'BELÉM','GASTRITE',0)
INSERT INTO CONSULTAS VALUES (8107,116,'20/08/2006','07:00')
INSERT INTO CONSULTAS VALUES (8107,122,'20/08/2006','08:30')
INSERT INTO CONSULTAS VALUES (8107,120,'20/08/2006','10:30')
INSERT INTO CONSULTAS VALUES (8107,115,'20/08/2006','11:30')
INSERT INTO CONSULTAS VALUES (8107,121,'20/08/2006','12:00')
INSERT INTO CONSULTAS VALUES (8107,119,'25/09/2006','07:00')
INSERT INTO CONSULTAS VALUES (6109,117,'25/09/2006','07:20')
INSERT INTO CONSULTAS VALUES (6109,123,'25/09/2006','08:00')
INSERT INTO CONSULTAS VALUES (5110,111,'25/09/2006','10:00')
INSERT INTO CONSULTAS VALUES (5110,105,'25/09/2006','10:30')
INSERT INTO CONSULTAS VALUES (3101,121,'25/09/2006','11:00')
INSERT INTO CONSULTAS VALUES (3101,119,'25/09/2006','11:30')
INSERT INTO CONSULTAS VALUES (9108,116,'25/09/2006','12:00')
INSERT INTO CONSULTAS VALUES (8107,122,'20/10/2006','07:30')
INSERT INTO CONSULTAS VALUES (8107,120,'20/10/2006','08:00')
INSERT INTO CONSULTAS VALUES (8107,115,'20/10/2006','09:00')
INSERT INTO CONSULTAS VALUES (0102,121,'20/10/2006','09:30')
INSERT INTO CONSULTAS VALUES (0102,119,'20/10/2006','10:00')
INSERT INTO CONSULTAS VALUES (0102,116,'20/10/2006','10:30')
INSERT INTO CONSULTAS VALUES (0102,122,'20/10/2006','11:00')
INSERT INTO CONSULTAS VALUES (0102,120,'20/10/2006','11:30')
INSERT INTO CONSULTAS VALUES (2104,115,'15/11/2006','07:30')
INSERT INTO CONSULTAS VALUES (2104,121,'15/11/2006','08:00')
INSERT INTO CONSULTAS VALUES (2104,119,'15/11/2006','08:30')
INSERT INTO CONSULTAS VALUES (2104,116,'15/11/2006','09:00')
INSERT INTO CONSULTAS VALUES (2105,122,'15/11/2006','10:30')
INSERT INTO CONSULTAS VALUES (2105,120,'18/11/2006','10:30')
INSERT INTO CONSULTAS VALUES (2105,115,'18/11/2006','11:30')
INSERT INTO CONSULTAS VALUES (8107,121,'18/11/2006','12:00')
INSERT INTO FUNCIONARIOS VALUES (101,44837611,'LUIZ ALFREDO',28,'FLORIANÓPOLIS',80
0,115)
INSERT INTO FUNCIONARIOS VALUES (102,23833331,'LUIZ OTÁVIO',38,'FLORIANÓPOLIS',1200,
116)
INSERT INTO FUNCIONARIOS VALUES (103,32437600,'ALINE MARQUES',34,'FLORIANÓPOLIS',8
00,119)
INSERT INTO FUNCIONARIOS VALUES (104,84837345,'MARIA JOSÉ',18,'PALHOÇA',800,121)
INSERT INTO FUNCIONARIOS VALUES (105,55537619,'BIANCA NOBRE',45,'PALHOÇA',1500,120
)
INSERT INTO FUNCIONARIOS VALUES (106,34837619,'LAURO VISEU',51,'PALHOÇA',1000,122)
INSERT INTO FUNCIONARIOS VALUES (107,23837611,'EDINELSON PENA',28,'PALHOÇA',800,11
7)
INSERT INTO FUNCIONARIOS VALUES (108,45833331,'MARCOS TAKASHI',38,'ANANINDEUA',1
200,0)
INSERT INTO FUNCIONARIOS VALUES (109,32437654,'ANA ELISA',34,'BELÉM',800,0)
INSERT INTO FUNCIONARIOS VALUES (110,84837345,'CAROLINA OLIVEIRA',18,'BELÉM',800,0
)
INSERT INTO FUNCIONARIOS VALUES (111,54337619,'JOSIAS AFONSO',45,'CASTANHAL',150
0,0)
INSERT INTO FUNCIONARIOS VALUES (112,34837619,'PATRICIA LIMA',51,'BENEVIDES',100
0,0)
INSERT INTO FUNCIONARIOS VALUES (113,44837611,'JOSÉ ALMEIDA',28,'ANANINDEUA',800,0
)
INSERT INTO FUNCIONARIOS VALUES (114,20393331,'MARCOS TAKASHI',38,'ANANINDEUA',1
200,0)
INSERT INTO FUNCIONARIOS VALUES (115,12444600,'ANA CLARA',38,'BELÉM',800,0)
INSERT INTO FUNCIONARIOS VALUES (116,84821345,'CAROLINA OLIVEIRA',18,'BELÉM',800,0
)
INSERT INTO FUNCIONARIOS VALUES (117,16537619,'JOSIAS AFONSO',45,'CASTANHAL',150
0,0)
INSERT INTO FUNCIONARIOS VALUES (118,00837613,'PATRICIA LIMA',51,'BENEVIDES',100
0,0)
-- 1
SELECT A.NROA,A.CAPACIDADE, M.NOME
FROM AMBULATORIOS A, MEDICOS M
WHERE ANDAR = 5
AND M.NROA = A.NROA
AND M.ESPECIALIDADE = 'ORTOPEDISTA'
-- 2
SELECT P.NOME, M.NOME, C.DATA, C.HORA
FROM CONSULTAS C, PACIENTES P, MEDICOS M
WHERE DATA = '25/09/2006' AND HORA BETWEEN '07:00' AND '12:00'
AND P.CODP = C.CODP
AND M.CODM = C.CODM
-- 3
SELECT DISTINCT F.NOME, F.SALARIO
FROM FUNCIONARIOS F, CONSULTAS C, PACIENTES P
WHERE F.CIDADE LIKE 'FLORIANÓPOLIS' OR F.CIDADE LIKE 'PALHOÇA'
AND P.CODF = F.CODF
AND C.CODP = P.CODP
-- 4
SELECT NOME, CPF
FROM MEDICOS
WHERE ESPECIALIDADE LIKE 'PEDIATRA'
AND NROA NOT IN (101,102)
-- 5
SELECT DISTINCT F.NOME, F.SALARIO
FROM FUNCIONARIOS F, CONSULTAS C, PACIENTES P
WHERE F.CIDADE LIKE 'FLORIANÓPOLIS' OR F.CIDADE LIKE 'PALHOÇA'
AND P.CODF = F.CODF
AND C.CODP = P.CODP
AND C.DATA = '2006-10-20'

-- 6
SELECT NROA, ANDAR
FROM AMBULATORIOS
WHERE NROA NOT IN (SELECT NROA FROM MEDICOS)
-- 7
SELECT A.NROA, M.CODM, M.NOME
FROM AMBULATORIOS A, MEDICOS M
WHERE M.NROA = A.NROA
-- 8
CREATE TABLE AGENDA (
ID INT IDENTITY NOT NULL PRIMARY KEY,
CODM INT NOT NULL,
DATA VARCHAR(30)NOT NULL,
)
CREATE TRIGGER TG_AGENDA ON CONSULTAS
FOR INSERT, UPDATE AS
DECLARE @MEDICOS INT
DECLARE @CONSULTAS VARCHAR
SELECT @MEDICOS = CODM,
@CONSULTAS = DATA
FROM INSERTED
UPDATE CONSULTAS
SET DATA = @CONSULTAS
WHERE CODM = @MEDICOS

INSERT INTO AGENDA VALUES (8107,'25/11/2010')


SELECT * FROM CONSULTAS WHERE CODM = 8107