"Semantic Data SQL Script"의 두 판 사이의 차이

DH 교육용 위키
이동: 둘러보기, 검색
177번째 줄: 177번째 줄:
 
where partName is not NULL
 
where partName is not NULL
 
order by partName, id
 
order by partName, id
 +
 +
 +
[[분류:튜토리얼]]

2019년 12월 12일 (목) 02:15 판

 
/* 
----------------------------------------------------------------------------------------------
				Semantic Data Table 생성 Script 
----------------------------------------------------------------------------------------------
 1. 이 파일을 에디터로 옮기고, "$project$"로 표시된 부분을  project 이름으로 바꾸세요. 	
    에디터의 '찾기 바꾸기' 기능을 이용
 2. 수정된 파일을 확장자 ".sql"인 파일로 저장하고, Sql Server Management Studio로 읽어들이세요.
 3. Query Editer 상에서 아래의 스트립트를 차례대로 실행하세요.
----------------------------------------------------------------------------------------------
*/
	

/* ************************************************************ 
	Step 1: 테이블 생성
		한 번만 수행하면 됩니다.
************************************************************ */

--① _____Data ....... 시맨틱 데이터의 노드
create table $project$Data (
	id nvarchar(40) not null,
	class nvarchar(40) null,
	groupName nvarchar(40) null,
	partName nvarchar(40) null,
	label nvarchar(80) null,
	infoUrl nvarchar(256) null,
	iconUrl nvarchar(256) null,
	primary key(id)
)

--② _____Links ...... 시맨틱 데이터의 링크 */
create table $project$Links (
	source  nvarchar(40)  not null,
	target  nvarchar(40) not null,
	relation nvarchar(40) not null,
	attribute nvarchar(40) null,
	primary key (source, target, relation )
)

--③ _____Schema ..... 각 노드의 계층적 분류
create table $project$Schema (
	id nvarchar(40) not null,
	class nvarchar(40) null,
	groupName nvarchar(40) null,
	partName nvarchar(40) null,
	label nvarchar(80) null,
	infoUrl nvarchar(256) null,
	iconUrl nvarchar(256) null,
	primary key(id)
)

--④ _____Category ... 계층 분류와 노드 사이의 관계
create table $project$Category (
	source  nvarchar(40)  not null,
	target  nvarchar(40) not null,
	relation nvarchar(40) not null,
	attribute nvarchar(40) null,
	primary key (source, target, relation )
)

--⑤ _____Nodes ... 노드 데이터와 분류 데이터 병합
create view $project$Nodes
as
	select * from $project$Data
	union
	select * from $project$Schema


/* ************************************************************ 
	Step 2: 업로드한 엑셀 데이터를 테이블에 삽입
		데이터를 새로 업로드 할 때마다 수행합니다.
************************************************************ */

-- Excel File 업로드: 'SQL Server 가져오기 및 내보내기 마법사' 사용

	/* 
	반복해서 업로드 할 경우 기존 데이터 파일을  삭제해야 합니다.
	그렇게 하지 않으면 중복 입력이 됩니다.
	drop table $project$Data$
	drop table $project$Links$
	*/

--① Node List 데이터 삽입

	/* 
	노드 데이터 테이블 안에 기존 데이터가 있으면 삭제하세요.
	delete $project$Data
	*/

insert into $project$Data (id, class, groupName, partName, label, infoUrl, iconUrl )
select id, class, groupName, partName, label, infoUrl, iconUrl  
	from $project$Data$

--② Link List 데이터 삽입

	/* 
	링크 데이터 테이블 안에 기존 데이터가 있으면 삭제하세요.
	delete $project$Links
	*/

insert into $project$Links (source, target, relation)
select source, target, relation from $project$Links$


/* ************************************************************ 
	Step 3: 계층적 메뉴 데이터 생성
		데이터가 바뀔 때마다 반복해서 수행합니다.
		※ 계층적 메뉴를 사용할 때만 필요한 루틴입니다.
************************************************************ */

--① 스키마 데이터 생성

	/* 
	스키마 데이터 테이블 안에 기존 데이터가 있으면 삭제하세요.
	delete $project$Schema
	*/

insert into $project$Schema (id, class, groupName, label)
values ('Thing', 'Category', 'Top', 'Thing' )

insert into $project$Schema (id, class, groupName, label, infoUrl)
select class as id, 'Category' as class, 'Thing' groupName, class as label, 'class:'+class as infoUrl
from $project$Data
group by class

insert into $project$Schema (id, class, groupName, label, infoUrl)
select groupName as id, 'Category' as class, class as groupName, groupName as label, 'groupName:'+groupName as infoUrl
from $project$Data
where groupName is not NULL
group by class, groupName

insert into $project$Schema (id, class, groupName, label, infoUrl)
select partName as id, 'Category' as class, groupName, partName as label, 'partName:'+partName as infoUrl
from $project$Data
where partName is not NULL
group by groupName, partName

--② 카테고리 데이터 생성

	/* 
	카테고리 데이터 테이블 안에 기존 데이터가 있으면 삭제하세요.
	delete $project$Category
	*/

--hasCategory (top->1st)
insert into $project$Category (source, target, relation )
select 'Thing' as source, class as target, 'hasCategory' as 'relation' from $project$Data
group by class

-- hasCategory (1st->2nd)
insert into $project$Category (source, target, relation )
select class as source, groupName as target, 'hasCategory' as 'relation' from $project$Data
where groupName is not NULL
group by class, groupName

-- hasCategory (2nd->3rd)
insert into $project$Category (source, target, relation )
select groupName as source, partName as target, 'hasCategory' as 'relation' from $project$Data
where partName is not NULL
group by class, groupName, partName

-- hasMember (Major Category)
insert into $project$Category (source, target, relation )
select class as source, id as target, 'hasMember' as 'relation' from $project$Data
where groupName is NULL
order by class, id

-- hasMember  (2nd Category)
insert into $project$Category (source, target, relation )
select groupName as source, id as target, 'hasMember' as 'relation' from $project$Data
where groupName is not NULL and partName is NULL
order by groupName, id

-- hasMember  (3rd Category)
insert into $project$Category (source, target, relation )
select partName as source, id as target, 'hasMember' as 'relation' from $project$Data
where partName is not NULL
order by partName, id


[[분류:튜토리얼]]