Você está na página 1de 2

create database biblioteca;

use biblioteca;
create table livro(
titulo char (30),
edicao varchar(30),
ano_publicacao date,
isbn char (30),
cod_livro integer primary key);

create table curso(


nome char(30),
cod_curso integer primary key);

create table atendente(


nome char (30),
email varchar (45),
cod_atendente integer);

create table emprestimo_livro(


emprestimo varchar(40),
data_prevista_emprestimo date,
cod_emprestimo integer primary key,
cod_livroo integer);

create table emprestimo(


hora time,
data date,
cod_emprestimo integer,
cod_atendente integer,
cod_usuario integer);

create table usuario(


nome varchar (60),
email varchar (60),
senha varchar (8),
cod_usuario integer,
cod_curso integer,
primary key (cod_usuario));

ALTER TABLE `biblioteca`.`emprestimo_livro`


ADD CONSTRAINT `cod_emprestimo`
FOREIGN KEY (`cod_emprestimo`)
REFERENCES `biblioteca`.`emprestimo` (`cod_emprestimo`)
ON DELETE CASCADE
ON UPDATE CASCADE;

ALTER TABLE `biblioteca`.`emprestimo`


ADD CONSTRAINT `cod_usuario`
FOREIGN KEY (`cod_usuario`)
REFERENCES `biblioteca`.`usuario` (`cod_usuario`)
ON DELETE CASCADE
ON UPDATE CASCADE;

ALTER TABLE `biblioteca`.`atendente`


ADD INDEX `cod_atendent_idx` (`cod_atendente` ASC)
'';
ALTER TABLE `biblioteca`.`atendente`
ADD CONSTRAINT `cod_atendent`
FOREIGN KEY (`cod_atendente`)
REFERENCES `biblioteca`.`emprestimo` (`cod_atendente`)
ON DELETE CASCADE
ON UPDATE CASCADE;

ALTER TABLE `biblioteca`.`usuario`


CHANGE COLUMN `cod_curso` `cod_curso` INT(11) NOT NULL '' ,
ADD UNIQUE INDEX `cod_curso_UNIQUE` (`cod_curso` ASC) COMMENT '';

select titulo, isbn


from livro
order by isbn asc;

select *, count(cod_usuario)
from usuario;

select usuario.cod_usuario, usuario.nome, curso.nome


from usuario left outer join curso on (usuario.cod_curso = curso.cod_curso);
select titulo
from livro right outer join empresa on (emprestimo

Você também pode gostar