검색 여닫기
검색
메뉴 여닫기
555
262
4
6.2천
noriwiki
둘러보기
대문
최근 바뀜
임의의 문서로
미디어위키 도움말
특수 문서 목록
파일 올리기
환경 설정 메뉴 여닫기
notifications
개인 메뉴 여닫기
로그인하지 않음
지금 편집한다면 당신의 IP 주소가 공개될 수 있습니다.
user-interface-preferences
한국어
개인 도구
로그인
Structure of SQL Queries 문서 원본 보기
noriwiki
문서 공유하기
다른 명령
←
Structure of SQL Queries
문서 편집 권한이 없습니다. 다음 이유를 확인해주세요:
요청한 명령은 다음 권한을 가진 사용자에게 제한됩니다:
사용자
.
문서의 원본을 보거나 복사할 수 있습니다.
상위 문서: [[SQL]] ==개요== 일반적인 SQL 질의는 다음 형식을 갖는다. <syntaxhighlight lang="sql"> select A1, A2, ..., An from r1, r2, ..., rm where P </syntaxhighlight> Ai는 속성(attribute)을 나타낸다. Ri는 관계(relation, 즉 테이블)을 나타낸다. P는 조건(predicate)을 나타낸다. 이때 SQL 질의의 결과는 하나의 관계(relation)이다. 즉, "테이블"을 반환한다. ==Basic Structure of SQL Queries== ===The select Clause=== '''select''' 절은 질의 결과로 원하는 속성들(attributes)을 나열한다. 이는 관계 대수(relational algebra)의 투영 연산(projection)에 해당한다. 예를 들어 모든 교수의 이름을 구하려면 <syntaxhighlight lang="sql"> select name from instructor </syntaxhighlight> 위와 같이 작성한다. 이때 SQL에서 이름은 대소문자를 구분하지 않는다. 이때 테이블은 튜플의 집합이기 때문에 원칙적으로는 중복된 튜플이 있을 수 없으나, SQL은 질의의 결과에서 중복된 튜플을 허용한다. 아래는 여러가지 select 문법 예제이다. <syntaxhighlight lang="sql"> select distinct dept_name --중복을 제거하려면 select뒤에 distinct 키워드를 사용한다. from instructor </syntaxhighlight> <syntaxhighlight lang="sql"> select all dept_name --중복을 제거하지 않으려면 all 키워드를 사용한다. from instructor </syntaxhighlight> <syntaxhighlight lang="sql"> --instructor table을 그대로 반환한다. select * --"*"은 모든 속성을 의미한다. from instructor </syntaxhighlight> <syntaxhighlight lang="sql"> select '437' --해당 리터럴 값을 출력한다. select '437' as colname --컬럼의 이름을 colname으로 설정한다. </syntaxhighlight> 위 코드의 실행 결과는 아래 테이블과 같다. {| class="wikitable" !437 |- |437 |} {| class="wikitable" !colname |- |437 |} <syntaxhighlight lang="sql"> --select 절에는 산술 연산이 가능하다. select ID, name, salary/12 as monthly_salary --salary를 12로 나눈 결과가 출력, 속성의 별칭을 따로 설정함 from instructor </syntaxhighlight> ===The where Clause=== '''where''' 절은 결과가 만족해야 하는 조건(predicate)을 지정하며, [[Relational algebra|relation algebra]]의 선택(selection) 연산에 해당한다. 예를 들어서 컴퓨터 공학과 교수의 이름을 구하기 위해서는 아래와 같은 코드를 사용한다. <syntaxhighlight lang="sql"> select name from instructor where dept_name = 'Comp. Sci.' </syntaxhighlight> 위에서 볼 수 있들이 비교 연산자 <, <=, >, >=, =, <>를 사용할 수 있으며, 논리 연산자 and, or, not 또한 사용 가능하다. 예를 들어, 컴퓨터공학 교수 중 연봉이 70000 초과인 사람은 아래와 같은 코드를 사용한다. <syntaxhighlight lang="sql"> select name from instructor where dept_name = 'Comp. Sci.' and salary > 70000 </syntaxhighlight> ===The from Clause=== '''From''' 절은 질의에 사용될 관계들(테이블들)을 나열하며, [[Relational algebra|relation algebra]]의 cartesian product 연산에 해당한다. 예를 들어 모든 instructor와 teaches의 조합이 생성하기 위해서는 아래와 같은 code를 사용한다. <syntaxhighlight lang="sql"> --사실상 Cartesian product 연산 select * from instructor, teaches </syntaxhighlight> 이때 두 table 간에 공통되는 속성(예: ID)이 존재한다면, 결과로 나오는 테이블에서는 instructor.ID, teaches.ID처럼 접두사로 테이블명을 붙여 구분한다. 이때 단독으로 cartesian product를 사용하지는 않지만, where 조건과 함께 join 연산의 기초로 사용된다. ==각주== [[분류:데이터베이스 시스템]]
Structure of SQL Queries
문서로 돌아갑니다.