Introducing ms sql_server_updated

42
www.elarion.com Microsoft SQL Server LARION_TDT@Internship_03 Never stop improving quality

Transcript of Introducing ms sql_server_updated

Page 1: Introducing ms sql_server_updated

www.elarion.com

Microsoft SQL Server

LARION_TDT@Internship_03

Never stop improving quality

Page 2: Introducing ms sql_server_updated

2

ContentsIntroduction

● Relational Database

● Database management system

● Microsoft SQL server

● Structures of Microsoft SQL server

● How to create index for each record

● Backup, Restore Database

● Stored Procedure, Trigger, Function

● View

● Supporting tools for MS SQL server

● Demo

Page 3: Introducing ms sql_server_updated

3

Relational Database (1 of 4)

Definition

● Relational Database is database that all data are

established many tables to have relationship each

other.

● Each table consists of many columns and many rows

that each column is usually called a register, each row

is described an attribute.

Page 4: Introducing ms sql_server_updated

4

Relational Database (2 of 4)

The main components

● The main components:

ª Table: each table stores information of an object

outside real life.

ª Primary key: a collection of one column or many

columns inside tables that it is not permit null

data and is only one.

ª Foreign key: a collection of one column or many

columns inside tables that they are a primary of

other tables and their data can be permitted null

Page 5: Introducing ms sql_server_updated

5

Relational Database (3 of 4)

Example 1:

Page 6: Introducing ms sql_server_updated

6

Relational Database (4 of 4)

Example 2

Page 7: Introducing ms sql_server_updated

Database Management System(1 of 3)

7

● Popular Database Management Systems

■ MS SQL server

■ MySQL

■ Oracle

■ PostgreSql

■ DB2

■ …

Page 8: Introducing ms sql_server_updated

8

Database Management System(2 of 3)

Definition

● Database management system is a

software to permit users to create,

control and access into database.

● Database management system has

maintained consistency of database

during its cycle life.

Page 9: Introducing ms sql_server_updated

9

Database Management System (3 of 3)

Function

● The main functions of database management system

■ Database definition

■ Database construction

■ Database manipulation: add, delete...

■ Database administrator: security...

■ Database protection: backup, restore

Page 10: Introducing ms sql_server_updated

10

Mircosoft SQL server(1 of 3)

● MS SQL Server is a database management

system that it is established and developed by

Microsoft based on Transact-SQL method in

order to exchange data between Client and MS

SQL Server.

Page 11: Introducing ms sql_server_updated

11

Mircosoft SQL server(2 of 3)

Adaptation● MS SQL Server is optimized in order to run Very

Large Database Environment (VLDE) even

though Tera-Byte, especially for serving user

thousands

● MS SQL Server can collaborate with other

servers such as Microsoft Internet Information

Server (IIS), Oracle...

Page 12: Introducing ms sql_server_updated

12

Mircosoft SQL server (3 of 3)

Versions● Versions of MS SQL Server

■ Enterprise Edition

■ Developer Edition

■ Standard Edition

■ Workgroup Edition

■ SQL Server Express Edition

Page 13: Introducing ms sql_server_updated

13

Mircosoft SQL server(1 of 3)

Structure● One of the most usual structures MS SQL Server

■ Client / Server Architecture

■ N-tier Architecture

Page 14: Introducing ms sql_server_updated

14

Structure of MS SQL server(2 of 3)

Client / Server Architecture

Page 15: Introducing ms sql_server_updated

15

N-tier Architecture

Structure of MS SQL server (3 of 3)

Page 16: Introducing ms sql_server_updated

16

How to create index for each record(1 of 2)

● Index is used to supporting and improving

for accessing more quick to data flows base

on value of one or many columns.

● Two forms:

■ Clustered index

■ Non-clustered index

Page 17: Introducing ms sql_server_updated

● Syntax:

CREATE [CLUSTERED | NONCLUSTERED] INDEX index_name

ON table_name(column_name [, column_name]...)

● For example for creating non-clustered index on

MADV of NHANVIEN columns:

CREATE NONCLUSTERED INDEX idx_nhanvien_madv

ON nhanvien(madv)

17

How to create index for each record(2 of 2)

Page 18: Introducing ms sql_server_updated

18

Backup, Restore Database(1 of 7)

Backup

What is Backup Database?

● That is the activity of copying files or

databases from SQL server to another

storage device.

● Having two backup database forms:

■ By backup database commands

■ By direct action in your SQL Server

Page 19: Introducing ms sql_server_updated

19

BACKUP DATABASE { database_name | @database_name_var }

TO < backup_device > [ ,...n ]

[ WITH

[ BLOCKSIZE = { blocksize | @blocksize_variable } ]

[ [ , ] DESCRIPTION = { 'text'| @text_variable } ]

[ [ , ] DIFFERENTIAL ]

[ [ , ] EXPIREDATE = { date | @date_var }

| RETAINDAYS = { days | @days_var } ]

[ [ , ] FORMAT | NOFORMAT ]

[ [ , ] { INIT | NOINIT } ]

[ [ , ] MEDIADESCRIPTION = { 'text'| @text_variable } ]

[ [ , ] MEDIANAME = { media_name | @media_name_variable } ]

[ [ , ] NAME = { backup_set_name | @backup_set_name_var } ]

[ [ , ] { NOSKIP | SKIP } ]

[ [ , ] { NOUNLOAD | UNLOAD } ]

[ [ , ] RESTART ]

[ [ , ] STATS [ =percentage ] ]

]

Backup, Restore Database(2 of 7)

Backup

Syntax of Backup Database Commands

Page 20: Introducing ms sql_server_updated

20

° BLOCKSIZE: automatic parameter

° DIFFERENTIAL: differential backup is only

necessary change datas

° EXPIREDATE: expiration of backup day

° FORMAT/NOFORMAT: destroying existing datas

or not

° INIT/NOINIT: replace datas or not

° DESCRIPTION: details of description

° STATS: appearance of programbar

Backup, Restore Database(3 of 7)

BackupDescription

Page 21: Introducing ms sql_server_updated

21

For example backup database

BACKUP DATABASE QLNGK

TO DISK= ‘F:\ ngk.bak’

WITH

DESCRIPTION = ‘BACK UP’,

STATS

GO

Backup, Restore Database(4 of 7)

Backup

Page 22: Introducing ms sql_server_updated

22

Result

Backup, Restore Database(5 of 7)

Backup

Page 23: Introducing ms sql_server_updated

23

Syntax

RESTORE DATABASE { database_name | @database_name_var }

[ FROM <backup_device> [ ,...n ] ]

[ WITH {

[ RECOVERY | NORECOVERY | STANDBY =

{standby_file_name | @standby_file_name_var } ]

| , <general_WITH_options> [ ,...n ] | , <replication_WITH_option>

| , <change_data_capture_WITH_option> | , <service_broker_WITH options>

| , <point_in_time_WITH_options—RESTORE_DATABASE>

} [ ,...n ]

]

[;]

Backup, Restore Database(6 of 7)

Restore

Page 24: Introducing ms sql_server_updated

24

For example:

RESTORE DATABASE users

FROM DISK='C:\backupusers.BAK'

WITH RECOVERY

Backup, Restore Database(end)

Restore

Page 25: Introducing ms sql_server_updated

25

Stored Procedure, Trigger (1 of 9)

Stored ProcedureWhat is Stored Procedure ?

● SP(Stored Procedure) is the name given to a

group of SQL statement is created and stored in

the server database.

● SP allows transmission parameters, and may be

called by many clients over the network with

different parameters. When SP is changed, all

clients will automatically receive the new

version, because the SP is stored at the Server,

not Client.

Page 26: Introducing ms sql_server_updated

26

Creating procedure

CREATE PROCEDURE procedure_name [;number]

[([parameter1] [,parameter2]...[parameter255])]

AS sql_statements

Stored Procedure, Trigger (2 of 9)

Stored Procedure

Page 27: Introducing ms sql_server_updated

27

For example

CREATE PROCEDURE sp_users_insert

(@firstname varchar(50),

@lastname varchar(50),

@birthday date,

@groupid int)

AS

BEGIN

if exists(select groupid from groups where groupid = @groupid)

BEGIN

insert into users(firstname, lastname, birthday, groupid)

values(@firstname,@lastname,@birthday,@groupid)

END

END

Stored Procedure, Trigger (3 of 9)

Stored Procedure

Page 28: Introducing ms sql_server_updated

28

● Trigger is a data type that is specific saved.

Otherwise, trigger to help ensure data integrity

● A table has maximum 3 trigger types:

■ Update trigger

■ Insert trigger

■ Delete trigger

● For example: employer salary is not permitted

higher than his top-banana

Stored Procedure, Trigger (4 of 9)

Trigger

Page 29: Introducing ms sql_server_updated

29

Syntax:

CREATE TRIGGER trigger_name

ON table_name | view_name

[WITH ENCRYPTION]

{FOR | AFTER | INSTEAD OF}{ [INSERT] [,] [UPDATE ] [,]

[DELETE] }

AS sql_statements

Stored Procedure, Trigger (5 of 9)

Trigger

Page 30: Introducing ms sql_server_updated

30

CREATE TRIGGER groups_users_delete

on groups

for delete

asBEGINDECLARE @groupid int;

Set @groupid=(Select groupid from DELETED);

delete from users where groupid = @groupid

END

Stored Procedure, Trigger (6 of 9)

Trigger

Page 31: Introducing ms sql_server_updated

31

Function is database object similar procedures.

However, the difference between procedures and

function that function returns to a value via its

function name while procedures do not.

Stored Procedure, Trigger (7 of 9)

Function

Page 32: Introducing ms sql_server_updated

32

SyntaxCREATE FUNCTION [ schema_name. ] function_name

( [ { @parameter_name [ AS ]

[ type_schema_name. ] parameter_data_type

[ = default ] [ READONLY ] }[ ,...n ]S

]

)

RETURNS return_data_type

[ WITH <function_option> [ ,...n ] ]

[ AS ]

BEGIN

function_body

RETURN scalar_expression

END

[ ; ]

Stored Procedure, Trigger (8 of 9)

Trigger

Page 33: Introducing ms sql_server_updated

33

For exampleCREATE FUNCTION f_users_tong()

RETURNS @bangthongke TABLE

(groupid int,groupname varchar(50),tonguser INT)

AS

BEGININSERT INTO @bangthongke

SELECT groups.groupid,groups.groupname,COUNT(userid)

FROM (groups INNER JOIN users

ON groups.groupid = users.groupid)

GROUP BY groups.groupid,groups.groupname

RETURN

END

Stored Procedure, Trigger (end)

Trigger

Page 34: Introducing ms sql_server_updated

34

View(1 of 4)

Definition

● View is a method that we can watch at data into

one or many tables of database.

● View is a virtual screen that it is created as a union

of rows and columns by one or many different

tables.

● Decreasing more complex in database

Page 35: Introducing ms sql_server_updated

35

View(2 of 4)

Syntax:

CREATE VIEW COMMAND used to create view:

CREATE VIEW <virtual_name_table>

AS

<select_statement>

Page 36: Introducing ms sql_server_updated

36

View(3 of 4)

For example:

CREATE VIEW DSSV

AS

SELECT masv, hodem, ten,

DATEDIFF (YY, ngaysinh,GETDATE())

AS tuoi, tenlop

FROM sinhvien, lop

Page 37: Introducing ms sql_server_updated

37

View(end)

Result

Page 39: Introducing ms sql_server_updated

39

Demo

● How to use Diagram

● Decentralized

● Backup

● Restore

● Function

● Store Procedure

● Trigger

Page 40: Introducing ms sql_server_updated

40

Reference

● Document of LARION_TDT@internship_02 Courses

● Website: www.msdn.microsoft.com

Page 41: Introducing ms sql_server_updated

41

Question and Answer

?

Page 42: Introducing ms sql_server_updated

42

Thank you!