From 7b65c535bfb3f0697abc5b8be8d9f93f99d0ee60 Mon Sep 17 00:00:00 2001 From: alainn-n Date: Thu, 23 Jun 2022 23:40:43 -0300 Subject: [PATCH] =?UTF-8?q?Entrega=20Exercicios=20Aula=20-=20Rela=C3=A7?= =?UTF-8?q?=C3=B5es=20em=20SQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modulo5/relacoes-sql/.md | 107 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 modulo5/relacoes-sql/.md diff --git a/modulo5/relacoes-sql/.md b/modulo5/relacoes-sql/.md new file mode 100644 index 0000000..be7cd8c --- /dev/null +++ b/modulo5/relacoes-sql/.md @@ -0,0 +1,107 @@ +## Ex 1 +A) Campo que estabelece o relacionamento entre duas tabelas. + +B) CREATE TABLE rating ( + id VARCHAR(255) PRIMARY KEY, + comment TEXT NOT NULL, + rate FLOAT NOT NULL, + movie_id VARCHAR(255), + FOREIGN KEY (movie_id) REFERENCES movie(id) +); +INSERT INTO rating (id, comment, rate, movie_id) +VALUES ( + "001", + "Muito Ruim!", + 1, + "001" +); +INSERT INTO rating (id, comment, rate, movie_id) +VALUES ( + "002", + "Muito Ruim!", + 1, + "002" +); +INSERT INTO rating (id, comment, rate, movie_id) +VALUES ( + "003", + "Muito Ruim!", + 1, + "003" +); +INSERT INTO rating (id, comment, rate, movie_id) +VALUES ( + "004", + "Muito Ruim!", + 1, + "004" +); +INSERT INTO rating (id, comment, rate, movie_id) +VALUES ( + "005", + "Muito Ruim!", + 1, + "005" +); + +c) Cannot add or update a child row: a foreign key constraint fails +Não é possível adicionar ou 'atualizar' um elemento filho; + +D) ALTER TABLE movie DROP COLUMN rating; + +E) Cannot delete or update a parent row: a foreign key constraint fails +Não é possível deletar ou 'atualizar' um banco de dados parente; + + +## Ex 2 +A) A ttabela relaciona o Ator (Actor) com o filme (Movie) + +B) CREATE TABLE movieCast ( + movie_id VARCHAR(255), + actor_id VARCHAR(255), + FOREIGN KEY (movie_id) REFERENCES movie(id), + FOREIGN KEY (actor_id) REFERENCES actor(id) +); +INSERT INTO movieCast(movie_id, actor_id) +VALUES( + "001", + "001" +); +INSERT INTO movieCast(movie_id, actor_id) +VALUES( + "001", + "002" +); +INSERT INTO movieCast(movie_id, actor_id) +VALUES( + "002", + "004" +); +INSERT INTO movieCast(movie_id, actor_id) +VALUES( + "003", + "007" +); +INSERT INTO movieCast(movie_id, actor_id) +VALUES( + "004", + "001" +); +INSERT INTO movieCast(movie_id, actor_id) +VALUES( + "004", + "005" +); +INSERT INTO movieCast(movie_id, actor_id) +VALUES( + "005", + "020" +); + +C) Cannot add or update a child row: a foreign key constraint fails +Não é possível adicionar ou 'atualizar' um elemento filho; + +D) ALTER TABLE movie DROP COLUMN rating; + +E) Cannot delete or update a parent row: a foreign key constraint fails +Não é possível deletar um elemento de outro banco e dados. \ No newline at end of file