72 lines
1.2 KiB
MySQL
72 lines
1.2 KiB
MySQL
|
DROP TABLE pet;
|
||
|
DROP TABLE person;
|
||
|
DROP TABLE person_pet;
|
||
|
|
||
|
-- ex2.sql
|
||
|
|
||
|
CREATE TABLE person (
|
||
|
id INTEGER PRIMARY KEY,
|
||
|
first_name TEXT,
|
||
|
last_name TEXT,
|
||
|
age INTEGER
|
||
|
);
|
||
|
|
||
|
CREATE TABLE pet (
|
||
|
id INTEGER PRIMARY KEY,
|
||
|
name TEXT,
|
||
|
breed TEXT,
|
||
|
age INTEGER,
|
||
|
dead INTEGER
|
||
|
);
|
||
|
|
||
|
CREATE TABLE person_pet (
|
||
|
person_id INTEGER,
|
||
|
pet_id INTEGER
|
||
|
);
|
||
|
|
||
|
-- ex3.sql
|
||
|
|
||
|
INSERT INTO person (id, first_name, last_name, age)
|
||
|
VALUES (0, "Zed", "Shaw", 37);
|
||
|
|
||
|
INSERT INTO pet (id, name, breed, age, dead)
|
||
|
VALUES (0, "Fluffy", "Unicorn", 1000, 0);
|
||
|
|
||
|
INSERT INTO pet VALUES (1, "Gigantor", "Robot", 1, 1);
|
||
|
|
||
|
-- ex4.sql
|
||
|
|
||
|
INSERT INTO person_pet (person_id, pet_id) VALUES (0, 0);
|
||
|
INSERT INTO person_pet VALUES (0, 1);
|
||
|
|
||
|
-- ex5.sql
|
||
|
|
||
|
SELECT * FROM person;
|
||
|
|
||
|
SELECT name, age FROM pet;
|
||
|
|
||
|
SELECT name, age FROM pet WHERE dead = 0;
|
||
|
|
||
|
SELECT * FROM person WHERE first_name != "Zed";
|
||
|
|
||
|
-- ex6.sql
|
||
|
|
||
|
SELECT pet.id, pet.name, pet.age, pet.dead
|
||
|
FROM pet, person_pet, person
|
||
|
WHERE
|
||
|
pet.id = person_pet.pet_id AND
|
||
|
person_pet.person_id = person.id AND
|
||
|
person.first_name = "Zed";
|
||
|
|
||
|
-- ex7.sql
|
||
|
|
||
|
SELECT name, age FROM pet WHERE dead = 1;
|
||
|
|
||
|
DELETE FROM pet WHERE dead = 1;
|
||
|
|
||
|
SELECT * FROM pet;
|
||
|
|
||
|
INSERT INTO pet VALUES (1, "Gigantor", "Robot", 1, 0);
|
||
|
|
||
|
SELECT * FROM pet;
|