( .., ..)
, , , . , , , SQL . SQLCODE ( SQLCOD ) . , SQL, B. SQLCODE , SQL. :
1. , . -:
) SELECT .
) FETCH , .
) INSERT (, , , ).
) UPDATE DELETE , , , .
, SQLCODE = 100.
2. , . , SQLCODE = 0.
3. . , , (. 23).
SQLCODE = , . , . , , , , , , , . , , .
, , , , :
Look_at_more:=True;
EXEC SQL OPEN CURSOR Londonsales;
while Look_at_more and SQLCODE = O do
begin
EXEC SQL FETCH Londonsales
INTO :id_num, :Salesperson, :loc, :comm;
writeln (id_num, Salesperson, loc, comm);
writeln ('Do you want to see more data? (Y/N)');
readln (response);
If response = 'N' then Look_at_more:=False;
end;
EXEC SQL CLOSE CURSOR Londonsales;
. , , , . , SQL GOTO. , SQL , GOTO , SQLCODE. WHENEVER. :
EXEC SQL WHENEVER SQLERROR GOTO Error_handler;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
SQLERROR , SQLCODE < 0; NOT FOUND , SQLCODE = 100. ( SQLWARNING.)
Error_handler , , (GOTO ). , , , ( ). , .
CONTINUE - SQLCODE. , WHENEVER, SQLCODE. , , , () . , INSERT, , , - , , . , :
EXEC SQL WHENEVER NOT FOUND GOTO No_rows;
No_rows , . , , :
EXEC SQL WHENEVER NOT FOUND CONTINUE;
, , , .
, , . , UPDATE DELETE. , , . 16, SQL , :
EXEC SQL DELETE FROM Customers
WHERE rating < (SELECT AVG (rating)
FROM Customers);
, , , , DELETE . :
EXEC SQL DECLARE Belowavg CURSOR FOR
SELECT *
FROM Customers
WHERE rating < (SELECT AVG (rating)
FROM Customers);
, :
EXEC SQL WHENEVER SQLERROR GOTO Error_handler;
EXEC SQL OPEN CURSOR Belowavg;
while not SOLCODE = 100 do
begin
EXEC SOL FETCH Belowavg INTO :a, :b, :c, :d, :e;
EXEC SOL DELETE FROM Customers
WHERE CURRENT OF Belowavg;
end;
EXEC SOL CLOSE CURSOR Belowavg;
WHERE CURRENT OF , DELETE , . , , DELETE , , , .
. , (. 21). , ORDER BY UNION, , , . , , . FETCH. UPDATE .
, =300, . :
EXEC SOL DECLARE CURSOR High_Cust AS
SELECT *
FROM Salespeople
WHERE snum IN (SELECT snum
FROM Customers
WHERE rating = 300);
:
EXEC SQL OPEN CURSOR High_cust;
while SQLCODE = 0 do
begin
EXEC SOL FETCH High_cust
INTO :id_num, :salesperson, :loc, :comm;
EXEC SQL UPDATE Salespeople
SET comm = comm + .01
WHERE CURRENT OF High_cust;
end;
EXEC SQL CLOSE CURSOR High_cust;
: , , UPDATE . FOR UPDATE <column list>. High_cust , UPDATE comm, :
EXEC SQL DECLARE CURSOR High_Cust AS
SELECT *
FROM Salespeople
WHERE snum IN (SELECT snum
FROM Customers
WHERE rating = 300)
FOR UPDATE OF comm;
, .
(NULLS) SQL. . NULL , NULL SQL, . NULL , , : SQLCODE , . , . , NULL , . , , NULL . , , indicator ().
indicator, SQL . , SQL. , , NULL , indicator, . indicator SQL , , - , , , INDICATOR.
indicator 0. , NULL, indicator . indicator, , NULL. , city comm, , NOT NULL, SQL , i_a i_b.
, indicator. indicator, indicator.
:
EXEC SQL OPEN CURSOR High_cust;
while SQLCODE = O do
begin
EXEC SQL FETCH High_cust
INTO :id_num, :salesperson, :loc, :i_a, :comm INDlCATOR, :i_b;
If i_a >= 0 and i_b >= 0
then {no NULLs produced}
begin
EXEC SQL UPDATE Salespeople
SET comm = comm + .01
WHERE CURRENT OF Hlgh_cust
end {then}
else {one or both NULL}
begin
If i_a < 0 then writeln('salesperson ', id_num, ' has no city');
If i_b < 0 then writeln('salesperson ', id_num, ' has no commission')
end {else}
end; {while}
EXEC SQL CLOSE CURSOR High_cust;
, , INDICATOR , , , . , UPDATE NULL .
NULL , , , NULL .
: indicator , , WHERE SQL.
, .
, indicator, , NULL SQL.
, , if ... then, indicator, =NULL. , -. , NULL city city, indicator i_city, city . , ; . , i_city . , :
If city = 'London' then comm := comm + .01
else comm := comm - .01
, city, 'London', . , , . SQL -:
EXEC SQL UPDATE Salespeople
SET comm = comm + .01
WHERE city = 'London';
EXEC SQL UPDATE Salespeople
SET comm = comm - .01;
WHERE city <> 'London';
, SQL .
city SQL NULL, , comm, , .
indicator, , , NULL :
If i_city >= O then
If city = 'London' then comm := comm + .01
else comm: = comm - .01;
, "", , city = NULL. , .
indicator NULL. UPDATE INSERT SELECT. indicator , NULL . , NULL city comm, , , indicator i_a i_b ; :
EXEC SQL INSERT INTO Salespeople
VALUES (:Id_num, :salesperson, :loc:i_a, :comm:i_b);
indicator , . SQL , . VARCHAR LONG ( C). , , . indicator, , , , , , .
, indicator > 0, < 0.
SQL , . SQL , . SQL , , , , , , . ANSI SQL : , , , PL/I. , . SQL, :
: , , . , , ( ). , , . , .
, , , -, , . , , ; , .
(. A .)
cnum rating
. .
, , .
( )
( DML)
SQL
SELECT onum, amt, odate
FROM Orders;
SELECT *
FROM Customers
WHERE snum = 1001;
SELECT city, sname, snum, comm
FROM Salespeople;
SELECT rating, cname
FROM Customers
WHERE city = 'SanJose';
SELECT DISTINCT snum
FROM Orders;
SELECT *
FROM Orders
WHERE amt > 1000;
SELECT sname, city
FROM Salespeople
WHERE city = 'London' AND comm > .10;
SELECT *
FROM Customers
WHERE rating > 100 OR city = 'Rome';
SELECT *
FROM Customers
WHERE NOT rating < = 100 OR city = 'Rome';
SELECT *
FROM Customers
WHERE NOT (rating < = 100 AND city < > 'Rome');
.
onum amt odate cnum snum
3001 18.69 10/03/1990 2008 1007
3003 767.19 10/03/1990 2001 1001
3005 5160.45 10/03/1990 2003 1002
3009 1713.23 10/04/1990 2002 1003
3007 75.75 10/04/1990 2004 1002
3008 4723.00 10/05/1990 2006 1001
3010 1309.95 10/06/1990 2004 1002
3011 9891.88 10/06/1990 2006 1001
onum amt odate cnum snum
3001 18.69 10/03/1990 2008 1007
3003 767.19 10/03/1990 2001 1001
3006 1098.16 10/03/1990 2008 1007
3009 1713.23 10/04/1990 2002 1003
3007 75.75 10/04/1990 2004 1002
3008 4723.00 10/05/1990 2006 1001
3010 1309.95 10/06/1990 2004 1002
3011 9891.88 10/06/1990 2006 1001
SELECT *
FROM Salespeople;
SELECT *
FROM Orders
WHERE odate IN (10/03/1990,10/04/1990);
SELECT *
FROM Orders
WHERE odate BETWEEN 10/03/1990 AND 10/04,1990;
SELECT *
FROM Customers
WHERE snum IN (1001,1004);
SELECT *
FROM Customers
WHERE cname BETWEEN 'A' AND 'H';
: ASCII Hoffman - H. G, Giovanni Grass. G Z, , , .
SELECT *
FROM Customers
WHERE cname LIKE 'C%';
SELECT *
FROM Orders
WHERE amt <> 0 AND (amt IS NOT NULL);
SELECT *
FROM Orders
WHERE NOT (amt = 0 OR amt IS NULL);
SELECT COUNT(*)
FROM Orders
WHERE odate = 10/03/1990;
SELECT COUNT (DISTINCT city)
FROM Customers;
SELECT cnum, MIN (amt)
FROM Orders
GROUP BY cnum;
SELECT MIN (cname)
FROM Customers
WHERE cname LIKE 'G%';
SELECT city, MAX (rating)
FROM Customers
GROUP BY city;
SELECT odate, count (DISTINCT snum)
FROM Orders
GROUP BY odate;
SELECT onum, snum, amt * .12
FROM Orders;
SELECT 'For the city ', city, ', the highest rating is ', MAX (rating)
FROM Customers
GROUP BY city;
SELECT rating, cname, cnum
FROM Customers
ORDER BY rating DESC;
SELECT odate, SUM (amt)
FROM Orders
GROUP BY odate
ORDER BY 2 DESC;
SELECT onum, cname
FROM Orders, Customers
WHERE Customers.cnum = Orders.cnum;
SELECT onum, cname, sname
FROM Orders, Customers, Salespeople
WHERE Customers.cnum = Orders.cnum AND Salespeople.snum = Orders.snum;
SELECT cname, sname, comm
FROM Salespeople, Customers
WHERE Salespeople.snum = Customers.snum AND comm * .12;
SELECT onum, comm * amt
FROM Salespeople, Orders, Customers
WHERE rating > 100 AND
Orders.cnum = Customers.cnum AND
Orders.snum = Salespeople.snum;
SELECT first.sname, second.sname
FROM Salespeople first, Salespeople second
WHERE first.city = second.city AND first.sname < second.sname;
.
SELECT cname, first.onum, second.onum
FROM Orders first, Orders second, Customers
WHERE first.cnum = second.cnum AND
first.cnum = Customers.cnum AND
first.onum < second.onum;
, .
SELECT a.cname, a.city
FROM Customers a, Customers b
WHERE a.rating = b.rating AND b.cnum = 2001;
SELECT *
FROM Orders
WHERE cnum = (SELECT cnum
FROM Customers
WHERE cname = 'Cisneros');
SELECT *
FROM Orders
WHERE cnum IN (SELECT cnum
FROM Customers
WHERE cname = 'Cisneros');
SELECT DISTINCT cname, rating
FROM Customers, Orders
WHERE amt > (SELECT AVG (amt)
FROM Orders)
AND Orders.cnum = Customers.cnum;
SELECT snum, SUM (amt)
FROM Orders
GROUP BY snum
HAVING SUM (amt) > (SELECT MAX (amt)
FROM Orders);
SELECT cnum, cname
FROM Customers outer
WHERE rating = (SELECT MAX (rating)
FROM Customers inner
WHERE inner.city = outer.city);
:
SELECT snum, sname
FROM Salespeople main
WHERE city IN (SELECT city
FROM Customers inner
WHERE inner.snum <> main.snum);
:
SELECT DISTINCT first.snum, sname
FROM Salespeople first, Customers second
WHERE first.city = second.city AND first.snum <> second.snum;
, , : - . . , city , snums . , , , . , .
SELECT *
FROM Salespeople first
WHERE EXISTS (SELECT *
FROM Customers second
WHERE first.snum = second.snum AND rating = 300);
SELECT a.snum, sname, a.city, comm
FROM Salespeople a, Customers b
WHERE a.snum = b.snum AND b.rating = 300;
SELECT *
FROM Salespeople a
WHERE EXISTS (SELECT *
FROM Customers b
WHERE b.city = a.city AND a.snum <> b.snum);
SELECT *
FROM Customers a
WHERE EXISTS (SELECT *
FROM Orders b
WHERE a.snum = b.snum AND a.cnum <> b.cnum)
SELECT *
FROM Customers
WHERE rating >= ANY (SELECT rating
FROM Customers
WHERE snum = 1002);
cnum cname city rating snum
2002 Giovanni Rome 200 1003
2003 Liu San Jose 200 1002
2004 Grass Berlin 300 1002
2008 Cisneros SanJose 300 1007
SELECT *
FROM Salespeople
WHERE city <> ALL (SELECT city
FROM Customers);
SELECT *
FROM Salespeople
WHERE NOT city = ANY (SELECT city
FROM Customers);
SELECT *
FROM Orders
WHERE amt > ALL (SELECT amt
FROM Orders a, Customers b
WHERE a.cnum = b.cnum AND b.city = 'London');
SELECT *
FROM Orders
WHERE amt > (SELECT MAX (amt)
FROM Orders a, Customers b
WHERE a.cnum = b.cnum AND b.city = 'London');
14
SELECT cname, city, rating, 'High Rating'
FROM Customers
WHERE rating >= 200
UNION
SELECT cname, city, rating, ' Low Ratlng'
FROM Customers
WHERE rating < 200;
SELECT cname, city, rating, 'High Rating'
FROM Customers
WHERE rating >= 200
UNION
SELECT cname, city, rating, ' Low Rating'
FROM Customers
WHERE NOT rating >= 200;
. , "Low Rating" , "High Rating" .
SELECT cnum, cname
FROM Customers a
WHERE 1 < (SELECT COUNT (*)
FROM Orders b
WHERE a.cnum = b.cnum)
UNION
SELECT snum, sname
FROM Salespeople a
WHERE 1 < (SELECT COUNT (*)
FROM Orders b
WHERE a.snum = b.snum)
ORDER BY 2;
SELECT snum
FROM Salespeople
WHERE city = 'San Jose'
UNION
(SELECT cnum
FROM Customers
WHERE city = 'San Jose'
UNION ALL
SELECT onum
FROM Orders
WHERE odate = 10/03/1990);
15
INSERT INTO Salespeople (city, cname, comm, cnum)
VALUES ('San Jose', 'Blanco', NULL, 1100);
DELETE FROM Orders WHERE cnum = 2006;
UPDATE Customers
SET rating = rating + 100
WHERE city = 'Rome';
UPDATE Customers
SET snum = 1004
WHERE snum = 1002;
16
INSERT INTO Multicust
SELECT *
FROM Salespeople
WHERE 1 < (SELECT COUNT (*)
FROM Customers
WHERE Customers.snum = Salespeople.snum);
DELETE FROM Customers
WHERE NOT EXISTS (SELECT *
FROM Orders
WHERE cnum = Customers.cnum);
UPDATE Salespeople
SET comm = comm + (comm * .2)
WHERE 3000 < (SELECT SUM (amt)
FROM Orders
WHERE snum = Salespeople.snum);
, , 1.0 (100%):
UPDATE Salespeople
SET comm = comm + (comm * .2)
WHERE 3000 < (SELECT SUM (amt)
FROM Orders
WHERE snum = Salespeople.snum)
AND comm + (comm * .2) < 1.0;
, .
17
CREATE TABLE Customers
(cnum integer,
cname char(10),
city char(10),
rating integer,
snum integer);
CREATE INDEX Datesearch ON Orders(odate);
( , .)
CREATE UNIQUE INDEX Onumkey ON Orders(onum);
CREATE INDEX Mydate ON Orders(snum, odate);
CREATE UNIQUE INDEX Combination ON Customers(snum, rating);
18
CREATE TABLE Orders
(onum integer NOT NULL PRIMARY KEY,
amt decimal,
odate date NOT NULL,
cnum integer NOT NULL,
snum integer NOT NULL,
UNIOUE (snum, cnum));
CREATE TABLE Orders
(onum integer NOT NULL UNIQUE,
amt decimal,
odate date NOT NULL,
cnum integer NOT NULL,
snum integer NOT NULL,
UNIQUE (snum, cnum));
.
CREATE TABLE Salespeople
(snum integer NOT NULL PRIMARY KEY,
sname char(15) CHECK (sname BETWEEN 'AA' AND 'MZ'),
city char(15),
comm decimal NOT NULL DEFAULT = .10);
CREATE TABLE Orders
(onum integer NOT NULL,
amt decimal,
odate date,
cnum integer NOT NULL,
snum integer NOT NULL,
CHECK ((cnum > snum) AND (onum > cnum)));
19
CREATE TABLE Cityorders
(onum integer NOT NULL PRIMARY KEY,
amt decimal,
cnum integer,
snum integer,
city char (15),
FOREIGN KEY (onum, amt, snum) REFERENCES Orders (onum, amt, snum),
FOREIGN KEY (cnum, city) REFERENCES Customers (cnum, city));
CREATE TABLE Orders
(onum integer NOT NULL,
amt decimal,
odate date,
cnum integer NOT NULL,
snum integer,
prev integer,
UNIQUE (cnum, onum),
FOREIGN KEY (cnum, prev) REFERENCES Orders (cnum,onum));
20
CREATE VIEW Highratings
AS SELECT *
FROM Customers
WHERE rating = (SELECT MAX (rating)
FROM Customers);
CREATE VIEW Citynumber
AS SELECT city, COUNT (DISTINCT snum)
FROM Salespeople
GROUP BY city;
CREATE VIEW Nameorders
AS SELECT sname, AVG (amt), SUM (amt)
FROM Salespeople, Orders
WHERE Salespeople.snum = Orders.snum
GROUP BY sname;
CREATE VIEW Multcustomers
AS SELECT *
FROM Salespeople a
WHERE 1 < (SELECT COUNT (*)
FROM Customers b
WHERE a.snum = b.snum);
21
#1 , DISTINCT.
#2 , , GROUP BY.
#3 , #1, .
CREATE VIEW Commissions
AS SELECT snum, comm
FROM Salespeople
WHERE comm BETWEEN .10 AND .20
WITH CHECK OPTION;
CREATE TABLE Orders
(onum integer NOT NULL PRIMARY KEY,
amt decimal,
odate date DEFAULT VALUE = CURDATE,
snum integer,
cnum integer);
CREATE VIEW Entryorders
AS SELECT onum, amt, snum, cnum
FROM Orders;
22
GRANT UPDATE (rating) ON Customers TO Janet;
GRANT SELECT ON Orders TO Stephen WITH GRANT OPTION;
REVOKE INSERT ON Salespeople FROM Claire;
1:
CREATE VIEW Jerrysview
AS SELECT *
FROM Customers
WHERE rating BETWEEN 100 AND 500
WITH CHECK OPTION;
2:
GRANT INSERT, UPDATE ON Jerrysview TO Jerry;
1:
CREATE VIEW Janetsview
AS SELECT *
FROM Customers
WHERE rating = (SELECT MIN (rating)
FROM Customers);
2:
GRANT SELECT ON Janetsview TO Janet;
23
CREATE DBSPACE Myspace
(pctindex 15,
pctfree 40);
CREATE SYNONYM Orders FOR Diane.Orders;
24
SELECT a.tname, a.owner, b.cname, b.datatype
FROM SYSTEMCATOLOG a, SYSTEMCOLUMNS b
WHERE a.tname = b.tname AND
a.owner = b.owner AND
a.numcolumns > 4;
: - , , a b . .
SELECT tname, synowner, COUNT (ALL synonym)
FROM SYTEMSYNONS
GROUP BY tname, synowner;
SELECT COUNT (*)
FROM SYSTEMCATALOG a
WHERE numcolumns/2 < (SELECT COUNT (DISTINCT cnumber)
FROM SYSTEMINDEXES b
WHERE a.owner = b.tabowner AND a.tname = b.tname);
EXEC SQL BEGIN DECLARE SECTION;
SQLCODE : integer;
{ }
cnum : integer;
snum : integer;
custnum : integer;
salesnum : integer;
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE Wrong_Orders AS CURSOR FOR
SELECT cnum, snum
FROM Orders a
WHERE snum <> (SELECT snum
FROM Customers b
WHERE a.cnum = b.cnum);
{ SQL . , . }
EXEC SQL DECLARE Cust_assigns AS CURSOR FOR
SELECT cnum, snum
FROM Customers;
{
, , , onum , Wrong_Orders. UPDATE WHERE onum =:ordernum ( ordernum ), WHERE CURRENT Of Wrong_Orders.
( ):
EXEC SQL BEGIN DECLARE SECTION;
SQLCODE : integer;
odernum : integer;
cnum : integer;
snum : integer;
custnum : integer;
salesnum : integer;
EXEC SQL END DECLARE SECTION;
EXEC SQL DECLARE Wrong_Orders AS CURSOR FOR
SELECT onum, cnum, snum
FROM Orders a
WHERE snum <> (SELECT snum
FROM Customers b
WHERE a.cnum = b.cnum);
EXEC SQL DECLARE Cust_assigns AS CURSOR FOR
SELECT cnum, snum
FROM Customer
B
SQL
, SQL ANSI, , .
, .
( 10) .
, - .
, , .
, .
ANSI
ANSI ( ):
TEXT .
CHAR (CHARACTER) , . , . , , 'text'. ('') (').
: , , , , .
EXACT NUMERIC
DEC (DECIMAL) ; , . : . . , . . , , . . = 0 .
NUMERIC DECIMAL , .
INT (INTEGER) . DECIMAL, , , 0. ( , ).
SMALLINT INTEGER, , , , ( ) , INTEGER.
APPROXIMATE NUMERIC
FLOAT 10 . , .
REAL , FLOAT, , . .
DOUBLE PRECISION (DOUBLE) , REAL, , , DOUBLE PRECISION REAL.
SQL , , SQL, (. 25). , SQL, . , SQL , ANSI SQL : , PL/I, , . , SQL , .
ANSI:
PL/1
SQL PL/1
CHAR CHAR
DECIMAL FIXED DECIMAL
INTEGER FIXED BINARY
FLOAT FLOAT BINARY
SQL
CHAR(<integer>) PIC X (<integer>)
INTEGER PIC S (<nines>) USAGE COMPUTATIONAL
NUMERIC PIC S (<nines with embedded V>) DISPLAY SING LEADING
SEPARATE
SQL
INTEGER INTEGER
REAL REAL
CHAR (<length>) PACKED ARRAY [1..<length>] OF CHAR
SQL
CHAR CHAR
INTEGER INTEGER
REAL REAL
DOUBLE PRECISION DOUBLE PRECISION
10.11.2021 - 12:37: - Personalias -> WHO IS WHO - - _. 10.11.2021 - 12:36: - Conscience -> . ? - _. 10.11.2021 - 12:36: , , - Upbringing, Inlightening, Education -> ... - _. 10.11.2021 - 12:35: - Ecology -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> - _. 10.11.2021 - 12:34: , - War, Politics and Science -> . - _. 10.11.2021 - 12:34: , , - Upbringing, Inlightening, Education -> , - _. 10.11.2021 - 09:18: - New Technologies -> , 5G- - _. 10.11.2021 - 09:18: - Ecology -> - _. 10.11.2021 - 09:16: - Ecology -> - _. 10.11.2021 - 09:15: , , - Upbringing, Inlightening, Education -> - _. 10.11.2021 - 09:13: , , - Upbringing, Inlightening, Education -> - _. |