所有代码都是正确的,但查询(位于最底部)似乎没有运行。我在 w3schools.com 上查看了更多信息,但我的代码似乎没有任何问题。
任何人都可以看一下并解释一下吗?
--Drop tables--
DROP TABLE Job;
DROP TABLE Employee;
DROP TABLE Purchase;
DROP TABLE SoccerBall;
DROP TABLE Client;
--Create tables--
CREATE TABLE Client
(
ClientId INT NOT NULL,
ClientName VARCHAR(20) NOT NULL,
Street VARCHAR(20) NOT NULL,
City VARCHAR(20) NOT NULL,
ZipCode VARCHAR(5) NOT NULL,
Phone VARCHAR(15) NULL,
EmailAddr VARCHAR(50) NULL,
PRIMARY KEY(ClientId)
);
CREATE TABLE SoccerBall
(
BallId INT NOT NULL,
BallSize NUMBER(1) NOT NULL,
Color VARCHAR(20) NOT NULL,
Material VARCHAR(20) NOT NULL,
PRIMARY KEY(BallId)
);
CREATE TABLE Purchase
(
PurchaseId INT NOT NULL,
ClientId INT NOT NULL,
BallId INT NOT NULL,
PurchaseDate DATE NOT NULL,
PRIMARY KEY (PurchaseId),
FOREIGN KEY (ClientId) REFERENCES Client (ClientId),
FOREIGN KEY (BallId) REFERENCES SoccerBall (BallId)
);
CREATE TABLE Employee
(
EmployeeId INT NOT NULL,
EmployeeName VARCHAR(50) NOT NULL,
PRIMARY KEY (EmployeeId)
);
CREATE TABLE Job
(
JobId INT NOT NULL,
PurchaseId INT NOT NULL,
EmployeeId INT NOT NULL,
AssignDate DATE NOT NULL,
PRIMARY KEY (JobId),
FOREIGN KEY (EmployeeId) REFERENCES Employee (EmployeeId)
);
--Insert data--
INSERT INTO Client VALUES
(1, 'Adidas', '123 Adidas Lane', 'Adida', '22209', '1-800-555-1987', 'corporate@adidas.com');
INSERT INTO Client VALUES
(2, 'Nike', '234 Nike Drive', 'Nikail', '22182', '1-888-555-9876', 'corporate@nike.com');
INSERT INTO Client VALUES
(3, 'Puma', '345 Puma Blvd', 'Puma City', '20912', '1-800-555-6247', 'corporate@puma.com');
INSERT INTO Client VALUES
(4, 'Reebok', '456 Reebok Cove', 'Old Reebok', '20902', '1-800-555-1000', 'corporate@reebok.com');
INSERT INTO Client VALUES
(5, 'Vans', '567 Vans Lane', 'Vancouver', '20725', '1-900-666-2000', 'corporate@vans.com');
INSERT INTO Client VALUES
(6, 'Jordan', '234 Nike Drive', 'Nikail', '22182', '1-888-555-8765', 'corporate@jordan.com');
INSERT INTO Client VALUES
(7, 'Diadora', '345 Puma Blvd', 'Puma City', '20912', '1-800-555-4126', 'corporate@diadora.com');
INSERT INTO SoccerBall VALUES
(11, 1, 'blue', 'felt');
INSERT INTO SoccerBall VALUES
(12, 3, 'red', 'leather');
INSERT INTO SoccerBall VALUES
(13, 3, 'yellow', 'faux leather');
INSERT INTO SoccerBall VALUES
(14, 5, 'black', 'leather');
INSERT INTO SoccerBall VALUES
(15, 4, 'white', 'faux leather');
INSERT INTO SoccerBall VALUES
(16, 3, 'purple', 'rubber');
INSERT INTO SoccerBall VALUES
(17, 1, 'blue', 'faux leather');
INSERT INTO Purchase VALUES
(21, 2, 11, DATE '2013-01-01');
INSERT INTO Purchase VALUES
(22, 4, 12, DATE '2012-03-24');
INSERT INTO Purchase VALUES
(23, 7, 15, DATE '2013-05-01');
INSERT INTO Purchase VALUES
(24, 5, 13, DATE '2005-12-30');
INSERT INTO Purchase VALUES
(25, 6, 16, DATE '1999-01-23');
INSERT INTO Employee VALUES
(111, 'Vivin Viswanathan');
INSERT INTO Employee VALUES
(222, 'Andy Edwards');
INSERT INTO Employee VALUES
(333, 'Frank Hellwig');
INSERT INTO Employee VALUES
(444, 'Vandana Janeja');
INSERT INTO Job VALUES
(1111, 21, 111, DATE '2013-01-02');
INSERT INTO Job VALUES
(5555, 22, 111, DATE '2012-03-26');
INSERT INTO Job VALUES
(2222, 23, 222, DATE '2013-05-08');
INSERT INTO Job VALUES
(3333, 24, 222, DATE '2006-01-02');
INSERT INTO Job VALUES
(4444, 25, 111, DATE '1999-01-30');
--Queries--
--Query 1--Show me the information in the database
SELECT * FROM Client;
SELECT * FROM SoccerBall;
SELECT * FROM Purchase;
SELECT * FROM Employee;
SELECT * FROM Job;
--"Forgot" Quantity Attribute. Adding that--
ALTER TABLE Purchase ADD Quantity NUMBER(5);
UPDATE Purchase SET Quantity = '12000' WHERE ClientId = '1';
UPDATE Purchase SET Quantity = '2492' WHERE ClientId = '2';
UPDATE Purchase SET Quantity = '94203' WHERE ClientId = '3';
UPDATE Purchase SET Quantity = '4394' WHERE ClientId = '4';
UPDATE Purchase SET Quantity = '0' WHERE ClientId = '5';
UPDATE Purchase SET Quantity = '4832' WHERE ClientId = '6';
UPDATE Purchase SET Quantity = '10002' WHERE ClientId = '7';
SELECT * FROM Purchase;
--Queries Continued--
--Query 2--I want to know how many clients we have
SELECT COUNT(*) FROM Client;
--Query 3--Which client ordered the most soccer balls?***********
SELECT MAX(Quantity), ClientName
FROM Purchase P, Client C
WHERE P.ClientId = C.ClientId;
--Query 4--How many projects does each employe have?***********
SELECT EmployeeId COUNT(*)
FROM Job
GROUP BY EmployeeId;
--Query 5--First purchases, first served. Show me the order of the purchases by date***********
SELECT ClientName, PurchaseDate
FROM Purchase P, Client C
WHERE P.ClientId = C.ClientId AND ORDER BY PurchaseDate;
--Query 6--Show me what kind of balls were purchased***********
SELECT B.BallId, B.BallSize, B.Color, B.Material
FROM SoccerBall B, Purchase P, Client C
WHERE P.ClientId = C.ClientId AND P.BallId = B.BallId AND C.ClientId = 1;
--Query 7--Show me who purchased what balls**************
SELECT B.BallId, B.BallSize, B.Color, B.Material, C.ClientId, ClientName
FROM SoccerBall B, Purchase P, Client C
WHERE P.ClientId = C.ClientId AND P.BallId = B.BallId GROUP BY ClientName;
非常感谢!