Jared Dunbar 3 лет назад
Родитель
Сommit
0fb1fbc143
Подписано: jared Идентификатор GPG ключа: CF202CC859BAC692
  1. 295
      HAFH.sql
  2. 134
      ZAGI.sql
  3. 33
      inclass.sql

295
HAFH.sql

@ -0,0 +1,295 @@
-- create tables
CREATE TABLE manager (
managerid CHAR(4) NOT NULL,
nfname VARCHAR(15) NOT NULL,
mlname VARCHAR(15) NOT NULL,
mbdate DATE NOT NULL,
msalary NUMERIC (9,2) NOT NULL,
mbonus NUMERIC (9,2),
mresbuildingid CHAR(3),
PRIMARY KEY (managerid)
);
CREATE TABLE managerphone (
managerid CHAR(4) NOT NULL,
mphone CHAR(11) NOT NULL,
PRIMARY KEY (managerid, mphone)
FOREIGN KEY (managerid) REFERENCES manager (managerid)
);
CREATE TABLE building (
buildingid CHAR(3) NOT NULL,
bnooffloors INT NOT NULL,
bmanagerid CHAR(4) NOT NULL,
PRIMARY KEY (buildingid),
FOREIGN KEY (bmanagerid) REFERENCES manager (managerid)
);
CREATE TABLE inspector (
insid CHAR(3) NOT NULL,
insname VARCHAR(15) NOT NULL,
PRIMARY KEY (insid)
);
CREATE TABLE inspecting (
insid CHAR(3) NOT NULL,
buildingid CHAR(3) NOT NULL,
datelast DATE NOT NULL,
datenext DATE NOT NULL,
PRIMARY KEY (insid, buildingid),
FOREIGN KEY (insid) REFERENCES inspector (insid),
FOREIGN KEY (buildingid) REFERENCES building (buildingid)
);
CREATE TABLE corpclient (
ccid CHAR(4) NOT NULL,
ccname VARCHAR(25) NOT NULL,
ccindustry VARCHAR(25) NOT NULL,
cclocation VARCHAR(25) NOT NULL,
ccidreferredby CHAR(4),
PRIMARY KEY (ccid),
UNIQUE (ccname),
FOREIGN KEY (ccidreferredby) REFERENCES corpclient (ccid)
);
CREATE TABLE apartment (
buildingid CHAR(3) NOT NULL,
aptno CHAR(5) NOT NULL,
anppfbedrooms INT NOT NULL,
ccid CHAR(4),
PRIMARY KEY (buildingid, aptno),
FOREIGN KEY (buildingid) REFERENCES building (buildingid),
FOREIGN KEY (ccid) REFERENCES corpclient (ccid)
);
CREATE TABLE staffmember (
smemberid CHAR(4) NOT NULL,
smembername VARCHAR(15) NOT NULL,
PRIMARY KEY (smemberid)
);
CREATE TABLE cleaning (
buildingid CHAR(3) NOT NULL,
aptno CHAR(5) NOT NULL,
smemberid CHAR(4) NOT NULL,
CONSTRAINT cleaningpk
PRIMARY KEY (buildingid, aptno, smemberid),
CONSTRAINT cleaningfk
FOREIGN KEY (buildingid, aptno) REFERENCES apartment (buildingid, aptno)
);
-- insert statements
INSERT INTO manager VALUES (
'M12', 'Boris', 'Grant', '1980-06-20', 60000, null, null
);
INSERT INTO manager VALUES (
'M23', 'Austin', 'Lee', '1975-10-30', 50000, 5000, null
);
INSERT INTO manager VALUES (
'M34', 'George', 'Sherman', '1976-01-11', 52000, 2000, null
);
INSERT INTO managerphone VALUES ('M12', '555-2222');
INSERT INTO managerphone VALUES ('M12', '555-3232');
INSERT INTO managerphone VALUES (`M23`, '555-9988');
INSERT INTO managerphone VALUES ('M34', '555-9999');
INSERT INTO building VALUES ('B1', '5', 'M12');
INSERT INTO building VALUES ('B2', '6', 'M23');
INSERT INTO building VALUES ('B3', '4', 'M23');
INSERT INTO building VALUES ('B4', '4', 'M34');
INSERT INTO inspector VALUES ('I11', 'Jane');
INSERT INTO inspector VALUES ('I22', 'Niko');
INSERT INTO inspector VALUES ('I33', 'Mick');
INSERT INTO inspecting VALUES (
'I11','B1','2012-05-15','2013-05-14'
);
INSERT INTO inspecting VALUES (
'I11','B2','2013-02-17','2013-05-17'
);
INSERT INTO inspecting VALUES (
'I22','B2','2013-02-17','2013-05-17'
);
INSERT INTO inspecting VALUES (
'I22','B3','2013-01-11','2014-01-11'
);
INSERT INTO inspecting VALUES (
'I33','B3','2013-01-12','2014-01-12'
);
INSERT INTO inspecting VALUES (
'I33','B4','2013-01-11','2014-01-11'
);
INSERT INTO corpclient VALUES (
'C111', 'BlingNotes', 'Music', 'Chicago', null
);
INSERT INTO corpclient VALUES (
'C222', 'SkyJet', 'Airline', 'Oak Park', 'C111'
);
INSERT INTO corpclient VALUES (
'C777', 'WindyCT', 'Music', 'Chicago', 'C222'
);
INSERT INTO corpclient VALUES (
'C888', 'SouthAlps', 'Sports', 'Rosemont', 'C777'
);
INSERT INTO apartment VALUES ('B1', '21', 1, 'C111');
INSERT INTO apartment VALUES ('B1', '41', 1, null);
INSERT INTO apartment VALUES ('B2', '11', 2, 'C222');
INSERT INTO apartment VALUES ('B2', '31', 2, null);
INSERT INTO apartment VALUES ('B3', '11', 2, 'C777');
INSERT INTO apartment VALUES ('B4', '11', 2, 'C777');
INSERT INTO staffmember VALUES ('5432', 'Brian');
INSERT INTO staffmember VALUES ('9876', 'Boris');
INSERT INTO staffmember VALUES ('7652', 'Caroline');
INSERT INTO cleaning VALUES ('B1', '21', '5432');
INSERT INTO cleaning VALUES ('B1', '41', '9876');
INSERT INTO cleaning VALUES ('B2', '31', '5432');
INSERT INTO cleaning VALUES ('B2', '11', '9876');
INSERT INTO cleaning VALUES ('B3', '11', '5432');
INSERT INTO cleaning VALUES ('B4', '11', '7652');
-- Alter Statement 5
ALTER TABLE manager
ADD CONSTRAINT fkresidesin
FOREIGN KEY (mresbuildingid) REFERENCES building (buildingid);
-- Update Statement 3
UPDATE manager
SET mresbuildingid = 'B1'
WHERE managerid = 'M12';
-- Update Statement 4
UPDATE manager
SET mresbuildingid = 'B2'
WHERE managerid = 'M23';
-- Update Statement 5
UPDATE manager
SET mresbuildingid = 'B3'
WHERE managerid = 'M34';
-- Alter Statement 6
ALTER TABLE manager
MODIFY mresbuildingid CHAR(3) NOT NULL;
-- drop first 7 tables
DROP TABLE cleaning;
DROP TABLE staffmember;
DROP TABLE apartment;
DROP TABLE corpclient;
DROP TABLE inspecting;
DROP TABLE inspector;
DROP TABLE managerphone;
-- Alter Statement 7
ALTER TABLE manager
DROP FOREIGN KEY fkresidesin;
-- drop last 2 tables
DROP TABLE building;
DROP TABLE manager;
DROP TABLE manager;
DROP TABLE building;
-- Query 39
SELECT c.ccname AS client, r.ccname AS recommender
FROM corpclient c, corpclient r
WHERE r.ccid = c.ccidreferredby;
-- Query 40
SELECT a.buildingid, a.aptno, c.ccname
FROM apartment a, corpclient c
ON a.ccid = c.ccid;
-- Query 41
SELECT a.buildingid, a.aptno, c.ccname
FROM apartment a, LEFT OUTER JOIN corpclient c
ON a.ccid = c.ccid;
-- Query 42
SELECT a.buildingid, a.aptno, c.ccname
FROM apartment a, RIGHT OUTER JOIN corpclient c
ON a.ccid = c.ccid;
-- Query 43
SELECT a.buildingid, a.aptno, c.ccname
FROM apartment a, LEFT OUTER JOIN corpclient c
ON a.ccid = c.ccid
UNION
--
SELECT a.buildingid, a.aptno, c.ccname
FROM apartment a, RIGHT OUTER JOIN corpclient c
ON a.ccid = c.ccid;
-- Query 44
SELECT m.managerid, m.fname, m.mlname, s.smemberid
FROM manager m, staffmember s
WHERE m.mfname = s.smembername;
-- Query 45
SELECT *
FROM manager
WHERE mbonus IS NULL;
-- Query 46
SELECT *
FROM building
WHERE EXISTS (
SELECT *
FROM manager
WHERE buildingid = mresbuildingid
);
-- Query 47
SELECT *
FROM building
WHERE NOT EXISTS (
SELECT *
FROM manager
WHERE buildingid = mresbuildingid
);
-- Create Table Statement 1
CREATE TABLE cleaningdenormalized (
buildingid CHAR(3) NOT NULL,
aptno CHAR(5) NOT NULL,
smemberid CHAR(4) NOT NULL,
smembername VARCHAR(15) NOT NULL,
PRIMARY KEY (buildingid, aptno, smemberid)
);
-- Insert Statement 2
INSERT INTO cleaningdenormalized
SELECT c.buildingid, c.aptno, s.smemberid, s.smembername
FROM cleaning c, staffmember s
WHERE c.smemberid = s.smemberid;

134
ZAGI.sql

@ -386,3 +386,137 @@ SELECT t.tid, t.tdate, p.productname, sv.noofitems AS quantity, (sv.noofitems *
FROM product p, salestransaction t, soldvia sv
WHERE sv.productid = p.productid AND sv.tid = t.tid
ORDER BY t.tid;
-- Alter Statement 1
ALTER TABLE vendor ADD (
vendorphonenumber CHAR(11)
);
-- Alter Statement 2
ALTER TABLE vendor DROP (
vendorphonenumber
);
-- Insert Statement 1
INSERT INTO product VALUES (
'7X7', 'Airy Sock', '1000', 'MK', 'CP'
);
-- Update Statement 1
UPDATE product
SET productprice = 10
WHERE productid = '7X7';
-- Alter Statement 3
ALTER TABLE product ADD (
discount CHAR(11)
);
-- Update Statement 2
UPDATE product
SET discount = 0.2;
-- Update Statement 3
UPDATE product
SET discount = 0.3
WHERE vendorid = 'MK';
-- Alter Statement 4
ALTER TABLE product DROP (
discount
);
-- Delete Statement 1
DELETE FROM product
WHERE productid = '7X7';
-- Create View Statement 1
-- first line may be broken
CREATE VIEW products_more_than_3_sold AS
SELECT productid, productname, productprice
FROM product
WHERE productid IN (
SELECT productid
FROM soldvia
GROUP BY productid
HAVING SUM(noofitems) > 3
);
-- Query 29a
SELECT *
FROM products_more_than_3_sold;
-- Create View Statement 2
-- first line may be broken
CREATE VIEW products_in_multiple_trnsc AS
SELECT productid, productname, productprice
FROM product
WHERE productid in (
SELECT productid
FROM soldvia
GROUP BY productid
HAVING COUNT(tid) > 1
);
-- Query 30a
SELECT *
FROM products_in_multiple_trnsc;
-- Drop View Statement 1
DROP VIEW products_more_than_3_sold;
-- Drop View Statement 2
DROP VIEW products_in_multiple_trnsc;
-- Query 36
SELECT *
FROM products_more_than_3_sold
UNION
SELECT *
FROM products_in_multiple_trnsc
-- Query 37
SELECT *
FROM products_more_than_3_sold
INNER JOIN products_in_multiple_trnsc
USING (
productid, productname, productprice
);
-- Query 38
SELECT DISTINCT *
FROM products_more_than_3_sold
WHERE (
productid, productname, productprice
) NOT IN (
SELECT *
FROM products_in_multiple_trnsc
);
-- Alternative Query 38
SELECT DISTINCT *
FROM products_more_than_3_sold
LEFT JOIN products_in_multiple_trnsc
USING (
productid, productname, productprice
)
WHERE products_in_multiple_trnsc.productid IS NULL;

33
inclass.sql

@ -0,0 +1,33 @@
-- Show the name and population in millions for the countries of the continent 'South America'.
-- Divide the population by 1000000 to get population in millions.
SELECT name, population/1000000
AS popMillions, continent
FROM world;
-- Show the countries which have a name that includes the word 'United'
SELECT name
FROM countries
WHERE name %like% 'United';
-- Show the countries that are big by area or big by population. Show name, population, and area.
SELECT name, population, area
FROM world
WHERE area > 3000000
OR population > 250000000;
-- For South America, show population
-- not correct
SELECT name, gdp
AS popMillions, gdp/1000000000
FROM world
WHERE continent = 'South America';
-- Show the name and capital where the name and the captial have the same number of characters
SELECT name, capital
FROM world
WHERE length(name) = length(capital)
Загрузка…
Отмена
Сохранить