Вы находитесь на странице: 1из 17

......................................................................................................................................................

1
.......................................................................................................................................................... 2
.......................................................................................................... 3
................................................................................................................... 5
SQL-..................................................... 5
.................................................................................................................................... 6
........................................................................................................................................... 7
................................................................................................................................................... 8
........................................................................................................................................................ 8
.................................................................................................................. 9
........................................................................................................................................................ 9
......................................................................................................................................... 11
................................................................................................................................................. 14
...................................................................................................................................................... 16
........................................................................................................... 17


SQL ,
,
. - ,
.
() .
SQL ANSI (
) ISO (
).
.
:
;
;
;
, , .
, , ,
MS SQL Server.
Microsoft SQL Server (),
Microsoft. TransactSQL, Microsoft Sybase. T-SQL
(
).

. 1

6 .
1

KODIZD
IMTOV
SOSTAV
ROSNZEN
KODIZG

Int
Char
Char
Smallmoney
Int


4
PK

20

100

4

4
FK

SKODIZD
SKODMAG
SDATAPOST
SKOLVO
OPTZEN

Int
Int
Smalldatatime
Int
Smallmoney


4
PK

4
FK

4
.
4

4

3

KODMAG
IMMAG
ADRMAG
TELMAG
MDATAPOST
MKOLVO
PROZENT

Int
Char
Char
Char
Smalldatatime
Int
Int


4
PK

20

100

10

4
.
4

4

PKODMAG
PFAM
PIM
POTCH
PDATAROG
PADR
PTEL
VIRMES
ZARPLATA

Int
Char
Char
Char
Smalldatatime
Char
Char
Smallmoney
Smallmoney


4
FK

20

20

20

4

100

10

4

4

MKODMAG
MFAM
MIM
MOTCH
MDATAROG
MADR
MTEL

Int
Char
Char
Char
Smalldatatime
Char
Char


4
FK

20

20

20

4

100

10

IKODIZG
STRANA
FIRMA
ADRESS
ITEL
PREDSTAV

Int
Char
Char
Char
Char
Char


4
PK

20

20

100

10


SQL-.
CREATE DATABASE [TOYMAG]


CREATE TABLE [IZDELIE] ( KODIZD INT NOT NULL PRIMARY KEY,
IMTOV CHAR(20),
SOSTAV CHAR(100),
ROSNZEN SMALLMONEY,
KODIZG INT NOT NULL)


CREATE TABLE [SKLAD] (SKODIZD INT NOT NULL PRIMARY KEY,
SKODMAG INT NOT NULL,
SDATAPOST SMALLDATETIME NOT NULL,
SKOLVO INT NOT NULL,
OPTZEN SMALLMONEY NOT NULL)


CREATE TABLE [MAGAZIN] ( KODMAG INT NOT NULL PRIMARY KEY,
IMMAG CHAR(20),
ADRMAG CHAR(100),
TELMAG CHAR(10),
MDATAPOST SMALLDATETIME NOT NULL,
MKOLVO INT,
PROZENT INT)


CREATE TABLE [PRODAV] ( MKODMAG INT NOT NULL,
PFAM CHAR(20) NOT NULL,
PIM CHAR(20) NOT NULL,
POTCH CHAR(20) NOT NULL,
PDATAROG SMALLDATETIME NOT NULL,
PADR CHAR(100),
PTEL CHAR(10),
VIRMES SMALLMONEY NOT NULL,
ZARPLATA SMALLMONEY)


CREATE TABLE [MANADGER] (MKODMAG INT NOT NULL,
MFAM CHAR(20) NOT NULL,
MIM CHAR(20) NOT NULL,
MOTCH CHAR(20) NOT NULL,
MDATAROG SMALLDATETIME NOT NULL,
MADR CHAR(100),
MTEL CHAR(10))


CREATE TABLE [IZGOTOV] (IKODIZG INT NOT NULL PRIMARY KEY,
STRANA CHAR(20),
FIRMA CHAR(20),
ADRESS CHAR(100),
ITEL CHAR(10),
PREDSTAV CHAR(50))


IZDELIE:
INSERT INTO IZDELIE VALUES (001, , ' ', NULL, 1001)
INSERT INTO IZDELIE VALUES (002, , ' , ', NULL, 1005)
INSERT INTO IZDELIE VALUES (003, '', ' ', NULL, 1002)
INSERT INTO IZDELIE VALUES (004, ', ' ', NULL, 1003)
INSERT INTO IZDELIE VALUES (005, , ' ', NULL, 1001)
INSERT INTO IZDELIE VALUES (006, "7"','', NULL, 1003)
INSERT INTO IZDELIE VALUES (007, , '', NULL, 1002)
INSERT INTO IZDELIE VALUES (008, ""','', NULL, 1004)
INSERT INTO IZDELIE VALUES (009, ""','', NULL, 1003)
INSERT INTO IZDELIE VALUES (010, ""',' ', NULL, 1001)
INSERT INTO IZDELIE VALUES (011, , ' ', NULL, 1004)
INSERT INTO IZDELIE VALUES (012, , ' ', NULL, 1001)
INSERT INTO IZDELIE VALUES (013, , '', NULL, 1003)
INSERT INTO IZDELIE VALUES (014, -', ' ', NULL, 1003)
INSERT INTO IZDELIE VALUES (015, ', '', NULL, 1004)
INSERT INTO IZDELIE VALUES (016, ', '', NULL, 1004)
INSERT INTO IZDELIE VALUES (017, , ' ', NULL, 1005)
INSERT INTO IZDELIE VALUES (018, , '', NULL, 1004)
INSERT INTO IZDELIE VALUES (019, ', ' ', NULL, 1001)
INSERT INTO IZDELIE VALUES (020, , '', NULL, 1002)
INSERT INTO IZDELIE VALUES (021, ','', NULL, 1002)
INSERT INTO IZDELIE VALUES (022, ', '', NULL, 1005)
INSERT INTO IZDELIE VALUES (023, ', '', NULL, 1005)
INSERT INTO IZDELIE VALUES (024, , '', NULL, 1002)
INSERT INTO IZDELIE VALUES (025, ', ', NULL, 1005)

SKLAD:
INSERT INTO SKLAD VALUES (001, 111, '22/09/2009', 105, 100.00)
INSERT INTO SKLAD VALUES (002, 112, '2/12/2009', 120, 150.00)
INSERT INTO SKLAD VALUES (003, 113, '20/11/2009', 520, 1000.00)
INSERT INTO SKLAD VALUES (004, 111, '20/05/2009', 115, 100.00)
INSERT INTO SKLAD VALUES (005, 113, '20/12/2009', 13, 200.00)
INSERT INTO SKLAD VALUES (006, 111, '22/09/2009', 1000, 2000.00)
INSERT INTO SKLAD VALUES (007, 113, '12/04/2009', 55, 100.00)
INSERT INTO SKLAD VALUES (008, 113, '10/12/2009', 110, 2000.00)
INSERT INTO SKLAD VALUES (009, 113, '20/11/2009', 140, 110.00)
INSERT INTO SKLAD VALUES (010, 112, '20/05/2009', 1355, 100.00)
INSERT INTO SKLAD VALUES (011, 111, '2/12/2009', 510, 200.00)
INSERT INTO SKLAD VALUES (012, 113, '20/11/2009', 1400, 50.00)
INSERT INTO SKLAD VALUES (013, 111, '20/05/2009', 320, 100.00)
INSERT INTO SKLAD VALUES (014, 112, '10/12/2009', 20, 200.00)
INSERT INTO SKLAD VALUES (015, 113, '20/12/2009', 112, 200.00)
INSERT INTO SKLAD VALUES (016, 112, '20/04/2009', 55, 100.00)
INSERT INTO SKLAD VALUES (017, 111, '30/10/2009', 55, 300.00)
INSERT INTO SKLAD VALUES (018, 112, '20/12/2009', 161, 150.00)
INSERT INTO SKLAD VALUES (019, 113, '20/05/2009', 165, 190.00)
INSERT INTO SKLAD VALUES (020, 113, '30/10/2009', 120, 100.00)
INSERT INTO SKLAD VALUES (021, 113, '20/12/2009', 120, 200.00)
INSERT INTO SKLAD VALUES (022, 111, '20/04/2009', 545, 100.00)
INSERT INTO SKLAD VALUES (023, 112, '20/11/2009', 451, 10.00)
INSERT INTO SKLAD VALUES (024, 113, '10/12/2009', 147, 30.00)
INSERT INTO SKLAD VALUES (025, 113, '20/05/2009', 452, 900.00)

MAGAZIN:
INSERT INTO MAGAZIN VALUES (111, '', '. 5', '72-82-35', '02/10/2009', 195, 25)

INSERT INTO MAGAZIN VALUES (112, '', '. 28','87-25-35','15/12/2009', 270, 7)


INSERT INTO MAGAZIN VALUES (113, '','. 2','12-89-45','23/11/2009', 507, 10)

PRODAV:
INSERT INTO PRODAV VALUES (111, '', '', '', '15/08/1980', '. 15', '7737-35', 50000.00, NULL)
INSERT INTO PRODAV VALUES (112, '', '', '', '08/08/1964', '. 15', '2737-30', 10000.00, NULL)
INSERT INTO PRODAV VALUES (113, '', '', '', '08/09/1974', '.
15', '24-60-54',2000.00, NULL)
INSERT INTO PRODAV VALUES (111, '', '', '', '19/08/1989', '. 15', '7935-05', 100000.00, NULL)
INSERT INTO PRODAV VALUES (112, '', '', '', '18/12/1974', '. 15',
'22-97-30', 15000.00, NULL)
INSERT INTO PRODAV VALUES (113, '', '', '', '01/10/1994', '. 15',
'84-66-04',5000.00, NULL)

MANADGER:
INSERT INTO MANADGER VALUES (111, '', '', '', '15/08/1980', '. 15', '72-82-35')
INSERT INTO MANADGER VALUES (112, '', '', ', '21/09/1961', '. 5', '12-84-55')
INSERT INTO MANADGER VALUES (113, '', '', ', '10/01/1975', '. 6', '28-06-54')

IZGOTOV:
INSERT INTO IZGOTOV VALUES (1001, '', '', ' . . 5', '77-545-45',
' ')
INSERT INTO IZGOTOV VALUES (1002, '', '', ' . . 6', '48-545-45',
' ')
INSERT INTO IZGOTOV VALUES (1003, '', '', ' . . 1', '85-59489', ' ')
INSERT INTO IZGOTOV VALUES (1004, '', '', ' . . 9', '55-54-45',
' ')
INSERT INTO IZGOTOV VALUES (1005, '', 'O&S', ' . - 5 ', '78-545-45', ' ')

1 (PREDSTAV1) , ,
, :
CREATE VIEW PREDSTAV1 AS SELECT IZDELIE.IMTOV, IZDELIE.SOSTAV, SKLAD.SDATAPOST,
SKLAD.OPTZEN,SKLAD.SKOLVO
FROM IZDELIE
INNER JOIN SKLAD ON IZDELIE.KODIZD=SKLAD.SKODIZD

2 (PREDSTAV2) , ,
(), :
CREATE VIEW PREDSTAV2 AS SELECT IZDELIE.IMTOV, IZGOTOV.STRANA, IZGOTOV.FIRMA, SKLAD.OPTZEN
FROM IZDELIE
INNER JOIN IZGOTOV ON IZDELIE.KODIZG=IZGOTOV.IKODIZG
INNER JOIN SKLAD ON IZDELIE.KODIZD=SKLAD.SKODIZD

3 (PREDSTAV3) ... ,
, , ... , :
CREATE VIEW PREDSTAV4 AS SELECT PRODAV.PFAM,PRODAV.PIM,PRODAV.POTCH, MAGAZIN.IMMAG,
MANADGER.MFAM,MANADGER.MIM,MANADGER.MOTCH
FROM MAGAZIN
INNER JOIN PRODAV ON MAGAZIN.KODMAG=PRODAV.MKODMAG
INNER JOIN MANADGER ON MAGAZIN.KODMAG=MANADGER.MKODMAG

4 (PREDSTAV4) ,
, , , , ,
, :
7

CREATE VIEW PREDSTAV3 AS SELECT IZDELIE.IMTOV, SKLAD.SKOLVO,MAGAZIN.IMMAG,


MAGAZIN.ADRMAG,MAGAZIN.TELMAG,SKLAD.OPTZEN, MAGAZIN.PROZENT,IZDELIE.ROSNZEN
FROM SKLAD
INNER JOIN IZDELIE ON SKLAD.SKODIZD=IZDELIE.KODIZD
INNER JOIN MAGAZIN ON SKLAD.SKODMAG=MAGAZIN.KODMAG

5 (PREDSTAV5) , ,
, :
CREATE VIEW PREDSTAV5 AS SELECT IZDELIE.IMTOV, IZGOTOV.FIRMA, IZGOTOV.STRANA, MAGAZIN.IMMAG
FROM SKLAD
INNER JOIN MAGAZIN ON SKLAD.SKODMAG=MAGAZIN.KODMAG
INNER JOIN IZDELIE ON SKLAD.SKODIZD=IZDELIE.KODIZD
INNER JOIN IZGOTOV ON IZDELIE.KODIZG=IZGOTOV.IKODIZG

1(PROCEDURA1) , 6%
:
CREATE PROCEDURE PROCEDURA1 AS
UPDATE PRODAV
SET ZARPLATA=VIRMES*0.06
EXEC PROCEDURA1

2(PROCEDURA2) , ,
:
CREATE PROCEDURE PROCEDURA2 AS
UPDATE PREDSTAV4
SET ROSNZEN=OPTZEN*PROZENT/100+OPTZEN
EXEC PROCEDURA2

1 :
SELECT * FROM IZDELIE ORDER BY IMTOV

2 , 2
2010 :
SELECT IMTOV, SDATAPOST,SOSTAV FROM IZDELIE, SKLAD WHERE SKODIZD=KODIZD AND SDATAPOST > '20/11/2009'

3 , :
SELECT SUM(VIRMES) AS ' ' FROM PRODAV

(IZDELIE)

(PRODAV)

(MAGAZIN)

(MANADGER)

(SKLAD)

(IZGOTOV)

10


1 (PREDSTAV1):

11

2 (PREDSTAV2):

3 (PREDSTAV3):

12

4 (PREDSTAV4):

13

5 (PREDSTAV5):

1(PROCEDURA1):

14

2(PROCEDURA2:

15


1 :

2 , 2
2010 :

3 , :

16


1. , .. SQL.-.: 1993;
2. www.maxitoys.ru

17