-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09. Table relations.sql
60 lines (46 loc) · 1.14 KB
/
09. Table relations.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
USE [Geography]
SELECT * FROM Mountains
SELECT * FROM Peaks
SELECT * FROM MountainsCountries
SELECT * FROM Countries
SELECT
MountainRange,
PeakName,
Elevation
FROM Mountains AS m
JOIN Peaks AS p ON m.Id = p.MountainId
WHERE m.MountainRange = 'Rila'
ORDER BY p.Elevation DESC
--
SELECT
c.CountryName,
MountainRange,
PeakName,
Elevation
FROM Mountains AS m
JOIN Peaks AS p ON m.Id = p.MountainId
JOIN MountainsCountries AS mc ON m.Id = mc.MountainId
JOIN Countries AS c ON mc.CountryCode = c.CountryCode
WHERE m.MountainRange = 'Rila'
ORDER BY p.Elevation DESC
CREATE DATABASE ForTry
CREATE TABLE Drivers(
DriverID INT PRIMARY KEY,
DriverName VARCHAR(50)
)
CREATE TABLE Cars(
CarID INT PRIMARY KEY,
DriverID INT,
CONSTRAINT FK_Car_Driver FOREIGN KEY(DriverID)
REFERENCES Drivers(DriverID)
)
SELECT
*
FROM Cars AS c
JOIN Drivers AS d ON c.DriverID = d.DriverID
ALTER TABLE Cars DROP CONSTRAINT FK_Car_Driver
ALTER TABLE Cars ADD CONSTRAINT FK_Car_Driver FOREIGN KEY(DriverID)
REFERENCES Drivers(DriverID) ON DELETE CASCADE
DELETE FROM Drivers
WHERE DriverID = 3
SELECT * FROM Cars