--
-- Estruturas create tables
--
create table livro(
Cod_livro int not null primary key identity(1,1),
titulo varchar(100),
ano_publicacao varchar(100),
edicao varchar(100),
isbn char(8));
create table atendente(
Cod_atendente int not null primary key identity(1,1),
nome varchar(100),
email varchar(100));
create table emprestimo(
Cod_emprestimo int not null primary key identity(1,1),
Cod_usuario int,
Cod_atendente int,
data char(8),
hora char(8),
foreign key (cod_atendente) references atendente (cod_atendente));
create table emprestimo_livro(
Cod_emprestimo_livro int not null primary key identity(1,1),
data_prevista_entrega date not null,
Cod_emprestimo int,
foreign key (cod_emprestimo) references emprestimo (cod_emprestimo),
Cod_livro int,
foreign key (cod_livro) references livro (cod_livro));
select * from emprestimo_livro
create table Curso(
Cod_curso int not null primary key identity(1,1),
Nome varchar(100));
create table Usuario(
Cod_usuario int not null primary key identity(1,1),
nome varchar(100),
email varchar(100),
senha char(8),
Cod_curso int,
foreign key (cod_curso) references curso(cod_curso));
select * from emprestimo_livro
alter table emprestimo add cod_usuario int alter cod_atendente
alter table emprestimo add foreign key (cod_usuario) references usuario(cod_usuario)


insert into usuario
values ('Anderson', 'anderson@ftec.com','111111', '3'),
('Pietro', 'pietro@ftec.com','222222', '2'),
('Fausto', 'fausto@ftec.com','333333', '1');
--
insert into livro
values ('A Guerra dos Tronos', '2010','Gelo e Fogo', '1'),
('A Fúria dos Reis', '2011','Gelo e Fogo', '2'),
('A Tormenta de Espadas', '2011','Gelo e Fogo', '3'),
('O Festim dos Corvos', '2012','Gelo e Fogo', '4'),
('A Dança dos Dragões', '2011','Gelo e Fogo', '5');
end
--
-- Estruturas create tables
--
create table livro(
Cod_livro int not null primary key identity(1,1),
titulo varchar(100),
ano_publicacao varchar(100),
edicao varchar(100),
isbn char(8));
create table atendente(
Cod_atendente int not null primary key identity(1,1),
nome varchar(100),
email varchar(100));
create table emprestimo(
Cod_emprestimo int not null primary key identity(1,1),
Cod_usuario int,
Cod_atendente int,
data char(8),
hora char(8),
foreign key (cod_atendente) references atendente (cod_atendente));
create table emprestimo_livro(
Cod_emprestimo_livro int not null primary key identity(1,1),
data_prevista_entrega date not null,
Cod_emprestimo int,
foreign key (cod_emprestimo) references emprestimo (cod_emprestimo),
Cod_livro int,
foreign key (cod_livro) references livro (cod_livro));
select * from emprestimo_livro
create table Curso(
Cod_curso int not null primary key identity(1,1),
Nome varchar(100));
create table Usuario(
Cod_usuario int not null primary key identity(1,1),
nome varchar(100),
email varchar(100),
senha char(8),
Cod_curso int,
foreign key (cod_curso) references curso(cod_curso));
select * from emprestimo_livro
alter table emprestimo add foreign key (cod_usuario) references usuario(cod_usuario)
alter table emprestimo add cod_usuario int after cod_atendente
select * from Curso
insert into Curso
values ('Redes');
insert into Curso
values ('ADS');
insert into Curso
values ('GTI');
select * from Curso
select * from Usuario
insert into usuario
values ('Anderson', 'anderson@ftec.com','111111', '3'),
('Pietro', 'pietro@ftec.com','222222', '2'),
('Fausto', 'fausto@ftec.com','333333', '1');
create procedure ps_insert_Usuario
@Cod_curso int,
@Nome varchar(100),
@Email varchar(100),
@senha char(8)
as
begin
set nocount on
insert into Usuario
(Cod_curso,
Nome,
Email,
Senha)
values
(@Cod_curso,
@Nome,
@Email,
@senha)
end
exec ps_insert_Usuario
'Gabriel', 'gabriel@ftec.com', '444444', '1'
go
select * from Usuario
select Case Cod_curso
when 1 then 'ADS'
when 2 then 'REDES'
WHEN 3 then 'GTI'
else 'ERROUUUUU'
end as curso
from usuario
create procedure ps_insert_atendente
@nome varchar (100),
@email varchar (100)
as
begin
set nocount on
insert into atendente
(
nome,
email
)
values
(
@nome,
@email
)
end
exec ps_insert_atendente
'paulo',
'paulo@ftec.com'
go
create procedure ps_insert_Usuario
@Cod_curso int,
@Nome varchar(100),
@Email varchar(100),
@senha char(8)
as
begin
set nocount on
insert into Usuario
(Cod_curso,
Nome,
Email,
Senha)
values
(@Cod_curso,
@Nome,
@Email,
@senha)
end
EXEC ps_insert_Usuario
'felipe', 'felipeto@ftec.com', '555555'
select * from livro
insert into livro
values ('A Guerra dos Tronos', '2010','Gelo e Fogo', '1'),
('A Fúria dos Reis', '2011','Gelo e Fogo', '2'),
('A Tormenta de Espadas', '2011','Gelo e Fogo', '3'),
('O Festim dos Corvos', '2012','Gelo e Fogo', '4'),
('A Dança dos Dragões', '2011','Gelo e Fogo', '5');
end
select * from livro
create procedure [dbo].[ps_del_usuario]
@Cod_usuario int
as
begin
set nocount on
delete Usuario
where Cod_usuario = @Cod_usuario
end
GO
exec ps_del_usuario
--@Cod_usuario = 7
@Nome = 'felipe'
select * from Usuario
Create procedure ps_sel_usuario
@Cod_usuario int
as
begin
Select
Cod_curso,
Nome,
Email,
Senha
From Usuario
Where Cod_usuario = @Cod_usuario
end
EXEC ps_sel_usuario
@Cod_usuario = 5
GO
select * from Usuario
Create procedure ps_upd_usuario
@nome varchar (100),
@senha char (8)
as
begin
update Usuario
set senha = @senha
where nome = @nome
end
exec ps_upd_usuario
@nome = 'felipe',
@senha = '888888'
select * from usuario
select * from livro
CREATE VIEW vwlivro
AS
SELECT Cod_livro AS Código
, titulo AS livro
, isbn
, ano_publicacao AS [Ano equivalente]
FROM
livro
select * from vwlivro
CREATE VIEW vwUsuario
AS
SELECT Cod_usuario AS Código
, nome AS Usuario
, senha
, email AS [Dominico FTEC]
FROM
Usuario
select * from vwUsuario
select * from atendente
select * from pessoa
exec ps_insert_atendente
'Igor', 'igor@ftec.com'
go
exec ps_insert_atendente
'Fisher', 'fisher@ftec.com'
go
exec ps_insert_atendente
'Schultz', 'schultz@ftec.com'
go
exec ps_insert_atendente
'Fernando', 'fernando@ftec.com'
go
exec ps_insert_atendente
'Rose', 'rose@ftec.com'
go
exec ps_insert_atendente
'Fabio', 'fabio@ftec.com'
go
exec ps_insert_atendente
'Rita', 'rita@ftec.com'
go
exec ps_insert_atendente
'Pontello', 'pontello@ftec.com'
go
exec ps_insert_atendente
'Vanessa', 'vanessa@ftec.com'
go
exec ps_insert_atendente
'Jessica', 'jessica@ftec.com'
go
CREATE TABLE pessoa
(Cod_atendente int not null primary key,
Nome Varchar (100))
GO
CREATE DATABASE JOINS
GO
SELECT atendente.*, Usuario.*
FROM atendente
INNER JOIN Usuario ON atendente.Cod_atendente = usuario.Cod_usuario
SELECT atendente.*, usuario.*
FROM atendente
LEFT JOIN usuario ON atendente.Cod_atendente = usuario.Cod_usuario
SELECT atendente.*, usuario.*
FROM atendente
FULL OUTER JOIN usuario ON atendente.Cod_atendente = usuario.Cod_usuario
select * from atendente
INSERT INTO ATENDENTE VALUES('Elvis',getdate());
CREATE TRIGGER TR_INSERT_ATENDENTE ON ATENDENTE
FOR INSERT
AS
SELECT 'DADOS A SEREM INSERIDOS:' ,* FROM INSERTED
GO
INSERT INTO ATENDENTE VALUES('Aleluia',getdate());
CREATE TRIGGER TR_DELETE_ATENDENTE ON ATENDENTE
FOR DELETE
AS
SELECT 'DADOS A SEREM APAGADOS:', * FROM DELETED
GO
Delete from ATENDENTE where Cod_atendente= 15
CREATE TRIGGER TR_BLOQUEIA_TABELA ON ATENDENTE
FOR INSERT , UPDATE , DELETE
AS
SELECT 'NAO COLOCA OS DEDOS ONDE NAO DEVE !!!'
ROLLBACK
GO
ALTER TABLE ATENDENTE DISABLE TRIGGER TR_BLOQUEIA_TABELA;
ALTER TABLE ATENDENTE DISABLE TRIGGER ALL;
ALTER TABLE ATENDENTE ENABLE TRIGGER TR_BLOQUEIA_TABELA;
ALTER TABLE ATENDENTE ENABLE TRIGGER ALL;
IF ( OBJECT_ID('dbo.TR_ATENDENTE_LOG') IS NOT NULL )
DROP TRIGGER dbo.TR_ATENDENTE_LOG
GO
-- TRIGGER DE UPDATE
CREATE TRIGGER TR_UPDATE_ATENDENTE ON atendente
FOR
UPDATE
AS
SELECT 'DADOS ANTIGOS: ', * FROM DELETED
SELECT 'DADOS NOVOS: ', * FROM INSERTED
GO
update atendente
set nome = 'Joaozinho'
where Cod_atendente= 4
CREATE TRIGGER TR_UPDATE_NOME_CAMPO ON atendente
FOR UPDATE
AS
IF (Select nome from Inserted) <> (Select nome from Deleted)
BEGIN
SELECT 'DADOS ANTIGOS: ', nome FROM DELETED
SELECT 'DADOS NOVOS: ', nome FROM INSERTED
END
GO
Create Trigger tru_audit on Atendente for insert,update as
update Atendente set usuario=suser_name(),dataAlterado=getdate() where codigo in
(select codigo from inserted);
CREATE TRIGGER TR_ATENDENTE_LOG ON cliente
FOR
UPDATE,INSERT,DELETE
AS
DECLARE
@value_type_query VARCHAR(10),
@cod_atendente INTEGER ,
@new_nome VARCHAR(50),
@new_email VARCHAR(50),
@old_nome VARCHAR(50),
@old_email CHAR(1);
SELECT @Cod_atendente = cod_atendente,
@old_nome = nome,
@old_email = email,
FROM DELETED
SELECT @Cod_atendente = @cod_atendente,
@new_nome = nome,
@new_email = email,
FROM INSERTED
Create procedure PS_Insert_curso
@nome varchar (100)
as
begin
set nocount on
insert into Curso
(nome) values (@nome)
end
select * from Usuario
Create procedure PS_Insert_Usuario1
@cod_curso int,
@nome varchar (100),
@email varchar (100),
@senha char (25)
as
begin
set nocount on
insert into Usuario
(
Cod_curso,
nome,
email,
senha
)
values
(
@Cod_curso,
@nome,
@email,
@senha
)
end
select * from emprestimo_livro
Create procedure PS_Insert_emprestimo_livro
@cod_emprestimo int,
@cod_livro int,
@emprestado char (1),
@data_prevista_entrega date
as
begin
set nocount on
insert into emprestimo_livro
(
cod_emprestimo,
cod_livro,
emprestado,
data_prevista_entrega
)
values
(
@cod_emprestimo,
@cod_livro,
@emprestado,
@data_prevista_entrega
)
end
EXEC PS_Insert_emprestimo_livro
1,2, 'Y', '2016-09-09'
Go
EXEC PS_Insert_emprestimo_livro
2,1, 'Y', '2016-10-10'
Go
EXEC PS_Insert_emprestimo_livro
2,2, 'Y', '2016-11-11'
Go
------------------ Tabela Auditoria ---------------------
create table auditoria
(
cod_auditoria int not null primary key identity (1,1),
data_auditoria datetime not null,
user_auditoria varchar (100) not null,
cod_tabela varchar (25) not null,
nome_tabela varchar (50) not null,
nome_coluna varchar (50) not null,
valor_antigo varchar (75) default null,
Valor_novo varchar (75) default null,
tipo varchar (25) default null);
IF (OBJECT_ID ('dbo_ps_auditoria') is not null)
drop procedure dbo.pc_auditoria
go
Create procedure dbo.ps_auditoria
@cod_tabela varchar (25),
@nome_tabela varchar (50),
@nome_coluna varchar (50),
@valor_antigo varchar (100),
@valor_novo varchar (100),
@tipo varchar (25)
as
begin
set nocount on
DECLARE @_DATA DATETIME = getdate();--decalara a variavel e captura data e hora
DECLARE @USER VARCHAR(40) = SYSTEM_USER;--decalara a variavel e captura usuário
insert into dbo.auditoria
VALUES(@_DATA,@USER,@cod_tabela,@nome_tabela,@nome_coluna,@valor_antigo,@valor_novo,@
tipo);
END
GO
IF (OBJECT_ID ('dbo_vw_atendente') is not null)
drop procedure dbo.vw_atendente
go
select * from atendente
create view vw_atendente
as
select cod_atendente as "codigo do cliente",
nome as "nome do atendente",
email as "email"
from atendente
go
select * from Curso
IF (OBJECT_ID ('dbo_vw_curso') is not null)
drop procedure dbo.vw_curso
go
create view vw_curso
as
select cod_curso as "codigo do curso",
nome as "nome do curso"
from Curso
go
select * from emprestimo
IF (OBJECT_ID ('dbo_vw_emprestimo') is not null)
drop procedure dbo.vw_emprestimo
go
create view vw_emprestimo_livro
as
select cod_emprestimo as "codigo do emprestimo",
cod_usuario as "codigo do usuario",
cod_atendente as "codigo do atendente",
data as "data",
hora as "hora"
from emprestimo
go
select * from emprestimo_livro
IF (OBJECT_ID ('dbo_vw_emprestimo_livro') is not null)
drop procedure dbo.vw_emprestimo_livro
go
create view vw_emprestimo_livro
as
select Cod_emprestimo_livro as "codigo do emprestimo do livro",
Cod_emprestimo as "codigo do emprestimo",
Cod_livro as "codigo do livro",
emprestimo as "Emprestado",
Case emprestado
when 's' then 'sim'
when 'n' then 'nao'
else 'nao identificado'
end as [emprestado],
data_prevista_entrega as "data prevista para entrega"
from emprestimo_livro
go
Create procedure PS_Insert_emprestimo
@cod_usuario int,
@cod_atendente int
as
begin
set nocount on
insert into emprestimo
(
Cod_usuario,
Cod_atendente
)
values
(
@cod_usuario,
@cod_atendente
)
end
alter table emprestimo_livro
add constraint emprestado
check (emprestado = 's' or emprestado = 'n')
alter table emprestimo_livro
add constraint fk_emprestimo_livro_livro foreign key (cod_livro)
references livro (cod_livro)
alter table emprestimo_livro
add constraint fk_emprestimo_livro_emprestimo foreign key (cod_emprestimo)
references emprestimo (cod_emprestimo)
alter table emprestimo
add constraint fk_emprestimo_usuario foreign key (cod_usuario)
references usuario (cod_usuario)
alter table emprestimo
add constraint fk_emprestimo_atendente foreign key (cod_atendente)
references atendente (cod_atendente)
alter table usuario
add constraint fk_emprestimo_curso foreign key (cod_curso)
references curso (cod_curso)
IF (OBJECT_ID ('dbo_tr_atendente_log') is not null)
drop trigger dbo.tr_atendente_log
go
select * from atendente
CREATE TRIGGER TR_ATENDENTE1_LOG ON atendente
FOR
UPDATE,INSERT,DELETE
AS
--declaração das variaveis usadas na trigger
DECLARE
@cod_atendente int,
@new_nome varchar(100),
@new_email varchar(100),
@old_nome varchar(100),
@old_email varchar(100);
SELECT
@cod_atendente = cod_atendente,
@new_nome = nome,
@new_email = email
FROM deleted
SELECT
@cod_atendente = cod_atendente,
@new_nome = nome,
@new_email = email
FROM inserted
--verificando o tipo de DML(insert, update, delete)
IF EXISTS (SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED)
BEGIN
SET @cod_atendente = 'UPDATE';
END
IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS(SELECT * FROM DELETED)
BEGIN
SET @cod_atendente = 'INSERT';
END
IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED)
BEGIN
SET @cod_atendente = 'DELETE';
END
select * from curso
IF (OBJECT_ID ('dbo_tr_curso_log') is not null)
drop trigger dbo.tr_curso_log
go
create trigger tr_curso_log on curso
FOR
UPDATE,INSERT,DELETE
AS
--declaração das variaveis usadas na trigger
DECLARE
@valor_tipo varchar (25),
@cod_curso int,
@new_nome varchar(50),
@old_nome varchar(50)
SELECT
@cod_curso = cod_curso,
@old_nome = nome
FROM deleted
SELECT
@cod_curso = cod_curso,
@new_nome = nome
FROM inserted
IF EXISTS (SELECT * FROM INSERTED) AND not EXISTS(SELECT * FROM DELETED)
BEGIN
SET @cod_curso = 'UPDATE';
END
IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS(SELECT * FROM DELETED)
BEGIN
SET @valor_tipo = 'INSERT';
END
IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED)
BEGIN
SET @valor_tipo = 'DELETE';
END
select * from livro
IF (OBJECT_ID ('dbo_tr_livro_log') is not null)
drop trigger dbo.tr_livro_log
go
create trigger tr_livro_log on livro
FOR
UPDATE,INSERT,DELETE
AS
--declaração das variaveis usadas na trigger
DECLARE
@valor_tipo varchar (25),
@cod_livro int,
@new_titulo varchar(100),
@new_ano_publicacao char(4),
@new_edicao int,
@new_isbn int,
@old_titulo varchar(100),
@old_ano_publicacao char(4),
@old_edicao int,
@old_isbn int
SELECT
@cod_livro = cod_livro,
@old_titulo = titulo,
@old_ano_publicacao = ano_publicacao,
@old_edicao = edicao,
@old_isbn = isbn
FROM deleted
SELECT
@cod_livro = cod_livro,
@new_titulo = titulo,
@new_ano_publicacao = ano_publicacao,
@new_edicao = edicao,
@new_isbn = isbn
FROM inserted
select * from Usuario
---- trigger usuario ----
IF (OBJECT_ID ('dbo_tr_usuario_log') is not null)
drop trigger dbo.tr_usuario_log
go
create trigger tr_usuario_log on usuario
FOR
UPDATE,INSERT,DELETE
AS
--declaração das variaveis usadas na trigger
DECLARE
@valor_tipo varchar (25),
@cod_usuario int,
@new_cod_curso int,
@new_nome varchar(100),
@new_email varchar(100),
@new_senha char(10),
@old_cod_curso int,
@old_nome varchar(100),
@old_email varchar(100),
@old_senha char(10)
SELECT
@cod_usuario = cod_usuario,
@old_cod_curso = cod_curso,
@old_nome = nome,
@old_email = email,
@old_senha = senha
FROM deleted
SELECT
@cod_usuario = cod_usuario,
@new_cod_curso = cod_curso,
@new_nome = nome,
@new_email = email,
@new_senha = senha
FROM inserted
select * from emprestimo_livro

SQL - Banco de Dados Biblioteca

  • 47.
    -- -- Estruturas createtables -- create table livro( Cod_livro int not null primary key identity(1,1), titulo varchar(100), ano_publicacao varchar(100), edicao varchar(100), isbn char(8)); create table atendente( Cod_atendente int not null primary key identity(1,1), nome varchar(100), email varchar(100)); create table emprestimo( Cod_emprestimo int not null primary key identity(1,1), Cod_usuario int, Cod_atendente int, data char(8), hora char(8), foreign key (cod_atendente) references atendente (cod_atendente)); create table emprestimo_livro( Cod_emprestimo_livro int not null primary key identity(1,1), data_prevista_entrega date not null, Cod_emprestimo int, foreign key (cod_emprestimo) references emprestimo (cod_emprestimo), Cod_livro int, foreign key (cod_livro) references livro (cod_livro)); select * from emprestimo_livro create table Curso( Cod_curso int not null primary key identity(1,1), Nome varchar(100)); create table Usuario( Cod_usuario int not null primary key identity(1,1), nome varchar(100), email varchar(100), senha char(8), Cod_curso int, foreign key (cod_curso) references curso(cod_curso)); select * from emprestimo_livro alter table emprestimo add cod_usuario int alter cod_atendente alter table emprestimo add foreign key (cod_usuario) references usuario(cod_usuario) 
  • 48.
     insert into usuario values('Anderson', 'anderson@ftec.com','111111', '3'), ('Pietro', 'pietro@ftec.com','222222', '2'), ('Fausto', 'fausto@ftec.com','333333', '1'); -- insert into livro values ('A Guerra dos Tronos', '2010','Gelo e Fogo', '1'), ('A Fúria dos Reis', '2011','Gelo e Fogo', '2'), ('A Tormenta de Espadas', '2011','Gelo e Fogo', '3'), ('O Festim dos Corvos', '2012','Gelo e Fogo', '4'), ('A Dança dos Dragões', '2011','Gelo e Fogo', '5'); end
  • 49.
    -- -- Estruturas createtables -- create table livro( Cod_livro int not null primary key identity(1,1), titulo varchar(100), ano_publicacao varchar(100), edicao varchar(100), isbn char(8)); create table atendente( Cod_atendente int not null primary key identity(1,1), nome varchar(100), email varchar(100)); create table emprestimo( Cod_emprestimo int not null primary key identity(1,1), Cod_usuario int, Cod_atendente int, data char(8), hora char(8), foreign key (cod_atendente) references atendente (cod_atendente)); create table emprestimo_livro( Cod_emprestimo_livro int not null primary key identity(1,1), data_prevista_entrega date not null, Cod_emprestimo int, foreign key (cod_emprestimo) references emprestimo (cod_emprestimo), Cod_livro int, foreign key (cod_livro) references livro (cod_livro)); select * from emprestimo_livro create table Curso( Cod_curso int not null primary key identity(1,1), Nome varchar(100)); create table Usuario( Cod_usuario int not null primary key identity(1,1), nome varchar(100), email varchar(100), senha char(8), Cod_curso int, foreign key (cod_curso) references curso(cod_curso)); select * from emprestimo_livro alter table emprestimo add foreign key (cod_usuario) references usuario(cod_usuario) alter table emprestimo add cod_usuario int after cod_atendente select * from Curso insert into Curso values ('Redes'); insert into Curso
  • 50.
    values ('ADS'); insert intoCurso values ('GTI'); select * from Curso select * from Usuario insert into usuario values ('Anderson', 'anderson@ftec.com','111111', '3'), ('Pietro', 'pietro@ftec.com','222222', '2'), ('Fausto', 'fausto@ftec.com','333333', '1'); create procedure ps_insert_Usuario @Cod_curso int, @Nome varchar(100), @Email varchar(100), @senha char(8) as begin set nocount on insert into Usuario (Cod_curso, Nome, Email, Senha) values (@Cod_curso, @Nome, @Email, @senha) end exec ps_insert_Usuario 'Gabriel', 'gabriel@ftec.com', '444444', '1' go select * from Usuario select Case Cod_curso when 1 then 'ADS' when 2 then 'REDES' WHEN 3 then 'GTI' else 'ERROUUUUU' end as curso from usuario create procedure ps_insert_atendente @nome varchar (100), @email varchar (100) as begin set nocount on insert into atendente ( nome, email ) values (
  • 51.
    @nome, @email ) end exec ps_insert_atendente 'paulo', 'paulo@ftec.com' go create procedureps_insert_Usuario @Cod_curso int, @Nome varchar(100), @Email varchar(100), @senha char(8) as begin set nocount on insert into Usuario (Cod_curso, Nome, Email, Senha) values (@Cod_curso, @Nome, @Email, @senha) end EXEC ps_insert_Usuario 'felipe', 'felipeto@ftec.com', '555555' select * from livro insert into livro values ('A Guerra dos Tronos', '2010','Gelo e Fogo', '1'), ('A Fúria dos Reis', '2011','Gelo e Fogo', '2'), ('A Tormenta de Espadas', '2011','Gelo e Fogo', '3'), ('O Festim dos Corvos', '2012','Gelo e Fogo', '4'), ('A Dança dos Dragões', '2011','Gelo e Fogo', '5'); end select * from livro create procedure [dbo].[ps_del_usuario] @Cod_usuario int as begin set nocount on delete Usuario where Cod_usuario = @Cod_usuario end GO exec ps_del_usuario --@Cod_usuario = 7 @Nome = 'felipe'
  • 52.
    select * fromUsuario Create procedure ps_sel_usuario @Cod_usuario int as begin Select Cod_curso, Nome, Email, Senha From Usuario Where Cod_usuario = @Cod_usuario end EXEC ps_sel_usuario @Cod_usuario = 5 GO select * from Usuario Create procedure ps_upd_usuario @nome varchar (100), @senha char (8) as begin update Usuario set senha = @senha where nome = @nome end exec ps_upd_usuario @nome = 'felipe', @senha = '888888' select * from usuario select * from livro CREATE VIEW vwlivro AS SELECT Cod_livro AS Código , titulo AS livro , isbn , ano_publicacao AS [Ano equivalente] FROM livro select * from vwlivro CREATE VIEW vwUsuario AS SELECT Cod_usuario AS Código , nome AS Usuario , senha , email AS [Dominico FTEC] FROM Usuario
  • 53.
    select * fromvwUsuario select * from atendente select * from pessoa exec ps_insert_atendente 'Igor', 'igor@ftec.com' go exec ps_insert_atendente 'Fisher', 'fisher@ftec.com' go exec ps_insert_atendente 'Schultz', 'schultz@ftec.com' go exec ps_insert_atendente 'Fernando', 'fernando@ftec.com' go exec ps_insert_atendente 'Rose', 'rose@ftec.com' go exec ps_insert_atendente 'Fabio', 'fabio@ftec.com' go exec ps_insert_atendente 'Rita', 'rita@ftec.com' go exec ps_insert_atendente 'Pontello', 'pontello@ftec.com' go exec ps_insert_atendente 'Vanessa', 'vanessa@ftec.com' go exec ps_insert_atendente 'Jessica', 'jessica@ftec.com' go CREATE TABLE pessoa (Cod_atendente int not null primary key, Nome Varchar (100)) GO CREATE DATABASE JOINS GO SELECT atendente.*, Usuario.* FROM atendente INNER JOIN Usuario ON atendente.Cod_atendente = usuario.Cod_usuario SELECT atendente.*, usuario.* FROM atendente LEFT JOIN usuario ON atendente.Cod_atendente = usuario.Cod_usuario
  • 54.
    SELECT atendente.*, usuario.* FROMatendente FULL OUTER JOIN usuario ON atendente.Cod_atendente = usuario.Cod_usuario select * from atendente INSERT INTO ATENDENTE VALUES('Elvis',getdate()); CREATE TRIGGER TR_INSERT_ATENDENTE ON ATENDENTE FOR INSERT AS SELECT 'DADOS A SEREM INSERIDOS:' ,* FROM INSERTED GO INSERT INTO ATENDENTE VALUES('Aleluia',getdate()); CREATE TRIGGER TR_DELETE_ATENDENTE ON ATENDENTE FOR DELETE AS SELECT 'DADOS A SEREM APAGADOS:', * FROM DELETED GO Delete from ATENDENTE where Cod_atendente= 15 CREATE TRIGGER TR_BLOQUEIA_TABELA ON ATENDENTE FOR INSERT , UPDATE , DELETE AS SELECT 'NAO COLOCA OS DEDOS ONDE NAO DEVE !!!' ROLLBACK GO ALTER TABLE ATENDENTE DISABLE TRIGGER TR_BLOQUEIA_TABELA; ALTER TABLE ATENDENTE DISABLE TRIGGER ALL; ALTER TABLE ATENDENTE ENABLE TRIGGER TR_BLOQUEIA_TABELA; ALTER TABLE ATENDENTE ENABLE TRIGGER ALL; IF ( OBJECT_ID('dbo.TR_ATENDENTE_LOG') IS NOT NULL ) DROP TRIGGER dbo.TR_ATENDENTE_LOG GO -- TRIGGER DE UPDATE CREATE TRIGGER TR_UPDATE_ATENDENTE ON atendente FOR UPDATE AS SELECT 'DADOS ANTIGOS: ', * FROM DELETED SELECT 'DADOS NOVOS: ', * FROM INSERTED GO update atendente set nome = 'Joaozinho' where Cod_atendente= 4 CREATE TRIGGER TR_UPDATE_NOME_CAMPO ON atendente
  • 55.
    FOR UPDATE AS IF (Selectnome from Inserted) <> (Select nome from Deleted) BEGIN SELECT 'DADOS ANTIGOS: ', nome FROM DELETED SELECT 'DADOS NOVOS: ', nome FROM INSERTED END GO Create Trigger tru_audit on Atendente for insert,update as update Atendente set usuario=suser_name(),dataAlterado=getdate() where codigo in (select codigo from inserted); CREATE TRIGGER TR_ATENDENTE_LOG ON cliente FOR UPDATE,INSERT,DELETE AS DECLARE @value_type_query VARCHAR(10), @cod_atendente INTEGER , @new_nome VARCHAR(50), @new_email VARCHAR(50), @old_nome VARCHAR(50), @old_email CHAR(1); SELECT @Cod_atendente = cod_atendente, @old_nome = nome, @old_email = email, FROM DELETED SELECT @Cod_atendente = @cod_atendente, @new_nome = nome, @new_email = email, FROM INSERTED Create procedure PS_Insert_curso @nome varchar (100) as begin set nocount on insert into Curso (nome) values (@nome) end select * from Usuario Create procedure PS_Insert_Usuario1 @cod_curso int, @nome varchar (100), @email varchar (100), @senha char (25) as begin set nocount on
  • 56.
    insert into Usuario ( Cod_curso, nome, email, senha ) values ( @Cod_curso, @nome, @email, @senha ) end select* from emprestimo_livro Create procedure PS_Insert_emprestimo_livro @cod_emprestimo int, @cod_livro int, @emprestado char (1), @data_prevista_entrega date as begin set nocount on insert into emprestimo_livro ( cod_emprestimo, cod_livro, emprestado, data_prevista_entrega ) values ( @cod_emprestimo, @cod_livro, @emprestado, @data_prevista_entrega ) end EXEC PS_Insert_emprestimo_livro 1,2, 'Y', '2016-09-09' Go EXEC PS_Insert_emprestimo_livro 2,1, 'Y', '2016-10-10' Go EXEC PS_Insert_emprestimo_livro 2,2, 'Y', '2016-11-11' Go ------------------ Tabela Auditoria --------------------- create table auditoria ( cod_auditoria int not null primary key identity (1,1), data_auditoria datetime not null, user_auditoria varchar (100) not null, cod_tabela varchar (25) not null, nome_tabela varchar (50) not null, nome_coluna varchar (50) not null,
  • 57.
    valor_antigo varchar (75)default null, Valor_novo varchar (75) default null, tipo varchar (25) default null); IF (OBJECT_ID ('dbo_ps_auditoria') is not null) drop procedure dbo.pc_auditoria go Create procedure dbo.ps_auditoria @cod_tabela varchar (25), @nome_tabela varchar (50), @nome_coluna varchar (50), @valor_antigo varchar (100), @valor_novo varchar (100), @tipo varchar (25) as begin set nocount on DECLARE @_DATA DATETIME = getdate();--decalara a variavel e captura data e hora DECLARE @USER VARCHAR(40) = SYSTEM_USER;--decalara a variavel e captura usuário insert into dbo.auditoria VALUES(@_DATA,@USER,@cod_tabela,@nome_tabela,@nome_coluna,@valor_antigo,@valor_novo,@ tipo); END GO IF (OBJECT_ID ('dbo_vw_atendente') is not null) drop procedure dbo.vw_atendente go select * from atendente create view vw_atendente as select cod_atendente as "codigo do cliente", nome as "nome do atendente", email as "email" from atendente go select * from Curso IF (OBJECT_ID ('dbo_vw_curso') is not null) drop procedure dbo.vw_curso go create view vw_curso as select cod_curso as "codigo do curso", nome as "nome do curso" from Curso go select * from emprestimo IF (OBJECT_ID ('dbo_vw_emprestimo') is not null) drop procedure dbo.vw_emprestimo go create view vw_emprestimo_livro
  • 58.
    as select cod_emprestimo as"codigo do emprestimo", cod_usuario as "codigo do usuario", cod_atendente as "codigo do atendente", data as "data", hora as "hora" from emprestimo go select * from emprestimo_livro IF (OBJECT_ID ('dbo_vw_emprestimo_livro') is not null) drop procedure dbo.vw_emprestimo_livro go create view vw_emprestimo_livro as select Cod_emprestimo_livro as "codigo do emprestimo do livro", Cod_emprestimo as "codigo do emprestimo", Cod_livro as "codigo do livro", emprestimo as "Emprestado", Case emprestado when 's' then 'sim' when 'n' then 'nao' else 'nao identificado' end as [emprestado], data_prevista_entrega as "data prevista para entrega" from emprestimo_livro go Create procedure PS_Insert_emprestimo @cod_usuario int, @cod_atendente int as begin set nocount on insert into emprestimo ( Cod_usuario, Cod_atendente ) values ( @cod_usuario, @cod_atendente ) end alter table emprestimo_livro add constraint emprestado check (emprestado = 's' or emprestado = 'n') alter table emprestimo_livro add constraint fk_emprestimo_livro_livro foreign key (cod_livro) references livro (cod_livro) alter table emprestimo_livro add constraint fk_emprestimo_livro_emprestimo foreign key (cod_emprestimo) references emprestimo (cod_emprestimo) alter table emprestimo
  • 59.
    add constraint fk_emprestimo_usuarioforeign key (cod_usuario) references usuario (cod_usuario) alter table emprestimo add constraint fk_emprestimo_atendente foreign key (cod_atendente) references atendente (cod_atendente) alter table usuario add constraint fk_emprestimo_curso foreign key (cod_curso) references curso (cod_curso) IF (OBJECT_ID ('dbo_tr_atendente_log') is not null) drop trigger dbo.tr_atendente_log go select * from atendente CREATE TRIGGER TR_ATENDENTE1_LOG ON atendente FOR UPDATE,INSERT,DELETE AS --declaração das variaveis usadas na trigger DECLARE @cod_atendente int, @new_nome varchar(100), @new_email varchar(100), @old_nome varchar(100), @old_email varchar(100); SELECT @cod_atendente = cod_atendente, @new_nome = nome, @new_email = email FROM deleted SELECT @cod_atendente = cod_atendente, @new_nome = nome, @new_email = email FROM inserted --verificando o tipo de DML(insert, update, delete) IF EXISTS (SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED) BEGIN SET @cod_atendente = 'UPDATE'; END IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS(SELECT * FROM DELETED) BEGIN SET @cod_atendente = 'INSERT'; END IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED) BEGIN SET @cod_atendente = 'DELETE'; END
  • 60.
    select * fromcurso IF (OBJECT_ID ('dbo_tr_curso_log') is not null) drop trigger dbo.tr_curso_log go create trigger tr_curso_log on curso FOR UPDATE,INSERT,DELETE AS --declaração das variaveis usadas na trigger DECLARE @valor_tipo varchar (25), @cod_curso int, @new_nome varchar(50), @old_nome varchar(50) SELECT @cod_curso = cod_curso, @old_nome = nome FROM deleted SELECT @cod_curso = cod_curso, @new_nome = nome FROM inserted IF EXISTS (SELECT * FROM INSERTED) AND not EXISTS(SELECT * FROM DELETED) BEGIN SET @cod_curso = 'UPDATE'; END IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS(SELECT * FROM DELETED) BEGIN SET @valor_tipo = 'INSERT'; END IF NOT EXISTS (SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED) BEGIN SET @valor_tipo = 'DELETE'; END select * from livro IF (OBJECT_ID ('dbo_tr_livro_log') is not null) drop trigger dbo.tr_livro_log go create trigger tr_livro_log on livro FOR UPDATE,INSERT,DELETE AS --declaração das variaveis usadas na trigger DECLARE @valor_tipo varchar (25), @cod_livro int,
  • 61.
    @new_titulo varchar(100), @new_ano_publicacao char(4), @new_edicaoint, @new_isbn int, @old_titulo varchar(100), @old_ano_publicacao char(4), @old_edicao int, @old_isbn int SELECT @cod_livro = cod_livro, @old_titulo = titulo, @old_ano_publicacao = ano_publicacao, @old_edicao = edicao, @old_isbn = isbn FROM deleted SELECT @cod_livro = cod_livro, @new_titulo = titulo, @new_ano_publicacao = ano_publicacao, @new_edicao = edicao, @new_isbn = isbn FROM inserted select * from Usuario ---- trigger usuario ---- IF (OBJECT_ID ('dbo_tr_usuario_log') is not null) drop trigger dbo.tr_usuario_log go create trigger tr_usuario_log on usuario FOR UPDATE,INSERT,DELETE AS --declaração das variaveis usadas na trigger DECLARE @valor_tipo varchar (25), @cod_usuario int, @new_cod_curso int, @new_nome varchar(100), @new_email varchar(100), @new_senha char(10), @old_cod_curso int, @old_nome varchar(100), @old_email varchar(100), @old_senha char(10) SELECT @cod_usuario = cod_usuario, @old_cod_curso = cod_curso, @old_nome = nome, @old_email = email,
  • 62.
    @old_senha = senha FROMdeleted SELECT @cod_usuario = cod_usuario, @new_cod_curso = cod_curso, @new_nome = nome, @new_email = email, @new_senha = senha FROM inserted select * from emprestimo_livro