Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE DATABASE intergalactique;
- CREATE TABLE base (
- id INT PRIMARY KEY IDENTITY(1,1),
- name VARCHAR(80) NOT NULL,
- );
- CREATE TABLE continent (
- id INT PRIMARY KEY IDENTITY(1,1),
- name VARCHAR(80) NOT NULL,
- );
- CREATE TABLE terrestrial (
- id INT PRIMARY KEY IDENTITY(1,1),
- name VARCHAR(80) NOT NULL,
- fkContinent INT,
- CONSTRAINT fkTerrestrialContinent
- FOREIGN KEY (fkContinent)
- REFERENCES continent(id)
- );
- CREATE TABLE martian (
- id INT PRIMARY KEY IDENTITY(1,1),
- name VARCHAR(80) NOT NULL,
- fkBase INT,
- CONSTRAINT fkBase
- FOREIGN KEY (id)
- REFERENCES base(id),
- fkTerrestrial INT,
- CONSTRAINT fkmartianTerrestrial
- FOREIGN KEY (fkTerrestrial)
- REFERENCES terrestrial(id)
- );
- -- filling the table base
- INSERT INTO base (name)
- VALUES ('base1');
- INSERT INTO base (name)
- VALUES ('base BABA');
- INSERT INTO base (name)
- VALUES ('base Luna');
- INSERT INTO base (name)
- VALUES ('base Lola');
- -- filling table continent
- INSERT INTO continent (name)
- VALUES ('Africa');
- INSERT INTO continent (name)
- VALUES ('North America');
- INSERT INTO continent (name)
- VALUES ('Europe');
- INSERT INTO continent (name)
- VALUES ('Asia');
- INSERT INTO continent (name)
- VALUES ('South America');
- INSERT INTO continent (name)
- VALUES ('Central America');
- INSERT INTO continent (name)
- VALUES ('Oceania');
- -- filling terrestrial table
- INSERT INTO terrestrial (name, fkContinent)
- VALUES ('Tata', 2);
- INSERT INTO terrestrial (name, fkContinent)
- VALUES ('Tete', 1);
- INSERT INTO terrestrial (name, fkContinent)
- VALUES ('Titi', 3);
- INSERT INTO terrestrial (name, fkContinent)
- VALUES ('Toto', 5);
- INSERT INTO terrestrial (name, fkContinent)
- VALUES ('Tutu', 4);
- -- filling martian table
- INSERT INTO martian (name, fkBase, fkTerrestrial)
- VALUES ('Gogo', 2, 4);
- INSERT INTO martian (name, fkBase, fkTerrestrial)
- VALUES ('Gaga', 1, 2);
- INSERT INTO martian (name, fkBase, fkTerrestrial)
- VALUES ('Gigi', 3, 1);
- INSERT INTO martian (name, fkBase, fkTerrestrial)
- VALUES ('Gugu', 1, 3);
- -- joins
- SELECT martian.id, martian.name AS martianName, base.name AS baseName, terrestrial.name AS terrestrialReferent, continent.name AS terrestrialContinent
- FROM martian
- INNER JOIN base
- ON base.id = martian.fkBase
- INNER JOIN terrestrial
- ON martian.fkTerrestrial = terrestrial.id
- INNER JOIN continent
- ON continent.id = terrestrial.fkContinent;
Add Comment
Please, Sign In to add comment