Anda di halaman 1dari 26

SQL server

SQL Server

P1
P3
P5
P6
ER P11

P14
P15
P16
P16
P16

P12

(join) P18
P19


http://192.168.184.250/lwcheng/
http://120.105.184.250/lwcheng/

SQL
http://www.1keydata.com/tw/sql/sql.html

SQL
http://idd.ccnet.com.tw/ebook/SOL.htm

PostgreSQL 7.4devel
http://jsp.dfes.tpc.edu.tw:8080/pgsqldoc/index.html

Transact-SQL ()
http://technet.microsoft.com/zh-tw/library/bb510741.aspx

1.

2.

Misplace

-1-

SQL server

3.

Ad-hoc query

Sequential Access File System

Direct Access File System


Data Dependence

XML

Data Independence

Data Independence

Select
Form
WhereSelect Form Where
Primary key
Homogeneous Distributed Database System
Heterogeneous Distributed Database
-2-

SQL server

System

[OLAP] Online Transaction ProcessData Warehouse

Data MiningData Warehouse

Data
Information
Knowledge
Intelligence

[ DBMS ] Database Management System
[ DBA ] Database AdministratorDatabaseDatabase System

End Users

Application Programs

-3-

SQL server

Application Programs

Anomaly

Candidate key

Data Redundancy

Database System

Data Independence

Data Warehouse

Database Consistency

Denormalization

Delete Anomaly

Direct Access File System

[ DBA ] Database Administrator

Entity Integrity( )

Functional Dependency

Insert Anomaly

Partial Functional Dependency

Repository Definition

Referential Integrity( )

Transitive Functional Dependeney

Update Anomaly

Attribute

Data

Database

Data Mining

End Users

Field

Foreign key

Information

Intelligence

Key

Knowledge

NULL

Normalization

Primary key

Query

Record

Relation

Row

ROLLUP

CUBE

Super Key

View

Sequential Access File System

Homogeneous Distributed Database System

[OLAP] Online Transaction Process

DBMSDatabase Management System


ER DiagramEntity-Relationship Diagram()
Heterogeneous Distributed Database System

Anomaly

Attribute

Candidate key

Data Independence

Data Mining

Data

Database Consistency

Entity Integrity( ) ex:

End Users

-4-

SQL server

ER DiagramEntity-Relationship Diagram

Field

Functional Dependency

Foreign key

Homogeneous Distributed Database System

Heterogeneous Distributed Database System

Information

Key

Knowledge

Normalization

NULL

[OLAP] Online Transaction Process

Primary key

Query DBMS SQL

Referential Integrity( ) ex: 2

Record

Relation

Repository Definition

Row()

Super Key

Unique Identifier()

View SQL

(one-to-one relationship)

(one-to-many relationship)

(denormalize)

(many-to-one relationship)

(many-to-many relationship)

(deadlock)

(dependencies)

(index)

(record) ()

(unique index)

(physical name)

(entity integrity)

(composite index)

-5-

SQL server

(composite key)

(junction table)

(join)

(identifier)

(relational database)

First Normal Form


Second Normal Form
Third Normal Form
Boyce_codd Normal Form
Fourth Normal Form
Fifth Normal Form

RA , B , C

R , ,

B or C B or C

R1A , B

R2B , C

BCNF: BCNF ""

-6-

SQL server

10
15
10

15
10
5

150
150
50

15

75

S01
S02
S03
S04

S50
S51
S52

S99

, , ,

-7-

S01
S01
S01
S02
S02
S03

SQL server

VB
Linux
Database
VB
Linux
Database

10
50
60
10
50
60

Ans
Ans

Ans

Ans , , ,

R A , B , C , D

R A , B , C , D

AnsR1A , B R2B , C , D
AnsR1A , B , C R2C , D
R1A , B , D R2B , C
R1C , D R2B , C R3 A , B

R A , B , C , D , E
AnsR1A , B , D , E R2B , C

R A , B , C , D , E
AnsR1A , B , D R2B , C
R3D , E

R A , B , C , D , E

AnsR1A , B , C R2B , E R3C , D

Q6R, , ,

R1,,R2,


Q7R, , ,


R1,R2,
R3,

Q8R, , ,

R1,,R2,

Q9R, , , ,


R1,,,R2,

-8-

SQL server

R ,,
2NF

1NF ( )

R1 ,
R2 ,

R ,,
3NF

2NF (
3NF )

R1 ,
R2 ,

R ,,
BCNF

3NF (
)

R,,

R ,,
BCNF

3NF(
)

R1 ,
R2 ,

R ,,
BCNF

3NF(
)

R1 ,
R2 ,

100
150

200
250
300
300

100
150
200
250
300
300

100
150
200
250
300
300

( )
-9-

SQL server

R( D , A , B , C , E )

( )

R1R2
R3R
R1R2
R3R

1
2
3

( )
( )

R1
R2
R3

- 10 -

SQL server

R1
R2

R1R2
R3

R1R2

R1R2
R3


R1R2
R1R2R3

- 11 -

SQL server

ER

1.

CDEJEFGHIG
2.

(A) (B) (C)(D)(E)(F)(G)(H) (I)(J)

3.
4.

5.

6.


- 12 -

SQL server

( , , , )
( , , , , )
( , , )

R1( A , B )
R2( C , D , A )
R3( E , F , C )
R1( A ,B )
R2( C , D , A ,E )
R3( E , F )
R1
R2
R3
R
R1

R1
R2
R3
R

(key)
(Primary key)
(Foreign key)

()
(Super Key)
(Unique Key)
(Unique Identifier)

- 13 -

SQL server

{ , , , }

S001

F111

S002

F222

S003

F333

2(4 )-1=15

{ }

{ , }

{ }
{ }

{ }

{ , }

{ , , }

{ , , }

{ , }

{ , , }

{ , }

{ , , }

{ , }

{ ,,, }

{ , }

{ }

F001

951

80

{ }

F001

952

70

{ }

F002

951

80

{,}

F002

952

65

{ , }

F003

951

90

{,}

F003

952

70

{,,}

{ }

P001

100

{ }

P002

50

{ }

P003

50

{ , }

P002

50

{ , }

{ , }
{ , , }

- 14 -

SQL server

{A}

100

{B}

200

{C}

100

{A,B}

50

{A,C}

{B,C}
{A,B,C}

F001

F002
F003
F004

{}

{}
F001
F001
F002

{}
{ , }

{ , }

{ , }
{ , , }

Create Table
Create Table (
char(4) primary Key,
VARCHAR(10),
CHAR(1)
)

INSERT INTO
VALUES ('S001', '', '1');

INSERT INTO ( , )
VALUES ('S003', '');
TABLE

DATETIME
ALTER TABLE
ADD DATETIME


Create Database im3a
GO

Use im3a
GO

Create View
v_1

DROP TABLE ;
.
UPDATE
SET ='1989/10/13'
WHERE ='S001'

- 15 -

SQL server

DESC

Order by group by

AVG

MAX

MIN

Count

SUM

Having

delete

Insert into

update

left join

Primary key

from

null

--
Create database
primary key
unique
Foreign key

--
Create table
( Varchar(10) primary key,
Varchar(10),
Varchar(2),
Varchar(10),
Varchar(10),
Varchar(10) unique,
Customer Fk_ Foreign key() references ())

94700
1

Judy

94700
2

Nick

035500055 Im94A

J123456789

Im94A

035500000 Im94B

J123456788

Im94B

--
---- 1
INSERT INTO "" (" 1", " 2", ...) VALUES (" 1", " 2", ...)
Insert into (,,,,,)
Values (947001,Judy,,035500055,Im94A,J123456789);
---- 2
INSERT INTO " 1" (" 1", " 2", ...)
SELECT " 3", " 4", ... FROM " 2"
Insert into (,,,,)
Select ,,,, From ;
--Create View
- 16 -

SQL server

Create View View_name as select from ;


-- Create Index
Create Index on ();

--
DROP TABLE ""

--
ALTER TABLE "table_name"
[]

--
TRUNCATE TABLE ""
--
DELETE FROM ""

--
UPDATE ""
SET " 1" = [ 1], " 2" = [ 2]

WHERE {}

WHERE {}

--
SELECT ""
FROM ""

--
SELECT DISTINCT ""
FROM ""

--
SELECT " 1"
FROM ""
WHERE " 2" []
(SELECT " 1"
FROM ""
WHERE [])

--
SELECT ""("")
FROM ""
-- AVG
SELECT AVG("")
FROM ""
-- COUNT
SELECT COUNT("")
FROM ""
-- MAX
SELECT MAX("")
FROM ""
-- MIN
SELECT MIN("")
FROM ""

--
- 17 -

SQL server

SELECT ""
FROM ""
WHERE ""

-- SUN
SELECT SUM("")
FROM ""

--AND
SELECT ""
FROM ""
WHERE "" AND ""

-- GROUP BY
SELECT " 1", SUM(" 2")
FROM ""
GROUP BY " 1"

--OR
SELECT ""
FROM ""
WHERE "" OR ""

--
SELECT " 1", SUM(" 2")
FROM ""
GROUP BY " 1"
HAVING ()

--IN
SELECT ""
FROM ""
WHERE "" IN ('', '', ...)
-- BETWEEN
SELECT ""
FROM ""
WHERE "" BETWEEN '' AND ''
-- ORDER BY
SELECT ""
FROM ""
[WHERE ""]
ORDER BY "" [ASC, DESC]
-- ASC DESC
ASC

-- EXISTS
SELECT " 1"
FROM " 1"
WHERE EXISTS
(SELECT *
FROM " 2"
WHERE [])
-- CASE
SELECT CASE ("")
WHEN " 1" THEN " 1"
WHEN " 2" THEN " 2"
...
[ELSE " N"]
END
FROM ""

--
SELECT ""." 1" ""
FROM "" ""
--AS
SELECT ""." 1" ""
FROM "" ""
- 18 -

SQL server

--AS
SELECT ""." 1" AS ""
FROM "" AS ""

UNION

Intersect

Difference

1 2

1 2

( 1 - 2 )

R1 R2 5

R1 R2 1

R1 - R2 2

Select *From R1

Select *From R1

Select *From R1

Union
Select *From R2

Intersect
Select *From R2

Except
Select *From R2

Inner Join()
R1
R2

R1*R2[,(Cartesian Product)]
M1 :2 M2 :2 M1+M2 :4
N1 :4 N2 :4 N1N2 :8

100

200

300

400

500

300

Union Rx Ry
Show
SELECT FROMR

SELECT FROMR

R R
SELECT
FROMR
R
SELECT
FROMR

Union

100

200

300

300

400

500

100

200

Union All Show Rx Ry


SELECT FROMR

SELECT FROMR

R R
SELECT
FROMR

SELECT
FROMR

- 19 -

SQL server

Linux

Linux

select , avg() as
from (select , avg( ) as
from 961
group by
union
select , avg( ) as
from 962
group by )v
group by

R1
A

100

200

200

400

select A, count(*)
from R1
group By A

R1
--

100

200

200

200

200

400

400

--

select A, max(B) as
from R1
group By A

--

select A, max(B) as

100

100

from R1

200

200

group By A

200

400

400

- 20 -

SQL server

R1

300

NULL

NULL

300

100

NULL

400

NULL

400

200

NULL

NULL

700

700

100

300

300

400

Select A , [X], [Y], [Z]

Select A , [X], [Y], [Z]

From ( Select A , B , C

From ( Select A , B , C

From R1)V

From R1)V

Pivot (Sum(c) For B In( [X], [Y], [Z]))PV

Pivot ( isnull(sum(c),0) For B In( [X], [Y],


[Z]))PV

R2

Select A , B ,

Select A , B ,

10

Sum(C)

10

Sum(C)

20

From R2

20

From R2

NULL

30

Group by A , B

NULL

30

Group by A , B

70

With rollup

NULL

40

With cube

50

70

NULL

120

50

60

NULL

120

70

NULL

90

NULL

130

60

NULL

NULL

280

70

NULL

130

NULL

50

NULL

NULL

280

--

create Table ( nvarchar(4),

nvarchar(10),

nvarchar(1),

nvarchar(3),

Constraint pk_student primary key() )

--
Insert into values ('s001','', '', '3A')
Insert into values ('s002','', '', '3B')
Insert into values ('s003',' ', '', '2A')

- 21 -

SQL server
Insert into values ('s004','', '', '3B')
Insert into values ('s005','', '', '3B')

--

create Table ( nvarchar(4),

S001

nvarchar(4),

S001

int,

S002

varchar(3),

S003

Constraint pk_merit primary key(,,) )

S004

--
Insert into values ('s001','', 1000, '961')
Insert into values ('s001','', 5000, '961')
Insert into values ('s002','', 1000, '961')
Insert into values ('s003','', 1000, '961')
Insert into values ('s004','', 5000, '961')

--Q1 '', ''

--Q2 '', ''

S001

select from

select from

where = ''

where = ''

and in(

and not in(

select from

select from

where = '')

where = '')

--Q3 pivot , [], []

S001

select , [], []

S002

from ( select * from )v

S002

S004

--

S001

3A

5000

--Q4 ,, , ,,

S001

3A

1000

select .,, , ,,

S002

3B

1000

from left join on .=.

S003

2A

1000

S004

3B

5000

S005

3B

NULL

NULL

PIVOT(count() for in ([], []) )PV

- 22 -

SQL server

--

--Q5 Q4,Q3,

select ,, [], []

from ( select .,, , ,,

from left join on .=.)v

PIVOT(count() for in ([], []) )PV

--

--Q6 Q4,Q3,

S001

1000

5000

select ,, [] ,[]

S002

1000

NULL

from ( select .,, , ,,

S003

1000

NULL

from left join on .=.)v

S004

NULL

5000

PIVOT( sum() for in ([], []) )PV

S005

NULL

NULL

P1

10

20

P2

25

40

P3

30

10

P1

10

20

P2

25

10

P1

10

P2

25

P1

10

--Q1
select

,count(*) as
from
group By

--Q2

P1

200

select

,,*as

1500

select

,sum(*) as

P2

1000

450

from

P3

300

from

175

group By

P1

200

50

P2

250

P1

50

P2

125

P1

50

--Q3

- 23 -

SQL server

--Q4

543

select avg() as
from(

--Q5"()"

450

select

,sum(*) as

175

from

50

group By

select

,sum(*) as
from

having sum(*)<(

group By

select avg()

)V

from(
select

,sum(*) as
from
group By

ER
1. .

2..

3.

S001

S001 12/01/2007 5

S002

S001 12/01/2007 6

S003

S002 12/01/2007 5

S004

S002 12/01/2007 6

--
- 24 -

SQL server

Create Database test


go
use test
go
--
Create Table (
char(4) primary key,
varchar(10)
)
go
--
Insert into dbo. (, ) values ('s001', '')
Insert into dbo. (, ) values ('s002', ' ')
Insert into dbo. (, ) values ('s003', '')
Insert into dbo. (, ) values ('s004', '')
GO
--
Create Table (
char(4),
datetime,
char(1),
varchar(10),
Constraint pk_ primary key(, , ),
Constraint Fk_ FOREIGN key() REFERENCES ()
)
--
INSERT INTO dbo.
INSERT INTO dbo.
INSERT INTO dbo.
INSERT INTO dbo.

VALUES ('S001','12/1/2007', '5', '')


VALUES ('S001','12/1/2007', '6', '')
VALUES ('S002','12/1/2007', '5', '')
VALUES ('S002','12/1/2007', '6', '')

--iNNER jOIN I
SELECT *
FROM dbo., dbo.
WHERE dbo..=dbo. .

--iNNER jOIN II
Select *
from inner join on .= .

-- s003 s007,
- 25 -

SQL server

update dbo.
set ='s007', =''
where ='s003'
--A.
SELECT *
FROM LEFT JOIN ON .= .
--B
SELECT *
FROM LEFT JOIN ON .= .
WHERE IS NULL
--C
SELECT ., , COUNT( )
FROM LEFT JOIN ON .= .
GROUP BY .,

- 26 -

Anda mungkin juga menyukai