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

5

, ,
( Microsoft SQL Server 7.0).
MS SQL Server
,., ..

MS SQL Server
MS SQL Server .

(TCP/IP, IPX/SPX, NetBEUI),
(Named Pipes) Windows NT.
Microsoft Windows NT (Windows2000, XP),
Windows9x (Windows Millennium).
, , .

. Windows NT (2000, XP)
.
- MS SQL Server



Client Network Utility , (, )
, . , ,
, .
() . ,
( ):


, ,
Enterprise Manager.

(, , ..)

, ,
.
,
.

Databases ( ),
. master, model, msdb, pubs, tempdb
.
(tables), (diagrams),
(views), (stored procedures), (users), (roles),
(rules), (defaults), (user-defined data
types).
,
,
, ,
. (,
) .
,
,
.

, .
, SQL-
SQL Query Analyzer.

SQL
, ,
. .

Bit , 0 1.
Int -231 (-2,147,483,648) 231-1 (2,147,483,647).
Smallint -215 (-32,768) 215-1 (32,767).
Tinyint 0 255.

Decimal -1038-1 1038-1.
Numeric Decimal.

Money -263 (-922,337,203,685,477.5808) 263-1


(+922,337,203,685,477.5807), .
Smallmoney -214,748.3648 +214,748.3647,
.

Float -1.79E+308 1.79E+308.
Real -3.40E+38 3.40E+38.

Datetime 1 1753 31 9999 ,
3.33 .
Smalldatetime 1 1900 6 2079 ,
.

Cursor ( ).
Timestamp .
Uniqueidentifier .
unicode-
Char 8000 .
Varchar 8000 .
Text 231-1 (2,147,483,647) .
unicode-
Nchar 4000 .
Nvarchar 4000 .
Ntext 230-1 (1,073,741,823) .

Binary 8000 .
Varbinary 8000 .
Image 231-1 (2,147,483,647) .

, , .
().
.


SQL Server .
, .. ,
, .

.
( ), Name Group
( Allow Nulls),
( Identity),
(Identity Seed) Identity Increment. Group
( ),
. .
.
.


,
, .. ()
( ).
.
, , ,
,
, ..
, .
Diagrams ,
New Database diagram, .
.


(,
) ,
, ( , Index).
.
,
, . ,
.
,
.
(, Constraint).
.
(
Enterprise Manager, Design Table), Table and
index properties .

Indexes/keys

Selected index
. New Delete
. Column Name , .
,
-, . Index Name
. Create Unique,
. Constraint/Index,
: () (Index)
(Constraint).
Ignore Duplicate Key
. Dont automatically recompute statistics
.
.


,
. :
.
.
,
, ,
.
, .
, .
: _x_yy_name, x , yy ,
name . ,
,

! ,
, .
.

:
, , .
( ).

( ), .

( )
,
,
. -
, .
.
1. .
- .
2. .
3. , -
. .

.
4. .
, . .
5. , .
(, ) ,
.
.
6. . ,
, ,
.
7. .
,
( ), .
8.
. .
9.
( ).
10. .

.
11. , .

6. SQL-
. .
SQL- ,
. , Enterprise Manager,
, . , ,
-
.

SQL Query Analyzer. ,
.
, , SQL.
, ,
, .
SQL Query Analyzer.
.

, ,
SQL,

F5 .
USE.
, -
,
. (
INSERT) _users, , (1 row(s) affected),
SELECT , .
, .
DELETE ( 1 row(s) affected).

, :
. , ..
( ) .

SQL
SQL ,
. SQL
, ,
, (, ).
, .
, SQL
.
.
.
, .

INSERT
INSERT .
INSERT INTO <> [(<>[,...])]
VALUES ( DEFAULT | NULL | <>[,...] ) | DEFAULT VALUES
, .
NULL (
) DEFAULT ( ,
). .

SELECT
SELECT . ,
,

. SELECT .
, , .

SELECT [ ALL | DISTINCT ]


[{TOP n [PERCENT]]
<_>
[INTO <_>]
[FROM {<>} [,...n]]
[WHERE <_>]
[GROUP BY <_> [,...n]
[ORDER BY <_> [ASC | DESC]]

<_> ::= {* | { <> | <>} [[AS] <_>]}


SELECT:
ALL , , .
.
DISTINCT , .
, .
TOP n [PERCENT] , n
. PERCENT, n
.
* () , ,
.
.
() . .
,
. ,
_users.ID ID _users.
, .
, , , .
_ ,
.
_
. , .
. .
4 .
_
. , . , text, ntext, image,
bit .
_, ,
. , .
. , ntext, text, image . ASC
() , DESC
. .
:
SELECT * FROM _users

_users. ,
.
SELECT [ID], [Name] FROM _users WHERE [Name] = Oleg

ID, Name _users , Name


Oleg.
SELECT * FROM goods WHERE [Date] > 13.10.1999 ORDER BY [Name]

goods. , Date
, 13 1999 .
Name.
SELECT * FROM [] WHERE [ ] = 4512 GROUP BY
[]

, 4512
.
,
, .

DELETE
DELETE FROM <> WHERE <_>

(),
SELECT.
:
DELETE FROM _users WHERE [ID] = 2387

_users , 2387 ID.


DELETE FROM [] WHERE [].[] IN (SELECT []
FROM [] WHERE [] = 2)

,
.
! DELETE FROM _table
!

UPDATE
UPDATE <> SET <_> WHERE <_>

. .
:
UPDATE _users SET [Name] = David WHERE [Name] = Oleg
Oleg _users David.
UPDATE [] SET [] = .. WHERE [] = ..
.
UPDATE [] SET [ ] = TRUE WHERE [] IN (SELECT []
FROM [] WHERE [] = 2)
, ,
( ).

(Views)
SELECT
. , ,
.
SQL SELECT ,
. ,
SELECT ,
, .
, ,
.
,
SQL SELECT.
. ,
, .

, ,
SELECT .
Views
Enterpise Manager
.

New view,
:

:
, ( ),
( ) SQL- , ,
( ).
, Add
table ( )
,
.
, .
, .
. ,
. ,

. ,
.
()
.
, . ,
. Add to output
( ) (

). Criteria
. >10, =15, =_ ..
SQL .
,
. .

.


,
.
, , ,
,
(, ).
, .
,
,
.
.
, .
, .
:
1. (). ,
, .
2. .
.
3. .
.
, .
4. . , ,
. .
5. . ,
,
.
,

. ,
COMMIT
.

SQL
SQL BEGIN TRANSACTION,
COMMIT,
ROLLBACK. ,


. .., SQL-:
BEGIN TRANSACTION fistTrans
DELETE FROM dbUsers WHERE sName = Daniil
BEGIN TRANSACTION secondTrans
DELETE FROM dbItems WHERE nLocation = 82794
COMMIT TRANSACTION firstTrans
-- !
COMMIT TRANSACTION secondTrans
-- !

:
BEGIN TRAN[SACTION] [ | @]
,
. , .
SQL.
BEGIN..COMMIT BEGIN..ROLLBACK. @
, .
char, varchar, nchar, nvarchar.
COMMIT [TRAN[SACTION] [ | @]]
, BEGIN TRANSACTION.
, .
COMMIT [WORK]
, COMMIT TRANSACTION
. COMMIT COMMIT WORK
SQL92.
SAVE TRAN[SACTION] { | @}
.
BEGIN TRANSACTION,
.
, .
ROLLBACK [TRAN[SACTION] [ | @]]

, ,
. (
),
.
ROLLBACK [WORK]
.
ROLLBACK ROLLBACK WORK SQL92.
,
,
,
.

6
, ,
.
, INSERT.
. .
.

BEGIN TRAN, COMMIT, ROLLBACK


, ,
COMMIT. INSERT, DELETE, UPDATE, SELECT.
, ,
. .
,
SELECT.
, .
,

, . SQL
Query Analyzer : SELECT * FROM < >
: _x_yy_name, x , yy
, name .
,
, !
,
SQL.
: , ,
; ; SQL,
, ;
.
(
):
1. .
: , , .
2. , , .
, , .
3. , 2001 .
, , .
4. , . ,
.
5. , .
.
6. , 1999 . , , .
7. , 10 2002 .
8. , ,
2001 .
9. ( 095). , ,
.
10. 2000 .
11. , . , .

7.
.

SELECT: , ..
. ,
, , .
.
( , pre-compiled)
, ,
.
, .
,
. 128.
:
CREATE PROC[EDURE] [;][
{@ _}
[VARYING] [= _] [OUTPUT]]
[,...n][
WITH { RECOMPILE | ENCRYPTION | RECOMPILE, ENCRYPTION }
]
AS
SQL [...n]

SQL.
, .
. @
SQL.
. ,
. VARYING
,
( cursor,
OUTPUT). OUTPUT ,
.
1024 .
RECOMPILE
, .
, ,
.
ENCRYPTION
. ,
.
SQL,
RETURN,
.


SQL Query Analyzer (
) .
(,
DROP PROC). Entreprise Manager
Stored Procedures, New
stored procedure,
. Check syntax
. ,

( Properties
).

Transact-SQL
SQL, (
),
, .
:
DECLARE @_ _ [,...]

@
SQL. SELECT,
:
SELECT @sUserName = Ivan

WHILE _ {}
{}
[CONTINUE]

[BREAK]

,
True () False ().
,
BEGIN..END. BREAK ,
CONTINUE .
: , --,
.
: ,
, , ,
[ ].
:
IF _

{}[ELSE

{}]

- ,
. IF .
:
, . , ,
( ):
CASE _
WHEN __1 THEN __1
[... ..]
[
ELSE ___
]
END

, (
):
CASE

END

WHEN __1 THEN __1


[... ..]
[
ELSE ___
]

: ,
(, SQL Query Analyzer):
PRINT '' | @ | @@ | _

: ,
. ,

. ALL ,
DISTINCT , .
AVG([ALL|DISTINCT] )
MAX([ALL|DISTINCT])

MIN([ALL|DISTINCT] )

SUM([ALL|DISTINCT] )

COUNT({[ALL|DISTINCT]]|*})
, * ( , ),
.
: AND, OR, NOT , ,
. : = , != ( <>) , < , > , <=
, >= , !< , !> .
: [NOT] LIKE

,
. ,
. , .
:

%
%% - ,

.
_ (.)
_ - ,
, . ..,
1, ..
[]
[A-Z]% - ,

.
[^]
[^0-9]% - ,

.
LIKE , ,
.
(
). : _lists,
_listItems, _listGroups. @i .

.
.
CREATE PROCEDURE [_DeleteFromList]
@i INT
AS
IF NOT EXISTS (SELECT [ID] FROM [_lists] WHERE [ID] = @i) RETURN
-3
BEGIN TRANSACTION
-- Delete items of list
DELETE FROM [_listItems] WHERE [ListID] = @i
-- Delete groups of list
DELETE FROM [_listGroups] WHERE [ListID] = @i
-- Delete list header
DELETE FROM [_lists] WHERE [ID] = @i
COMMIT

RETURN 0


,
. EXECUTE
[[EXEC[UTE]]
{
[@_ =]
{_ [;] | @__ }
=] { | @ [OUTPUT] | [DEFAULT]]
[WITH RECOMPILE]

[[@
[,...n]

, ,
. ,
. char, varchar, nchar, nvarchar.
.
, DEFAULT
( , ).
. _myProcedure, @p1,
@p2, @p3. , :
EXEC @j = _myProcedure 'Parameter1', , 50
EXEC @j = _myProcedure @p1 = 'Parameter1', @p2 = DEFAULT, @p3= 50

,
( ) .
.

.
:
CREATE TRIGGER trigger_name
ON { table | view }
[ WITH ENCRYPTION ]
[NOT FOR REPLICATION]
{
{ { FOR | INSTEAD OF } { [ INSERT ] [ , ] [ UPDATE ] [ , ]
[DELETE] }
AS
[ { IF UPDATE ( column )
[ { AND | OR } UPDATE ( column ) ]
[ ...n ]
} ]
sql_statement [ ...n ]
}
}

:
WITH ENCRYPTION
syscomments
INSTEAD OF ,

{ [DELETE] [,] [INSERT] [,] [UPDATE] }
,
.
NOT FOR REPLICATION

AS , .

Transact-SQL,
, .
IF UPDATE(column) IF,
, .
, UPDATE(column)
. UPDATE(column)
. INSERT,
UPDATE(column) , ,
, NULL .
UPDATE(column) .
( )
inserted deleted. , ,

. ,
SELECT * FROM inserted.
.
sql_statement


SQL,
,
. ROLLBACK
TRANSACTION , ,
.
,
, . ROLLBACK TRANSACTION
, . ,
RETURN.
, ,
.
,
.
(
),
.
, ,
, ( INSTEAD
OF). , ,

, , . INSTEAD OF
,
, .

,
.
, ,
,


.
.
, ,
. 32 .

. ,
, .
32 . ,
, , ,
, , ,
.
,
.


:
ALTER DATABASE
CREATE DATABASE
DISK INIT
DISK RESIZE
DROP DATABASE
LOAD DATABASE
LOAD LOG
RECONFIGURE
RESTORE DATABASE
RESTORE LOG

.
, ,
, .
INSTEAD OF
,
.
, WITH CHECK OPTION.

Insert

inserted.
,
.
. .
CREATE TRIGGER Order_Insert
ON [Order Details]
FOR INSERT
AS
UPDATE Prod SET
Kol = (Prod.Kol - Ins.Kol)
FROM Products AS Prod INNER JOIN Inserted AS Ins
ON Prod.ProdN = Ins.ProdN

Delete

deleted. , ,
,
.
. , .
CREATE TRIGGER Kategor_Delete
ON Kategories
FOR DELETE
AS
UPDATE Prod SET Prizn =
FROM Products AS Prod INNER JOIN deleted AS del
ON Prod.KategN = del. KategN

Update

, .
deleted inserted .

.
. ,

CREATE TRIGGER Sotr_Update


ON Sotr
FOR UPDATE
AS
IF UPDATE (TabN)
BEGIN
RAISERROR (' .\
***** .', 10, 1)
ROLLBACK TRANSACTION
END

Instead of
, .

(INSERT, UPDATE, or DELETE).
.
, , .
CREATE TRIGGER Clients_Update
ON ClientsView
INSTEAD OF UPDATE AS
DECLARE @Country nvarchar(15)
SET @Country = (SELECT Country FROM Inserted)
IF @Country = ''
BEGIN
UPDATE ClientsGer
SET ClientsGer.Phone = Inserted.Phone
FROM ClientsGer JOIN Inserted
ON ClientsGer. ClientID = Inserted. ClientID

ELSE

END
IF @Country = ''
BEGIN
UPDATE ClientsRus
SET ClientsRus.Phone = Inserted.Phone
FROM ClientsRus JOIN Inserted
ON ClientsRus.ClientID = Inserted.ClientID
END



ALTER TRIGGER.
:
ALTER TRIGGER trigger_name
ON table
[WITH ENCRYPTION]
{{FOR {[,] [DELETE] [,] [UPDATE] [,][INSERT]}
[NOT FOR REPLICATION]
AS
sql_statement [...n] }
|
{FOR {[,] [INSERT] [,] [UPDATE]}
[NOT FOR REPLICATION]
AS
IF UPDATE (column)
[{AND | OR} UPDATE (column) [,...n]]
sql_statement [...n] }
}



Transact-SQL ALTER TABLE:
ALTER TABLE table
{ENABLE | DISABLE} TRIGGER
{ALL | trigger_name[,n]}

7

, .
. :
, ; ;
; ; .

SQL Query Analyzer,
.
: _x_yy_name, x , yy
, name . ,
,
!
( ):
1. .
.


, .
2. , ..
, .
, ,
.
3. - .

.
4. .

.
5. , .
, ,
.
6. - .
.
7. .
10 000 5 .
8.
.

5% .
9. .
,
.
,
.
10. .
, .
11.
.
,
.

8.

Internet
. World Wide Web,
, -
. , ,
, . WWW
: ,
, .
, HTML (. HyperText Markup Language). ,
.


WWW -
:
1. 1. IP -
, DNS.

2. 2. Web-,
IP- Web.
HTML ,
.
( ,
, ) ( ),
.
.
:
Internet,
.
,

.
HTML .
. , <> ( <HTML>).
(
/ : , </HTML>).

,
(<_ 1=1 []>) .
HTML , Web-browser ( ) ,
HTML ( )
, , .
, ,
.
, .


HTML <HTML> </HTML>.
HTML : (HEAD) (BODY) .
:
<HTML>
<HEAD>

</HEAD>
<BODY>

</BODY>
</HTML>

.
<TITLE> </TITLE> ( '' - (
), ).
,
, , . <BODY>: BGCOLOR
(FFFFFF _), BACKGROUND -
, ( .\images\1.bmp).



.
: <H1> </H1>, , <H6> ... </H6> <Hi> ( i
1 6) .
, .
: <P> ... </P>. . ,
<P> </P>, .
<Hi> <P> ALIGN
, : <H1 ALIGN=CENTER> </H1>.
: LEFT, CENTER, RIGHT, JUSTIFY( ).
: <PRE> </PRE>. ,
<PRE> </PRE> ( preformatted ),
,
.
.
, .
, .

: <UL> </UL>.
<LH> - <LI> - .
, .
: <OL> </OL>. ,
.
: <DL> </DL>.
<DT> - <DD> - .

, : <LI> <OL> </OL>.
: <TABLE> </TABLE>. ( )
. :
- - CAPTION
- - BORDER (0 )
- - ALIGN .
- - WIDTH ( %
)
- - BGCOLOR .
- - CELLPADDING
.
- - CELLSPACING .
<TABLE> .
<TR> </TR>.
(BGCOLOR), (ALIGN)
(VALIGN, TOP, MIDDLE BOTTOM) .
<TH> </TH> : <TR>
<TH> 1</TH> <TH> N</TH> </TR>.
<TD> </TD>, .
, .
, ,
. .
: , .
,
. <A> </A>.
HREF (Hyper Text
Reference). ,
_.htm ,
- HTTP:///_.htm.
(
. ancor) NAME , <A NAME =
>. HREF _#,
, #.
, .
(
).
, , :
<A HREF= _.htm#1 NAME=2> 1
</A>. ,
, .

: <FONT FACE="Times New


Roman" SIZE="5" COLOR="#0080FF"> </FONT>. ,
, . , ,
<B> </B>, - <I> </I> - <U>
</U>.
: <IMG
SRC="__"
WIDTH="400"
HEIGHT="300"
ALT="_">. SRC ,
WIDTH HEIGHT , ALT ,
,
. ,
.
: <HR>
. ALIGN, WIDTH SIZE .
: HTML
, ,
<BR>.


.htm
.html. ,
Windows (Notepad).
, .
C:\SHTTPS\WWW\.
.( 450501).
.



Small HTTP Server. HTTP-.
> > Small HTTP Server.
. MS Internet Explorer. IE

HTTP://localhost/_/_.htm


HTML HTML.
,
.
.
HTML.
,
HTML
.
.

Оценить