"인문정보학2020기말준비"의 두 판 사이의 차이

phj
이동: 둘러보기, 검색
(새 문서: *xml table 만들기 <pre> create table 테이블이름넣기 ( id nvarchar(20) not null, xmltxt xml, primary key(id) ) </pre> *xml data 넣기 <pre> insert into xmltable3 (xmltxt)...)
 
6번째 줄: 6번째 줄:
 
primary key(id)
 
primary key(id)
 
)
 
)
 +
</pre>
 +
 +
*다른db에서 복사해오기
 +
<pre>
 +
select * into xmltable3 from Class2020.dbo.xmltable3
 
</pre>
 
</pre>
  

2020년 12월 4일 (금) 15:39 판

  • xml table 만들기
create table 테이블이름넣기 (
id nvarchar(20) not null,
xmltxt xml,
primary key(id)
)
  • 다른db에서 복사해오기
select * into xmltable3 from Class2020.dbo.xmltable3 
  • xml data 넣기
insert into xmltable3 (xmltxt) values
(N' xml 원본넣기 ')
  • 트리거
create trigger xmltable3getid on xmltable3 instead of insert
as /*as 아래와같이 동작하라*/
	insert into xmltable3(id, xmltxt)
	select
		that.xmltxt.value('(//./@id)[1]', 'nvarchar(20)') as id,
		that.xmltxt as xmltxt	/* this, that 모두 상관없음 지금 입력되고 있는 그 테이블*/
	from inserted that
  • 업데이트
update xmltable3
set xmltxt=N'업데이트내용'
where  id='작가-강학태' /*바꾸고 싶은 곳 지정해주기*/
  • xml에서 필요한 자료 추출 및 view 만들기
/* 종합: XML 문서로부터 데이터 활용에 필요한 메타데이터 추출 */ 

select id,
	xmltxt.value('(//범주/프로젝트)[1]', 'nvarchar(40)') as projcet,
	xmltxt.value('(//범주/클래스)[1]', 'nvarchar(40)') as class,
	xmltxt.value('(//표제/이름[./@표기="국문"])[1]', 'nvarchar(40)') as hangeul,
	xmltxt.value('(//표제/이름[./@표기="한자"])[1]', 'nvarchar(40)') as hanja,
	xmltxt.value('(//표제/이름[./@표기="영문"])[1]', 'nvarchar(80)') as english,
	xmltxt.value('(//속성/@틀)[1]', 'nvarchar(20)') as template,
	xmltxt.value('(//속성/정의)[1]', 'nvarchar(256)') as definition
from xmltable3

/* 추출한 메타데이터를 가상 테이블(뷰, view)에 저장 */ 

create view xmlmeta as
select id,
	xmltxt.value('(//범주/프로젝트)[1]', 'nvarchar(40)') as projcet,
	xmltxt.value('(//범주/클래스)[1]', 'nvarchar(40)') as class,
	xmltxt.value('(//표제/이름[./@표기="국문"])[1]', 'nvarchar(40)') as hangeul,
	xmltxt.value('(//표제/이름[./@표기="한자"])[1]', 'nvarchar(40)') as hanja,
	xmltxt.value('(//표제/이름[./@표기="영문"])[1]', 'nvarchar(80)') as english,
	xmltxt.value('(//속성/@틀)[1]', 'nvarchar(20)') as template,
	xmltxt.value('(//속성/정의)[1]', 'nvarchar(256)') as definition
from xmltable3

select * from xmlmeta
  • join을 써서 보여주고싶은 데이터 보여주기
/*클라스가 사건인 데이타를 뽑아내고싶다*/
select * from xmlmeta
where class='사건'

/*클라스가 사건인 데이타를 뽑아내어 xmltable3도 같이 보여줘라, 다보고싶으면 where 빼고*/
select xmlmeta.*,xmltxt.query('//속성'), xmltxt.query('//개설') from xmlmeta
join xmltable3 on xmlmeta.id=xmltable3.id
where class='사건'
  • 인덱스(CROSS APPLY)를 만들어서 VIEW 만들기
create view xmltable3_인명 as
	select id, node.value('.', 'nchar(80)' ) as 인명, node.value('./@id', 'nchar(80)' ) as 인명_id
	from xmltable3 CROSS APPLY xmltxt.nodes('//인명') as R(node) 
  • 빈도 찾아보기
select 용어, 용어_id, count(*) as 빈도
 from xmltable3_용어
 group by 용어, 용어_id
 order by 용어, 용어_id
  • 네트워크그래프 기본 포멧
/*항상 기본 포멧으로 쓰기,shell그래프노드를 표현가능, 샌드박스의 파이선 프로그램 쓰기*/
CREATE TABLE shellData (
	id nvarchar(40) NOT NULL,
	class nvarchar(40) NULL,
	groupName nvarchar(40) NULL,
	partName nvarchar(40) NULL,
	label nvarchar(80) NULL,
	hangeul nvarchar(80) NULL,
	hanja nvarchar(80) NULL,
	english nvarchar(160) NULL,
	infoUrl nvarchar(256) NULL,
	iconUrl nvarchar(256) NULL,
    PRIMARY KEY(id)
)
CREATE TABLE shellLinks (
	source nvarchar(40) NOT NULL,
	target nvarchar(40) NOT NULL,
	relation nvarchar(40) NOT NULL,
	attribute nvarchar(80) NULL,
	PRIMARY KEY(source, target, relation)
)
/*자료넣기*/
insert into friendsData (id, class, groupName, label )
values
('김현', 'Actor', '교수', '김현'),
('박현정', 'Actor', '학생', '박현정'),/*아이디가 박현정인 노드는 클래스가 액터이고 그룹네임이 학생이고 라벨이 박현정이다*/
('정송이', 'Actor', '학생', '정송이'),
('강훈혁', 'Actor', '학생', '강훈혁'),
('김수현', 'Actor', '학생', '김수현'),
('이정민', 'Actor', '학생', '이정민'),
('최원재', 'Actor', '학생', '최원재'),
('조원희', 'Actor', '교수', '조원희'),
('DH201', 'Event', '수업', '수업:인문정보 데이터베이스'),
('한중연', 'Actor', '기관', '한국학중앙연구원'),
('한국학대학원', 'Actor', '기관', '한국학대학원')
('디지털인문학연구소', 'Actor', '기관', '디지털인문학연구소')

insert into friendsLinks(source, target, relation)
values
('김현', '한국학대학원', '~의 교수이다'),
('조원희', '한국학대학원', '~의 교수이다'),
('박현정', '한국학대학원', '~의 학생이다'),
('정송이', '한국학대학원', '~의 학생이다'),
('강훈혁', '한국학대학원', '~의 학생이다'),
('김수현', '한국학대학원', '~의 학생이다'),
('이정민', '한국학대학원', '~의 학생이다'),
('최원재', '한국학대학원', '~의 학생이다'),
('한중연', '한국학대학원', '부속기구 ~가 있다'),
('한중연', '디지털인문학연구소', '부속기구 ~가 있다'),
('한국학대학원', 'DH201', '교과목 ~를 개설하다'),
('김현', 'DH201', '~를 강의하다'),
('조원희','DH201', '~를 수강하다'),
('박현정', 'DH201', '~를 수강하다'),
('정송이', 'DH201', '~를 수강하다'),
('강훈혁', 'DH201', '~를 수강하다'),
('김수현', 'DH201', '~를 수강하다'),
('이정민', 'DH201', '~를 수강하다'),
('최원재', 'DH201', '~를 수강하다'),
('조원희',  'DH201', '~를 수강하다'),
('김현',  '디지털인문학연구소', '~의 소장이다'),
('박현정', '디지털인문학연구소', '~의 연구원이다'),
('정송이', '디지털인문학연구소', '~의 연구원이다')

/*관행적으로 첫글자는 대문자로 쓰기, 파이선이 인식하려면 프로젝트명+Data(노드)와 프로젝트명+Link(링크)는 절대 바꾸면 안됨*/
select * from friendsData
select * from friendsLinks

/*네트워크 그래프 출력*/
http://dh.aks.ac.kr/~sandbox/cgi-bin/Story01.py?db='자기_데이터베이스'&project=friends&key=DH201