48
័យិា Microsoft SQL Server 2008

ស្វ័យសិក្សា with Ms SQL Server 2008

Embed Size (px)

DESCRIPTION

[email protected]

Citation preview

Page 1: ស្វ័យសិក្សា with Ms SQL Server 2008

ស្វ័យសិ្ក្សា Microsoft SQL Server 2008

Page 2: ស្វ័យសិក្សា with Ms SQL Server 2008

សាកលវិទ្យាល័យប ៀលប្រាយ កិច្ចការស្រាវជ្រាវរបស់ និសសិត ឆ្ន ាំ ទី៣ ជាំនាន់ទី ៨ ជ្រកុម A1 បនទប់ ២០៤ ឯកទទស Information System ននមហាវទិាល័យ វទិាាស្រសត និង បទច្ចកវទិា។ ទរៀបទរៀងទោយ៖ ចៅ វុន ថាយ Email : [email protected]

Phone : 089 74 66 67

Page 3: ស្វ័យសិក្សា with Ms SQL Server 2008

DATABASE FILES

Data Files : គឺជ្រតូវបានទជ្របើរកាទុក និង Object ដូច្ាាៈ Table, Indexes, Views, Triggers, និង Store Procedure ទៅទលើ Database របស់ Server ។ Data Files: Primary (*.MDF) & Secondary

(*.NDF) Data Files ។

Log Files: គឺជ្រតូវបានទជ្របើរកា ទុកពត៌មានរបស់ Transaction Log Transaction Log: គឺការកត់ជ្រា User ទលើ Server & Client Connection

ចូ្លកមមវធីិ SQL Server (*.LDF) ។

Page 4: ស្វ័យសិក្សា with Ms SQL Server 2008

FILE GROUP

File group: គឺាការោក់ទ ម្ ោះ ជ្រកុមនន File មួយជ្រកុម។

Fixed group:គឺា Primary File group។

File growth:គឺការកាំនត់ទាំហាំសាំរាប់ File growth

- Megabytes : Size 1MB.

- Percent : Max size (Unlimited)

Page 5: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើត DATABASE ថមី

Right Click “Databases” យក “New Database”

Page 6: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើត DATABASE ថមី

Database Name ោក់ទ ម្ ោះ “DB_Test”

Page 7: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើត DATABASE ថមី

ទរ ើសយកទីាាំងរកា File រចួ្ចុ្ច្ប ូតុង “OK”

Page 8: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើតាមសទសរ CODE

Use Master

GO

Create Database DB_Test

On Primary

(Name='DB_Test_Data',

FileName='D:\DataBase\DB_Test_Data.MDF',

Size=1MB,MaxSize=100MB,Filegrowth=1MB),

Page 9: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើតាមសទសរ CODE

Filegroup G1

(Name='DB_Test_Data2',FileName='E:\DB\DB_Test_Data2.NDF',

Size=1MB,MaxSize=100MB,Filegrowth=1MB),

Log On

(Name='DB_Test_Log',

FileName='D\DataBase\DB_Test_Log.LDF',

Size=1MB,MaxSize=100MB,Filegrowth=1MB);

Page 10: ស្វ័យសិក្សា with Ms SQL Server 2008

ជ្របទភទទិនន័យ

ជ្របទភទទិនន័យ អាា ធិបាយ ទាំហាំ TinyInt ទិនន័យាទលខគត់ 0 ទៅ 255 1byte

SmallInt ទិនន័យាទលខគត់ -215 ទៅ 215 2byte

Int ទិនន័យាទលខគត់ -231 ទៅ 231 4byte

BigInt ជ្របទភទទិនន័យាទលខ 8byte

Decimal(p,[s]) P: ច្ាំនួនទសសភាគ, S:តាំនលអតិបរមាដដលរកានូវច្ាំនួនទសសភាគ។

8byte

Real ាតាំនលទសសភាគ៖ 40E+38 3.40E+38 8byte

Numeric(p,[s]) ដូច្ Decimal 8yte

Page 11: ស្វ័យសិក្សា with Ms SQL Server 2008

ជ្របទភទទិនន័យ

Float ទិនន័យាទសសភាគ 1.79E+308 1.79E + 308 8byte

Char ទិនន័យាអកសរមិន Unicode n(1 - 8000) nbyte

VarChar ទិនន័យាអកសរមិន Unicode n អកសរ NChar ាអកសរ n(1 - 4000) Unicode 1តួ 2byte 2byte

NVarChar ទិនន័យាអកសរ រ ឺUnicode n (1- 4000) 2byte

Text ទិនន័យាអកសរ 231 -1 16byte

NText ទិនន័យាអកសរ រ ឺUnicode 230 -1 2byte 16byt

for pointer

Mony តាំនលរបិូយវតាុ -215 ទៅ-215 8byte

Page 12: ស្វ័យសិក្សា with Ms SQL Server 2008

ជ្របទភទទិនន័យ

Small Money ទិនន័យរូបិយវតាុ -214,748.3648 214,748.3648 4byte

Date Time 8byte

Small Date Time 4byte

Date

Bit (0,1) 1byte

Binary ទជ្របើសាំរាប់ា binary n(1 - 8000) N+4byte

Image ទជ្របើសាំរាប់ Var ា binary 232 -1 16byte

Page 13: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើត TABLE ថមី

Right Click “Tables” ទរ ើសយក “New Table”

Page 14: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើត TABLE ថមី

បទងកើត tblCategory

Page 15: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើត TABLE ាម CODE

Syntax: “Create Table”

Create Table <tablename>

(<Fieldname><datatype>[Not Null],[Default null],

[…, ]

[Constraint <Constraint name><Constraint type>]

)[ON <Filegroup>];

Page 16: ស្វ័យសិក្សា with Ms SQL Server 2008

CONSTRAINTS

Primary Key

Foreign Key

Check

Not Null

Default

Unique

Identity

Page 17: ស្វ័យសិក្សា with Ms SQL Server 2008

CONSTRAINTS

Primary Key: ាសាំនុាំ Field(s) មួយឬទជ្រច្ើនសាំរាប់សាំគាល់ Entity + លកខណាៈ - Unique

- Not Null

- References

Page 18: ស្វ័យសិក្សា with Ms SQL Server 2008

CONSTRAINTS

Foreign Key:ា Fields សាំរាប់ភាា ប់ Relationship ទៅ Primary Key

+លកខណាៈ - Data ជ្រតូវបានបញ្ាូ លកនុង Primary Key ាមុន - Delete & Update ទលើ Primary Key

* Cascade

* Set Null

* Set Default

* No Action

Page 19: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើត TABLE ាម CODE

Use DB_Test

GO

Create Table tbCategory

(CatID varchar(50),

CatName varchar(50),

Description varchar(50),

Constraint PK_Cat PRIMARY KEY(CatID));

Page 20: ស្វ័យសិក្សា with Ms SQL Server 2008

បទងកើត TABLE ាម CODE

Create table tblProduuct

(PID varchar(50) not null , Barcode varchar(100)not null,

PName varchar(50)not null Unique, Unit varchar(50),

UnitInstock numeric(18,2) not null Check(UnitInstock>=0) default 0,

UnitPrice numeric(18,2) not null Check(UnitPrice>=0) default 0,

Photo Image, CatID varchar(50) not null,

Constraint PK_Pro Primary Key(PID),

Constraint FK_Pro_Cat Foreign Key(CatID) References tblCategory On

Update Cascade)

Page 21: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

CREATE

ALTER

DROP

GRENT

REVOKE

Page 22: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

ALTER Culomn

Alter Table <Table name>

Alter Column <Column Definition>;

Ex: Alter table tblCategory

alter Column CatName Nvarchar(100) not null;

Page 23: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

ADD Column

Alter Table <table name>

Add <Column Definition>;

Ex: Alter table tblCategory

Add [Description] Ncarchar(200);

Page 24: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

Drop Column

Alter Table <table name>

Drop Column <Column Name>;

Ex: Alter Table tblCategory

Drop Column [Description];

Page 25: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

ADD Constraint

Alter Table <Table Name>

Add Constraint <Constraint Definition>;

Ex: Alter Table tblEmployee

Add Constraint CK_DOB Check(DOB<GetDate());

Page 26: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

Drop Constraint

Alter Table <table name>

Drop Constraint <Constraint name>;

Ex: Alter Table tblEmployee

Drop Constraint PK_Emp;

Page 27: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

Rename Table

Execute SP_Rename

„ DB_Text . tblCategory ‟, „ tblCat ‟

Rename Column

Exec SP_Rename

„ DB_Test . Description ‟, „ Des ‟, „ Column ‟;

Page 28: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

DROP Table

Drop Table <Table name>;

Ex: Drop tblProduct;

Create Index

Create [Unique|Clustered|Nonclustered] Index <Index name>

On <table name>(<Column nam>[,..]); Ex: Create Index IX_EmpName On tblEmployee(EmployeeName);

Page 29: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA DEFINITION

DROP INDEX

Drop Index <Index name>;

Ex: Drop Index IX_EmpName;

Page 30: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

INSERT

DELETE

UPDATE

- VIEW

- STORED PROCEDURE

- FUNCTIONS

- TRIGGER

- CURSOR

Page 31: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

INSERT

INSERT INTO <table name>[(<field name>)]

VALUES(<Value List>);

INSERT INTO <table name>[<field name>]

Select <field list> From <table name>

[Where Condition];

Page 32: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

DELETE

DELETE [From] <table name> [Where Condition];

UPDATE

UPDATE <table name>

SET Field1=Values1[,…]

[Where Condition];

Page 33: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

VIEW

Create View <View name> AS

Select Statement;

Ex: Create View vStudent AS

Select Count(*) as [N-Stu],Max(Score) as [Max-Score],Min(Score)

as [Min-Score],Avg(Score) as [AVG-Score]

From tblStudent

Page 34: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

STORED PROCEDURE

Create Procedure <Pro name>[ Parameter List] AS

[Begin]

<Body>

[End ;]

Page 35: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Ex: Create Procedure SP_AddPro @ProID varchar(10),

@ProName varchar(50),@Qty int,@Price Money AS

Begin

Insert Into tblProduct(ProID,ProName,Qty,Price)

values(@ProID,@ProName,@Qty,@Price)

End ;

Call SP_AddPro „P1‟,‟ABC‟,50,30

Page 36: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

FUNCTIONS

* Function Return Scalar

* Function Return Table

Page 37: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Function Return Scalar

Create Function <fun name>([Parameter]) Returns <DataType>

AS

[Begin]

<Body>

Return <Value>

[End ;]

Page 38: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Ex: Create Function MySum(@a int,@ b int) Returns int AS

Begin

Declare @Result int

Set @Result=@a+@b

Return @Result

End ;

Page 39: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Function Return Table

1. Implicit

Create Function <Fun name>([Parameter List])Returns Table AS

Return (Select SQL);

Page 40: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Ex: Create Function YSale(@ProID varchar(10))Returns Table AS

Return(Select Year(SaleDate) as Years,Sum(Qty*Price) as Sales

From tblSale Inner Join tblSaleDetail On tblSale . SaleID

=tblSaleDetail.SaleID

Where ProID=@ProID

Group By ProID,Year(SaleDate)

);

Page 41: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

2. Explicit

Create Function <functon name>([Parameter List]) Returns

@TableName Table(<field list>) AS

Begin

<body>

Return

End ;

Page 42: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Ex: Create Function YSale(@ProID varchar(10))Returns @T

Table(Year int Sales money) AS

Begin

Insert Into @T Select Year(SaleDate) as Years,Sum(Qty*Price) as Sales

From tblSale Inner Join tblSaleDetail On tblSale . SaleID =tblSaleDetail.SaleID

Where ProID=@ProID Group By ProID,Year(SaleDate)

Return

End ;

Page 43: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

TRIGGER: គឺា Procedure ពិទសសមួយដដល Run ទលើ Events

Insert, Delete, Update ទៅទលើ Table ណាមួយ។ Create Trigger <Trigger name> On <table name>

[FOR|AFTER] INSERT,UPDATE,DELETE AS

[Begin]

<body>

[End ;]

Page 44: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Ex: Create Trigger InsertProduct On tblProduct After Insert AS

Begin

declare @ProID varchar(10),@Price numeric(18,2)

Select @ProID=ProID,@Price=Price From Inserted

Insert Into tblPriceHistory(Hdate,ProID,Price)

Values(GetDate(),@ProID,@Price)

End

Page 45: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Ex: Create Trigger UpdateProduct On tblProduct After Update AS

If Update(“Price”)

Begin

declare @ProID varchar(10),@Price numeric(18,2)

Select @ProID=ProID,@Price=Price From Inserted

Insert Into tblPriceHistory(Hdate,ProID,Price)

Values(GetDate(),@ProID,@Price)

End

Page 46: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Cursor: គឺា Special variable របស់ SQL Server សាំរាប់ផទុក Address របស់ Records ដដល Return ទច្ញ Select SQL Statement។

Declare Cursor Cursor Name

[ SCROLL ] [ Dynamic | KeySet | Static]

For Select SQL

Page 47: ស្វ័យសិក្សា with Ms SQL Server 2008

DATA MODIFICATION

Cursor Name : ាទ ម្ ោះអទថរ Cursor

SCROLL : ទដើមបីទោយទគោច្ more Cursor បានជ្រគប់ Direction

(Next, First, Last, Pointer) (Default : Forward-Only)

Dynamic: Dynamic Cursor(Auto Refresh Data ទលើ Cursor)

Static: Do not Refresh Data ទលើ Cursor

KeySet : Refresh Only none-key value ច្ាំដនក key value

អត់ Refresh

Page 48: ស្វ័យសិក្សា with Ms SQL Server 2008

សូមអរគណុ!

រក្សាសិទ្ទគ្រប់យ៉ាងដោយ៖ ដៅ វុន ថាយ

Email: [email protected]