A1. Structured Query Language, Referential Integrity
A2. Most important difference is that a table is not a set but a relation is.
A3. From any table choose a PK and add some other attribute to it
A4. Dept_Locations has a composite PK : Dnumber, Dlocation
A5. SELECT
dbo.Account.accountNo, dbo.Account.currentBalance,
dbo.Account.currentBalance * x.rate AS Expr1
FROM
dbo.Account CROSS JOIN
(SELECT e.rate
FROM
dbo.ExchangeRates AS e INNER JOIN
dbo.Currency AS c ON e.fromId = c.id INNER JOIN
dbo.Currency AS s ON e.toId = s.id
WHERE (c.name = 'Cdn Dollar') AND (s.name = 'Swe Kronor') ) AS x
A6. SELECT Essn, Dependent_name, Sex, Bdate, Relationship
FROM
dbo.DEPENDENT AS d
WHERE (NOT EXISTS
(SELECT Fname, Minit, Lname, Ssn, Bdate, Address, Sex, Salary, Super_ssn, Dno
FROM
dbo.EMPLOYEE AS e
WHERE (d.Essn = Ssn)))
CREATE VIEW [B1]
AS
SELECT Owner.ownerNo, Employee.id
FROM
Employee INNER JOIN Owner
ON Employee.firstName = Owner.firstName
AND Employee.lastName =Owner.lastName
CREATE VIEW [B2]
AS
SELECT accountNo, SUM(
CASE WHEN Event.[event] = 'deposit' THEN 1
WHEN Event.[event] = 'withdrawal' THEN 2
ELSE 0 END) AS ServiceCharges
FROM
Event
GROUP BY accountNo
CREATE VIEW [B3]
AS
SELECT
e.lastName AS [Employee Last Name], s.lastName AS [Supervisor Last Name]
FROM
Employee AS e INNER JOIN Employee AS s
ON e.supervisorId = s.id AND e.salary > s.salary
CREATE VIEW [B4]
AS
SELECT MAX(currentBalance) AS Maximum, MIN(currentBalance) AS Minimum
FROM
Account
CREATE VIEW [B5]
AS
SELECT TOP 1 accountNo, currentBalance
FROM
dbo.Account
ORDER BY currentBalance DESC
CREATE VIEW [B6]
AS
SELECT dbo.Account.accountNo
, dbo.Account.currentBalance AS [Canadian Dollar]
, dbo.Account.currentBalance * dbo.ExchangeRates.rate AS [Swedish Kronor]
FROM
dbo.Currency AS [from] INNER JOIN dbo.ExchangeRates
ON [from].id = dbo.ExchangeRates.fromId
INNER JOIN dbo.Currency AS [to]
ON dbo.ExchangeRates.toId = [to].id CROSS JOIN dbo.Account
WHERE ([to].name = 'Swe Kronor') AND ([from].name = 'Cdn Dollar')
CREATE VIEW [B7]
AS
SELECT dbo.Range.rangeName AS [Range Name]
, COUNT(dbo.Account.currentBalance) AS [Number of Accounts]
FROM
dbo.Range LEFT OUTER JOIN dbo.Account
ON dbo.Range.miimumValue <= dbo.Account.currentBalance
AND dbo.Range.maximumValue > dbo.Account.currentBalance
GROUP BY dbo.Range.rangeNumber, dbo.Range.rangeName
OR
SELECT
rangeName, (select COUNT(accountNo)
from Account
where currentBalance >= miimumValue
and currentBalance < maximumValue )
FROM dbo.Range
order by rangeNumber
CREATE VIEW [C1]
AS
SELECT Fname, Minit, Lname, Ssn, Bdate, Address, Sex, Salary, Super_ssn, Dno
FROM
dbo.EMPLOYEE
WHERE (Sex NOT IN ('male', 'female'))
CREATE VIEW [C2]
AS
SELECT Dname, Dnumber, Mgr_ssn, Mgr_start_date
FROM
dbo.DEPARTMENT
WHERE (Mgr_ssn NOT IN
(SELECT Mgr_ssn
FROM
dbo.DEPARTMENT AS DEPARTMENT_1
GROUP BY Mgr_ssn
HAVING (COUNT(*) = 1)))
3.a)
CREATE VIEW [C3] AS
SELECT Essn, Dependent_name, Sex, Bdate, Relationship
FROM
dbo.DEPENDENT
WHERE (Essn NOT IN
(SELECT Ssn
FROM
dbo.EMPLOYEE))
3.b)
Cascade delete
Question 4
ALTER TABLE [EMPLOYEE] ADD CONSTRAINT [SuperviseOthers] CHECK (ssn <> super_ssn)
CREATE VIEW [C5]
AS
SELECT SUM(CASE WHEN relationship = 'spouse' THEN 1 ELSE 0 END) AS Spouses
, SUM(CASE WHEN relationship = 'son' THEN 1 ELSE 0 END) AS Sons
, SUM(CASE WHEN relationship = 'daughter' THEN 1 ELSE 0 END) AS Daughter
FROM
dbo.DEPENDENT
© Copyright 2026 Paperzz